Download Lecture 41

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lecture 41
Animation
The secret of animation is to continually redraw a picture with slight
changes in each new drawing.
We need a mechanism to control the interval between redrawings.
Two Possibilities
1. Call a method that delays for a short amount of time.
2. Use an object that signals at regular intervals.
Timer Class
The objects of this class can be designed to fire ActionEvent objects
separated by specified intervals.
To respond to such an event, we need to implement the interface
ActionListener and register it with the Timer object.
Constructor for Timer
Timer t = new Timer(delay, listener);
where
delay
an integer value giving the interval between event firings
in milliseconds (thousandths of a second) and
listener an object whose class implements ActionListener.
Once a Timer object is created, it can be started with the instance
method start.
Example
Copyright © 2002 by Ken Slonneger
41-1
We place a panel on a frame and draw the word “Growing”
repeatedly on the panel, first increasing its font size from 10 up
to 150 points and then decreasing it back to 10 points.
The word will be redrawn every 30 milliseconds as controlled by
a Timer object.
The panel class will provide the drawing mechanism and serve
as the ActionListener.
One Problem
Timer belongs to the Swing set of graphics classes in Java, so we
write the code using Swing graphics. Notice the different classes and
the content pane that acts as a Container on the frame.
Code for WordGrower
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WordGrower
{
public static void main(String [] args)
{
JFrame jf = new JFrame();
WordPanel wp = new WordPanel();
wp.setBackground(Color.orange);
wp.setForeground(Color.black);
Container cp = jf.getContentPane();
cp.add(wp);
jf.setSize(600, 250);
jf.setVisible(true);
Timer t = new Timer(30, wp);
Copyright © 2002 by Ken Slonneger
41-2
// Swing
t.start();
}
}
class WordPanel extends JPanel implements ActionListener
{
int ds = 0;
// Amount to add to 10 for font size
boolean growing = true; // Is word growing or shrinking?
public void actionPerformed(ActionEvent e)
{
if (growing)
if (ds < 140) ds = ds+1;
else growing = false;
else
// shrinking
if (ds > 0) ds = ds-1;
else growing = true;
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// repaint background
g.setFont(new Font("Serif", Font.BOLD, 10+ds));
g.drawString("Growing", 20, 150);
}
}
Copyright © 2002 by Ken Slonneger
41-3
To run this program on Macintosh CodeWarrior, you need to add the
file swingall.jar to the project.
Copyright © 2002 by Ken Slonneger
41-4
CS II Preview
CS II is a continuation of CS I:
Computer science fundamentals,
software development,
and Java programming
Major Topics
1. Object-oriented programming
 Review
 Abstract classes and interfaces
 Inner classes
2. Abstract windowing toolkit and Swing
 Components
 Event handling
 Listeners and Adapters
 Buttons, Labels, Text Fields, Choices, Checkboxes
 Menus
 Mouse events
3. Exception handling
 Checked exception
 Exception objects
 try blocks
4. Data Representation
 Binary and hexadecimal
 Twos complement
Copyright © 2002 by Ken Slonneger
41-5
 Arithmetic
 Ascii and unicode
Copyright © 2002 by Ken Slonneger
41-6
5. Input and Output
 Byte and character streams
 Keyboard input
 File IO
 Serialization
6. Collection Framework
 List and Set interfaces
 ArrayList, LinkedList, HashSet, and TreeSet
 Iterators
 Map, HashMap, and TreeMap
7. Network programming
 InetAdresses
 Sockets and streams
 Clients, servers, and protocols
8. Threads
 Thread creation
 Producer-consumer problem
 Monitors and synchronization
 Animation
9. Computer organizaton
 Architecture
 Instructions
 Assembly language
 Bit operations in Java
Copyright © 2002 by Ken Slonneger
41-7
10. Software development
 Software lifecycle
 Methodologies
 UML
Use cases
Copyright © 2002 by Ken Slonneger
41-8