Jump to content

How do you add an animation loop?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
kaila2000

kaila2000

    Newbie

  • Members
  • Pip
  • 3 posts
i am really stuck here because i missed one of my java programing class and do not know how to do an animation loop.
Here's what i have to do, i have to write a billboard program where the message inside the box have to scroll from left to right. so...

•Add a run() method (with the standard animation loop), and an updateFrame() method.
•When you first position your label, position it off the right side of the screen. As you draw each frame of your animation, move it to the left by two pixels. When the entire label has scrolled off the screen to the left, position it back on the right-hand side again.
•Each time the updateFrame() method is called, simply change the position of the label. Do not create a new Glabel object.

here's my code:

import acm.program.*;

import acm.graphics.*;

import java.awt.*;

import java.util.Random;

import javax.swing.*;

import java.awt.event.*;

/**

 * Write a one-sentence summary of your program here.

 * Follow that with additional details about its purpose,

 * what it represents, and how to use it.

 * 

 * @author (place your name here)

 * @version (place the date here)

 */

public class BillBoard extends GraphicsProgram

{

    public static final int APPLICATION_WIDTH = 600;

    public static final int APPLICATION_HEIGHT = 200;

    private boolean done = false;

    private JButton show, color;

    private GLabel message;

    private JTextField type;

    

    public void init()

    {

        show = new JButton("Show It!");

        color = new JButton("Color");

        type = new JTextField(20);

        setBackground(Color.BLACK);

        

        message = new GLabel("Enter Message");

        Font a = new Font("ARIAL", Font.BOLD, 48);

        message.setFont(a);

        double width = message.getWidth();

        double height = message.getHeight();

        

        message.setColor(Color.RED);

        add(message, 300 - width / 2, 100 - height / 4);

        add(show, SOUTH);

        add(type, SOUTH);

        add(color, SOUTH);

        

        addActionListeners();

        

    }

    

    public void run()

    {

        final int FRAME_RATE = 1000 / 30;

        

        do

        {

            updateFrame();

            pause(FRAME_RATE);

        } while (! done);

    }

    

    public void updateFrame()

    {

        // I have no idea what to put here.

    }

    

    public void actionPerformed(ActionEvent e)

    {

        Object clicked = e.getSource();

        

        if (clicked == color)

        {

            int a = (int)(Math.random() * 7);

            switch (a)

            {

                case 1:

                    message.setColor(Color.RED);

                    break;

                case 2:

                    message.setColor(Color.ORANGE);

                    break;

                case 3:

                    message.setColor(Color.YELLOW);

                    break;

                case 4:

                    message.setColor(Color.GREEN);

                    break;

                case 5:

                    message.setColor(Color.BLUE);

                    break;

                case 6:

                    message.setColor(Color.WHITE);

                    break;

                case 7:

                    message.setColor(Color.PINK);

                    break;

            }            

        }

        if (clicked == show)

        {

            String text = type.getText();

            message.setLabel(text);

        }

    }

}



#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Before I get into the code necessary to make this animation occur, I'd like to ask a question myself:
public class BillBoard extends GraphicsProgram
Does the GraphicsProgram class implement Runnable and ActionListener? If not, then the two methods you use to match those interfaces won't help when using things that require those interfaces, despite having the methods.

    private GLabel message;
I don't have access to the GLabel class, only JLabel. I'll be treating the GLabel precisely like it's a JLabel.

When you update the frames, you only need to provide one frame of animation. This program is using an animation scheme which is actually very bad (relying on framerate to pace how fast to animate), instead you should have an independent thread that keeps time for events in the animation. Nonetheless, the way to do this is by simply moving the location of the GLabel by two pixels, then using the repaint() method to have the program redraw where the GLabel is NOW located.
    public void updateFrame()

    {

        Point loc = message.getLocation();

        if ((loc.x += 2) > getSize().width)

        {

            loc.x = 0;

        }

        message.setLocation(loc);

        update();

    }
Untested, and may have some graphical bugs (for example, using getLocation/setLocation like that may not always work on continuous animation), but this should give you an idea of what to do.
Wow I changed my sig!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users