39
Animation Revisited The Timer class is in Swing. Animation can be done without using the Timer class. Using threads

Animation Revisited The Timer class is in Swing. Animation can be done without using the Timer class. Using threads

  • View
    233

  • Download
    0

Embed Size (px)

Citation preview

Animation Revisited

The Timer class is in Swing. Animation can be done without using the Timer class.Using threads

Example: Rebound2.javaimport 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;

Example: 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); }

Example: Rebound2.java public void run() { while (Thread.currentThread() == thread) { x += moveX; y += moveY; if (x <= 0 || x >= APPLET_WIDTH-IMAGE_SIZE) moveX = moveX * -1; if (y <= 0 || y >= APPLET_HEIGHT-IMAGE_SIZE) moveY = moveY * -1; repaint(); try { Thread.currentThread().sleep(delay); } catch (InterruptedException e){} } }

Example: Rebound2.java public void start() { thread = new Thread(this); thread.start(); }

public void stop() { thread = null; }

}

Graphical User Interface (GUI)

Abstract Windows Toolkit (AWT): java.awtGUI elements: Primitive

Button, Label, Checkbox, Scrollbar, etc. Container

Panel, Frame, Dialog, etc.

Layout managers: FlowLayout, BorderLayout, etc.

Supporting classes

The Component Hierarchy

The Swing Components

Layout 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

.

The Layout Manager Hierarchy

Buttons and Flow Layout

width=400 height=50

width=100 height=120

Buttons 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")); }}

Border Layout

Border Layout

import java.awt.*;import java.applet.Applet;

public 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); }}

Grid Layout

row=3 col=2

row=1 col=0

row=0 col=1

Grid Layout

import java.awt.*;import java.applet.Applet;

public 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; }

Grid 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")); }}

Nested Panels

Nested Panelspublic 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);

Nested 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("two");choice.addItem("three");choice.addItem("four");choice.addItem("five");south.add(choice);messageBar = new Label("This is a message bar."); south.add(messageBar);

Nested 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); }

}

Swing Frame Demo

Example: FrameDemo.javaimport 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(); }}

Example: NestedPanels2.javaimport 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");

Example: NestedPanels2.javapublic 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);

Example: NestedPanels2.java choice = new JComboBox(); choice.addItem("one"); choice.addItem("two"); choice.addItem("three"); 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);

Example: NestedPanels2.java ChoiceEventHandler chandler = new ChoiceEventHandler(); choice.addItemListener(chandler); ButtonEventHandler bhandler = new ButtonEventHandler(); southButton1.addActionListener(bhandler); northButton1.addActionListener(bhandler); eastButton1.addActionListener(bhandler); westButton1.addActionListener(bhandler); centerButton1.addActionListener(bhandler); DialogButtonEventHandler dhandler = new DialogButtonEventHandler(); northButton2.addActionListener(dhandler); eastButton2.addActionListener(dhandler); westButton2.addActionListener(dhandler); helpButton.addActionListener(bhandler);}

Example: NestedPanels2.java class ChoiceEventHandler implements ItemListener { public void itemStateChanged(ItemEvent 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()); } }

Example: NestedPanels2.java class DialogButtonEventHandler implements ActionListener { public void actionPerformed(ActionEvent event) { JButton source = (JButton) event.getSource(); if (source != null) JOptionPane.showMessageDialog(null, "Button pushed: " + source.getText()); } } }

Using 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

Package Declaration

package name;Example:

package cs1; public class Keyboard {

  // ... }  

package edu.depaul.csc224; public class MyProject {

  // ...   public static void main(String[] args) {     // ...   } }

Package and Directory Structure

The directory structure must match the package structure

Package Directory

default package. (current working directory)

cs1  .\cs1\edu.depaul.csc224

.\edu\depaul\csc224\

Using Packages: ExamplesExample: 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

Jar Files and Jar ToolTo 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

Class 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 directoryclasspath switch of javac and javaCLASSPATH environment variableextension mechanism: place JAR file in C:\jdk1.3\jre\lib\ext\

Class Path Examples

Example: Using cs1.Keyboard class in MyProg.java in the default packageMyProg.java and cs1 are in the same directory C:\   +-- csc224         |-- MyProg.java         +-- cs1              +-- Keyboard.class

javac MyProg.javajava MyProg

Class Path ExamplesMyProg.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 CLASSPATH=C:\book javac MyProg.java java MyProg

Class Path Examples

Pack cs.Keyboard in a JAR file. C:\

  |-- csc224   |     +-- MyProg.java   |   +-- book

        +-- kb.jar

Class Path Examples

In C:\csc224 javac -classpath C:\book\kb.jar MyProg.java java -classpath C:\book\kb.jar MyProg

Or set CLASSPATH=C:\book\kb.jar javac MyProg.java java MyProg

Or copy kb.jar to C:\jdk1.3\jre\lib\ext\ javac MyProg.java java MyProg