GoliathBT
Jan 23rd, 2007, 10:12 AM
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 (http://www.r3lax.nl/squares/Squares.htm).
How do I get them to move smoothly. :confused:
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() );
}
}
}
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 (http://www.r3lax.nl/squares/Squares.htm).
How do I get them to move smoothly. :confused:
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() );
}
}
}