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

    Default change color on mouse click

    Someone please help. I need to write a program that sets the background of a panel as black when the mouse is clicked and white when the mouse is released. I have a TrackMouse program but it just tells if it's in the panel or outside and if it's pressed or dragged and the coordinates. I'm not sure what parts to take out and I think I need a few if statments. I don't think it's complicated just not sure where to begin.

    Here's my code

    import java.awt.*;
    import java.awt.event.*;

    // Java extension packages
    import javax.swing.*;

    public class TrackMouse extends JFrame
    implements MouseListener, MouseMotionListener {

    private JLabel statusBar;

    // set up GUI and register mouse event handlers
    public TrackMouse()
    {
    super( "Demonstrating Mouse Events" );

    statusBar = new JLabel();
    getContentPane().add( statusBar, BorderLayout.SOUTH );

    // application listens to its own mouse events
    addMouseListener( this );
    addMouseMotionListener( this );

    setSize( 275, 100 );
    setVisible( true );
    }

    // MouseListener event handlers

    // handle event when mouse released immediately after press
    public void mouseClicked( MouseEvent event )
    {
    statusBar.setText( "Clicked at [" + event.getX() +
    ", " + event.getY() + "]" );
    }

    // handle event when mouse pressed
    public void mousePressed( MouseEvent event )
    {
    statusBar.setText( "Pressed at [" + event.getX() +
    ", " + event.getY() + "]" );
    }

    // handle event when mouse released after dragging
    public void mouseReleased( MouseEvent event )
    {
    statusBar.setText( "Released at [" + event.getX() +
    ", " + event.getY() + "]" );
    }

    // handle event when mouse enters area
    public void mouseEntered( MouseEvent event )
    {
    statusBar.setText( "Mouse inside window" );
    }

    // handle event when mouse exits area
    public void mouseExited( MouseEvent event )
    {
    statusBar.setText( "Mouse outside window" );
    }

    // MouseMotionListener event handlers

    // handle event when user drags mouse with button pressed
    public void mouseDragged( MouseEvent event )
    {
    statusBar.setText( "Dragged at [" + event.getX() +
    ", " + event.getY() + "]" );
    }

    // handle event when user moves mouse
    public void mouseMoved( MouseEvent event )
    {
    statusBar.setText( "Moved at [" + event.getX() +
    ", " + event.getY() + "]" );
    }

    // execute application
    public static void main( String args[] )
    {
    TrackMouse application = new TrackMouse();

    application.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE );
    }

    }

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

    Default

    How is this thread different from your previous thread here?
    background color with jpanel

    Please don't start another thread on the same subject.

    Thank you for your cooperation.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    I think you should put your attention with the code

    Java Code:
    public void mouseClicked( MouseEvent event )
    {
    statusBar.setText( "Clicked at [" + event.getX() +
    ", " + event.getY() + "]" );
    }
    
    // handle event when mouse pressed
    public void mousePressed( MouseEvent event )
    {
    statusBar.setText( "Pressed at [" + event.getX() +
    ", " + event.getY() + "]" );
    }
    control your color in that area ;)

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

    Default

    Isn't this the exact same thing you wanted in the other thread? on click bg is white, and on release, bg is black? Anyway, I already showed you how, and Webuser, he shouldn't use mouseClicked, because a click requires that the mouse doesn't move between the press and release. There is mouseReleased() for what he is trying to do.

Similar Threads

  1. change object color on mouse click
    By gotenks05 in forum Java Applets
    Replies: 1
    Last Post: 04-05-2009, 07:14 PM
  2. Using an EMG signal to create a mouse click
    By cmc419 in forum New To Java
    Replies: 1
    Last Post: 03-27-2009, 05:38 PM
  3. Replies: 6
    Last Post: 03-17-2009, 02:23 AM
  4. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 04:43 PM
  5. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 10:42 PM

Posting Permissions

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