Advertisement
Shaun_B

Applet example: drawing a house with back buffering

Oct 31st, 2012
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.Graphics;
  4. /**
  5.  * This will draw a simple house in an Applet at a location
  6.  * specified in the void method init manipulated by the xAxis
  7.  * and yAxis variables
  8.  *
  9.  * @author Shaun B
  10.  * @version 2012-10-31
  11.  */
  12. public class House extends JApplet
  13. {
  14.     // This array will draw a simple house:
  15.     private static int house[] =
  16.     {
  17.         100, 10,  80, 20,
  18.          80, 20, 120, 20,
  19.         120, 20, 100, 10,
  20.          90, 20,  90, 50,
  21.         110, 20, 110, 50,
  22.          90, 50, 110, 50,
  23.         -1
  24.     };
  25.     // This will be used to get the width and height of the Applet
  26.     private int width=0;
  27.     private int height=0;
  28.     // This will be used to index through the array above:
  29.     private int index=0;
  30.     // Read up about back buffering, as it's important ;-)
  31.     private Image backBuffer = null;
  32.     private Graphics backg = null;
  33.      /**
  34.      * Called by the browser or applet viewer to inform this JApplet that it
  35.      * has been loaded into the system. It is always called before the first
  36.      * time that the start method is called.
  37.      */
  38.     @Override
  39.     public void init()
  40.     {
  41.         // this is a workaround for a security conflict with some browsers
  42.         // including some versions of Netscape & Internet Explorer which do
  43.         // not allow access to the AWT system event queue which JApplets do
  44.         // on startup to check access. May not be necessary with your browser.
  45.         JRootPane rootPane = this.getRootPane();    
  46.         rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
  47.         // provide any initialisation necessary for your JApplet
  48.         // Gets the current width and height and creates a back buffer
  49.         // to that height:
  50.         width = getSize().width;
  51.         height = getSize().height;
  52.         backBuffer = createImage(width, height);
  53.         // Creates instance of the back buffer:
  54.         backg = backBuffer.getGraphics();
  55.         // Sets default behaviour as focusable:
  56.         setFocusable(true);
  57.         // Alter this to move the position of the house:
  58.         int xAxis = 100;
  59.         int yAxis = 100;
  60.         // Calls the method to draw the house:
  61.         drawHouse(xAxis, yAxis);
  62.         // Calls repaint to flip the buffers:
  63.         repaint();
  64.     }
  65.     public void drawHouse(int x, int y)
  66.     {
  67.         // This will step through the array positions to draw
  68.         // the house:
  69.         while(house[index]>=0)
  70.         {
  71.             int x0 = x+(house[index+0]);
  72.             int y0 = y+(house[index+1]);
  73.             int x1 = x+(house[index+2]);
  74.             int y1 = y+(house[index+3]);
  75.             backg.drawLine( x0, y0, x1, y1 );
  76.             index += 4;
  77.         }
  78.         // Resets index to zero, incase the JApplet is reloaded or something:
  79.         index = 0;
  80.     }
  81.     /**
  82.      * Called by the browser or applet viewer to inform this JApplet that it
  83.      * should start its execution. It is called after the init method and
  84.      * each time the JApplet is revisited in a Web page.
  85.      */
  86.     @Override
  87.     public void start()
  88.     {
  89.         // Call to parent:
  90.         super.start();
  91.     }
  92.     /**
  93.      * Called by the browser or applet viewer to inform this JApplet that
  94.      * it should stop its execution. It is called when the Web page that
  95.      * contains this JApplet has been replaced by another page, and also
  96.      * just before the JApplet is to be destroyed.
  97.      */
  98.     @Override
  99.     public void stop()
  100.     {
  101.         // Call to parent:
  102.         super.stop();
  103.     }
  104.     // Main paint method (called on repaint(); I think):
  105.     @Override
  106.     public void paint(Graphics g)
  107.     {
  108.         // Calls to the update method:
  109.         update(g);
  110.     }
  111.     public void update(Graphics g)
  112.     {
  113.         // Gets the backBuffer and draws it to the canvas:
  114.         g.drawImage(backBuffer,0,0,this);
  115.         // the sync toolkit is used for animations as it stops flicker:
  116.         getToolkit().sync();
  117.     }
  118.     /**
  119.      * Called by the browser or applet viewer to inform this JApplet that it
  120.      * is being reclaimed and that it should destroy any resources that it
  121.      * has allocated. The stop method will always be called before destroy.
  122.      */
  123.     @Override
  124.     public void destroy()
  125.     {
  126.         // Calls the garbage collector before calling parent:
  127.         System.gc();
  128.         super.destroy();
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement