Results 1 to 5 of 5
- 01-14-2012, 11:08 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Using threads for animation in a non-main class.
I am trying to create an abstract class that will control any JComponent with the arrow keys when extended, but I don't know how. Right now, I havent added a KeyAdapter for the arrow keys, but I have added a run method that calls another method called "move" which is supposed to print the word "hello" in the output every 33 milliseconds. Once I quit the application, all of the "hello"s hit me at once, but they are not timed. Here is my code so far.
Main Class:
Ship Class:Java Code:package shoot; import javax.swing.JFrame; class Main extends Thread{ JFrame frame = new JFrame("Shooter"); public static void main(String[] args){ Main main = new Main(); main.init(); } private void init(){ Thread thread = new Thread(this); thread.start(); Ship ship = new Ship(); frame.setVisible(true); frame.setSize(500, 500); frame.getContentPane().add(ship); ship.run(); } }
Controllable Class:Java Code:package shoot; import java.awt.Graphics; public class Ship extends Controllable{ @Override public void paintComponent(Graphics g){ g.fillRect(20, 0, 10, 10); g.fillRect(0, 10, 50, 30); repaint(); } @Override public void move() { System.out.print("hello"); } }
Any ideas?Java Code:package shoot; import java.awt.Graphics; import javax.swing.JPanel; public abstract class Controllable extends JPanel implements Runnable{ int sleepTime = 33; public Controllable(){ } @Override public void run() { while(true){ try { move(); Thread.sleep(33); } catch (InterruptedException ex) { System.out.print("Thread in class Controls Interrupted \n at line 17"); } } } public abstract void move(); public int getSleepTime(){ return sleepTime; } @Override public abstract void paintComponent(Graphics g); }
- 01-14-2012, 11:17 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Using threads for animation in a non-main class.
1) NEVER invoke repaint() from within the paintComponent() method. This will cause a loop.
2) Don't use Threads for animation. See: How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
3) Don't use KeyEvents to handle the arrow keys. See: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- 01-14-2012, 11:58 PM #3
Re: Using threads for animation in a non-main class.
Moved from 'New to Java'
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-15-2012, 05:16 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
- 01-15-2012, 05:20 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
Variables from another class won't assign to main class
By erthbound0 in forum New To JavaReplies: 3Last Post: 12-15-2011, 10:07 PM -
Can't Get variable calculated in another class to function in main class
By hkp30 in forum New To JavaReplies: 0Last Post: 10-23-2011, 10:49 PM -
Could not find or load main class BubbleSort.class
By blaqkout in forum New To JavaReplies: 5Last Post: 09-12-2011, 07:54 PM -
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
How should an Animation class be implemented?
By kiregad in forum New To JavaReplies: 9Last Post: 06-18-2010, 07:39 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks