Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.

Slides:



Advertisements
Similar presentations
Chapter 13 Graphics.
Advertisements

Custom Painting Gestione della Grafica customizzata Vedi anche:
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
Draw Shapes Introduction to simple graphics. What is a graphics context? An instance of the Graphics class Graphics is ABSTRACT! You can extend Graphics.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 14 Graphics.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Graphics.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
1 L37 Graphics and Java 2D™ (2). 2 OBJECTIVES To use methods of class Graphics to draw lines,  rectangles,  rectangles with rounded corners,  three-dimensional.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
 2005 Pearson Education, Inc. All rights reserved Graphics and Java 2D™
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
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.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Modernās Programmēšanas Tehnoloģijas (Advanced Programming Technologies) Edgars Celms, Mārtiņš Opmanis Latvijas Universitātes Matemātikas.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Chapter 6: Graphics Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Graphics.
Java Graphics Swing Graphics
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
12 Graphics and Java 2D™.
Java Graphics CS 2511.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
JAVA 2 Design and programming of GUI
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
12 Graphics and Java 2D™.
Presentation transcript:

Chapter 16: Graphics

Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors Draw lines and shapes Learn more about fonts Draw with Java 2D graphics 2Java Programming, Seventh Edition

Learning About the paint() and repaint() Methods Rerender – To redisplay a display surface Painting – System-triggered painting – Application-triggered painting paint() method – Write your own method to override the default – Method header public void paint(Graphics g) 3Java Programming, Seventh Edition

Learning About the paint() and repaint() Methods (cont’d.) Graphics object – Preconfigured with the appropriate values for drawing on the component repaint() method – Use when a window needs to be updated – Calls the paint() method – Creates a Graphics object 4Java Programming, Seventh Edition

Using the setLocation() Method Place a component at a specific location within a JFrame ’s content pane Change the position of a component by using the setLocation() method – pressMe.setLocation(100, 50); 5Java Programming, Seventh Edition

Creating Graphics Objects Call the paint() method – Use the automatically created Graphics object – Instantiate any Graphics object Graphics draw = getGraphics(); – getGraphics() method 6Java Programming, Seventh Edition

Using the drawString() Method drawString() method – Allows you to draw a String in a JFrame window – Requires three arguments: String x-axis coordinate y-axis coordinate – Is a member of the Graphics class 7Java Programming, Seventh Edition

8 Using the drawString() Method (cont’d.)

Using the setFont() and setColor() Methods setFont() method – Requires a Font object You can instruct a Graphics object to use a font – somegraphicsobject.setFont(someFont); 9Java Programming, Seventh Edition

Using Color setColor() method – Designates a Graphics color – Use 13 Color class constants as arguments brush.setColor(Color.GREEN); 10Java Programming, Seventh Edition

Drawing Lines and Shapes Java provides several methods for drawing a variety of lines and geometric shapes 11Java Programming, Seventh Edition

Drawing Lines drawLine() method – Draws a straight line between any two points – Takes four arguments: x- and y-coordinates of the line’s starting point x- and y-coordinates of the line’s ending point 12Java Programming, Seventh Edition

Drawing Rectangles drawRect() method – Draws the outline of a rectangle fillRect() method – Draws a solid or filled rectangle Both require four arguments: – x- and y-coordinates of the upper-left corner of the rectangle – The width and height of the rectangle 13Java Programming, Seventh Edition

Drawing Rectangles (cont’d.) clearRect() method – Draws a rectangle – Requires four arguments: x- and y-coordinates of the upper-left corner of the rectangle The width and height of the rectangle – Appears empty or “clear” drawRoundRect() method – Creates rectangles with rounded corners – Requires six arguments 14Java Programming, Seventh Edition

Creating Shadowed Rectangles draw3DRect() method – A minor variation on the drawRect() method – Draws a rectangle that appears to have “shadowing” on two edges – Contains a Boolean value argument: true if the rectangle is darker on the right and bottom false if the rectangle is darker on the left and top fill3DRect() method – Creates filled three-dimensional rectangles 15Java Programming, Seventh Edition

Drawing Ovals drawOval() and fillOval() methods – Draw ovals using the same four arguments that rectangles use 16Java Programming, Seventh Edition

Drawing Arcs drawArc() method arguments: – x- and y-coordinates of the upper-left corner of an imaginary rectangle that represents the bounds of the imaginary circle that contains the arc – The width and height of the imaginary rectangle that represents the bounds of the imaginary circle that contains the arc – The beginning arc position – The arc angle 17Java Programming, Seventh Edition

Drawing Arcs (cont’d.) 18Java Programming, Seventh Edition

Drawing Arcs (cont’d.) fillArc() method – Creates a solid arc Two straight lines are drawn from the arc endpoints to the center of the imaginary circle whose perimeter the arc occupies 19Java Programming, Seventh Edition

Creating Polygons drawPolygon() method – Draws complex shapes – Requires three arguments: The integer array, which holds a series of x-coordinate positions The second array, which holds a series of corresponding y-coordinate positions The number of pairs of points to connect 20Java Programming, Seventh Edition

Creating Polygons (cont’d.) fillPolygon() method – Draws a solid shape – If the beginning and ending points are not identical, two endpoints are connected by a straight line before the polygon is filled with color addPoint() method – Adds points to a polygon indefinitely 21Java Programming, Seventh Edition

Copying an Area copyArea() method – Requires six parameters: The x- and y-coordinates of the upper-left corner of the area to be copied The width and height of the area to be copied The horizontal and vertical displacement of the destination of the copy 22Java Programming, Seventh Edition

Using the paintComponent() Method with JPanel s Use the paintComponent() method when creating drawings on a JPanel JFrame is not a child of JComponent – Does not have its own paintComponent() method 23Java Programming, Seventh Edition

Learning More About Fonts getAvailableFontFamilyNames() method – Is part of the GraphicsEnvironment class defined in the java.awt package – Returns an array of String objects that are names of available fonts You cannot instantiate the GraphicsEnvironment object directly – Get the reference object to the current computer environment Call the static getLocalGraphicsEnvironment() method 24Java Programming, Seventh Edition

Discovering Screen Statistics Using the Toolkit Class getDefaultToolkit() method – Provides information about the system in use getScreenResolution() method – Returns the number of pixels as an integer You can create a Toolkit object and get the screen resolution using the following code: Toolkit tk = Toolkit.getDefaultToolkit(); int resolution = tk.getScreenResolution(); 25Java Programming, Seventh Edition

Discovering Screen Statistics Using the Toolkit Class (cont’d.) Dimension class – Use for representing the width and height of a user interface component – Constructors: Dimension() creates an instance of Dimension with a width and height of 0 Dimension(Dimension d) creates an instance of Dimension whose width and height are the same as for the specified dimension Dimension(int width, int height) constructs a Dimension and initializes it to the specified width and height 26Java Programming, Seventh Edition

Discovering Screen Statistics Using the Toolkit Class (cont’d.) getScreenSize() method – Is a member of the Toolkit object – Returns an object of type Dimension, which specifies the width and height of the screen in pixels 27Java Programming, Seventh Edition

Discovering Font Statistics Leading – The amount of space between baselines Ascent – The height of an uppercase character from the baseline to the top of the character Descent – Measures the parts of characters that “hang below” the baseline Height of a font – The sum of leading, ascent, and descent 28Java Programming, Seventh Edition

Discovering Font Statistics (cont’d.) 29Java Programming, Seventh Edition

Discovering Font Statistics (cont’d.) getFontMetrics() method – Discovers a font’s height – Returns the FontMetrics object Use one of the FontMetrics class methods with the object to return one of a Font ’s statistics: – public int getLeading() – public int getAscent() – public int getDescent() – public int getHeight() 30Java Programming, Seventh Edition

Discovering Font Statistics (cont’d.) stringWidth() method – Returns the integer width of a String – Requires the name of the String – Is a member of the FontMetrics class 31Java Programming, Seventh Edition

Drawing with Java 2D Graphics Java 2D – Higher quality, two-dimensional (2D) graphics, images, and text Graphics2D class – Features include: Fill patterns Strokes Anti-aliasing 32Java Programming, Seventh Edition

Drawing with Java 2D Graphics (cont’d.) Graphics2D class (cont’d.) – Found in the java.awt package – Produced by casting, or converting, and promoting a Graphics object The process of drawing with Java 2D objects: – Specify the rendering attributes – Set a drawing stroke – Create objects to draw 33Java Programming, Seventh Edition

Specifying the Rendering Attributes Use the setColor() method – Specify 2D colors – Use a Graphics2D object and set the color to black gr2D.setColor(Color.BLACK); Fill patterns – Control how a drawing object is filled in – Can be a solid, gradient, texture, or pattern – Created by using the setPaint() method of Graphics2D with a fill pattern object 34Java Programming, Seventh Edition

Specifying the Rendering Attributes (cont’d.) Gradient fill – A gradual shift from one color at one coordinate point to a different color at a second coordinate point – Acyclic gradient – Cyclic gradient 35Java Programming, Seventh Edition

36Java Programming, Seventh Edition

Setting a Drawing Stroke Stroke – Represents a single movement – setStroke() method Stroke interface BasicStroke class Endcap styles – Apply to the ends of lines that do not join with other lines Juncture styles – For lines that join 37Java Programming, Seventh Edition

Setting a Drawing Stroke (cont’d.) 38Java Programming, Seventh Edition

Creating Objects to Draw Objects drawn in Java 2D are created by defining them as geometric shapes – Use the java.awt.geom package classes – Define the shape – Use the shape as an argument to the draw() or fill() methods Lines Line2D.Float Line2D.Double Point2D.Float Point2D.Double 39Java Programming, Seventh Edition

Creating Objects to Draw (cont’d.) Rectangles Rectangle2D.Float Rectangle2D.Double Rectangle2D.Float rect = new Rectangle2D.Float(10F, 10F, 50F, 40F); Ovals Ellipse2D.Float Ellipse2D.Double Ellipse2D.Float ell = new Ellipse2D.Float(10F, 73F, 40F, 20F); 40Java Programming, Seventh Edition

Creating Objects to Draw (cont’d.) Arcs Arc2D.Float Arc2D.Double Arc2D.PIE Arc2D.CHORD Arc2D.OPEN Arc2D.Float ac = new Arc2D.Float (10,133,30,33,30,120,Arc2D.PIE); 41Java Programming, Seventh Edition

Creating Objects to Draw (cont’d.) Polygons Define movements from one point to another GeneralPath object GeneralPath pol = new GeneralPath(); moveTo() lineTo() closePath() 42Java Programming, Seventh Edition

You Do It Using the drawString() Method Using Fonts and Colors Creating Your Own Graphics Object Examining Screen Coordinates Creating a Drawing Copying an Area 43Java Programming, Seventh Edition

You Do It (cont’d.) Using FontMetrics Methods to Compare Fonts Using FontMetrics Methods to Place a Border Around a String Using Drawing Strokes Working with Shapes 44Java Programming, Seventh Edition

Don’t Do It Don’t forget to call super.paint() as the first statement in the paint() method Don’t forget that the setLocation() method works correctly only when it is used after the layout manager has finished positioning all of the application’s components Don’t forget that the lower-left corner of a String is placed at the coordinates used when you call drawString() Don’t forget to use paintComponent() rather than paint() when creating graphics on a JPanel 45Java Programming, Seventh Edition

Summary paint() method drawString() method – Draws a String in a JApplet window Methods for drawing a variety of lines and geometric shapes getAvailableFontFamilyNames() method – Discovers fonts available on a system Java 2D – Higher quality 46Java Programming, Seventh Edition