Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.Graphics;
- /**
- * This will draw a simple house in an Applet at a location
- * specified in the void method init manipulated by the xAxis
- * and yAxis variables
- *
- * @author Shaun B
- * @version 2012-10-31
- */
- public class House extends JApplet
- {
- // This array will draw a simple house:
- private static int house[] =
- {
- 100, 10, 80, 20,
- 80, 20, 120, 20,
- 120, 20, 100, 10,
- 90, 20, 90, 50,
- 110, 20, 110, 50,
- 90, 50, 110, 50,
- -1
- };
- // This will be used to get the width and height of the Applet
- private int width=0;
- private int height=0;
- // This will be used to index through the array above:
- private int index=0;
- // Read up about back buffering, as it's important ;-)
- private Image backBuffer = null;
- private Graphics backg = null;
- /**
- * Called by the browser or applet viewer to inform this JApplet that it
- * has been loaded into the system. It is always called before the first
- * time that the start method is called.
- */
- @Override
- public void init()
- {
- // this is a workaround for a security conflict with some browsers
- // including some versions of Netscape & Internet Explorer which do
- // not allow access to the AWT system event queue which JApplets do
- // on startup to check access. May not be necessary with your browser.
- JRootPane rootPane = this.getRootPane();
- rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
- // provide any initialisation necessary for your JApplet
- // Gets the current width and height and creates a back buffer
- // to that height:
- width = getSize().width;
- height = getSize().height;
- backBuffer = createImage(width, height);
- // Creates instance of the back buffer:
- backg = backBuffer.getGraphics();
- // Sets default behaviour as focusable:
- setFocusable(true);
- // Alter this to move the position of the house:
- int xAxis = 100;
- int yAxis = 100;
- // Calls the method to draw the house:
- drawHouse(xAxis, yAxis);
- // Calls repaint to flip the buffers:
- repaint();
- }
- public void drawHouse(int x, int y)
- {
- // This will step through the array positions to draw
- // the house:
- while(house[index]>=0)
- {
- int x0 = x+(house[index+0]);
- int y0 = y+(house[index+1]);
- int x1 = x+(house[index+2]);
- int y1 = y+(house[index+3]);
- backg.drawLine( x0, y0, x1, y1 );
- index += 4;
- }
- // Resets index to zero, incase the JApplet is reloaded or something:
- index = 0;
- }
- /**
- * Called by the browser or applet viewer to inform this JApplet that it
- * should start its execution. It is called after the init method and
- * each time the JApplet is revisited in a Web page.
- */
- @Override
- public void start()
- {
- // Call to parent:
- super.start();
- }
- /**
- * Called by the browser or applet viewer to inform this JApplet that
- * it should stop its execution. It is called when the Web page that
- * contains this JApplet has been replaced by another page, and also
- * just before the JApplet is to be destroyed.
- */
- @Override
- public void stop()
- {
- // Call to parent:
- super.stop();
- }
- // Main paint method (called on repaint(); I think):
- @Override
- public void paint(Graphics g)
- {
- // Calls to the update method:
- update(g);
- }
- public void update(Graphics g)
- {
- // Gets the backBuffer and draws it to the canvas:
- g.drawImage(backBuffer,0,0,this);
- // the sync toolkit is used for animations as it stops flicker:
- getToolkit().sync();
- }
- /**
- * Called by the browser or applet viewer to inform this JApplet that it
- * is being reclaimed and that it should destroy any resources that it
- * has allocated. The stop method will always be called before destroy.
- */
- @Override
- public void destroy()
- {
- // Calls the garbage collector before calling parent:
- System.gc();
- super.destroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement