|
-
Jan 23rd, 2007, 11:12 AM
#1
Thread Starter
New Member
MouseMotionListener & Canvas, problem
Hello, I'm having a little trouble with an applet I'm creating.
I want to make an applet where little squares (canvas) can be added to the screen. Which can move individualy, when you drag them.
The squares can be moved (sort of), but not really smooth. A working demo can be found here.
How do I get them to move smoothly.
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Squares extends Applet
{
private Square square1, square2, square3;
public void init()
{
square1 = new Square();
square2 = new Square();
square3 = new Square();
add( square1 );
add( square2 );
add( square3 );
}
}
class Square extends Canvas
{
int x, y;
public Square()
{
setSize( 100, 50 );
addMouseMotionListener( new MouseMotionHandler() );
}
public void paint( Graphics g )
{
g.drawRect( 0, 0, 99, 49 );
g.setColor( Color.blue );
g.fillOval( 0, 0, 60, 30 );
}
public void setXY( int x, int y )
{
this.x = x;
this.y = y;
this.setLocation( x, y );
}
class MouseMotionHandler extends MouseMotionAdapter
{
public void mouseDragged( MouseEvent e )
{
setXY( e.getX(), e.getY() );
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|