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 five-point star in an Applet and then
- * bounce it around the canvas.
- *
- * @author Shaun B
- * @version 2012-10-31
- */
- public class starAnimation extends JApplet implements Runnable
- {
- // This array will draw a simple house:
- private static int star[] =
- {
- /** co-ordinates in array read as
- * x0, y0 to x1, y1. -1 terminates */
- 0, 28, 30, 28,
- 30, 28, 39, 0,
- 39, 0, 50, 28,
- 50, 28, 79, 28,
- 79, 28, 55, 46,
- 55, 46, 64, 73,
- 64, 73, 40, 57,
- 39, 57, 15, 73,
- 15, 73, 23, 45,
- 23, 45, 0, 28,
- -1
- };
- // Starting position of star:
- private int xAxis = 0;
- private int yAxis = 0;
- // Sets the height and width of the image:
- private int widthOfStar = 80;
- private int heightOfStar = 73;
- // Sets the direction of the animation
- // positive to move right/down and negative
- // to move left/up:
- private int xDirection = 1;
- private int yDirection = 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;
- // This will be our thread, you need to know about threads too:
- private Thread runner = 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);
- setVisible(true);
- }
- public void animate(int x, int y)
- {
- // Calls drawImage method:
- drawImage(xAxis, yAxis, star);
- }
- public void drawImage(int x, int y, int img[])
- {
- // Sets the default foreground colour:
- backg.setColor(Color.black);
- // This will step through the array points to draw
- // the star object. There is probably also a fillPolygon
- // or drawPolygon method that could also be used:
- while(star[index]>=0)
- {
- int x0 = x+(star[index+0]);
- int y0 = y+(star[index+1]);
- int x1 = x+(star[index+2]);
- int y1 = y+(star[index+3]);
- backg.drawLine( x0, y0, x1, y1 );
- index += 4;
- }
- // Resets index to zero, incase the JApplet is reloaded or something:
- index = 0;
- }
- public void clearBackBuffer()
- {
- // This will clear the canvas so that there is no trail left by the star
- // by setting the default background colour and then filling it to the
- // width and height of the canvas:
- backg.setColor(Color.white);
- backg.fillRect(0, 0, width, height);
- }
- /**
- * 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()
- {
- // Sets up the thread:
- if(runner == null)
- {
- runner = new Thread(this);
- runner.start();
- }
- // Call to parent (not needed):
- // 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();
- }
- @Override
- public void run()
- {
- // Checks if this thread has been set to runnable in the start method:
- Thread thisThread = Thread.currentThread();
- while (runner == thisThread)
- {
- // Calls our method to draw the star:
- animate(xAxis, yAxis);
- try
- {
- // This is the time that it will pause in milliseconds
- // 1000 = 1 second:
- Thread.sleep(20);
- }
- catch (InterruptedException e)
- {
- }
- repaint();
- // This will move the x and y co-ordinates of our object:
- xAxis += xDirection;
- yAxis += yDirection;
- // This will check the boundries of the current applet canvas:
- if(xAxis >= (width-widthOfStar))
- {
- xDirection =-1;
- }
- if(xAxis <=0)
- {
- xDirection = 1;
- }
- if(yAxis >= (height-heightOfStar))
- {
- yDirection =-1;
- }
- if(yAxis <=0)
- {
- yDirection = 1;
- }
- // Clears the canvas, so there is no 'trail'
- // left by the moving star:
- clearBackBuffer();
- }
- }
- // 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:
- runner = null;
- System.gc();
- super.destroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement