Results 1 to 7 of 7
  1. #1
    hannerz06 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default background color with jpanel

    Hi can anyone please help..

    I want to write a program to use a jpanel and set the background to black when the mouse is pressed and white when released.
    Last edited by hannerz06; 03-30-2010 at 06:02 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You will need to read the Sun tutorial on how to create and use MouseListeners, and then create one that calls setBackground(Color c) on the JPanel. Then please come on back with your code attempt if it doesn't work, and let's help you work through it.

    Much luck with your project and welcome to the forum!

  3. #3
    hannerz06 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    This is what I have so far and I'm sure it isn't evn close to being right but any help would be great! I've kind of pieced together some code from other projects I've done and written some... does any of it look right?

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.*;
    
    public class Circle extends JFrame implements MouseListener, MouseMotionListener {
    
        private JLabel statusBar;    
    
        public Circle(){
            setTitle("Circle");
            setSize(300, 400);//size of window
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//program exits when window is closed
    
            GraphicPanel panel = new GraphicPanel();
            panel.setLayout(new BorderLayout());
    
            add(panel);
            setVisible(true);     
        }
    
        public void TrackMouse(){
    
          statusBar = new JLabel();
          getContentPane().add( statusBar, BorderLayout.SOUTH );
    
          // application listens to its own mouse events
          addMouseListener( this );
          addMouseMotionListener( this );
    
          setSize(275, 100);
          setVisible(true);
       }
    
    class GraphicPanel extends JPanel{
         //sets the border with the title
        int rw=100; //width
        int rh=50; //height
    
        public GraphicPanel(){ //sets the title for the other side of the pane where the rectangle is drawn
            setBorder(new TitledBorder("Draw Here"));
        }//close graphic panel
    
        @Override
        protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //g.setColor(Color.BLACK);
        g.fillRect(0, 0, getWidth(),
        getHeight());
    
        g.setColor(Color.WHITE);
        g.fillOval(getWidth()/2 - 45,
        getHeight()/2 - 45, 90, 90);
        }
    
        public void main(String[] args) {
            new Circle();
             
            TrackMouse application = new TrackMouse();
    
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
        }
    }
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-30-2010 at 10:15 PM. Reason: Moderator Edit: Code tags added

  4. #4
    hannerz06 is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    Fubarable -- it was posted because the programs are similar but not the same and it's two different programs... similar, but different, they are both event driven programs so I'm not sure why it was closed because like I said they are different programs with different goals to try to accomplish.

    Thanks

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by hannerz06 View Post
    This is what I have so far and I'm sure it isn't evn close to being right but any help would be great! I've kind of pieced together some code from other projects I've done and written some... does any of it look right?
    This won't compile (as you should already know). Have you looked at the tutorial? It gives you sample code that's a working example of a MouseListener.

    A few things though jump out at me re your code. For one, why are you using a MouseMotionListener? Are there other requirements here for catching mouse motion that we don't know about? Next, anytime your class implements an interface, like a MouseListener, the class has to implement the methods of the interface, and yours doesn't do this. If not, the compiler will complain that you haven't implemented all the methods of the interface, and it won't compile unless it's an abstract class.

  6. #6
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Here's the program, take a look at it and try to figure out what it's doing.
    Java Code:
       import java.awt.event.MouseListener;
       import java.awt.event.MouseEvent;
       import java.awt.Color;
       import javax.swing.JPanel;
       import javax.swing.JFrame;
       //this class extends JPanel, so when you make an object of it, it has all the methods of JPanel, and can be added to a JFrame or any other component
        public class mouseClick extends JPanel implements MouseListener
          {
          //constructor, called when a mouseClick object is made in the main method below
           public mouseClick()
             {
             //this adds a mouselistener to the JPanel
             addMouseListener(this);
             }
             //the rest of the mothods are required because this class implements MouseListener
           public void mouseClicked( MouseEvent event )
             {
             }
       
           public void mousePressed( MouseEvent event )
             {
             //this sets the background of the current object to black when pressed
             this.setBackground(Color.black);
             }
       
           public void mouseReleased( MouseEvent event )
             {
             //to white when releaseed
             this.setBackground(Color.white);
             }
       
           public void mouseEntered( MouseEvent event )
             {
             }
       
           public void mouseExited( MouseEvent event )
             {
             }
           public static void main(String args[])
             {
             //creates a JFrame, sets the size, adds a JPanel (mouseClick) and displays it
             JFrame window = new JFrame();
             window.setSize(400,400);
             window.add(new mouseClick());
             window.setVisible(true);
             }
       
          }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by hannerz06 View Post
    Fubarable -- it was posted because the programs are similar but not the same and it's two different programs... similar, but different, they are both event driven programs so I'm not sure why it was closed because like I said they are different programs with different goals to try to accomplish.

    Thanks
    Got it -- unlocked. And sorry for the misunderstanding!

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2009, 10:54 AM
  2. my code doesn't change background color
    By javanoobita in forum New To Java
    Replies: 5
    Last Post: 02-22-2009, 04:30 AM
  3. Replies: 13
    Last Post: 10-21-2008, 02:32 PM
  4. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 04:54 PM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •