Advertisement
Shaun_B

Bouncing a star around in an Applet with back buffering

Oct 31st, 2012
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.95 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.Graphics;
  4. /**
  5.  * This will draw a five-point star in an Applet and then
  6.  * bounce it around the canvas.
  7.  *
  8.  * @author Shaun B
  9.  * @version 2012-10-31
  10.  */
  11. public class starAnimation extends JApplet implements Runnable
  12. {
  13.     // This array will draw a simple house:
  14.     private static int star[] =
  15.     {
  16.         /** co-ordinates in array read as
  17.          * x0, y0 to x1, y1. -1 terminates */
  18.          0, 28, 30, 28,
  19.         30, 28, 39,  0,
  20.         39,  0, 50, 28,
  21.         50, 28, 79, 28,
  22.         79, 28, 55, 46,
  23.         55, 46, 64, 73,
  24.         64, 73, 40, 57,
  25.         39, 57, 15, 73,
  26.         15, 73, 23, 45,
  27.         23, 45,  0, 28,
  28.         -1
  29.     };
  30.     // Starting position of star:
  31.     private int xAxis = 0;
  32.     private int yAxis = 0;
  33.     // Sets the height and width of the image:
  34.     private int widthOfStar = 80;
  35.     private int heightOfStar = 73;
  36.     // Sets the direction of the animation
  37.     // positive to move right/down and negative
  38.     // to move left/up:
  39.     private int xDirection = 1;
  40.     private int yDirection = 1;
  41.     // This will be used to get the width and height of the Applet
  42.     private int width=0;
  43.     private int height=0;
  44.     // This will be used to index through the array above:
  45.     private int index=0;
  46.     // Read up about back buffering, as it's important ;-)
  47.     private Image backBuffer = null;
  48.     private Graphics backg = null;
  49.     // This will be our thread, you need to know about threads too:
  50.     private Thread runner = null;
  51.      /**
  52.      * Called by the browser or applet viewer to inform this JApplet that it
  53.      * has been loaded into the system. It is always called before the first
  54.      * time that the start method is called.
  55.      */
  56.     @Override
  57.     public void init()
  58.     {
  59.         // This is a workaround for a security conflict with some browsers
  60.         // including some versions of Netscape & Internet Explorer which do
  61.         // not allow access to the AWT system event queue which JApplets do
  62.         // on startup to check access. May not be necessary with your browser.
  63.         JRootPane rootPane = this.getRootPane();    
  64.         rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
  65.         // Provide any initialisation necessary for your JApplet
  66.         // Gets the current width and height and creates a back buffer
  67.         // to that height:
  68.         width = getSize().width;
  69.         height = getSize().height;
  70.         backBuffer = createImage(width, height);
  71.         // Creates instance of the back buffer:
  72.         backg = backBuffer.getGraphics();
  73.         // Sets default behaviour as focusable:
  74.         setFocusable(true);
  75.         setVisible(true);
  76.     }
  77.     public void animate(int x, int y)
  78.     {
  79.         // Calls drawImage method:
  80.         drawImage(xAxis, yAxis, star);
  81.     }
  82.     public void drawImage(int x, int y, int img[])
  83.     {
  84.         // Sets the default foreground colour:
  85.         backg.setColor(Color.black);
  86.         // This will step through the array points to draw
  87.         // the star object. There is probably also a fillPolygon
  88.         // or drawPolygon method that could also be used:
  89.         while(star[index]>=0)
  90.         {
  91.             int x0 = x+(star[index+0]);
  92.             int y0 = y+(star[index+1]);
  93.             int x1 = x+(star[index+2]);
  94.             int y1 = y+(star[index+3]);
  95.             backg.drawLine( x0, y0, x1, y1 );
  96.             index += 4;
  97.         }
  98.         // Resets index to zero, incase the JApplet is reloaded or something:
  99.         index = 0;
  100.     }
  101.     public void clearBackBuffer()
  102.     {
  103.         // This will clear the canvas so that there is no trail left by the star
  104.         // by setting the default background colour and then filling it to the
  105.         // width and height of the canvas:
  106.         backg.setColor(Color.white);
  107.         backg.fillRect(0, 0, width, height);
  108.     }
  109.     /**
  110.      * Called by the browser or applet viewer to inform this JApplet that it
  111.      * should start its execution. It is called after the init method and
  112.      * each time the JApplet is revisited in a Web page.
  113.      */
  114.     @Override
  115.     public void start()
  116.     {
  117.        // Sets up the thread:
  118.        if(runner == null)
  119.        {
  120.            runner = new Thread(this);
  121.            runner.start();
  122.        }
  123.        // Call to parent (not needed):
  124.        // super.start();
  125.     }
  126.     /**
  127.      * Called by the browser or applet viewer to inform this JApplet that
  128.      * it should stop its execution. It is called when the Web page that
  129.      * contains this JApplet has been replaced by another page, and also
  130.      * just before the JApplet is to be destroyed.
  131.      */
  132.     @Override
  133.     public void stop()
  134.     {
  135.         // Call to parent:
  136.         super.stop();
  137.     }
  138.     @Override
  139.     public void run()
  140.     {
  141.         // Checks if this thread has been set to runnable in the start method:
  142.         Thread thisThread = Thread.currentThread();
  143.         while (runner == thisThread)
  144.         {
  145.             // Calls our method to draw the star:
  146.             animate(xAxis, yAxis);
  147.             try
  148.             {
  149.                 // This is the time that it will pause in milliseconds
  150.                 // 1000 = 1 second:
  151.                 Thread.sleep(20);
  152.             }
  153.             catch (InterruptedException e)
  154.             {
  155.             }
  156.             repaint();
  157.             // This will move the x and y co-ordinates of our object:
  158.             xAxis += xDirection;
  159.             yAxis += yDirection;
  160.             // This will check the boundries of the current applet canvas:
  161.             if(xAxis >= (width-widthOfStar))
  162.             {
  163.                 xDirection =-1;
  164.             }
  165.             if(xAxis <=0)
  166.             {
  167.                 xDirection = 1;
  168.             }
  169.             if(yAxis >= (height-heightOfStar))
  170.             {
  171.                 yDirection =-1;
  172.             }
  173.             if(yAxis <=0)
  174.             {
  175.                 yDirection = 1;
  176.             }
  177.             // Clears the canvas, so there is no 'trail'
  178.             // left by the moving star:
  179.             clearBackBuffer();
  180.         }
  181.     }
  182.     // Main paint method (called on repaint(); I think):
  183.     @Override
  184.     public void paint(Graphics g)
  185.     {
  186.         // Calls to the update method:
  187.         update(g);
  188.     }
  189.     public void update(Graphics g)
  190.     {
  191.         // Gets the backBuffer and draws it to the canvas:
  192.         g.drawImage(backBuffer,0,0,this);
  193.         // the sync toolkit is used for animations as it stops flicker:
  194.         getToolkit().sync();
  195.     }
  196.     /**
  197.      * Called by the browser or applet viewer to inform this JApplet that it
  198.      * is being reclaimed and that it should destroy any resources that it
  199.      * has allocated. The stop method will always be called before destroy.
  200.      */
  201.     @Override
  202.     public void destroy()
  203.     {
  204.         // Calls the garbage collector before calling parent:
  205.         runner = null;
  206.         System.gc();
  207.         super.destroy();
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement