Hi there people! I'll add my problem the list (?)
I am trying to make smooth movement. Basicly, there is a window, you click in the window, a grey square goes from its current position to where you clicked, smoothly and relativly slowly. I wrote some code that I think should work, and I really have no idea why its not working. Could someone help me? This is in java. I also had the great idea of trying a formula for this, which I thought would be
curentx = targetx-curentx/interval but I guess that didnt work...
[code]import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends Applet
implements MouseListener, MouseMotionListener {
int width, height;
int cx, cy, tx, ty; // the mouse coordinates
boolean isButtonPressed = false;
boolean isMoving = false;
public void init() {
width = getSize().width;
height = getSize().height;
setBackground( Color.black );
cx = 100;
cy = 100;
addMouseListener( this );
addMouseMotionListener( this );
}
public synchronized void MoveIt() throws InterruptedException{
if(isMoving != true){
isMoving = true;
int i = 0;
double interval = 0.1;
while(cx!=tx&cy!=ty){
if(cx>tx){
System.out.println("cx>tx");
cx-=interval;
}
else if(cx<tx){
System.out.println("cx<tx");
cx+=interval;
}
else if(java.lang.Math.abs(cx-tx)<interval){
System.out.println("java.lang.Math.abs(cx-tx)<interval");
cx=tx;
break;
}
if(cy>ty){
System.out.println("cy>ty");
cy-=interval;
}
else if(cy<ty){
System.out.println("cy<ty");
cy+=interval;
}
else if(java.lang.Math.abs(cy-ty)<interval){
System.out.println("java.lang.Math.abs(cy-ty)<interval");
cy=ty;
break;
}
repaint();
System.out.println("Repainted. X:"+cx+" Y: "+cy);
this.wait(50);
}
isMoving = false;
}
}
public void mouseEntered( MouseEvent e ) {
// called when the pointer enters the applet's rectangular area
}
public void mouseExited( MouseEvent e ) {
// called when the pointer leaves the applet's rectangular area
}
public void mouseClicked( MouseEvent e ) {
this.tx = e.getX();
this.ty = e.getY();
try {
this.MoveIt();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void mousePressed( MouseEvent e ) { // called after a button is pressed down
}
public void mouseReleased( MouseEvent e ) { // called after a button is released
}
public void mouseMoved( MouseEvent e ) { // called during motion when no buttons are down
}
public void mouseDragged( MouseEvent e ) { // called during motion with buttons down
}
public void paint( Graphics g ) {
g.setColor(Color.RED);
g.fillRect( cx-20, cy-20, 40, 40 );
}
}
[/code]
You should probably be double buffering your drawing.
[QUOTE=Wikipedia]In computer graphics, double buffering is a technique for drawing graphics that shows no (or less) flicker, tearing, and other artifacts.
It is difficult for a program to draw a display so that pixels do not change more than once. For instance to update a page of text it is much easier to clear the entire page and then draw the letters than to somehow erase all the pixels that are not in both the old and new letters. However, this intermediate image is seen by the user as flickering. In addition computer monitors constantly redraw the visible video page (at around 60 times a second), so even a perfect update may be visible momentarily as a horizontal divider between the "new" image and the un-redrawn "old" image, known as tearing.[/QUOTE]
I'm not sure how you would do this in Java.
[editline]14th November 2010[/editline]
Maybe look at this:
[url]http://www.realapplets.com/tutorial/DoubleBuffering.html[/url]
Or just [url=http://google.com/search?q=How+to+Double+Buffer+in+Java+Applet]Google it[/url]
Thank you, I have never heard of double buffering before. :)
Your equation is almost right, except you probably want it to be more like "currentx += targetx-curentx/interval"
(Note the += instead of =)
Sorry, you need to Log In to post a reply to this thread.