When I run the animation it runs but doesn't animate. It goes from point a to point b with the Thread's delay, so it's working but doesn't show the animation inbetween.
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Animation extends Applet implements Runnable, KeyListener {
static int UDc1=125; //Up & Down Counter -> When user presses up the counter goes up negatively, if down, vice versa.
static int LRc1=300; //Left & Right Counter -> When user presses right the counter goes up positively, if left, vice versa.
static int Line=UDc1;
static int counter=0;
static Boolean upFlag=false;
static Boolean downFlag=false;
static Boolean rightFlag=false;
static Boolean leftFlag=false;
static int x=9; //x=9, y=24 = Up and Down
static int y=24; //x=24, y=9 = Left and Right
public void init() {
setSize(600,600);
addKeyListener(this);
setFocusable(true);
requestFocusInWindow(true);
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode==KeyEvent.VK_UP) {
// handle up
upFlag=true;
rightFlag=false;
leftFlag=false;
downFlag=false;
System.out.print("UP");
init();
start();
run();
} else if (keyCode==KeyEvent.VK_DOWN) {
// handle down
downFlag=true;
upFlag=false;
rightFlag=false;
leftFlag=false;
System.out.print("DOWN");
init();
start();
run();
} else if (keyCode==KeyEvent.VK_LEFT) {
// handle left
leftFlag=true;
rightFlag=false;
upFlag=false;
downFlag=false;
System.out.print("LEFT");
init();
start();
run();
} else if (keyCode==KeyEvent.VK_RIGHT) {
// handle right
rightFlag=true;
leftFlag=false;
downFlag=false;
upFlag=false;
System.out.print("RIGHT");
init();
start();
run();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public void start() {
Thread th = new Thread (this);
th.start ();
}
public void stop() {
}
public void destroy() {
}
public void delay() {
try {
Thread.sleep (20);
}
catch (InterruptedException ex) {
}
}
public void run () {
if (upFlag) {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (!upFlag||UDc1>0) {
UDc1--;
repaint();
delay();
System.out.println("asdf");
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
} else if (downFlag) {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (!downFlag||UDc1<600) {
UDc1++;
repaint();
delay();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
} else if (upFlag) {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (!upFlag||UDc1>0) {
UDc1--;
repaint();
delay();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
} else if (leftFlag) {
y=9; //Flips the oval
x=24;
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (!leftFlag||LRc1>0) {
LRc1--;
repaint();
delay();
}
} else if (rightFlag) {
y=9; //Flips the oval
x=24;
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (!rightFlag||LRc1<600) {
LRc1++;
repaint();
delay();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
}
public void paint (Graphics g) {
super.paint(g);
this.setBackground(new Color(0, 0, 128));
g.setColor(Color.black);
//-----------THE GRID------------//
for (int i=-40; i<=600; i++) {
i=i+40;
g.drawLine(i, 0, i, 600);
//Thickening the line
//g.drawLine(i-1, 0, i+1, 600);
//g.drawLine(i+1, 0, i+1, 600);
}
for (int i=-40; i<=600; i++) {
i=i+40;
g.drawLine(0, i, 600, i);
//Thickening the line
//g.drawLine(0, i-1, 600, i-1);
//g.drawLine(0, i+1, 600, i+1);
}
g.setColor(new Color(SecondMenu.rd, SecondMenu.gr, SecondMenu.yw));
g.drawLine(LRc1+4, Line, LRc1+4, UDc1);
g.fillOval (LRc1, UDc1, x, y);
}
}

New Topic/Question
Reply



MultiQuote








|