<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">
<channel>
<title>press</title>
<link>http://www.computersight.com/tags/press</link>
<description>New posts about press</description>
<item>
<title>Responding to Mouse Events with Java</title>
<link>http://www.computersight.com/Programming/Java/Responding-to-Mouse-Events-with-Java.86242</link>
<description>
<![CDATA[<p>Java can detect a number of mouse actions including mouse presses, releases, clicks, movements, and drags, and also when the mouse enters and exit's the program's window.</p>
 
<p>There are about three things you need to do in order to allow Java to respond to mouse events. First you must implement MouseListener and MouseMotionListener. The code to do that would look something like this:</p>
 
<pre>
class MyProgram extends JFrame implements MouseListener, MouseMotionListener {
 
&amp;hellip;
 
}
 
Once you've implemented the mouse listeners, you then need to &amp;ldquo;activate&amp;rdquo; them. Simply add the lines to your main program like this:
 
MyProgram() {
 
&amp;hellip;
 
addMouseListener(this);
 
addMouseMotionListener(this);
 
&amp;hellip;
 
}

</pre> 
<p>By now, you've already told Java to use and listen for mouse events. The only thing left to do now is make Java respond to those events. The MouseListener allows us to detect when the mouse is pressed, released, clicked, or when the mouse enters or exits the program's window. The MouseMotionListener adds handlers for when the mouse is moved or dragged. We can handle each event by adding methods for each one. The code for that would be:</p>
 
<pre>
public void mousePressed(MouseEvent me) {}
 
public void mouseReleased(MouseEvent me) {}
 
public void mouseClicked(MouseEvent me) {}
 
public void mouseEntered(MouseEvent me) {}
 
public void mouseExited(MouseEvent me) {}
 
public void mouseMoved(MouseEvent me) {}
 
public void mouseDragged(MouseEvent me) {}
</pre>

 
<p>Notice that in each of the methods a MouseEvent is returned. With the MouseEvent, &amp;ldquo;me&amp;rdquo;, you can get the current coordinates of the mouse, relative to the program's window by using getX() and getY(). This piece of code, demonstrates how you can get the mouse's coordinates.</p>
 
<pre>
public void mouseMoved(MouseEvent me) {
 
..
 
mouseX = me.getX();
 
mouseY = me.getY();
 
&amp;hellip;
 
}

</pre> 
<p>That's about it! By now you should be able to detect mouse events with Java. If you don't quite feel comfortable with mouse events yet, or if you're interested in seeing the code in action, check out the sample program I wrote below.</p>
 
<p>Java Mouse Detector</p>
 
<p>This program, written in Java, demonstrates how you could detect and respond to mouse events. It monitors the mouse's movements and actions, and displays them on the screen.</p>
 
<p>Note: The lines proceeded by &amp;ldquo;//&amp;rdquo; are called comments. They aren't necessary for the program and are skipped by the compiler. I've separated the code into code blocks in order to explain them more easily.</p>
 
<p>//Code Block 1</p>
 
<pre>
import java.awt.*;
 
import java.awt.event.*;
 
import javax.swing.*;
 
//End of Code Block 1
 
class MouseDetecor extends JFrame implements MouseListener, MouseMotionListener{
 
int mouseX, mouseY;
 
String mouseStatus;
 
public static void main(String args[]) {
 
new MouseDetecor();
 
}
</pre>
 
<p>//Code Block 2</p>
 
<pre>
MouseDetecor() {
 
setTitle("Java Mouse Detector");
 
setSize(300, 100);
 
setVisible(true);
 
addMouseListener(this);
 
addMouseMotionListener(this);
 
addWindowListener(
 
new WindowAdapter() {
 
public void windowClosing(WindowEvent we) {
 
System.exit(0);
 
}
 
}
 
);
 
}

</pre> 

 
<p>//Code Block 3</p>
 
<pre>
public void paint(Graphics g) {
 
g.setColor(new Color(255, 255, 255));
 
g.fillRect(0, 0, 300, 100);
 
g.setColor(new Color(0,0, 0));
 
g.drawString("Mouse status:" + mouseStatus, 10, 60);
 
g.drawString("Mouse X:" + mouseX + " Mouse Y: " + mouseY, 10, 70);
 
}
</pre>
 

 
<p>//Code Block 4</p>
 
<pre>
public void updateMouseInfo(int x, int y, String status){
 
mouseX = x;
 
mouseY = y;
 
mouseStatus = status;
 
repaint();
 
}
</pre>
 

 
<p>//Code Block 5</p>
 
<pre>
public void mousePressed(MouseEvent me){
 
updateMouseInfo(me.getX(), me.getY(), "Pressed");
 
}
 
public void mouseReleased(MouseEvent me){
 
updateMouseInfo(me.getX(), me.getY(), "Released");
 
}
 
public void mouseClicked(MouseEvent me){
 
updateMouseInfo(me.getX(), me.getY(), "Clicked");
 
}
 
public void mouseEntered(MouseEvent me){
 
updateMouseInfo(me.getX(), me.getY(), "Entered");
 
}
 
public void mouseExited(MouseEvent me){
 
updateMouseInfo(me.getX(), me.getY(), "Exited");
 
}
 
public void mouseMoved(MouseEvent me){
 
updateMouseInfo(me.getX(), me.getY(), "Moved");
 
}
 
public void mouseDragged(MouseEvent me){
 
updateMouseInfo(me.getX(), me.getY(), "Dragged");
 
}
</pre>
 

 
<p>}</p>
 
<p>Understanding the code</p>
 
<p>Code Block 1:</p>
 
<p>This code simply imports the packages we need to run our program. &amp;ldquo;Java.awt&amp;rdquo; contains the user interface toolkit. &amp;ldquo;Java.awt.event&amp;rdquo; allows Java to respond when the user closes the program. &amp;ldquo;Javax.swing&amp;rdquo; let's us use a JFrame.</p>
 
<p>Code Block 2:</p>
 
<p>First, we set the text in the program's title bar to &amp;ldquo;Java Mouse Detector.&amp;rdquo; Next, we set the size of the program's window to 300 by 100. Using &amp;ldquo;setVisible(true)&amp;rdquo; we make the program appear on the screen. After that we add the MouseListener and MouseMotionListeners so the program can detect mouse events. Finally, we add a WindowListener to detect when the program is closed.</p>
 
<p>Code Block 3:</p>
 
<p>This section of code is our paint method. In this example, we use it to display information about the mouse. First we set the drawing color to white, which in RGB format is: 255, 255, 255. Next we fill an area of 300 by 100 (the size of the program's window) with the color white. Then, we set the drawing color to black or 0, 0, 0 in RGB. Lastly, we draw two strings, one containing the current action of the mouse (stored in &amp;ldquo;mouseStatus&amp;rdquo;), and the second containing the mouse's x and y coordinates (stored in mouseX and mouseY respectively).</p>
 
<p>Code Block 4:</p>
 
<p>This is the function we use to, as the name says, update the information about the mouse on the screen. This function receives three variables: the x and y coordinates of the mouse, and the mouse's status. Once it has stored that information into the appropriate variables, it calls the paint method (using &amp;ldquo;repaint()&amp;rdquo;) to redraw the information on the screen.</p>
 
<p>Code Block 5:</p>
 
<p>All the mouse actions in this program do the same thing: get the mouse's x and y coordinates, change &amp;ldquo;mouseStatus&amp;rdquo; to the appropriate event, and repaint the screen. Instead of writing those same three instructions over and over for each handler, we wrote one function to do those three things, &amp;ldquo;updateMouseInfo&amp;rdquo;. When we call &amp;ldquo;updateMouseInfo&amp;rdquo;, we pass three variables to the method: the mouse's x coordinate, the mouse's y coordinate, and the appropriate status.</p>
 
<p>Uses for Detecting Mouse Events</p>
 
<p>Probably the biggest uses for mouse events are games. Many Java games, in some form or another, need information about the mouse. Drawing and photo manipulation programs often also need to detect mouse actions. Basically, whenever you plan on having some sort of Graphical User Interface, it's good to know how to detect mouse events.</p>
 
<p>Hopefully, you could grab a point or two out of this article that helped and I'm always open to suggestions. Thanks for checking this article out. Good luck coding in Java!</p><a href="http://www.pheedo.com/click.phdo?x=&u=http%3A%2F%2Fwww.computersight.com%2FProgramming%2FJava%2FResponding-to-Mouse-Events-with-Java.86242"><img src="http://www.pheedo.com/img.phdo?x=&u=http%3A%2F%2Fwww.computersight.com%2FProgramming%2FJava%2FResponding-to-Mouse-Events-with-Java.86242" border="0"/></a>]]></description>
<pubDate>Thu, 21 Feb 2008 08:37:52 PST</pubDate></item>
</channel>
</rss>
