50 likes | 165 Vues
This guide explains the key concepts of animation in Java using threads and the Runnable interface. It covers the implementation of a custom thread class (MyThread) that facilitates smooth animations by utilizing the sleep method to control timing and the repaint method to update the graphical user interface (GUI). The tutorial also discusses the essential components of setting up a new thread in the main class and emphasizes the role of paintComponent for rendering graphics. Explore how to animate objects effectively while ensuring a responsive GUI.
E N D
Animation Albert Johnson Molly Richardson Andrew Kruth
Concepts • Threads • Runnable interface • sleep() • GUIs • repaint() • paintComponent()
Quick Thread Review • Class that implements Runnable • run() • try – catch • sleep() • class MyThread implements Runnable { • public MyThread(…..parameters…..){ • ……..Initialize class variables…… • } • public void run() { • for........ { • Stuff…………. • try { Thread.sleep(DELAY); } • catch(InterruptedException e) {} • }
Quick Thread Review cont… • Main class that creates your Runnable object • public static void main (String args[]) { • …..set your initial values….. • MyThread thread = new MyThread(..parameters..); • Thread t = new Thread(thread); • Thread.start(t); • }
The Animation Part • Animation = threads + GUIs. • Basic Ideas for animation: • paintComponent() • this is where your GUIs will be. • MyThread • run() • This is where you change your positions • sleep() – necessary to be able to see the animation • repaint()