Results 1 to 4 of 4
Thread: change color on mouse click
- 03-30-2010, 09:34 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 5
- Rep Power
- 0
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 );
}
}
-
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.
- 03-31-2010, 07:42 PM #3
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
I think you should put your attention with the code
control your color in that area ;)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() + "]" ); }
- 03-31-2010, 09:46 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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
-
change object color on mouse click
By gotenks05 in forum Java AppletsReplies: 1Last Post: 04-05-2009, 07:14 PM -
Using an EMG signal to create a mouse click
By cmc419 in forum New To JavaReplies: 1Last Post: 03-27-2009, 05:38 PM -
change the delay between double click in mouse listener
By itaipee in forum AWT / SwingReplies: 6Last Post: 03-17-2009, 02:23 AM -
mouse click do not work after repaint
By nobody in forum Java 2DReplies: 8Last Post: 12-07-2008, 04:43 PM -
mouse click alert
By amir in forum AWT / SwingReplies: 1Last Post: 08-05-2008, 10:42 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks