5 Replies - 585 Views - Last Post: 30 May 2012 - 06:17 PM Rate Topic: -----

#1 L1M1Tgmr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-May 12

Applet animation problem, not animating.

Posted 17 May 2012 - 10:12 AM

I'm making a version of TRON's Light Cycle for my programming course and I'm stuck on the animation mainly.
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);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Applet animation problem, not animating.

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Applet animation problem, not animating.

Posted 17 May 2012 - 10:19 AM

Moved to the Java forum. Java and Javascript are two very different things.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10816
  • View blog
  • Posts: 40,319
  • Joined: 27-December 08

Re: Applet animation problem, not animating.

Posted 17 May 2012 - 12:06 PM

Your KeyListener methods shouldn't be responsible for invoking init() and start(). Also, use of Threading is overcomplicated for what you are trying to accomplish, and the Applet class is basically dead. Use the more modern JApplet instead. The KeyListener methods should simply update the position of the Object, and the Swing Timer's ActionListener (so use Swing Timer instead of Threads) should handle the repaint(). The Timer will fire to the ActionListener every n milliseconds.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Applet animation problem, not animating.

Posted 17 May 2012 - 03:32 PM

Put a System.out.println() in your paint() method
You will see that despite you call repaint() many times in your run() method, paint() is only called once when the Thread finishes

So
		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) {


will print "asdf" few times then the Thread will finish and the paint() method will finally be executed once.
Was This Post Helpful? 0
  • +
  • -

#5 L1M1Tgmr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-May 12

Re: Applet animation problem, not animating.

Posted 30 May 2012 - 06:12 PM

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#6 L1M1Tgmr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-May 12

Re: Applet animation problem, not animating.

Posted 30 May 2012 - 06:17 PM

Sorry for separate posts.

So, maxosnerd101, the Swing Timer and the change from Applet to JApplet made it run just how I wanted it.
and pbl, that was probably the problem with the thread, but I found java timer less confusing, thanks for the help though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1