Why Java isn't letting me change the background color of the current Panel?

Basically, I just try to learn all that there is on interactive mouse movements and seeks to build a kind of frankenstein-ish drawing program.

Here is the code:
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class MyPaint extends JPanel
{
    JButton button;
    JFrame frame;
    int currentX;
    int currentY;
    int counter = 1;
    
    public MyPaint()
    {
        createComponents();
        addComponentsToPanels();
        setFrameProperties();
        activateListeners();
    }

    public void addComponentsToPanels()
    {
        this.add(button);
    }

    public void createComponents()
    {
        button = new JButton("Button");
        button.setToolTipText("This is the first button");
    }

    public void setFrameProperties()
    {
        frame = new JFrame();
        frame.setSize(400,400);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
    }

    public void activateListeners()
    {
        MyMouseListener listener = new MyMouseListener();
        button.addMouseListener(listener);
        button.addMouseMotionListener(listener);
    }

    private class MyMouseListener implements MouseListener, MouseMotionListener
    {
        MyPaint myPaint = new MyPaint();

        public void mouseClicked(MouseEvent e) throws UnsupportedOperationException
        {
            JOptionPane.showMessageDialog(null,"The mouse was clicked!");
        }

        public void mousePressed(MouseEvent e) throws UnsupportedOperationException
        {
            JOptionPane.showMessageDialog(null,"The mouse was pressed!");
        }

        public void mouseReleased(MouseEvent e) throws UnsupportedOperationException
        {
            JOptionPane.showMessageDialog(null,"The mouse was released!");
        }

        public void mouseEntered(MouseEvent e) throws UnsupportedOperationException
        {
            myPaint.setBackground(Color.RED); // why isn't this working? - I want the original panel to be red (not the newly created windows on top)
        }

        public void mouseExited(MouseEvent e) throws UnsupportedOperationException
        {
            myPaint.setBackground(Color.BLUE); // why isn't this working? - I want the original panel to be blue (not the newly created windows on top)
        }

        public void mouseDragged(MouseEvent e) throws UnsupportedOperationException
        {
            currentX = e.getX();
            currentY = e.getY();
            repaint();
        }

        public void mouseMoved(MouseEvent e) throws UnsupportedOperationException // why is this only being called once?
        {
            System.out.println("mouseMoved: " + counter);
            counter++;
        }
    }

    public static void main(String[] args)
    {
        new MyPaint();
    }
}
I just want to know how to make the inner class to set the background of the Panel of the outer class to the same window, instead of doing what I want, but in a newly opened window.

Any help to resolve this would be greatly appreciated!
Thanks in advance!

MyPaint myPaint = new MyPaint();

Get rid of this line, it is originally an unlimited number of images to create.

Then replace the following lines:

//            myPaint.setBackground(Color.RED);
            setBackground(Color.RED); 

...
//            myPaint.setBackground(Color.BLUE);
            setBackground(Color.BLUE); 

Tags: Java

Similar Questions

  • Why window7 doesn't let me change the ip address?

    give my site a static ip address

    Describe in detail, exactly what you're trying to do and how you try to do.  Define exactly what that "don't let me" means.  If you see error messages, display their full text, not edited.

  • Adobe Muse doesn't let me change the States of thumbnails in a lightbox.

    Muse doesn't let me change the States of thumbnails for my lightbox. The only thing that it is allowing me to do the right now is to change the opacity of each State. I would like to than the 'normal' State of my nail of the thumb to be a solid color, the "Rollover" State to reveal the image that is displayed in the Viewer, the State of "mouse down" to be another solid color and the 'active' State of being of the same color together as a 'normal' State I changed the color, and I even tried to fill it with a real image with a solid color file. Nothing will work - the State remains just to display the main images in the thumbnails. I would appreciate help! I don't understand why it won't work. States work for everything else.

    Hello

    With startup, if you add a picture as a thumbnail then it could appear as hero image.

    We cannot install according to States with the thumbnails of the images, any image or fill added to the would be added as a background image. You can change the image of the active panel, but still we can not install different images with the States.

    Better idea would be to use the composition where you can set up reports for the triggers and the images separate targets.

    Thank you

    Sanjit

  • I sent a song to someone in a different country and it won't let me change the recipient, even if she has not been redeemed yet.

    I sent a song to one of my friends who live in Canada, while I'm in the United States and I didn't know that it wasn't going to work. She was not able to buy since she lives outside the United States, but it won't let me change the recipient. Is it possible that I can change the recipient or even recover my $1.29? Thank you.

    That's what you have tried to send it back: return gifts iTunes - Apple Support ?

    If this does not work you can try to contact iTunes Support and see if they will refund you: http://reportaproblem.apple.com or https://www.apple.com/emea/support/itunes/contact.html

  • iTunes will not take my Danish credit card and does not leave my country to change since I live in Spain. So I can't enter my Spanish credit card because it won't let me change the country before giving another card... I don't know what to do?

    I recently moved to Spain, but I'm not allowed to type in my Spanish credit card because it won't let me change the country. It keeps telling me that I have to put in a card that works. But as soon as I put in my Danish credit card, it will not accept it? I have to money to iTunes, then, what is the problem?

    What happens when you try to change your country on your account, you follow these instructions: change your iTunes Store country or region - Apple Support ? While in Spain, you can only use the Spanish store, and you can use a card issued by a Spanish bank (and who is registered in the same name and address you put on your account)

  • My Windows XP pro would not let me change the option 'Use the home screen', so I removed a vital .dll file. I can't start Windows now!

    My Windows XP pro would not let me change the "Use welcome screen" option under: user accounts/change the way users connect or power off.

    He gave a message to the effect (I forgot to write it!) "a recently installed program does not change this setting. Search in the file named "RtlGina2.dll for a clue. So as silly as I can be, I did a search, finds the file RtlGina2.dll in the Windows/System32 folder and cut and paste on my desk. My thought was to isolate this file to see if it will address the problem of not being able to change the option use Windows screen.

    Now, I can't run Windows! It gives a message that this file RtlGina2.dll is not found! Specifically, it says, User Interface failure. The UI for logon RtlGina2.dll DLL failed to load. Contact your system administrator to replace the DLL or restore the original dll.

    How can I replace the dll if I can't even get started? Help, please! I know just enough to get me in trouble I see.

    WyoPathfinder,
    Are you able to boot mode safe mode with command prompt?  If Yes, then you can search for the file "dir /s /a RtlGina2.dll" lists the location where you moved to.  Then you can just copy (path\filename) c:\Windows/System32\(filename)

    If you do not work in safe mode, then you can boot from a XP disc and go into the recovery console and do the same thing.

    Hope that helps. QQ learn manage!

  • I can't delete a folder because I do not have the permission, but I am the administrator. It doesn't let me change the permissions. Help?

    First of all, sorry if this is in the wrong place. I had no idea what section to post this in.
    Today, I was using a program for the 'Minecraft' game called MultiMC and removing files that it created I don't have anymore, I came across a folder, I could not open, delete, or do something with. When I try to open it it says ' (place) is not accessible. Access is denied. " When I try to remove it, he said ' you must be authorized to perform this action. You need computer administrator permissions to change this folder ' (though, I'm the administrator - only account on the computer). It also gives me 'Retry' and the options to cancel, nor necessary. On properties it says "Unable to display current owner" and won't let me change the owner, even to the administrator. The file is 0 bytes in size, but I want to get rid of because I might want to remove the folder that it is in the future, besides I don't know if it's a virus or whatever it is. Please can someone help.

    Try this:
     
    How to take possession of an item in Vista. Works also under Windows 7
    http://www.howtogeek.com/HOWTO/Windows-Vista/add-take-ownership-to-Explorer-right-click-menu-in-Vista/ 
    And it is necessary:
     
    Unlocker File Remover
    http://www.emptyloop.com/Unlocker/
     
    Alternative download sites.
     
    Download it from FileHippo
    http://filehippo.com/download_unlocker/
     
  • How can I change the current page on Acrobat Pro DC?

    I'm trying to change the current page in MS Pro, but instead of giving me an option to change only the current page, that he "convert" all pages of the pdf document scanned to make "editable". I wouldn't have a problem with Acrobat do this put that he significantly enlarged the size of the file when it does.

    Extract a page into a separate file. Edit and OCR only one page, backup and replace the original page with the newly edited page.

  • How can I create a new all-white or all-black layer without changing the current foreground color or background?

    I need to create a filled layer of black or white.  I need to do WITHOUT changing the current foreground or background colors. So I can't use the bucket tool. The reason is that I have created these layers inside Action and the need the current colors remain unchanged.

    Here's the easiest way I found to date:

    1. Layer-> new fill layer-> solid color...  Select one of the choices other than 'None '.  In the color picker dialog box, place the code hex #fff and click OK
    2. Layer-> rasterize-> content fill
    3. Layer-> Layer Mask-> remove

    Can someone suggest a simpler method?

    You can simply create a new empty layer and then go to Edit > fill and choose either black or white

  • change the current date for testing purposes.

    For testing purposes, we must be able to set the current date, mainly so that we can test 3 successive months, in about a period of 15 days.
    It's siebel-web-determinations (10,2) runnning under tomcat

    Other than changing the server clock or
    replace all occurrences in the rules where the date function perceive is used with a placeholder attribute as the temp "today's date"
    is there another way to change the current date for testing purposes?

    Thank you
    Allan

    Hi Allan

    An alternative would be to use the current date only once in your rules, for example: the date of assessment = the current date.

    Then, change the rest of your rules to use the valuation date instead of the current date. It works perfectly in production.

    If you set the value of the valuation date seeding date you want to use Siebel, then the current date function will not be triggered (you set a deducted directly attribute, so the rule proving this attribute will not run).

    See you soon

    Anthony

  • in the edition browser doesn't let me change the text, why?

    If for some reason, it allows me to change the images but no text? any idea?

    If the font that you are using is a Muse system font will convert these text boxes to raster images to render properly in the browser. This is the reason why you can't edit them in BIE as these text are actually images now.

    You will need to use one of the following font to make it editable in BIE.

    • Standard fonts
    • Web fonts
    1. Typekit Fonts
    2. Edge of the Web fonts
    3. Gites hosted fonts

    Kind regards

    Vivek

  • Why a slide doesn't let me change the slide duration?

    I have slides with the audio, and I want the slide lasting only a few seconds longer than the slide audio so time reflects on how long the lesson is in fact. Most of them is right from 400 to 500 seconds now, whereas they should be much less.

    They have buttons that make a break for you move between slides. I tried to cut a wide blade or two seconds after the blade stops editing the slide properties and changing the time. It worked on my first 3 slides, but on my 3 last he does not accept the least time and jump just to some amount of time, it was before. For example, I want to slide 5 to 36 seconds in length, but it is currently at 453.3 seconds, and whenever I try to change it, it returns immediately.

    Calculate the calendar of the legend is not on, and nothing new appears on the timeline, after the blade stops at 34 seconds.

    Thank you!

    There is an element of the Clipboard which is 453.3.seconds. You cannot change the length of the blade to nothing less than the duration for which a element that is configured to display.

  • Windows 7 won't let me change the settings to import images

    Hello
    I hope someone can help with this one!

    When I try to import images using Auto play "Import pictures and videos using windows" dialogue opens that gives me the choice of 'Mark' photos. The import settings"(in blue) shows also available to change the import settings but when I click it nothing happens? Out of curiosity I went to profiles, my partner profile has attempted to do the same thing and when I clicked on the "import setting" a new dialog box open for inviting me to make changes, I tried. So my question is:

    Why does Windows 7 let me not change my own profile import settings, and how do I reset the import settings?
    I have searched questions and knowledge base but cannot find an answer to my specific problem.
    Any help is appreciated!
    Laurie

    Hello

    I suggest to create a new user account and check. If it works in a new user account, then probably your user account have been damaged and would need a fix.

    Create a user profile.
    http://Windows.Microsoft.com/en-us/Windows7/create-a-user-account

    Now search for the question.

    Follow the link provided below to fix the damaged user profile.
    http://Windows.Microsoft.com/en-us/Windows7/fix-a-corrupted-user-profile

  • CS6 Fireworks won't let me change the type of

    Long story short: until a few weeks ago, Fireworks CS6 worked fine on my iMac, with the rest of the CS6 (ID, PS, DW). The computer started having problems, and I discovered a bad RAM Board which was replaced. So I decided to wipe the drive and do a fresh reinstall of the OS and all software. No problems.

    Except this one major: CS6 Fireworks no longer allows me to use the text tool. Try to select an area of existing text, or try to create a new text box results in a spinning beachball.

    I use Linotype FontExplorer X. Thinking that it might be a problem with a glitch somewhere in the libraries of fonts (which I had copied on the disc clone I did before erasing), I served them ALL and spent two hours to recreate them from scratch. Then in FW expecting it to work. No change: spinning beachball.

    I deleted and re-installed FW completely. No change: spinning beachball.

    I went to use FW CS5.1 and it let me not again change the text. Hooray, a temporary solution at least stay productive. However the second time, I went to FW CS5.1, exactly the same problem that manifests itself. Spinning beachball.

    I deleted my files preferably of the fireworks at the root level. No change: spinning beachball.

    I'm at my wits end. I have work to do! How can I fix it?

    iMac (mid-2011) 2.5 GHz, 16 GB RAM, OS Snow Leopard 10.6.8

    I solved the problem.

    It cost me money, but I've updated my FontExplorerX software to the current version. Now the Fireworks text tool works reliably again.

    I don't know why it was necessary, because the previous version of FontExplorerX did the job well enough, and Fireworks has never had a problem with it before. But after the download, the question which is stopped, so I guess that the upgrade process must be tweaked in the right place in my system to get the FW to replay nice.

    Just this ad here on a case where someone else has experienced this problem.

  • I have a file corrupted XP and it won't let me change the virtual memory settings. Any ideas?

    Thank you

    Gary

    There is a light at the end of the tunnel...

    If you search your system for the file, maybe you find because as your AV software may be quarantined / deleted the file, but given that the AV software was not sure about the startup item, she let alone... pointing to a file that does not exist, where the error.  These things you have trouble hand.

    Have you looked on the "all" tab?  There is also a search option in the toolbar.

    I have to find something in the registry that appear in Autoruns at one time or another or I would not have said in my notes.

    There are no cancellations or exit without save in the registry so editor before making any changes to your registry, save it with this free and popular tool:

    http://www.SnapFiles.com/get/ERUNT.html

    To perform a search in the registry, matches my computer upstairs, change it, find.  If that can't find, drill down and find the offending entry in the Local computer or the sections of the current user:

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

    Hkey_current_user\\software\\microsoft\\windows\\currentversion\\run

    Right click and you can choose to export this key to record off somewhere, then delete it.

    If you don't know what to do, ask.

    Here are a few popular backup and imaging software (with the permission of the user PML) who might be attractive (use Google to find them):

    Acronis
    Macrium Reflect
    Paragon backup and restore
    DriveImageXML

    Some disk cloning ideas of software for you to Google (some new hard drives come with software cloning):

    Acronis True Image Home 11
    EASEUS ToDo Backup
    Paragon Drive Copy or Partition Manager Personal
    Casper
    Norton Ghost

    Sorry I connect them not for you :)

Maybe you are looking for