1 / 39

Animation Revisited

Animation Revisited. The Timer class is in Swing. Animation can be done without using the Timer class. Using threads. Example: Rebound2.java. import java.applet.Applet; import java.awt.*; public class Rebound2 extends Applet implements Runnable {

josiah
Download Presentation

Animation Revisited

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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); }

  4. 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){} } }

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

  6. Graphical 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

  7. The Component Hierarchy

  8. The Swing Components

  9. Layout Managers • The layout of the elements in a container is handled by the layout managerassociated 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 • .

  10. The Layout Manager Hierarchy

  11. Buttons and Flow Layout width=400 height=50 width=100 height=120

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

  13. Border Layout

  14. 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); } }

  15. Grid Layout row=0 col=1 row=3 col=2 row=1 col=0

  16. 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; }

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

  18. Nested Panels

  19. Nested 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);

  20. 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);

  21. 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); } }

  22. Swing Frame Demo

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

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

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

  26. 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);

  27. 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); }

  28. 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()); } }

  29. 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()); } } }

  30. 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

  31. Package Declaration • packagename; • Example: package cs1; public class Keyboard {   // ... } package edu.depaul.csc224; public class MyProject {   // ...   public static void main(String[] args) {     // ...   } }

  32. Package Directory default package . (current working directory) cs1  .\cs1\ edu.depaul.csc224 .\edu\depaul\csc224\ Package and Directory Structure • The directory structure must match the package structure

  33. Using 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

  34. Jar 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

  35. 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 directory • classpath switch of javac and java • CLASSPATH environment variable • extension mechanism: place JAR file in C:\jdk1.3\jre\lib\ext\

  36. Class 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

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

  38. Class Path Examples • Pack cs.Keyboard in a JAR file. C:\   |-- csc224   |     +-- MyProg.java   |   +-- book         +-- kb.jar

  39. 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

More Related