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.

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

Chapter 2: Using Objects Part 2. Assume you wish to test the behaviour of some method. This is accomplished by providing a tester class: Supply a main.
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.
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.
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.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
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.
Web Design & Development Lecture 18. Java Graphics.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
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,
Java Applets. Topics What is an applet? To convert GUI applications to Applets Hello World applet example!! Applets life cycle examples Invoking applets.
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.
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.
Introduction to Java Applets Sangeetha Parthasarathy 05/21/2001.
Intro to Applets August 19, 2008 Mrs. C. Furman. Java Applets vs. Java Applications Java Applet: a program that is intended for use on the web. Java Applet:
Fall 2006Adapded from Java Concepts Companion Slides1 Event Handling Advanced Programming ICOM 4015 Lecture 13 Reading: Java Concepts Chapter 12.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
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.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance Java Applets –what is an applet vs. an application? –where do applets.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine and Ten Graphics.
ICOM 4015: Advanced Programming Lecture 3 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Three:
Creating a GUI with JFC/Swing. What are the JFC and Swing? JFC –Java Foundation Classes –a group of features to help people build graphical user interfaces.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Graphical User Interfaces Lecture 24, Tue Apr
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 2 – Using Objects.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 9 Chapter.
Fall 2006Adapted from Java Concepts Companion Slides1 Programming Graphics Advanced Programming ICOM 4015 Lecture 14 Reading: Java Concepts Chapter 5.
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.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Event Handling and GUI Components.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
2/4: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
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.
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
Import javax.swing.*; class Check { public static void main(String[] args) { JFrame frame = new JFrame("Check"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//DO_NOTHING_ON_CLOSE.
Chapter Goals To learn about variables
Chapter Goals To learn about variables
Chapter 2 Not Glasses, Classes!!! Using Classes
Chapter 5 Programming Graphics
Chapter 2 – Using Objects
Chapter Three: Implementing Classes
GUI AND GRAPHICS.
Using Objects.
Chapter 2 – Using Objects
Chapter Three - Implementing Classes
Chapter 12 Event Handling
Drawing in Java.
Handout-14 Applets and Graphics
Outline Character Strings Variables and Assignment
Presentation transcript:

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 the JComponent class Place drawing instructions inside the paintComponent method. That method is called whenever the component needs to be repainted: public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { Drawing instructions go here } } 2.12 Drawing on a Component

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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: public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) { // Recover Graphics2D Graphics2D g2 = (Graphics2D) g;... } } Classes Graphics and Graphics2D

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Call method draw of the Graphics2D class to draw shapes, such as rectangles, ellipses, line segments, polygons, and arcs: public class RectangleComponent extends JComponent { public void paintComponent(Graphics g) {... Rectangle box = new Rectangle(5, 10, 20, 30); g2.draw(box);... } Classes Graphics and Graphics2D

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Drawing Rectangles

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1 import java.awt.Graphics; 2 import java.awt.Graphics2D; 3 import java.awt.Rectangle; 4 import javax.swing.JComponent; 5 6 /** 7 A component that draws two rectangles. 8 */ 9 public class RectangleComponent extends JComponent 10 { 11 public void paintComponent(Graphics g) 12 { 13 // Recover Graphics2D 14 Graphics2D g2 = (Graphics2D) g; // Construct a rectangle and draw it 17 Rectangle box = new Rectangle(5, 10, 20, 30); 18 g2.draw(box); 19 ch02/rectangles/RectangleComponent.java 20 // Move rectangle 15 units to the right and 25 units down 21 box.translate(15, 25); // Draw moved rectangle 24 g2.draw(box); 25 } 26 }

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.Construct a frame. 2.Construct an object of your component class: RectangleComponent component = new RectangleComponent(); 3.Add the component to the frame: frame.add(component); 4.Make the frame visible. Using a Component

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1 import javax.swing.JFrame; 2 3 public class RectangleViewer 4 { 5 public static void main(String[] args) 6 { 7 JFrame frame = new JFrame(); 8 9 frame.setSize(300, 400); 10 frame.setTitle("Two rectangles"); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); RectangleComponent component = new RectangleComponent(); 14 frame.add(component); frame.setVisible(true); 17 } 18 } ch02/rectangles/RectangleViewer.java

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. How do you modify the program to draw two squares? Answer: Rectangle box = new Rectangle(5, 10, 20, 20); Self Check 2.29

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. How do you modify the program to draw one rectangle and one square? Answer: Replace the call to box.translate(15, 25) with box = new Rectangle(20, 35, 20, 20); Self Check 2.30

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. What happens if you call g.draw(box) instead of g2.draw(box) ? Answer: The compiler complains that g doesn’t have a draw method. Self Check 2.31

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Applet: program that runs 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... } } Applets

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 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 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 Applets

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1 import java.awt.Graphics; 2 import java.awt.Graphics2D; 3 import java.awt.Rectangle; 4 import javax.swing.JApplet; 5 6 /** 7 An applet that draws two rectangles. 8 */ 9 public class RectangleApplet extends JApplet 10 { 11 public void paint(Graphics g) 12 { 13 // Prepare for extended graphics 14 Graphics2D g2 = (Graphics2D) g; // Construct a rectangle and draw it 17 Rectangle box = new Rectangle(5, 10, 20, 30); 18 g2.draw(box); 19 ch02/applet/RectangleApplet.java 20 // Move rectangle 15 units to the right and 25 units down 21 box.translate(15, 25); 22 // Draw moved rectangle 23 g2.draw(box); 24 } 25 }

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1 2 ch02/applet/RectangleApplet.html

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved Two rectangles Here is my first applet : ch02/applet/RectangleAppletExplained.html

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Applets