November 4th, 2014
Classwork/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); } }