Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;

Similar presentations


Presentation on theme: "Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;"— Presentation transcript:

1 Java Applets Adding Animation

2 Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;

3 Declaring the Main Class Your declaration will still include: public class ClassName extends Applet{ But now you need to add something else to the end: public class ClassName extends Applet implements Runnable{ This will allow for threads to be run during the run process.

4 Threads A thread is a piece of code that runs alongside other lines of code instead of only running by itself. Threads are declared under the class Thread which has 3 main functions: –start() –stop() –sleep() Threads are often created inside the method start( )

5 public void start( ) This is where you would control your thread. A typical class would look like: public void start( ) { Thread threadName = new Thread(this); threadName.start( ); } “this” tells the program what this thread will run parallel to. “threadName.start( );” starts the thread, “threadName.stop( );” stops the thread and “threadName.sleep(int x);” would pause the thread for x amount of milliseconds.

6 public void stop( ) Once again we will not really be using this method, but you could include “threadName.stop( );” if you created the new thread globally. That way if you ever had a reason to stop the thread, you would call this method. It would look like: public void stop( ) { threadName.stop( ); }

7 public void run( ) This is a new method that we will be including into our applet. This is what keeps the thread running instead of simply being run once. This method will contain a while loop that will keep it running. You will also include repaint( ) so that every time it runs, the screen will be painted to fit those specifications.

8 Sample public void run ( ) { Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (true) { repaint();//repaints the screen try { Thread.sleep (10); } catch (InterruptedException ex) { // do nothing } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); }

9 public void run( ) If you noticed, you saw some other things included into the method. Thread.currentThread().setPriority(Thread.MIN_PRIORITY); and Thread.currentThread().setPriority(Thread.MAX_PRIORITY); make sure that that thread gets run during the run time. “Thread.sleep (10);” controls how often the thread reruns. 10 means that every 10 milliseconds, the screen will rerun. “catch (InterruptedException ex)” is in case the applet encounters an error, although nothing will happen.

10 public void paint(Graphics g) We will still most likely use the following tidbits of code: –g.setColor(Color.white); –g.drawString(“Hello World!”, 70, 100); You can also use the paint method to insert pictures and sounds.

11 Double Buffering Although this method is not necessary, it makes your program run more smoothly. Inside your class you would add the lines: –private Image dbImage; –private Graphics dbg; You would declare the method as such after the method paint(): public void update (Graphics g) {

12 public void update(Graphics g) In order to initialize the buffering, add the lines: if (dbImage == null) { dbImage = createImage(this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } To clear the screen in the background insert these lines of code: dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

13 public void update(Graphics g) To draw the screen in the background, include: dbg.setColor (getForeground()); paint (dbg); To draw the image on the screen, include this line: g.drawImage (dbImage, 0, 0, this);

14 Sample public void update (Graphics g)//double buffer { if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbg = dbImage.getGraphics (); } dbg.setColor (getBackground ()); dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); dbg.setColor (getForeground()); paint (dbg); g.drawImage (dbImage, 0, 0, this); } * This code is universal for just about every applet.

15 The End Now you have the general idea of how an applet runs.


Download ppt "Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;"

Similar presentations


Ads by Google