simple problem w/ appelets which i cant figure out
Im trying to figure out the mouse classes for a larger project im working on. This is a really simple applet i wrote, and based off of the sun micro systems tutorial it seems like it should work. The program should simply redraw the square where ever the mouse clicks. However it doesnt do it. As i undersatnd the applet should draw, wait for a mouse command, then redraw right? but in my program my mouseUp fires (i have console out to tell me every time it fires) but then it does fire paint again, so i must be mistaken about the way applets work. What am i missing, how do i get it to redraw? and how would i get this app to work?
/////////
Code :
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class MovingSquare extends Applet
{
private int xPos = 0;
private int yPos = 0;
private Color currentColor = Color.BLACK;
public void init()
{
addMouseListener(this);
xPos = 200;
yPos = 200;
setBackground(Color.GRAY);
}
public void paint(Graphics g)
{
g.setColor(currentColor);
g.fillRect(xPos,yPos,30,30);
System.out.println("RUN_PAINT");
}
public boolean mouseUp (Event event, int x, int y)
{
System.out.println(x);
System.out.println(y);
xPos = x;
yPos = y;
return true;
}
}
Re: simple problem w/ appelets which i cant figure out
also, normally i dont have the "addMouseListener(this);" line there. i know it wont compile like it is now(i dont know why), but i was trying to figure out why it wont work and forgot to take the line out.
Re: simple problem w/ appelets which i cant figure out
How about you try and implement the MouseListener interface, then your line should be alright there.
Code :
public class MovingSquare extends Applet implements MouseListener
// Json
Re: simple problem w/ appelets which i cant figure out
that doesnt compile.MouseListener is in awt so it should find it right?, but it says it cant find it, where do i import it?
Re: simple problem w/ appelets which i cant figure out
import java.awt.event.MouseListener;
That should be enough.
// Json
Re: simple problem w/ appelets which i cant figure out
It'll compile now. I had assumed that java.awt.*; wouldcover it but i forgot that it wont go two deep.
That however makesmy program worse not better. The mouseUp wont even call now where before it would.
What exactly does the addMouseListener(this); do?
And why doesnt paint(g) get called again after the mouseUp is called, and how do i make it do so because i cant call paint inside the mouseUp method.
Re: simple problem w/ appelets which i cant figure out
addMouseListener(this) will register this as in your current class as a mouse listener and all events for the mouse will be sent to the methods that you had to implement. In these methods you can then get a hold of the information you need.
// Json
Re: simple problem w/ appelets which i cant figure out
just found out about the repaint method after search some example codes....
i feel pretty stoopid now. but everything works,thanks for the help