Results 1 to 1 of 1
  1. #1
    speedsolve is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Stopping a Moving animation using swing

    Hi Guys,

    I am creating a program that moves a picture of a hockey puck into a picture of a hockey goal. I attached the pictures. Here is the code:

    import java.awt.Graphics;
    import java.applet.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class MovingAnimation extends JApplet implements Runnable {
    int frame;
    int delay;
    Thread animator;

    Dimension offDimension;
    Image offImage;
    Graphics offGraphics;

    Image Net;
    Image Puck;

    public void init() {
    String str = getParameter("fps");
    int fps = (str != null) ? Integer.parseInt(str) : 500;
    delay = (fps > 0) ? (1000 / fps) : 100;

    Net = getImage(getCodeBase(), "HockeyNet.png");
    Puck = getImage(getCodeBase(), "HockeyPuc.jpg");
    }
    public void start() {
    animator = new Thread(this);
    animator.start();
    }
    public void run() {
    long tm = System.currentTimeMillis();
    while (Thread.currentThread() == animator) {
    repaint();


    try {
    tm += delay;
    Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
    }
    catch (InterruptedException e) {
    break;
    }
    frame++;
    }
    }
    public void stop() {
    animator = null;
    offImage = null;
    offGraphics = null;
    }
    public void update(Graphics g) {
    Dimension d = size();

    if ((offGraphics == null)
    || (d.width != offDimension.width)
    || (d.height != offDimension.height)) {
    offDimension = d;
    offImage = createImage(d.width, d.height);
    offGraphics = offImage.getGraphics();
    }

    offGraphics.setColor(getBackground());
    offGraphics.fillRect(0, 0, d.width, d.height);
    offGraphics.setColor(Color.black);

    paintFrame(offGraphics);

    g.drawImage(offImage, 0, 0, null);
    }

    public void paint(Graphics g) {
    update(g);
    }
    public void paintFrame(Graphics g) {
    Dimension d = size();
    int w = world.getWidth(this);
    int h = world.getHeight(this);

    if ((w > 0) && (h > 0)) {
    g.drawImage(world, 0, 20, this);
    }
    h = car.getHeight(this);

    if ((w > 0) && (h > 0)) {
    w += d.width;
    g.drawImage(car, d.width - ((frame * 5) % w), (d.height - h)/2, this);
    }
    }
    }


    How do I make the Hockey Puck Stop?
    Attached Thumbnails Attached Thumbnails Stopping a Moving animation using swing-hockeypuc.jpg   Stopping a Moving animation using swing-hockeynet.png  

Similar Threads

  1. Stopping a program
    By Majeh in forum New To Java
    Replies: 7
    Last Post: 02-08-2011, 08:10 PM
  2. ordering moving images in swing
    By IAnkit in forum AWT / Swing
    Replies: 2
    Last Post: 09-21-2010, 03:56 PM
  3. Stopping a thread
    By userj2ee in forum New To Java
    Replies: 3
    Last Post: 08-13-2010, 07:57 PM
  4. Can we animation in Java Swing?
    By Riaz Ali in forum AWT / Swing
    Replies: 5
    Last Post: 05-06-2010, 03:03 AM
  5. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 09:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •