1 / 21

Applications of Java to Physics Teaching (Part II)

Applications of Java to Physics Teaching (Part II). S S Tong Department of Physics CUHK. 6. Simple Animations. Animations many frames displayed in a given order Thread part of the program running on its own multi-tasking ( actually multi-threading) How to create a thread?

ross
Download Presentation

Applications of Java to Physics Teaching (Part II)

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. Applications of Java to Physics Teaching (Part II) S S Tong Department of Physics CUHK

  2. 6. Simple Animations • Animations • many frames displayed in a given order • Thread • part of the program running on its own • multi-tasking (actually multi-threading) • How to • create a thread? • display the frames? • terminate the thread under certain conditions?

  3. calls the run() method • Implement the interface Runnable • Declare a Thread public class Spin extends Applet implements Runnable { Thread runner = null; ...... } • Load a series of image for (int i = 0; i < 16; i++) spin[i] = getImage(getCodeBase(), "images/Spin" + i + ".gif"); • Create and run a Thread runner = new Thread(this); runner.start();

  4. Create a loop, call the paint method repeatedly public void run() { int i = 0; while (runner != null) { currentImage = spin[i]; repaint(); i++; if (i > 15) i = 0; pause(50); } } public void paint(Graphics screen) { if (currentImage != null) { screen.drawImage( currentImage, 100, 100, this); ...... } }

  5. call paint(...)once init() images loaded ...getImage(...) Thread created and initialized ...new Thread(this) runner.start() run() paint(...) loop inside while (...) { ...... } draw images repaint() • Flow of the applet • Stop a thread runner.stop(); runner = null;

  6. call the paint method directly • Reduce flickering • Override the update(...) method, so that the screen is not erased before repainting public void update(Graphics screen) { paint(screen); } • Create an off-screen image and a Graphics object • do all drawing on the off-screen image first • replace the on-screen image with the updated off-screen image ...... Graphics offScreen; Image offScreenImage; public void init() { offScreenImage = createImage(300,300); ......

  7. associate the off-screen image and Graphics erasing off-screen drawing off-screen put the off-screen image on-screen • Reduce flickering (contiune) public void paint(Graphics screen) { j++; offScreen = offScreenImage.getGraphics(); if (currentImage != null) { offScreen.setColor(Color.white); offScreen.fillRect(0,0,300,300); offScreen.drawImage(currentImage, 100, 100, this); offScreen.setColor(Color.red); offScreen.drawString("Frame no " + j, 30, 30); screen.drawImage(offScreenImage, 0, 0, this); } }

  8. 7 Building simple interface • Basic components Label TextField Button Checkbox Choice

  9. items contents length • Adding components to an applet • Declare the object type Button theButton; Checkbox theCheckbox; Choice theChoice; TextField theField; • Assign labels and attributes to the components theButton = new Button("I am a button"); theCheckbox = new Checkbox("I am a checkbox"); theChoice = new Choice(); theChoice.add("I am item 1"); theChoice.add("I am item 2"); theChoice.add("I am item 3"); theField = new TextField("long field", 20); • Add the components to the applet add(...);

  10. set background color set size draw something on the canvas • Canvas - an area for drawing • create a subclass of Canvas class MyCanvas extends Canvas { MyCanvas() { setBackground(Color.cyan); setSize(300,300); } public void paint(Graphics screen) { ...... } } • create an instance of this subclass in the applet canvas = new MyCanvas(); • repaint the canvas canvas.repaint();

  11. North West Center East South • How to arrange the components? • Use Layout Manager • We discuss the BorderLayout() only public void init() { setLayout(new BorderLayout()); ......

  12. add("Center", canvas); Panel p = new Panel(); p.add(theLabel); p.add(theButton); ...... add("South",p) • Adding components add("South", buttonA); add("Center", canvasB); • Using a panel

  13. ActionListener for Button ItemListener for Choice and Checkbox Listeners are interfaces • Firing events from the components • Listeners: • Implementing the Listeners import java.awt.event.*; public class EventTest extends Applet implements ActionListener, ItemListener { ...... • Adding Listeners to the component button1.addActionListener(this); theChoice.addItemListener(this); theCheckbox.addItemListener(this);

  14. identify which component triggers the event get the index of the selected item get the state (true/false) of the checkbox • Buttons trigger the actionPerformed method public void actionPerformed(ActionEvent evt) { if (evt.getSource() == button1) ...... if (evt.getSource() == button2) ...... canvas.repaint(); } • Choice menus and checkboxes trigger itemStateChanged method public void itemStateChanged(ItemEvent evt) { if (evt.getSource() == theChoice) { int itemNo = theChoice.getSelectedIndex(); ....... if (evt.getSource() == theCheckbox) { boolean boxState = theCheckbox.getState(); .......

  15. EventCanvas is defined inside EventTest - an inner class • Updating the canvas import java.awt.event.*; ........ public class EventTest extends Applet implements ActionListener, ItemListener { String currentString = ""; ...... public void actionPerformed(ActionEvent evt) { ...... canvas.repaint(); } public void itemStateChanged(ItemEvent evt) { ...... canvas.repaint(); } class EventCanvas extends Canvas {....... screen.drawString(currentString, 50, 50); ...... } }

  16. gives the text of field field.getText() field.setText("string") sets the text of field to "string" call to the superclass's constructor string  double double  string • Number I/O for a TextField • Converting numbers to strings and vice versa class MyTextField extends TextField { MyTextField(String s, int l) { super(s,l); } double getNumber() { return Double.valueOf( getText()).doubleValue(); } void setNumber(double num) { setText(String.valueOf(num)); } }

  17. Example AnimateWaves.java • superposition of two waves • amplitude, wavelengths, periods read from text fields • mode of display - choice menu • start and stop - buttons • Exercise ProjectileEx.java • initial position and velocity read from text fields • start and reset - buttons • Example ShootHead.java • Modification of Projectile.java • Shooting a free-falling object

  18. MouseListener mouse pressed, released, enter, exit mouse moved, dragged MouseMotionListener Listeners are interfaces • Listening to mouse events • Listeners: • Implementing the Listeners import java.awt.event.*; public class EventTest extends Applet implements MouseListener, MouseMotionListener { ...... • Adding Listeners to the component canvas.addMouseListener(this); canvas.addMouseMotionListener(this);

  19. Let the canvas listens to mouse events • Interact with hot spots, drag and drop objects etc. • Mouse events trigger public void mousePressed(MouseEvent evt) {...} public void mouseClicked(MouseEvent evt) {...} public void mouseReleased(MouseEvent evt) {...} public void mouseEntered(MouseEvent evt) {...} public void mouseExited(MouseEvent evt) {...} • Mouse motion events trigger public void mouseMoved(MouseEvent evt) {...} public void mouseDragged(MouseEvent evt) {...} • Gets the mouse position inside this methods e.g., Point p = evt.getPoint();

  20. Example AddVectors.java • addition and subtraction of two vectors • changing the text fields updates the screen • dragging the vectors updates the text fields

  21. 8 References • L. Lemay and R. Cadenhead, Teach Yourself Java 2 in 21 days, SAMS • M. Campione and K. Walrath, The Java Tutorial (2nd Ed), Addison Wesley • http://java.sun.com/

More Related