More on Event Listeners:
Classwork:
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; /** This component displays a rectangle that can be moved. */ public class RectangleMovingComponent extends JComponent { private static final int BOX_X = 100; private static final int BOX_Y = 100; private static final int BOX_WIDTH = 20; private static final int BOX_HEIGHT = 30; private Rectangle box; public RectangleMovingComponent() { // The rectangle that the paintComponent method draws box = new Rectangle(BOX_X, BOX_Y, BOX_WIDTH, BOX_HEIGHT); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.draw(box); } /** Moves the rectangle by a given amount. @param x the amount to move in the x-direction @param y the amount to move in the y-direction */ public void moveBy(int dx, int dy) { box.translate(dx, dy); repaint(); } }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.Timer; /** This program moves the rectangle. */ public class RectangleMover { private static final int FRAME_WIDTH = 300; private static final int FRAME_HEIGHT = 400; public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("An animated rectangle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final RectangleMovingComponent component = new RectangleMovingComponent(); frame.add(component); frame.setVisible(true); class TimerListener implements ActionListener { public void actionPerformed(ActionEvent event) { component.moveBy(1, 1); } } ActionListener listener = new TimerListener(); final int DELAY = 100; // Milliseconds between timer ticks Timer t = new Timer(DELAY, listener); t.start(); } }
Use the programs given to write a program, YI_BouncingBlock.java to make the block bounce when it reaches a all.
Homework:
1. Write a program, YI_MovingCar.java that animates a car so that it moves across a frame.
2. Write a program, YI_TwoMovingCars.java that animates two cars moving across a frame in opposite directions (but at different heights so that they don’t collide.)
import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /** This program demonstrates how to install an action listener. */ public class ButtonViewer { private static final int FRAME_WIDTH = 100; private static final int FRAME_HEIGHT = 60; public static void main(String[] args) { /************ Inner Class Define Before is used ***************************/ /** An action listener that prints a message. */ class ClickListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("I was clicked."); } } /************ Inner Class End ***************************/ JFrame frame = new JFrame(); JButton button = new JButton("Click me!"); frame.add(button); ActionListener listener = new ClickListener(); button.addActionListener(listener); frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }