| | |
Applet question..
![]() |
•
•
Join Date: Sep 2008
Posts: 38
Reputation:
Solved Threads: 0
I have the following code that produces an applet of two eyeballs. I have a mouseMotionListener that is set so that when the mouse is moved around the pair of eyes, the eyeballs follow the pointer. I have the eyeballs set up in four locations. To the left, right, north, and south in the eye. However, when I move the mouse onto the applet all four are displayed. Is there a simple way so that the correct set of eyes are displayed?
Thank you in advance, here is my code....
Thank you in advance, here is my code....
package project5;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Main extends JApplet
{
private int X;
private int Y;
boolean mouseLeft= false;
boolean mouseRight = false;
boolean mouseUp = false;
boolean mouseDown = false;
public void init()
{
getContentPane().setBackground(Color.WHITE);
addMouseListener(new MyMouseListener());
addMouseMotionListener(new MyMouseMotionListener());
}
public void paint(Graphics g)
{
super.paint(g);
//Original
g.setColor(Color.BLACK);
g.drawOval(50,50,50,50);
g.drawOval(200, 50, 50, 50);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
//this is what is drawn when in zone- left
if (mouseLeft)
{
g.fillOval(45,65, 20,20);
g.fillOval(195, 65, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
//right
if (mouseRight)
{
g.setColor(Color.BLACK);
g.fillOval(85,65, 20,20);
g.fillOval(235, 65, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
//up
if (mouseUp)
{
g.setColor(Color.BLACK);
g.fillOval(65,45, 20,20);
g.fillOval(215, 45, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
//down
if (mouseDown)
{
g.setColor(Color.BLACK);
g.fillOval(65,85, 20,20);
g.fillOval(215, 85, 20, 20);
g.setColor(Color.WHITE);
g.fillOval(65,65, 20, 20);
g.fillOval(215, 65, 20, 20);
}
else
{
g.fillOval(65,65, 20, 20);
}
}
private class MyMouseListener implements MouseListener
{
public void mousePressed(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
private class MyMouseMotionListener implements MouseMotionListener
{
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
X = e.getX();
Y = e.getY();
mouseLeft = true;
if (X <= 50)
{
repaint();
}
mouseRight = true;
if (X >= 250)
{
repaint();
}
mouseUp = true;
if(Y < 50)
{
repaint();
}
mouseDown = true;
if(Y> 250)
{
repaint();
}
}
}
}•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
You should only be setting mouseLeft, mouseRight, etc to true if, in fact, the mouse was moved in that sector. When your mouseMoved method gets called, you are setting ALL of them to true, then calling repaint. The way you're currently doing it, if mouseLeft were to be set true, it would always remain true, which is one problem. Also, currently, if mouseDown were true, it would call repaint. However, the mouseMoved method just set mouseUp, mouseDown, mouseLeft, and mouseRight ALL to true - so all of them will be repainted.
hope that helps.
hope that helps.
Last edited by BestJewSinceJC; Dec 3rd, 2008 at 1:20 am.
•
•
Join Date: Sep 2008
Posts: 1,566
Reputation:
Solved Threads: 196
To demonstrate the second point I made above, consider what happens when you move the mouse. Lets say you move the mouse down, but you do not move it in any other direction. The mouseMoved method is called, and mouseLeft is set to true. Now, lets say you eventually make it down to mouseDown = true. Then, repaint is called where you have if (Y > 250). Now, you enter the repaint method. Since mouseLeft is true, and mouseRight is true... both of those things will be repainted.
Now, another thing will happen: since you never reset mouseLeft, mouseRight, mouseUp, and mouseDown to "false"... every time you enter the repaint method, these things might be repainted. Consider using "else if" statements inside of mouseMoved to call repaint, so that repaint is never called when you don't want it to be. Also, make sure that at the end of mouseMoved, you reset all of the mouseUp, down, left, and right variables to false. Also, do not set these variables to "true" until you have determined that they should be true. In other words, setting them to true should be done inside the if statement, not outside of it.
Also, I'm not exactly sure how your assignment works, but you should consider that any time you color something, you are never un-coloring it. So lets say you color something because of mouseDown. What happens when the thing you colored isn't in the "mouseDown" sector anymore? Shouldn't you un-color it?
Now, another thing will happen: since you never reset mouseLeft, mouseRight, mouseUp, and mouseDown to "false"... every time you enter the repaint method, these things might be repainted. Consider using "else if" statements inside of mouseMoved to call repaint, so that repaint is never called when you don't want it to be. Also, make sure that at the end of mouseMoved, you reset all of the mouseUp, down, left, and right variables to false. Also, do not set these variables to "true" until you have determined that they should be true. In other words, setting them to true should be done inside the if statement, not outside of it.
Also, I'm not exactly sure how your assignment works, but you should consider that any time you color something, you are never un-coloring it. So lets say you color something because of mouseDown. What happens when the thing you colored isn't in the "mouseDown" sector anymore? Shouldn't you un-color it?
•
•
•
•
Java Syntax (Toggle Plain Text)
public void mouseMoved(MouseEvent e) { X = e.getX(); Y = e.getY(); mouseLeft = true; if (X <= 50) { repaint(); } mouseRight = true; if (X >= 250) { repaint(); } mouseUp = true; if(Y < 50) { repaint(); } mouseDown = true; if(Y> 250) { repaint(); } } } }
Last edited by BestJewSinceJC; Dec 3rd, 2008 at 1:23 am.
![]() |
Similar Threads
- Access Control Exception vs. Applet Viewer (Java)
- Applet Help (Java)
- Writing Applet ActionListener Mortgage Calculator (Java)
- Basic data and cobtrol structure,applet,class,eventhandling (JSP)
- scrollbar applet (Java)
- Java Applet Help (Java)
- Question about java.lang.SecurityException (Java)
- JMF question: question about the unrealized player (Java)
Other Threads in the Java Forum
- Previous Thread: Koriean text in CSV file...
- Next Thread: Lost with how to start
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






