Java Coordinate System

Similar documents
Unit 7: Event driven programming

Chủ đề 3: Graphics & Java2D

Lecture 7 A First Graphic Program And Data Structures & Drawing

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

G51PRG: Introduction to Programming Second semester Applets and graphics

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Graphics Applets. By Mr. Dave Clausen

Graphic User Interfaces. - GUI concepts - Swing - AWT

Appendix F: Java Graphics

Java Applet Basics. Life cycle of an applet:

User interfaces and Swing

Graphical User Interfaces 2

Here is a list of a few of the components located in the AWT and Swing packages:

Appendix F: Java Graphics

Graphics Applets. By Mr. Dave Clausen

Lecture 3: Java Graphics & Events

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Graphical User Interfaces 2

Chapter 14: Applets and More

AP CS Unit 12: Drawing and Mouse Events

Programming graphics

SIMPLE APPLET PROGRAM

Lecture 5: Java Graphics

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

Graphics -- To be discussed

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

AP CS Unit 11: Graphics and Events

Graphical User Interfaces 2

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Programming: You will have 6 files all need to be located in the dir. named PA4:

FirstSwingFrame.java Page 1 of 1

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Computer Science 210: Data Structures. Intro to Java Graphics

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

Object Orientated Programming in Java. Benjamin Kenwright

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

cs Java: lecture #5

DM503 Programming B. Peter Schneider-Kamp.

Learn Java Programming, Dr.Hashamdar. Getting Started with GUI Programming

CS2110. GUIS: Listening to Events

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

public static void main(string[] args) { GTest mywindow = new GTest(); // Title This program creates the following window and makes it visible:

AplusBug dude = new AplusBug(); A+ Computer Science -

Programming Languages and Techniques (CIS120)

Introduction to the JAVA UI classes Advanced HCI IAT351

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

IT101. Graphical User Interface

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

We are on the GUI fast track path

Graphics and Painting

Course: CMPT 101/104 E.100 Thursday, November 23, 2000

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

TWO-DIMENSIONAL FIGURES

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

2IS45 Programming

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Assignment 2. Application Development

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Advanced Java Unit 6: Review of Graphics and Events

Dr. Hikmat A. M. AbdelJaber

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Java Programming Lecture 6

PESIT Bangalore South Campus

CS 201 Advanced Object-Oriented Programming Lab 1 - Improving Your Image Due: Feb. 3/4, 11:30 PM

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

Final Examination Semester 2 / Year 2011

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

Programming Mobile Devices J2SE GUI

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

Java - Applets. public class Buttons extends Applet implements ActionListener

AP Computer Science Unit 13. Still More Graphics and Animation.

APPENDIX. public void cekroot() { System.out.println("nilai root : "+root.data); }

Image Java Foundation Classes (JFC) java.awt.image JFC. Image. Image. Image PNG GIF JPEG

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Programmierpraktikum

UI Software Organization

Final Examination Semester 2 / Year 2012

PIC 20A GUI with swing

CS193k, Stanford Handout #3. Swing 1

JFrame In Swing, a JFrame is similar to a window in your operating system

Chapter 7 Applets. Answers

Chapter 12 Advanced GUIs and Graphics

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Implementing Graphical User Interfaces

CSIS 10A Assignment 14 SOLUTIONS

Packages: Putting Classes Together

Object-oriented programming in Java (2)

Transcription:

Java Graphics Drawing shapes in Java such as lines, rectangles, 3-D rectangles, a bar chart, or a clock utilize the Graphics class Drawing Strings Drawing Lines Drawing Rectangles Drawing Ovals Drawing Arcs Drawing Polygons Copyright 2012 R.M. Laurie 1 Java Coordinate System x (0, 0) X Axis Y Axis y Y Axis (x, y) Java Coordinate System Convention al Coordinate System (0, 0) X Axis Each GUI Component Has its Own Coordinate System (x3, y3) (0, 0) Component c3 c3 s coordinate system (x2, y2) (0, 0) Component c2 (x1, y1) (0, 0) Component c1 c2 s coordinate system c1 s coordinate system Copyright 2012 R.M. Laurie 2 Copyright 2012 R.M. Laurie 1

JPanel class paintcomponent method paintcomponent method used to draw graphics JPanel is canvas upon which you paint components paintcomponent method utilized by Java Virtual Machine to draw things on a component as needed JPanel class paintcomponent method should never be invoked directly in program, let JVM do it Subclass needs to be created to inherit JPanel Define a class that extends JPanel Override JPanel paintcomponent method so it can be utilized to specify what to draw paintcomponent method is set to protected to prevent user invocation Copyright 2012 R.M. Laurie 3 1. import javax.swing.*; Graphics TestPaint Example 2. import java.awt.graphics; 3. public class TestPaint extends JFrame { 4. public TestPaint() { 5. GraphicsPanel gpnmain = new GraphicsPanel(); 6. add(new GraphicsPanel()); 8. public static void main(string[] args) { 9. TestPaint framain = new TestPaint(); 10. framain.settitle("testpaintcomponent"); 11. framain.setsize(200, 200); 12. framain.setlocationrelativeto(null); // Center the frame 13. framain.setdefaultcloseoperation(jframe.exit_on_close); 14. framain.setvisible(true); 15. } 16. } 17. // Overriding the JPanel class paintcomponent method 18. // requires creating new class and inherit JPanel 19. class GraphicsPanel extends JPanel { 20. // protected visibility to prevent user from invoking 21. protected void paintcomponent(graphics gfxdraw) { 22. // Call superclass paintcomponent to clear canvas 23. super.paintcomponent(gfxdraw); 24. gfxdraw.drawstring("this is how you draw a circle", 10, 20); 25. gfxdraw.drawoval(40, 40, 100, 100); 26. gfxdraw.drawline(90, 90, 125, 125); 27. gfxdraw.drawstring("radius", 80, 125); 28. } 29. } Copyright 2012 R.M. Laurie 2

Drawing Strings drawstring(string s, int x, int y); (0, 0) (getwidth(), 0) drawline(int x1, int y1, int x2, int y2); (0, 0) (g etwidth(), 0) (x1, y1) (x, y) s is display (x2, y2) (0, g etheight()) (getwidth(), getheight()) (0, getheight()) (getwidth(), getheight()) drawoval(int x, int y, int w, int h); (x, y) h w Copyright 2012 R.M. Laurie 5 Graphics Class You can draw strings, lines, rectangles, ovals, arcs, polygons, and polylines, using the methods in the Graphics class java.awt.graphics +setcolor(color: Color): void +setfont(font: Font): void +drawstring(s: Strin g, x: int, y: int): void +drawline(x1: int, y1: int, x2: in t, y2: int): void +drawrect(x: int, y: int, w: int, h : int): void +fillrect(x: in t, y: int, w: int, h: int): void +drawroundrect(x: int, y: int, w: int, h: int, aw: int, ah: int): void +fillroundrect(x: int, y: in t, w: int, h: int, aw: int, ah: int): void +draw3drect(x: int, y: int, w: int, h: int, raised: boolean): void +fill3drect(x: int, y: int, w: int, h: int, raised: boolean): void +drawoval(x: int, y: int, w: int, h : int): void +filloval(x: int, y: int, w: int, h: int): void +drawarc(x: int, y: int, w: int, h: int, startangle: int, arcangle: int): void +fillarc(x: int, y: in t, w: int, h: int, startangle: int, arcangle: int): void +drawpolygon(xpoints: int[], ypoints: int[], npoints: int): void +fillpolygon(xpoints: int[], ypoints: int[], npoints: int): void +drawpolygon(g: Polygon ): void +fillpolygon(g: Polygon): void +drawpolyline(xpoints: int[], ypoints: int[], npoints: int): void Sets a new color for subsequent drawings. Sets a new font for subsequent drwings. Draws a string starting at point (x, y). Draws a line from (x1, y1) to (x2, y2). Draws a rectangle with specified upper-left corner poin t at (x, y) and wid th w and height h. Draws a filled rectangle with specified upper-left corner poin t at (x, y) and width w and height h. Draws a round -cornered rectangle with specified arc width aw and arc height ah. Draws a filled round-cornered rectangle with specified arc width aw and arc height ah. Draws a 3-D rectangle raised above the surface or sunk into the surface. Draws a filled 3-D rectangle raised above the surface or sunk into the surface. Draws an oval bounded by the rectangle specified by th e parameters x, y, w, and h. Draws a filled oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws an arc conceived as part of an oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws a filled arc conceived as part of an oval bounded by the rectangle specified by the parameters x, y, w, and h. Draws a closed polygon defined b y arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point. Draws a filled polygon defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point. Draws a closed polygon defined b y a Polygon object. Draws a filled polygon defined by a Polygon object. Draws a polyline defined by arrays of x and y coordinates. Each pair of (x[i], y[i]) coordinates is a point. Copyright 2012 R.M. Laurie 3

Drawing Rectangles drawrect(int x, int y, int w, int h); ( x, y) fillrect(int x, int y, int w, int h); ( x, y) h w h (x, y) ah /2 w aw /2 h drawroundrect(int x, int y, int w, int h, int aw, int ah); w fillroundrect(int x, int y, int w, int h, int aw, int ah); Copyright 2012 R.M. Laurie 7 Stacking Order is Last On Top and Color and Font Running as an Applet 1. import javax.swing.*; 2. import java.awt.*; 3. public class PaintInColor extends JApplet { 4. // Applet extends JApplet not JFrame and delete main() 5. public PaintInColor() { 6. add(new PaintCanvas()); 8. } 9. class PaintCanvas extends JPanel { 10. protected void paintcomponent(graphics gfxpaintcolors) { 11. super.paintcomponent(gfxpaintcolors); 12. // For color changes set before draw method 13. // Stacking order is Last draw on Top 14. setbackground(new Color(204, 255, 204)); // Change JPanel background color 15. gfxpaintcolors.setcolor(new Color( 255, 255, 153)); // Change brush color 16. gfxpaintcolors.fillrect(0, 0, 600, 40); 17. gfxpaintcolors.setcolor(new Color( 153, 0, 0)); // Change brush color 18. gfxpaintcolors.setfont(new Font("Arial", Font.BOLD, 20)); // Change Font 19. gfxpaintcolors.drawstring("setting Colors is Easy", 30, 30); 20. gfxpaintcolors.setcolor(color.black); // Change brush color 21. gfxpaintcolors.drawroundrect(10, 50, 240, 100, 30, 30); 22. gfxpaintcolors.setcolor(color.dark_gray); // Change brush color 23. gfxpaintcolors.fillroundrect(20, 60, 220, 80, 26, 26); 24. gfxpaintcolors.setcolor(new Color( 255, 220, 153)); // Change brush color 25. gfxpaintcolors.setfont(new Font("Consolas", Font.BOLD, 14)); // Change Font 26. gfxpaintcolors.drawstring("change color or font", 40, 90); 27. gfxpaintcolors.drawstring("before you apply the", 40, 110); 28. gfxpaintcolors.drawstring("draw or fill methods!", 40, 130); 29. } 30. } Copyright 2012 R.M. Laurie 4

Drawing Polylines and Polygons int[] x = {40, 70, 60, 45, 20}; int[] y = {20, 40, 80, 45, 60}; g.drawpolyline(x,y, x.length); (x[0], y[0]) (x[3], y[3]) (x[1], y[1]) int[] x = {40, 70, 60, 45, 20}; int[] y = {20, 40, 80, 45, 60}; g.drawpolygon(x, y, x.length); (x[0], y[0]) (x[4], y[4]) (x[3], y[3]) (x[2], y[2]) (x[4], y[4]) (x[1], y[1]) (x[2], y[2]) // Using Polygon Class to draw Polygon polygon = new Polygon(); polygon.addpoint(40, 59); polygon.addpoint(40, 100); polygon.addpoint(10, 100); g.drawpolygon(polygon); Copyright 2012 R.M. Laurie 9 Drawing Polygons Example Running as an Applet 1. import javax.swing.*; 2. import java.awt.*; 3. public class PentagonStar extends JApplet { 4. // Applet by extends JApplet not JFrame and delete main() 5. public PentagonStar() { 6. add(new PentagonCanvas()); 8. } 9. class PentagonCanvas extends JPanel { 10. protected void paintcomponent(graphics gfxpentagon) { 11. super.paintcomponent(gfxpentagon); 12. setbackground(new Color(0, 102, 0)); // Change JPanel background color 13. // For color changes set before draw method 14. // Stacking order is Last draw on Top 15. gfxpentagon.setcolor(new Color( 255, 255, 153)); // Change brush color 16. gfxpentagon.fillrect(0, 0, 600, 40); 17. gfxpentagon.setcolor(new Color( 153, 0, 0)); // Change brush color 18. gfxpentagon.setfont(new Font("Arial", Font.BOLD, 20)); // Change Font 19. gfxpentagon.drawstring("pentagon and Star", 10, 30); 20. gfxpentagon.setcolor(new Color( 102, 204, 102)); // Change brush color 21. int nxpenta[] = {100, 170, 140, 60, 30}; 22. int nypenta[] = {60, 105, 180, 180, 105}; 23. gfxpentagon.fillpolygon(nxpenta, nypenta, nxpenta.length); 24. gfxpentagon.setcolor(new Color(255, 51, 51)); // Change brush color 25. gfxpentagon.drawpolygon(nxpenta, nypenta, nxpenta.length); 26. int nxstar[] = {100, 140, 30, 170, 60}; 27. int nystar[] = {60, 180, 105, 105, 180}; 28. gfxpentagon.setcolor(new Color(255, 255, 0)); // Change brush color 29. gfxpentagon.drawpolygon(nxstar, nystar, nxpenta.length); 30. } 31. } Copyright 2012 R.M. Laurie 5

Graphics2D class has more controls 1. import javax.swing.*; 2. import java.awt.*; 3. import java.awt.geom.*; // Access 2D shapes 4. public class PentagonStar2D extends JApplet { 5. public PentagonStar2D() { 6. add(new PentaStar2DCanvas()); 8. } 9. class PentaStar2DCanvas extends JPanel { 10. protected void paintcomponent(graphics gfxpentastar) { 11. super.paintcomponent(gfxpentastar); 12. setbackground(new Color(204, 255, 204)); 13. // Graphics2D object is cast from Graphics object 14. Graphics2D g2dpntstr = (Graphics2D)gfxPentaStar; 15. gfxpentastar.setcolor(new Color(255, 255, 153)); 16. gfxpentastar.fillrect(0, 0, 600, 40); 17. gfxpentastar.setcolor(new Color( 153, 0, 0)); 18. gfxpentastar.setfont(new Font("Arial", Font.BOLD, 20)); 19. gfxpentastar.drawstring("pentagon and Star", 10, 30); 20. gfxpentastar.setcolor(new Color( 51, 153, 51)); 21. int nxpenta[] = {100, 170, 140, 60, 30}; 22. int nypenta[] = {60, 105, 180, 180, 105}; 23. gfxpentastar.fillpolygon(nxpenta, nypenta, nxpenta.length); 24. gfxpentastar.setcolor(new Color(153, 103, 0)); // Change brush color 25. g2dpntstr.setstroke(new BasicStroke(10.0f, BasicStroke.CAP_ROUND, 26. BasicStroke.JOIN_MITER)); 27. g2dpntstr.setbackground(color.red); 28. Polygon shppentagon = new Polygon(nXpenta, nypenta, nxpenta.length); 29. g2dpntstr.draw(shppentagon); 30. int nxstar[] = {100, 140, 30, 170, 60}; 31. int nystar[] = {60, 180, 105, 105, 180}; 32. g2dpntstr.setstroke(new BasicStroke(4.0f, BasicStroke.CAP_ROUND, 33. BasicStroke.JOIN_MITER)); 34. gfxpentastar.setcolor(new Color(255, 255, 0)); // Change brush color 35. g2dpntstr.draw(new Polygon(nXstar, nystar, nxstar.length)); // Draw Star 36. } 3 Displaying Image Icons Can display image icons in labels and buttons An image icon displays a fixed-size image java.awt.image class provides resize flexibility Image can be created from an image icon using the getimage() 1. import java.awt.*; 2. import javax.swing.*; 3. public class DisplayImage extends JApplet { 4. public DisplayImage() { 5. add(new PlayPanel()); 6. } 8. class ImagePanel extends JPanel { 9. private ImageIcon icnduck = new ImageIcon("resources/BalloonDuck.png"); 10. private Image imgduck = icnduck.getimage(); 11. /** Draw image on the panel */ 12. protected void paintcomponent(graphics gfxmain) { 13. super.paintcomponent(gfxmain); 14. setbackground(new Color(204, 204, 255)); 15. if (imgduck!= null) 16. gfxmain.drawimage(imgduck, 0, 0, 143, 200, this); 1 18. } Copyright 2012 R.M. Laurie 12 Copyright 2012 R.M. Laurie 6

Event ActionListener ZoomIn & ZoomOut 1. import java.awt.*; 2. import java.awt.event.*; 3. import javax.swing.*; 4. public class BalloonGame extends JApplet { 5. private JButton btnzoomin = new JButton("Zoom In"); 6. private JButton btnzoomout = new JButton("Zoom Out"); 7. private BalloonPanel pancanvas = new BalloonPanel(); 8. public BalloonGame() { 9. JPanel pancontrols = new JPanel(); 10. pancontrols.add(btnzoomin); 11. pancontrols.add(btnzoomout); 12. this.setlayout(new BorderLayout(5, 5)); 13. this.add(pancanvas, BorderLayout.CENTER); 14. this.add(pancontrols, BorderLayout.NORTH); 15. btnzoomin.addactionlistener(new ActionListener() { 16. public void actionperformed(actionevent eventzmin) { 17. pancanvas.zoomin(); 18. } 19. } ); 20. btnzoomout.addactionlistener(new ActionListener() { 21. public void actionperformed(actionevent eventzmout) { 22. pancanvas.zoomout(); 23. } 24. } ); 25. } 26. } Content from Section 16.3 Copyright 2012 R.M. Laurie 13 Event ActionListener ZoomIn & ZoomOut Content from Section 16.3 1. import java.awt.*; 2. import javax.swing.*; 3. class BalloonPanel extends JPanel { 4. private int nh = 200, nw = 143; 5. private ImageIcon icnduck = new ImageIcon("resources/BallonDuck.png"); 6. private Image imgduck = icnduck.getimage(); 7. public void zoomin() { 8. nh *= 1.11111; 9. nw *= 1.11111; 10. repaint(); 11. } 12. public void zoomout() { 13. nh *= 0.9; 14. nw *= 0.9; 15. repaint(); 16. } 17. protected void paintcomponent(graphics gfxballoon) { 18. super.paintcomponent(gfxballoon); 19. setbackground(new Color(204, 204, 255)); 20. if (imgduck!= null) 21. gfxballoon.drawimage(imgduck, 0, 0, nw, nh, this); 22. } 23. } Copyright 2012 R.M. Laurie 14 Copyright 2012 R.M. Laurie 7

java.awt.event.inputevent +getwhen(): long +isaltdown(): boolean +iscontroldown(): boolean +ismetadown(): boolean +isshiftdown(): boolean MouseEvent Returns the timestamp when this event occurred. Returns whether or not the Alt modifier is down on this event. Returns whether or not the Control modifier is down on this event. Returns whether or not the Meta modifier is down on this event Returns whether or not the Shift modifier is down on this event. java.awt.event.mouseevent +getbutton(): int +getclickcount(): int +getpoint(): java.awt.point +getx(): int +gety(): int java.awt.event.mouselistener +mousepressed(e: MouseEvent): void+mousereleased(e: MouseEvent): void+mouseclicked(e: MouseEvent): void+mouseentered(e: MouseEvent): void +mouseexited(e: MouseEvent): void Indicates which mouse button has been clicked. Returns the number of mouse clicks associated with this event. Returns a Point object containing the x and y coordinates. Returns the x-coordinate of the mouse point. Returns the y-coordinate of the mouse point. Invoked when mouse button pressed on source component. Invoked when mouse button released the source component. Invoked when mouse button clicked (pressed and released) Invoked when the mouse enters the source component. Invoked when the mouse exits the source component. With Timer and MouseListener Events 1. import java.awt.*; 2. import java.awt.event.*; 3. import javax.swing.*; 4. import java.net.url; 5. import java.applet.*; 6. public class BalloonGame1 extends JApplet { 7. private JButton btnzoomin = new JButton("Zoom In"); 8. private JButton btnzoomout = new JButton("Zoom Out"); 9. private JButton btnflame = new JButton("Flame"); 10. private BalloonPanel pancanvas = new BalloonPanel(); 11. public BalloonGame1() { 12. JPanel pancontrols = new JPanel(); 13. pancontrols.add(btnzoomin); 14. pancontrols.add(btnzoomout); 15. pancontrols.add(btnflame); 16. this.setlayout(new BorderLayout(5, 5)); 17. this.add(pancanvas, BorderLayout.CENTER); 18. this.add(pancontrols, BorderLayout.NORTH); 19. btnzoomin.addactionlistener(new ActionListener() { 20. public void actionperformed(actionevent eventzmin) { 21. pancanvas.zoomin(); 22. } 23. } ); 24. btnzoomout.addactionlistener(new ActionListener() { 25. public void actionperformed(actionevent eventzmout) { 26. pancanvas.zoomout(); 2 28. } ); 29. btnflame.addmouselistener(new MouseAdapter() { 30. public void mousepressed(mouseevent eventflame) { 31. pancanvas.flameon(); 32. } 33. public void mousereleased(mouseevent eventflame) { 34. pancanvas.flameoff(); 35. } 36. } ); 3 38. } Decent and Flame Content from Section 16.10 Copyright 2012 R.M. Laurie 16 Copyright 2012 R.M. Laurie 8

Graphics Panel and GUI Event Methods Content from Section 16.10 39. class BalloonPanel extends JPanel { 40. private int nh = 200, nw = 143, nx = 20, ny = 0, ntickx, nticky = 100; 41. private int nhalfwidth, nheight; 42. private double dh = 200, dw = 143, dtrig, dvely, dy; 43. private boolean bflameon; 44. private String smessage = ""; 45. private URL urlballoonduck = getclass().getresource("resources/balloonduck.png"); 46. private ImageIcon icnduck = new ImageIcon(urlBalloonDuck); 47. private Image imgduck = icnduck.getimage(); 48. URL urlflame = getclass().getresource("resources/flame.wav"); 49. private AudioClip audioflame = Applet.newAudioClip(urlFlame); 50. BalloonPanel() { 51. Timer tmr30msec = new Timer(33, new TimerListener()); 52. tmr30msec.start(); 53. } 54. public void zoomin() { 55. dh *= 1.11111; 56. dw *= 1.11111; 57. nh = (int)dh; 58. nw = (int)dw; 59. repaint(); 60. } 61. public void zoomout() { 62. dh *= 0.9; 63. dw *= 0.9; 64. nh = (int)dh; 65. nw = (int)dw; 66. repaint(); 6 68. public void flameon() { 69. bflameon = true; 70. audioflame.play(); 71. nticky = 500; 72. } 73. public void flameoff() { 74. bflameon = false; 75. audioflame.stop(); 76. } 77. Timer ActionListener Class and paintcomponent method Content from Section 16.10 78. protected void paintcomponent(graphics gfxballoon) { 79. super.paintcomponent(gfxballoon); 80. setbackground(new Color(204, 204, 255)); 81. gfxballoon.drawstring(string.format("x = %d", nx), 10, 15); 82. gfxballoon.drawstring(string.format("y = %d", ny), 10, 30); 83. gfxballoon.drawstring(string.format("dy/dt = %6.3f", dvely), 10, 45); 84. gfxballoon.drawstring(string.format("%s", smessage), 10, 60); 85. if(imgduck!= null) 86. gfxballoon.drawimage(imgduck, nx, ny, nw, nh, this); 8 88. class TimerListener implements ActionListener { 89. public void actionperformed(actionevent eventtimer) { 90. nhalfwidth = (getwidth()/2 - nw/2); 91. nheight = getheight(); 92. if(ny < nheight - nh) { 93. ntickx++; 94. nticky++; 95. dtrig = Math.sin((double)nTickX/100); 96. nx = nhalfwidth - (int)((double)nhalfwidth * dtrig); 97. dvely = (0.0005 * (double)nticky/100) + dvely; 98. dy = (dvely * (double)nticky/100) + dy; 99. ny = (int)dy; 100. smessage = ""; 101. } 102. else 103. { 104. if(dvely > 0.1 && dvely!= 0) 105. smessage = String.format("You Crashed: dy/dt = %6.3f > 0.1", dvely); 106. else if(dvely <= 0.1 && dvely!= 0) 107. smessage = String.format("Soft Landing: dy/dt = %6.3f > 0.1", dvely); 108. dvely = 0; 109. } 110. if(bflameon) { 111. dvely = -(0.002 * (double)nticky/100) + dvely; 112. dy = (dvely * (double)nticky/100) + dy; 113. ny = (int)dy; 114. } 115. repaint(); 116. } 11 118. } Copyright 2012 R.M. Laurie 9