Title: Animation Revisited
1Animation Revisited
- The Timer class is in Swing.
- Animation can be done without using the Timer
class. - Using threads
2Example Rebound2.java
import java.applet.Applet import
java.awt. public class Rebound2 extends Applet
implements Runnable private final int
APPLET_WIDTH 200 private final int
APPLET_HEIGHT 100 private final int
IMAGE_SIZE 35 private final int DELAY
20 private Thread thread private int delay
100 private Image image private int x,
y, moveX, moveY
3Example Rebound2.java
public void init() x 0 y 40
moveX moveY 3 image getImage
(getCodeBase(), "happyFace.gif")
setBackground (Color.black) public void
paint (Graphics page) page.drawImage
(image, x, y, this)
4Example Rebound2.java
public void run() while
(Thread.currentThread() thread) x
moveX y moveY if (x lt 0
x gt APPLET_WIDTH-IMAGE_SIZE) moveX
moveX -1 if (y lt 0 y gt
APPLET_HEIGHT-IMAGE_SIZE) moveY moveY
-1 repaint() try
Thread.currentThread().sleep(delay)
catch (InterruptedException e)
5Example Rebound2.java
public void start() thread new
Thread(this) thread.start() public
void stop() thread null
6Graphical User Interface (GUI)
- Abstract Windows Toolkit (AWT) java.awt
- GUI elements
- Primitive
- Button, Label, Checkbox, Scrollbar, etc.
- Container
- Panel, Frame, Dialog, etc.
- Layout managers
- FlowLayout, BorderLayout, etc.
- Supporting classes
7The Component Hierarchy
8The Swing Components
9Layout Managers
- The layout of the elements in a container is
handled by the layout manager associated with the
container. - Relative positions of the elements are specified,
not their absolute coordinates. - The positions and sizes of the element will be
automatically adjusted when the window is resized
10The Layout Manager Hierarchy
11Buttons and Flow Layout
width400 height50
width100 height120
12Buttons and Flow Layout
Layout elements in horizontal rows. import
java.awt. import java.applet.Applet public
class Flow extends Applet public Flow ()
setLayout(new FlowLayout()) add(new
Button("Java")) add(new Button("C"))
add(new Button("Perl")) add(new
Button("Ada")) add(new Button("Smalltalk"))
add(new Button("Eiffel"))
13Border Layout
14Border Layout
import java.awt. import java.applet.Applet pub
lic class Border extends Applet public Border
() setLayout(new BorderLayout())
add(new Button("North"), BorderLayout.NORTH)
add(new Button("South"), BorderLayout.SOUTH)
add(new Button("East"), BorderLayout.EAST)
add(new Button("West"), BorderLayout.WEST)
add(new Button("Center"), BorderLayout.CENTER)
15Grid Layout
row0 col1
row3 col2
row1 col0
16Grid Layout
import java.awt. import java.applet.Applet pub
lic class Grid extends Applet public void
init () int row 0, col 0 String
att getParameter("row") if (att ! null)
row Integer.parseInt(att) att
getParameter("col") if (att ! null)
col Integer.parseInt(att) if (row 0
col 0) row 3 col 2
17Grid Layout
setLayout(new GridLayout(row, col))
add(new Button("Java")) add(new
Button("C")) add(new Button("Perl"))
add(new Button("Ada")) add(new
Button("Smalltalk")) add(new
Button("Eiffel"))
18Nested Panels
19Nested Panels
public class NestedPanels extends Applet
protected Label messageBar protected Choice
choice public NestedPanels () // set up
the center panel Panel center new Panel()
center.setLayout(new BorderLayout())
center.add(new Button("south"),
BorderLayout.SOUTH) center.add(new
Button("north"),
BorderLayout.NORTH) center.add(new
Button("east"),
BorderLayout.EAST) center.add(new
Button("west"),
BorderLayout.WEST) center.add(new
Button("center"),
BorderLayout.CENTER)
20Nested Panels
// set up the south panel Panel south new
Panel() south.setLayout(new FlowLayout())
south.add(new Button("Help")) choice new
Choice() choice.addItem("one") choice.addItem("t
wo") choice.addItem("three") choice.addItem("fou
r") choice.addItem("five") south.add(choice) me
ssageBar new Label("This is a message bar.")
south.add(messageBar)
21Nested Panels
// set up the outer panel setLayout(new
BorderLayout()) add(new Button("North"),
BorderLayout.NORTH) add(new Button("East"),
BorderLayout.EAST) add(new Button("West"),
BorderLayout.WEST) add(south,
BorderLayout.SOUTH) add(center,
BorderLayout.CENTER)
22Swing Frame Demo
23Example FrameDemo.java
import java.awt. import javax.swing. public
class FrameDemo extends JFrame public
FrameDemo() super("Swing Frame Demo")
setDefaultCloseOperation(EXIT_ON_CLOSE)
setSize(400, 200) public static void
main(String args) FrameDemo frame new
FrameDemo() frame.getContentPane().setLayout
(new BorderLayout()) frame.getContentPane().
add(new NestedPanels2(),
BorderLayout.CENTER) frame.show()
24Example NestedPanels2.java
import java.awt. import javax.swing. import
java.awt.event. public class NestedPanels2
extends JPanel JLabel message_bar
JComboBox choice JButton southButton1 new
JButton("south") JButton northButton1 new
JButton("north") JButton eastButton1 new
JButton("east") JButton westButton1 new
JButton("west") JButton centerButton1 new
JButton("center") JButton northButton2 new
JButton("North") JButton eastButton2 new
JButton("East") JButton westButton2 new
JButton("West") JButton helpButton new
JButton("Help")
25Example NestedPanels2.java
public NestedPanels2 () JPanel center new
JPanel() center.setLayout(new
BorderLayout()) center.add(southButton1,
BorderLayout.SOUTH) center.add(northButton1,
BorderLayout.NORTH) center.add(eastButton1,
BorderLayout.EAST) center.add(westButton1,
BorderLayout.WEST) center.add(centerButton1,
BorderLayout.CENTER) JPanel south new
JPanel() south.setLayout(new FlowLayout())
south.add(helpButton)
26Example NestedPanels2.java
choice new JComboBox() choice.addItem("one"
) choice.addItem("two") choice.addItem("thre
e") choice.addItem("four")
choice.addItem("five") south.add(choice)
message_bar new JLabel("This is a message
bar.") south.add(message_bar)
setLayout(new BorderLayout())
add(northButton2, BorderLayout.NORTH)
add(eastButton2, BorderLayout.EAST)
add(westButton2, BorderLayout.WEST) add(south,
BorderLayout.SOUTH) add(center,
BorderLayout.CENTER)
27Example NestedPanels2.java
ChoiceEventHandler chandler new
ChoiceEventHandler() choice.addItemListene
r(chandler) ButtonEventHandler bhandler
new ButtonEventHandler() southButton1.addActi
onListener(bhandler) northButton1.addActionList
ener(bhandler) eastButton1.addActionListener(bh
andler) westButton1.addActionListener(bhandler)
centerButton1.addActionListener(bhandler)
DialogButtonEventHandler dhandler new
DialogButtonEventHandler() northButton2.addAct
ionListener(dhandler) eastButton2.addActionList
ener(dhandler) westButton2.addActionListener(dh
andler) helpButton.addActionListener(bhandler)
28Example NestedPanels2.java
class ChoiceEventHandler implements
ItemListener public void itemStateChanged(It
emEvent event) JComboBox choice
(JComboBox) event.getSource() if (choice
! null) message_bar.setText("Choice
selected "
event.getItem()) class
ButtonEventHandler implements ActionListener
public void actionPerformed(ActionEvent event)
JButton source (JButton)
event.getSource() if (source ! null)
message_bar.setText("Button pushed "
source.getText())
29Example NestedPanels2.java
class DialogButtonEventHandler
implements ActionListener public void
actionPerformed(ActionEvent event)
JButton source (JButton) event.getSource()
if (source ! null) JOptionPane.showMes
sageDialog(null, "Button pushed "
source.getText())
30Using Packages
- A package contains classes and other packages.
Packages form a hierarchy. - Package names
- pkg1, pkg1.pag2, pkg1.pkg2.pkg3, ...
- Convention use the reverse of the internet
domain of your company/organization - e.g., edu.depaul.csc224
31Package Declaration
- package name
- Example
- package cs1
- public class Keyboard  // ... Â
- package edu.depaul.csc224
- public class MyProject  // ...  public
static void main(String args) Â Â Â // ... Â
32Package and Directory Structure
- The directory structure must match the package
structure
33Using Packages Examples
- Example cs1.Keyboard
- Source code .\cs1\Keyboard.java
- Compile in the current working directory
- javac cs1\Keyboard.java
- Byte code file .\Keyboard.class
- Example edu.depaul.csc224.MyProject
- Source code .\edu\depaul\csc224\MyProject.java
- Compile in the current working directory
- javac edu\depaul\csc224\.java
- Byte code file .\edu\depaul\csc224\.class
- Run in the current working directory
- java edu.depaul.csc224.MyProject
34Jar Files and Jar Tool
- To pack files and directories into a single file
for the purpose of distribution, deployment, etc. - Pack jar cvf filename.jar files-or-dirs
- Unpack jar xvf filename.jar
- Examples
- pack all the classes in the default package
- jar cvf hw.jar .class
- pack all the classes in the cs1 package
- jar cvf kb.jar cs1\Keyboard.class
- pack all the classes in the edu.depaul.csc224
package - jar cvf csc224.jar edu\depaul\csc224\.class
- pack all the classes in the edu package
- jar cvf edu.jar edu
35Class Path
- where the Java compiler and virtual machine look
for the source or byte code files. - consists of a list directories and/or jar files,
separated by - Â (on Windows)
- Â (on Unix/Linux)
- default all the J2SDK standard classes and the
current directory - classpath switch of javac and java
- CLASSPATH environment variable
- extension mechanism place JAR file in
C\jdk1.3\jre\lib\ext\
36Class Path Examples
- Example Using cs1.Keyboard class in MyProg.java
in the default package - MyProg.java and cs1 are in the same directory
- C\ Â -- csc224 Â Â Â Â Â Â Â -- MyProg.java
       -- cs1             -- Keyboard.class
- javac MyProg.java
- java MyProg
37Class Path Examples
- MyProg.java and cs1 are in different directory
- C\ Â -- csc224 Â Â Â Â Â -- MyProg.java Â
 -- book        -- cs1             --
Keyboard.class - In csc224
- javac -classpath C\book MyProg.java
- java -classpath C\book MyProg
- Or
- set CLASSPATHC\book
- javac MyProg.java
- java MyProg
38Class Path Examples
- Pack cs.Keyboard in a JAR file.
- C\ Â -- csc224 Â Â Â Â Â -- MyProg.java Â
 -- book        -- kb.jar
39Class Path Examples
- In C\csc224
- javac -classpath C\book\kb.jar MyProg.java
- java -classpath C\book\kb.jar MyProg
- Or
- set CLASSPATHC\book\kb.jar
- javac MyProg.java
- java MyProg
- Or
- copy kb.jar to C\jdk1.3\jre\lib\ext\
- javac MyProg.java
- java MyProg