Fall 2006Adapted from Java Concepts Companion Slides1 Programming Graphics Advanced Programming ICOM 4015 Lecture 14 Reading: Java Concepts Chapter 5.

Slides:



Advertisements
Similar presentations
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
Advertisements

Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Intro to Graphics.
CPSC 2100 University of Tennessee at Chattanooga – Fall 2013 Object-Oriented Design & Patterns 2 nd edition Cay S. Horstmann Chapter 4: Interface Types.
Using Classes Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Not Glasses, Classes!!!
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Java Applets A lab course by Dr. Junaid Ahmed Zubairi SUNY Fredonia.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
A Simple Applet.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
Java Unit 5: Applets and Graphics Web Pages and Viewing Applets.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
CHAPTER 2 Using Objects. Basic Programming Terminology  Computer program process values.  Numbers (digits)  Words (Strings)  These values are different.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Two: Using Objects.
Chapter Two: Using Objects. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about parameters.
Chapter 2 – Using Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Three: Implementing Classes.
Chapter 12 Event Handling. Chapter Goals To understand the Java event model To install action and mouse event listeners To accept input from buttons,
ICOM 4015: Advanced Programming Lecture 2 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Two: Using.
Chapter 3 – Implementing Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 4: Applets and Graphics 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 4 Applets and Graphics.
Chapter 4 Fundamental Data Types. Chapter Goals To be able to write simple applets To display graphical shapes such as lines and ellipses To use colors.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Previous programs used a JLabel for OUTPUT. Another Swing component that can be used for both user input and output is the JTextfield. Suppose we want.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. In order to display a drawing in a frame, define a class that extends.
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
ICOM 4015: Advanced Programming Lecture 3 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Three:
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 4 Interface Types and Polymorphism: Graphics, Timer, Animation.
CS 151: Object-Oriented Design October 1 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 3 – Implementing Classes.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
Chapter Goals To learn about variables
Chapter Goals To learn about variables
Chapter 2 Not Glasses, Classes!!! Using Classes
Chapter 5 Programming Graphics
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter 2 – Using Objects
Chapter 3 – Implementing Classes
Chapter Three: Implementing Classes
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Chapter 2 – Using Objects
Chapter Three - Implementing Classes
Chapter 12 Event Handling
Fundamental Data Types
Handout-14 Applets and Graphics
Presentation transcript:

Fall 2006Adapted from Java Concepts Companion Slides1 Programming Graphics Advanced Programming ICOM 4015 Lecture 14 Reading: Java Concepts Chapter 5

Fall 2006Adapted from Java Concepts Companion Slides2 Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors To display drawings consisting of many shapes To read input from a dialog box To develop test cases that validate the correctness of your programs

Fall 2006Adapted from Java Concepts Companion Slides3 Frame Windows The JFrame class import javax.swing.*; JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("An Empty Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true);

Fall 2006Adapted from Java Concepts Companion Slides4 A Frame Window Figure 1: A Frame Window

Fall 2006Adapted from Java Concepts Companion Slides5 File EmptyFrameViewer.java 01: import javax.swing.*; 02: 03: public class EmptyFrameViewer 04: { 05: public static void main(String[] args) 06: { 07: JFrame frame = new JFrame(); 08: 09: final int FRAME_WIDTH = 300; 10: final int FRAME_HEIGHT = 400; 11: 12: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 13: frame.setTitle("An Empty Frame"); 14: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15: 16: frame.setVisible(true); 17: } 18: }

Fall 2006Adapted from Java Concepts Companion Slides6 Self Check 1.How do you display a square frame with a title bar that reads "Hello, World!" ? 2.How can a program display two frames at once?

Fall 2006Adapted from Java Concepts Companion Slides7 Answers 1.Modify the EmptyFrameViewer program as follows: 2.Construct two JFrame objects, set each of their sizes, and call setVisible(true) on each of them frame.setSize(300, 300); frame.setTitle("Hello, World!");

Fall 2006Adapted from Java Concepts Companion Slides8 Drawing Shapes paintComponent : called whenever the component needs to be repainted: public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g;... } }

Fall 2006Adapted from Java Concepts Companion Slides9 Drawing Shapes Graphics class lets you manipulate the graphics state (such as current color) Graphics2D class has methods to draw shape objects Use a cast to recover the Graphics2D object from the Graphics parameter java.awt package Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box);

Fall 2006Adapted from Java Concepts Companion Slides10 Drawing Rectangles Figure 2: Drawing Rectangles

Fall 2006Adapted from Java Concepts Companion Slides11 Rectangle Drawing Program Classes RectangleComponent : its paintComponent method produces the drawing RectangleViewer : its main method constructs a frame and a RectangleComponent, adds the component to the frame, and makes the frame visible Continued…

Fall 2006Adapted from Java Concepts Companion Slides12 Rectangle Drawing Program Classes 1.Construct a frame 2.Construct an object of your component class: 3.Add the component to the frame However, if you use an older version of Java (before Version 5), you must make a slightly more complicated call: 4.Make the frame visible RectangleComponent component = new RectangleComponent(); frame.add(component); frame.getContentPane().add(component);

Fall 2006Adapted from Java Concepts Companion Slides13 File RectangleComponent.java 01: import java.awt.Graphics; 02: import java.awt.Graphics2D; 03: import java.awt.Rectangle; 04: import javax.swing.JPanel; 05: import javax.swing.JComponent; 06: 07: /** 08: A component that draws two rectangles. 09: */ 10: public class RectangleComponent extends JComponent 11: { 12: public void paintComponent(Graphics g) 13: { 14: // Recover Graphics2D 15: Graphics2D g2 = (Graphics2D) g; 16: Continued…

Fall 2006Adapted from Java Concepts Companion Slides14 File RectangleComponent.java 17: // Construct a rectangle and draw it 18: Rectangle box = new Rectangle(5, 10, 20, 30); 19: g2.draw(box); 20: 21: // Move rectangle 15 units to the right and 25 units // down 22: box.translate(15, 25); 23: 24: // Draw moved rectangle 25: g2.draw(box); 26: } 27: }

Fall 2006Adapted from Java Concepts Companion Slides15 File RectangleViewer.java 01: import javax.swing.JFrame; 02: 03: public class RectangleViewer 04: { 05: public static void main(String[] args) 06: { 07: JFrame frame = new JFrame(); 08: 09: final int FRAME_WIDTH = 300; 10: final int FRAME_HEIGHT = 400;11: 12: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 13: frame.setTitle("Two rectangles"); 14: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15: Continued…

Fall 2006Adapted from Java Concepts Companion Slides16 File RectangleViewer.java 16: RectangleComponent component = new RectangleComponent(); 17: frame.add(component); 18: 19: frame.setVisible(true); 20: } 21: }

Fall 2006Adapted from Java Concepts Companion Slides17 Self Check 1.How do you modify the program to draw two squares? 2.How do you modify the program to draw one rectangle and one square? 3.What happens if you call g.draw(box) instead of g2.draw(box) ?

Fall 2006Adapted from Java Concepts Companion Slides18 Answers 1. 2.Replace the call to with 3.The compiler complains that g doesn't have a draw method Rectangle box = new Rectangle(5, 10, 20, 20); box.translate(15, 25) box = new Rectangle(20, 35, 20, 20);

Fall 2006Adapted from Java Concepts Companion Slides19 Applets Applets are programs that run inside a web browser To implement an applet, use this code outline: public class MyApplet extends JApplet { public void paint(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g; // Drawing instructions go here... } }

Fall 2006Adapted from Java Concepts Companion Slides20 Applets This is almost the same outline as for a component, with two minor differences: 1.You extend JApplet, not JComponent 2.You place the drawing code inside the paint method, not inside paintComponent To run an applet, you need an HTML file with the applet tag

Fall 2006Adapted from Java Concepts Companion Slides21 Applets An HTML file can have multiple applets; add a separate applet tag for each applet You view applets with the applet viewer or a Java enabled browser appletviewer RectangleApplet.html

Fall 2006Adapted from Java Concepts Companion Slides22 File RectangleApplet.java 01: import java.awt.Graphics; 02: import java.awt.Graphics2D; 03: import java.awt.Rectangle; 04: import javax.swing.JApplet; 05: 06: /** 07: An applet that draws two rectangles. 08: */ 09: public class RectangleApplet extends JApplet 10: { 11: public void paint(Graphics g) 12: { 13: // Prepare for extended graphics 14: Graphics2D g2 = (Graphics2D) g; Continued…

Fall 2006Adapted from Java Concepts Companion Slides23 File RectangleApplet.java 15: 16: // Construct a rectangle and draw it 17: Rectangle box = new Rectangle(5, 10, 20, 30); 18: g2.draw(box); 19: 20: // Move rectangle 15 units to the right and 25 units // down 21: box.translate(15, 25); 22: 23: // Draw moved rectangle 24: g2.draw(box); 25: } 26: } 27:

Fall 2006Adapted from Java Concepts Companion Slides24 File RectangleApplet.html <applet code="RectangleApplet.class" width="300" height="400" >

Fall 2006Adapted from Java Concepts Companion Slides25 File RectangleAppletExplained.html Two rectangles Here is my first applet :

Fall 2006Adapted from Java Concepts Companion Slides26 Applets Figure 3: An Applet in the Applet Viewer

Fall 2006Adapted from Java Concepts Companion Slides27 Applets Figure 4: An Applet in a Web Browser

Fall 2006Adapted from Java Concepts Companion Slides28 Graphical Shapes Rectangle, Ellipse2D.Double, and Line2D.Double describe graphical shapes We won't use the.Float classes These classes are inner classes–doesn't matter to us except for the import statement: Must construct and draw the shape Ellipse2D.Double ellipse = new Ellipse2D.Double(x, y, width, height); g2.draw(ellipse); import java.awt.geom.Ellipse2D; // no.Double

Fall 2006Adapted from Java Concepts Companion Slides29 An Ellipse Figure 6: An Ellipse and Its Bounding Box

Fall 2006Adapted from Java Concepts Companion Slides30 Drawing Lines To draw a line: or, Line2D.Double segment = new Line2D.Double(x1, y1, x2, y2); Point2D.Double from = new Point2D.Double(x1, y1); Point2D.Double to = new Point2D.Double(x2, y2); Line2D.Double segment = new Line2D.Double(from, to);

Fall 2006Adapted from Java Concepts Companion Slides31 Drawing Strings Figure 7: Basepoint and Baseline g2.drawString("Message", 50, 100);

Fall 2006Adapted from Java Concepts Companion Slides32 Self Test 1.Give instructions to draw a circle with center (100, 100) and radius 25 2.Give instructions to draw a letter "V" by drawing two line segments 3.Give instructions to draw a string consisting of the letter "V"

Fall 2006Adapted from Java Concepts Companion Slides33 Answers 1. Line2D.Double segment1 = new Line2D.Double(0, 0, 10, 30); g2.draw(segment1); Line2D.Double segment2 = new Line2D.Double(10, 30, 20, 0); g2.draw(segment2); g2.draw(new Ellipse2D.Double(75, 75, 50, 50); g2.drawString("V", 0, 30);

Fall 2006Adapted from Java Concepts Companion Slides34 Colors Standard colors Color.BLUE, Color.RED, Color.PINK etc. Specify red, green, blue between 0.0F and 1.0F Color magenta = new Color(1.0F, 0.0F, 1.0F); // F = float Set color in graphics context Color is used when drawing and filling shapes g2.setColor(magenta); g2.fill(rectangle); // filled with current color

Fall 2006Adapted from Java Concepts Companion Slides35 Self Check 1.What are the RGB color values of Color.BLUE ? 2.How do you draw a yellow square on a red background?

Fall 2006Adapted from Java Concepts Companion Slides36 Answers F, 0.0F, and 0.1F 2. First fill a big red square, then fill a small yellow square inside: g2.setColor(Color.RED); g2.fill(new Rectangle(0, 0, 200, 200)); g2.setColor(Color.YELLOW); g2.fill(new Rectangle(50, 50, 100, 100));

Fall 2006Adapted from Java Concepts Companion Slides37 Drawing Complex Shapes Good practice: Make a class for each graphical shape Plan complex shapes by making sketches on graph paper class Car {... public void draw(Graphics2D g2) { // Drawing instructions... } }

Fall 2006Adapted from Java Concepts Companion Slides38 Drawing Cars Draw two cars: one in top-left corner of window, and another in the bottom right Compute bottom right position, inside paintComponent method: getWidth and getHeight are applied to object that executes paintComponent If window is resized paintComponent is called and car position recomputed int x = getWidth() - 60; int y = getHeight() - 30; Car car2 = new Car(x, y); Continued…

Fall 2006Adapted from Java Concepts Companion Slides39 Drawing Cars Figure 8: The Car Component Draws Two Shapes

Fall 2006Adapted from Java Concepts Companion Slides40 Plan Complex Shapes on Graph Paper Figure 9: Using Graph Paper to Find Shape Coordinates

Fall 2006Adapted from Java Concepts Companion Slides41 File CarComponent.java 01: import java.awt.Graphics; 02: import java.awt.Graphics2D; 03: import javax.swing.JComponent; 04: 05: /** 06: This component draws two car shapes. 07: */ 08: public class CarComponent extends JComponent 09: { 10: public void paintComponent(Graphics g) 11: { 12: Graphics2D g2 = (Graphics2D) g; 13: 14: Car car1 = new Car(0, 0); 15: Continued…

Fall 2006Adapted from Java Concepts Companion Slides42 File CarComponent.java 16: int x = getWidth() - Car.WIDTH; 17: int y = getHeight() - Car.HEIGHT; 18: 19: Car car2 = new Car(x, y); 20: 21: car1.draw(g2); 22: car2.draw(g2); 23: } 24: }

Fall 2006Adapted from Java Concepts Companion Slides43 File Car.java 01: import java.awt.Graphics2D; 02: import java.awt.Rectangle; 03: import java.awt.geom.Ellipse2D; 04: import java.awt.geom.Line2D; 05: import java.awt.geom.Point2D; 06: 07: /** 08: A car shape that can be positioned anywhere on the screen. 09: */ 10: public class Car 11: { 12: /** 13: Constructs a car with a given top left corner x the x coordinate of the top left corner y the y coordinate of the top left corner 16: */ Continued…

Fall 2006Adapted from Java Concepts Companion Slides44 File Car.java 17: public Car(int x, int y) 18: { 19: xLeft = x; 20: yTop = y; 21: } 22: 23: /** 24: Draws the car. g2 the graphics context 26: */ 27: public void draw(Graphics2D g2) 28: { 29: Rectangle body 30: = new Rectangle(xLeft, yTop + 10, 60, 10); 31: Ellipse2D.Double frontTire 32: = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10); 33: Ellipse2D.Double rearTire Continued…

Fall 2006Adapted from Java Concepts Companion Slides45 File Car.java 34: = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10); 35: 36: // The bottom of the front windshield 37: Point2D.Double r1 38: = new Point2D.Double(xLeft + 10, yTop + 10); 39: // The front of the roof 40: Point2D.Double r2 41: = new Point2D.Double(xLeft + 20, yTop); 42: // The rear of the roof 43: Point2D.Double r3 44: = new Point2D.Double(xLeft + 40, yTop); 45: // The bottom of the rear windshield 46: Point2D.Double r4 47: = new Point2D.Double(xLeft + 50, yTop + 10); 48: 49: Line2D.Double frontWindshield 50: = new Line2D.Double(r1, r2); Continued…

Fall 2006Adapted from Java Concepts Companion Slides46 File Car.java 51: Line2D.Double roofTop 52: = new Line2D.Double(r2, r3); 53: Line2D.Double rearWindshield 54: = new Line2D.Double(r3, r4); 55: 56: g2.draw(body); 57: g2.draw(frontTire); 58: g2.draw(rearTire); 59: g2.draw(frontWindshield); 60: g2.draw(roofTop); 61: g2.draw(rearWindshield); 62: } 63: 64: public static int WIDTH = 60; 65: public static int HEIGHT = 30; 66: private int xLeft; 67: private int yTop; 68: }

Fall 2006Adapted from Java Concepts Companion Slides47 File CarViewer.java 01: import javax.swing.JFrame; 02: 03: public class CarViewer 04: { 05: public static void main(String[] args) 06: { 07: JFrame frame = new JFrame(); 08: 09: final int FRAME_WIDTH = 300; 10: final int FRAME_HEIGHT = 400; 11: 12: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 13: frame.setTitle("Two cars"); 14: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Continued…

Fall 2006Adapted from Java Concepts Companion Slides48 File CarViewer.java 15: 16: CarComponent component = new CarComponent(); 17: frame.add(component); 18: 19: frame.setVisible(true); 20: } 21: }

Fall 2006Adapted from Java Concepts Companion Slides49 Self Check 1.Which class needs to be modified to have the two cars positioned next to each other? 2.Which class needs to be modified to have the car tires painted in black, and what modification do you need to make? 3.How do you make the cars twice as big?

Fall 2006Adapted from Java Concepts Companion Slides50 Answers 1. CarComponent 2. In the draw method of the Car class, call 3. Double all measurements in the draw method of the Car class g2.fill(frontTire); g2.fill(rearTire);

Fall 2006Adapted from Java Concepts Companion Slides51 Drawing Graphical Shapes Rectangle leftRectangle = new Rectangle(100, 100, 30, 60); Rectangle rightRectangle = new Rectangle(160, 100, 30, 60); Line2D.Double topLine = new Line2D.Double(130, 100, 160, 100); Line2D.Double bottomLine = new Line2D.Double(130, 160, 160, 160);

Fall 2006Adapted from Java Concepts Companion Slides52 Computer Graphics Figure 10: Diagrams

Fall 2006Adapted from Java Concepts Companion Slides53 Computer Graphics Figure 11: Scene

Fall 2006Adapted from Java Concepts Companion Slides54 Computer Graphics Figure 12: Manipulated Image

Fall 2006Adapted from Java Concepts Companion Slides55 Reading Text Input A graphical application can obtain input by displaying a JOptionPane The showInputDialog method displays a prompt and waits for user input The showInputDialog method returns the string that the user typed Continued… String input = JOptionPane.showInputDialog("Enter x"); double x = Double.parseDouble(input);

Fall 2006Adapted from Java Concepts Companion Slides56 Reading Text Input Figure 13: An Input Dialog Box

Fall 2006Adapted from Java Concepts Companion Slides57 File ColorViewer.java 01: import java.awt.Color; 02: import javax.swing.JFrame; 03: import javax.swing.JOptionPane; 04: 05: public class ColorViewer 06: { 07: public static void main(String[] args) 08: { 09: JFrame frame = new JFrame(); 10: 11: final int FRAME_WIDTH = 300; 12: final int FRAME_HEIGHT = 400; 13: 14: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 15: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16: 17: String input; 18: Continued…

Fall 2006Adapted from Java Concepts Companion Slides58 File ColorViewer.java 19: // Ask the user for red, green, blue values 20: 21: input = JOptionPane.showInputDialog("red:"); 22: double red = Double.parseDouble(input); 23: 24: input = JOptionPane.showInputDialog("green:"); 25: double green = Double.parseDouble(input); 26: 27: input = JOptionPane.showInputDialog("blue:"); 28: double blue = Double.parseDouble(input); 29: 30: Color fillColor = new Color( 31: (float) red, (float) green, (float) blue); 32: ColoredSquareComponent component 33: = new ColoredSquareComponent(fillColor); 34: frame.add(component); 35: 36: frame.setVisible(true); 37: } 38: }

Fall 2006Adapted from Java Concepts Companion Slides59 File ColoredSquareComponent.java 01: import java.awt.Color; 02: import java.awt.Graphics; 03: import java.awt.Graphics2D; 04: import java.awt.Rectangle; 05: import javax.swing.JComponent; 06: 07: /** 08: A component that shows a colored square. 09: */ 10: public class ColoredSquareComponent extends JComponent 11: { 12: /** 13: Constructs a component that shows a colored square. aColor the fill color for the square 15: */ 16: public ColoredSquareComponent(Color aColor) Continued…

Fall 2006Adapted from Java Concepts Companion Slides60 File ColoredSquareComponent.java 17: { 18: fillColor = aColor; 19: } 20: 21: public void paintComponent(Graphics g) 22: { 23: Graphics2D g2 = (Graphics2D) g; 24: 25: // Select color into graphics context 26: 27: g2.setColor(fillColor); 28: 29: // Construct and fill a square whose center is 30: // the center of the window 31: Continued…

Fall 2006Adapted from Java Concepts Companion Slides61 File ColoredSquareComponent.java 32: final int SQUARE_LENGTH = 100; 33: 34: Rectangle square = new Rectangle( 35: (getWidth() - SQUARE_LENGTH) / 2, 36: (getHeight() - SQUARE_LENGTH) / 2, 37: SQUARE_LENGTH, 38: SQUARE_LENGTH); 39: 40: g2.fill(square); 41: } 42: 43: private Color fillColor; 44: }

Fall 2006Adapted from Java Concepts Companion Slides62 Output Figure 14: A Square Filled with a User- Specified Color

Fall 2006Adapted from Java Concepts Companion Slides63 Self Check 1.Why does this program produce three separate dialog boxes instead of inviting the user to type all three values in a single dialog box? 2.Why does this program place the showInputDialog call into the main method of the ColorViewer class and not into the paintComponent method of the ColorComponent class?

Fall 2006Adapted from Java Concepts Companion Slides64 Answers 1.If the user entered a string, such as " ", you would need to break it up into three separate strings. That can be done, but it is more tedious to program than three calls to showInputDialog. 2.You don't want the dialog boxes to appear every time the component is repainted.

Fall 2006Adapted from Java Concepts Companion Slides65 Comparing Visual and Numerical Information Compute intersection between circle and vertical line Circle has radius r = 100 and center (a, b) = (100, 100) Line has constant x value Continued…

Fall 2006Adapted from Java Concepts Companion Slides66 Comparing Visual and Numerical Information Calculate intersection points using mathematics: Equation of a circle with radius r and center point (a, b) is If you know x, then you can solve for y:

Fall 2006Adapted from Java Concepts Companion Slides67 Comparing Visual and Numerical Information That is easy to compute in Java: Plot circle, line, computed intersection points Visual and numerical results should be the same double root = Math.sqrt(r * r - (x - a) * (x - a)); double y1 = b + root; double y2 = b - root;

Fall 2006Adapted from Java Concepts Companion Slides68 Intersection of a Line and a Circle Figure 15 Intersection of a Line and a Circle

Fall 2006Adapted from Java Concepts Companion Slides69 File IntersectionComponent.java 01: import java.awt.Graphics; 02: import java.awt.Graphics2D; 03: import java.awt.geom.Ellipse2D; 04: import java.awt.geom.Line2D; 05: import javax.swing.JComponent; 06: 07: /** 08: A component that computes and draws the intersection points 09: of a circle and a line. 10: */ 11: public class IntersectionComponent extends JComponent 12: { 13: /** 14: Constructs the component from a given x-value for the line anX the x-value for the line (between 0 and 200) 16: */ Continued…

Fall 2006Adapted from Java Concepts Companion Slides70 File IntersectionComponent.java 17: public IntersectionComponent(double anX) 18: { 19: x = anX; 20: } 21: 22: public void paintComponent(Graphics g) 23: { 24: Graphics2D g2 = (Graphics2D) g; 25: 26: // Draw the circle 27: 28: final double RADIUS = 100; 29: 30: Ellipse2D.Double circle 31: = new Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS); 32: g2.draw(circle); 33: 34: // Draw the vertical line 35: Continued…

Fall 2006Adapted from Java Concepts Companion Slides71 File IntersectionComponent.java 36: Line2D.Double line 37: = new Line2D.Double(x, 0, x, 2 * RADIUS); 38: g2.draw(line); 39: 40: // Compute the intersection points 41: 42: double a = RADIUS; 43: double b = RADIUS; 44: 45: double root = Math.sqrt(RADIUS * RADIUS - (x - * (x - a)); 46: double y1 = b + root; 47: double y2 = b - root; 48: 49: // Draw the intersection points 50: 51: LabeledPoint p1 = new LabeledPoint(x, y1); 52: LabeledPoint p2 = new LabeledPoint(x, y2); Continued…

Fall 2006Adapted from Java Concepts Companion Slides72 File IntersectionComponent.java 53: 54: p1.draw(g2); 55: p2.draw(g2); 56: } 57: 58: private double x; 59: }

Fall 2006Adapted from Java Concepts Companion Slides73 File IntersectionViewer.java 01: import javax.swing.JFrame; 02: import javax.swing.JOptionPane; 03: 04: public class IntersectionViewer 05: { 06: public static void main(String[] args) 07: { 08: JFrame frame = new JFrame(); 09: 10: final int FRAME_WIDTH = 300; 11: final int FRAME_HEIGHT = 400; 12: 13: frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); 14: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15: Continued…

Fall 2006Adapted from Java Concepts Companion Slides74 File IntersectionViewer.java 16: String input = JOptionPane.showInputDialog("Enter x"); 17: double x = Double.parseDouble(input); 18: IntersectionComponent component 19: = new IntersectionComponent(x); 20: frame.add(component); 21: 22: frame.setVisible(true); 23: } 24: }

Fall 2006Adapted from Java Concepts Companion Slides75 File LabeledPoint.java 01: import java.awt.Graphics2D; 02: import java.awt.geom.Ellipse2D; 03: 04: /** 05: A point with a label showing the point's coordinates. 06: */ 07: public class LabeledPoint 08: { 09: /** 10: Construct a labeled point. anX the x coordinate aY the y coordinate 13: */ 14: public LabeledPoint(double anX, double aY) 15: { 16: x = anX;17: y = aY; 18: } Continued…

Fall 2006Adapted from Java Concepts Companion Slides76 File LabeledPoint.java 19: 20: /** 21: Draws the point as a small circle with a coordinate label. g2 the graphics context23: */ 24: public void draw(Graphics2D g2) 25: { 26: // Draw a small circle centered around (x, y) 27: 28: Ellipse2D.Double circle = new Ellipse2D.Double( 29: x - SMALL_CIRCLE_RADIUS, 30: y - SMALL_CIRCLE_RADIUS, 31: 2 * SMALL_CIRCLE_RADIUS, 32: 2 * SMALL_CIRCLE_RADIUS); 33: 34: g2.draw(circle); 35: 36: // Draw the label Continued…

Fall 2006Adapted from Java Concepts Companion Slides77 File LabeledPoint.java 37: 38: String label = "(" + x + "," + y + ")"; 39: 40: g2.drawString(label, (float) x, (float) y); 41: } 42: 43: private static final double SMALL_CIRCLE_RADIUS = 2; 44: 45: private double x; 46: private double y; 47: }

Fall 2006Adapted from Java Concepts Companion Slides78 Self Check 1.Suppose you make a mistake in the math, say, by using a + sign instead of a - sign in the formula for root. How can you tell that the program does not run correctly? 2.Which intersection points does the program draw when you provide an input of 0?

Fall 2006Adapted from Java Concepts Companion Slides79 Answers 1.The intersection points will be drawn at a location that is different from the true intersection of the line and the circle 2.The point (0, 100) is drawn twice