Here is my AnimationPanel class which makes a rectangle and holds the animation code:
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AnimationPanel extends JPanel
{
private ArrayList<MoveableShape> boxes;
private Timer timer;
private int direction;
private int interval;
private class TimerListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
switch (direction)
{
case 1: //turn right
for(int i =0; i < boxes.size(); i++){
Point location = boxes.get(i).getLocation();
int dx = location.x;
boxes.get(i).moveTo(dx++, location.y);
repaint();
}
case 2: //turn left
for(int i =0; i < boxes.size(); i++){
Point location = boxes.get(i).getLocation();
int dx = location.x;
boxes.get(i).moveTo(dx--, location.y);
repaint();
}
}
}
}
public AnimationPanel (int direction, int interval)
{
this.direction = direction;
this.interval = interval;
boxes = new ArrayList<MoveableShape>();
boxes.add(new MoveableRectangleShape());
timer = new Timer (interval, new TimerListener());
}
/**
* adds shapes to be displayed and moved
*/
public void addMoveableShape (MoveableShape shape)
{
boxes.add(shape);
}
public void addMoveableShape () //for testing purposes
{
boxes.add(new MoveableRectangleShape());
}
/**
* this method should call the draw method of all of
* the MoveableShapes that have been added
*/
public void paintComponent (Graphics g)
{
super.paintComponent(g);
for(int i =0; i < boxes.size(); i++)
boxes.get(i).draw(g);
}
/**
* A method to change direction of the motion
*/
public void setDirection (int newDirection)
{
if (newDirection == 1 || newDirection == 2)
direction = newDirection;
else
System.out.println("Enter 1 for right, and 2 for left");
}
/**
* A method to start the movement of the animation
*/
public void start ()
{
timer.start();
}
/**
* A method to stop the movement of the animation
*/
public void stop()
{
timer.stop();
}
}
Here is my AnimationControlPanel class which makes a JFrame and puts all the components together:
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AnimationControlPanel extends JPanel
{
private AnimationPanel animationPanelObject;
private AnimationControlPanel animationControlPanelObject;
JPanel animationControlPanel;
JPanel animationPanel;
JButton rightDirectionButton;
JButton leftDirectionButton;
private class DirectionListener implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
String direction = e.getActionCommand();
if(direction.equals("Right"))
animationPanelObject.setDirection(1);
else if(direction.equals("Left"))
animationPanelObject.setDirection(2);
}
}
public AnimationControlPanel()
{
JFrame animationFrame = new JFrame("This is an animation"); //create the JFrame
animationFrame.setSize(640,480);
animationControlPanelObject = this; //create the AnimationControlPanel object and AnimationPanel object
animationPanelObject = new AnimationPanel(1, 500);
animationControlPanel = new JPanel();
animationPanel = new JPanel();
rightDirectionButton = new JButton("Right");
leftDirectionButton = new JButton("Left");
ActionListener directionListener = new DirectionListener();
animationControlPanel.add(rightDirectionButton);
animationControlPanel.add(leftDirectionButton);
animationPanel.add(animationPanelObject);
rightDirectionButton.addActionListener(directionListener);
leftDirectionButton.addActionListener(directionListener);
animationFrame.add(animationControlPanel, BorderLayout.SOUTH);
animationFrame.add(animationPanel, BorderLayout.CENTER);
animationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
animationFrame.setVisible(true);
}
public AnimationPanel getAnimationPanel()
{
return animationPanelObject;
}
}

New Topic/Question
Reply




MultiQuote





|