 2005 Pearson Education, Inc. All rights reserved. 1 12 Graphics and Java 2D™

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
Chapter 9 Color, Sound and Graphics
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.
1 Graphics -- Introduction zThe use of graphics is common among modern software systems  Java has strong API support for graphics in the java.awt (abstract.
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.
Graphics in Java Part II. Lecture Objectives Learn how to use Graphics in Java.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects.
©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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 12.1 Introduction 12.2 Graphics Contexts and Graphics Objects 12.3 Color Control 12.4 Font Control.
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.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
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.
Chapter 11 - Graphics and Java 2D Outline 11.1 Introduction 11.2 Graphics Contexts and Graphics Objects 11.3 Color Control 11.4 Font Control 11.5 Drawing.
Modernās Programmēšanas Tehnoloģijas (Advanced Programming Technologies) Edgars Celms, Mārtiņš Opmanis Latvijas Universitātes Matemātikas.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java 2D Outline 11.1 Introduction 11.2 Graphics Contexts and Graphics Objects.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
2D Graphics: Rendering Details
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
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.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java2D Outline 11.1Introduction 11.2Graphics Contexts and Graphics Objects 11.3Color.
(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.
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.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Java Graphics and Java2D
Intro to Graphics from Chapter 2 of Java Software Solutions
12 Graphics and Java 2D™.
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Java Graphics CS 2511.
JAVA 2 Design and programming of GUI
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
12 Graphics and Java 2D™.
Chapter 8 Graphics.
Presentation transcript:

 2005 Pearson Education, Inc. All rights reserved Graphics and Java 2D™

 2005 Pearson Education Inc. All rights reserved. 2 One picture is worth ten thousand words. — Chinese proverb Treat nature in terms of the cylinder, the sphere, the cone, all in perspective. — Paul Cézanne Colors, like features, follow the changes of the emotions. — Pablo Picasso Nothing ever becomes real till it is experienced— even a proverb is no proverb to you till your life has illustrated it. — John Keats

 2005 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.  To understand and be able to manipulate fonts.  To use methods of class Graphics to draw lines, rectangles, rectangles with rounded corners, three- dimensional rectangles, ovals, arcs and polygons.  To use methods of class Graphics2D from the Java 2D API to draw lines, rectangles, rectangles with rounded corners, ellipses, arcs and general paths.  To be able to specify Paint and Stroke characteristics of shapes displayed with Graphics2D.

 2005 Pearson Education, Inc. All rights reserved Introduction 12.2 Graphics Contexts and Graphics Objects 12.3 Color Control 12.4 Font Control 12.5 Drawing Lines, Rectangles and Ovals 12.6 Drawing Arcs 12.7 Drawing Polygons and Polylines 12.8 Java 2D API 12.9 Wrap-Up

 2005 Pearson Education, Inc. All rights reserved Introduction Java contains support for graphics that enable programmers to visually enhance applications Java contains many more sophisticated drawing capabilities as part of the Java 2D API Classes – Color – Font, FontMetrics – Graphics2D – Polygon – BasicStroke – GradientPaint, TexturePaint – Java 2D shape classes

 2005 Pearson Education, Inc. All rights reserved. 6 Fig | Classes and interfaces used in this chapter from Java’s original graphics capabilities and from the Java 2D API. [Note: Class Object appears here because it is the superclass of the Java class hierarchy.]

 2005 Pearson Education, Inc. All rights reserved Introduction Java coordinate system – Upper-left corner of a GUI component has the coordinates (0, 0) – Contains x-coordinate (horizontal coordinate) - horizontal distance moving right from the left of the screen – Contains y-coordinate (vertical coordinate) - vertical distance moving down from the top of the screen Coordinate units are measured in pixels. A pixel is a display monitor’s smallest unit of resolution.

 2005 Pearson Education, Inc. All rights reserved. 8 Fig | Java coordinate system. Units are measured in pixels.

 2005 Pearson Education, Inc. All rights reserved. 9 Portability Tip 12.1 Different display monitors have different resolutions (i.e., the density of the pixels varies). This can cause graphics to appear to be different sizes on different monitors or on the same monitor with different settings.

 2005 Pearson Education, Inc. All rights reserved Graphics Contexts and Graphics Objects A Java graphics context enables drawing on the screen Class Graphics – Manages a graphics context and draws pixels on the screen – An abstract class – contributes to Java’s portability Method paintComponent – Used to draw graphics – Member of class JComponent, subclass of Component – Graphics object passed to paintComponent by the system when a lightweight Swing component needs to be repainted – If programmer needs to have paintComponent execute, a call is made to method repaint

 2005 Pearson Education, Inc. All rights reserved Color Control Class Color declares methods and constants for manipulating colors in a Java program Every color is created from a red, a green and a blue component – RGB values

 2005 Pearson Education, Inc. All rights reserved. 12 Fig | Color constants and their RGB values.

 2005 Pearson Education, Inc. All rights reserved. 13 Fig | Color methods and color-related Graphics methods.

 2005 Pearson Education, Inc. All rights reserved. 14 Outline ColorJPanel.java (1 of 2) Method paintComponent paints JPanel Set current drawing color with method setColor Draw filled rectangle using current color Draw text value of current colorSet current drawing color, specify float arguments to Color constructor Set current drawing color using Color constant

 2005 Pearson Education, Inc. All rights reserved. 15 Outline ColorJPanel.java (2 of 2) Retrieving RGB values using methods getRed, getGreen and getBlue

 2005 Pearson Education, Inc. All rights reserved. 16 Outline ShowColors.java

 2005 Pearson Education, Inc. All rights reserved. 17 Look-and-Feel Observation 12.1 Everyone perceives colors differently. Choose your colors carefully to ensure that your application is readable. Try to avoid using many different colors in close proximity.

 2005 Pearson Education, Inc. All rights reserved. 18 To change the color, you must create a new Color object (or use one of the predeclared Color constants). Like String objects, Color objects are immutable (not modifiable). Software Engineering Observation 12.1

 2005 Pearson Education, Inc. All rights reserved Color Control JColorChooser GUI component enables application users to select colors – Method showDialog creates a JColorChooser object, attaches it to a dialog box and displays the dialog – Modal dialog – Allows the user to select a color from a variety of color swatches – Tabs – Swatches, HSB and RGB

 2005 Pearson Education, Inc. All rights reserved. 20 Outline ShowColors2JFrame.java (1 of 2) Import JColorChooser class

 2005 Pearson Education, Inc. All rights reserved. 21 Outline ShowColors2JFrame.java (2 of 2) Display JColorChooser dialog Reference to parent componentTitle bar text Initial selected color Change background color of JPanel

 2005 Pearson Education, Inc. All rights reserved. 22 Outline ShowColors2.java (1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 23 Outline ShowColors2.java (2 of 2) Select a color from one of the color swatches.

 2005 Pearson Education, Inc. All rights reserved. 24 Fig | HSB and RGB tabs of the JColorChooser dialog. Sliders to select the red, green and blue color components

 2005 Pearson Education, Inc. All rights reserved Font Control Class Font – Constructor takes three arguments—the font name, font style and font size Font name – any font currently supported by the system on which the program is running Font style – Font.PLAIN, Font.ITALIC or Font.BOLD. Font styles can be used in combination Font sizes – measured in points. A point is 1/72 of an inch. – Methods getName, getStyle and getSize retrieve information about Font object – Graphics methods getFont and setFont retrieve and set the current font, respectively

 2005 Pearson Education, Inc. All rights reserved. 26 Fig | Font-related methods and constants. (Part 1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 27 Fig | Font-related methods and constants. (Part 2 of 2)

 2005 Pearson Education, Inc. All rights reserved. 28 Portability Tip 12.2 The number of fonts varies greatly across systems. Java provides five logical font names— Serif, Monospaced, SansSerif, Dialog and DialogInput —that can be used on all Java platforms. The Java runtime environment (JRE) on each platform maps these logical font names to actual fonts installed on the platform. The actual fonts used may vary by platform.

 2005 Pearson Education, Inc. All rights reserved. 29 Outline FontJPanel.java (1 of 2) Creating Font objects Font nameFont style Font size

 2005 Pearson Education, Inc. All rights reserved. 30 Outline FontJPanel.java (2 of 2) Combining styles Retrieve font name and size of Graphics object’s current Font

 2005 Pearson Education, Inc. All rights reserved. 31 Outline Fonts.java

 2005 Pearson Education, Inc. All rights reserved. 32 Software Engineering Observation 12.2 To change the font, you must create a new Font object. Font objects are immutable—class Font has no set methods to change the characteristics of the current font.

 2005 Pearson Education, Inc. All rights reserved. 33 Font Metrics Font class methods – getFamily – returns name of font family to which the current font belongs – isPlain, isBold, isItalic – used to determine font style Font metrics – precise information about a font – Height – Descent – amount a character dips below the baseline – Ascent – amount a character rises above the baseline – Leading – the interline spacing – Class FontMetrics declares several methods for obtaining font metrics

 2005 Pearson Education, Inc. All rights reserved. 34 Fig | Font metrics.

 2005 Pearson Education, Inc. All rights reserved. 35 Fig | FontMetrics and Graphics methods for obtaining font metrics.

 2005 Pearson Education, Inc. All rights reserved. 36 Outline MetricsJPanel.java (1 of 2) Retrieve FontMetrics object of current Font Retrieve font metric values

 2005 Pearson Education, Inc. All rights reserved. 37 Outline MetricsJPanel.java (2 of 2)

 2005 Pearson Education, Inc. All rights reserved. 38 Outline Metrics.java

 2005 Pearson Education, Inc. All rights reserved Drawing Lines, Rectangles and Ovals Graphics methods for drawing lines, rectangles and ovals – fillRoundRect and drawRoundRect – draw rectangles with rounded corners – bounding rectangle—the area in which a rounded rectangle or oval will be drawn – draw3DRect and fill3DRect – draw a 3D rectangle that is either raised or lowered – drawOval and fillOval – draw ovals

 2005 Pearson Education, Inc. All rights reserved. 40 Fig | Graphics methods that draw lines, rectangles and ovals. (Part 1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 41 Fig | Graphics methods that draw lines, rectangles and ovals. (Part 2 of 2)

 2005 Pearson Education, Inc. All rights reserved. 42 Outline LinesRectsOvals JPanel.java (1 of 2) Draw a straight lineDraw an empty rectangleDraw a filled rectangle

 2005 Pearson Education, Inc. All rights reserved. 43 Outline LinesRectsOvals JPanel.java (2 of 2) Draw a filled rectangle with rounded cornersDraw an empty rectangle with rounded cornersDraw an empty rectangle that is raisedDraw a filled rectangle that is loweredDraw an empty ovalDraw a filled oval

 2005 Pearson Education, Inc. All rights reserved. 44 Outline LinesRectsOvals.java (1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 45 Outline LinesRectsOvals.java (1 of 2) fillRoundRect drawRoundRect drawOval fillOval drawLine drawRect fillRect draw3DRect fill3DRect

 2005 Pearson Education, Inc. All rights reserved. 46 Fig | Arc width and arc height for rounded rectangles.

 2005 Pearson Education, Inc. All rights reserved. 47 Fig | Oval bounded by a rectangle.

 2005 Pearson Education, Inc. All rights reserved Drawing Arcs An arc is drawn as a portion of an oval Arcs sweep (i.e., move along a curve) from a starting angle by the number of degrees specified by their arc angle – Counterclockwise sweep measured in positive degrees – Clockwise sweep measured in negative degrees Graphics methods drawArc and fillArc are used to draw arcs

 2005 Pearson Education, Inc. All rights reserved. 49 Fig | Positive and negative arc angles.

 2005 Pearson Education, Inc. All rights reserved. 50 Fig | Graphics methods for drawing arcs.

 2005 Pearson Education, Inc. All rights reserved. 51 Outline ArcsJPanel.java (1 of 2) x- and y-coordinates for upper left corner of bounding rectangle Width and height of bounding rectangle Starting angle Sweep angle Draw empty arcs

 2005 Pearson Education, Inc. All rights reserved. 52 Outline ArcsJPanel.java (2 of 2) Draw filled arcs Negative values indicate arc should sweep clockwise

 2005 Pearson Education, Inc. All rights reserved. 53 Outline DrawArcs.java (1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 54 Outline DrawArcs.java (2 of 2)

 2005 Pearson Education, Inc. All rights reserved Drawing Polygons and Polylines Polygons – Closed multisided shapes composed of straight line segments – Graphics methods drawPolygon and fillPolygon to display polygons – Polygons can be represented using class Polygon – class contains method addPoint to add points to a Polygon Polylines – Sequences of connected points – Graphics method drawPolyline to display polylines

 2005 Pearson Education, Inc. All rights reserved. 56 Fig | Graphics methods for polygons and class Polygon methods. (Part 1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 57 Fig | Graphics methods for polygons and class Polygon methods. (Part 2 of 2)

 2005 Pearson Education, Inc. All rights reserved. 58 Outline PolygonsJPanel.java (1 of 2) Create Polygon object from sets of x- and y-coordinates Draw an empty Polygon Draw polyline from sets of x- and y-coordinates

 2005 Pearson Education, Inc. All rights reserved. 59 Outline PolygonsJPanel.java (2 of 2) Draw polygon from sets of x- and y-coordinates, without creating Polygon object Add coordinates to Polygon with method addPoint

 2005 Pearson Education, Inc. All rights reserved. 60 Outline DrawPolygons.java (1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 61 Outline DrawPolygons.java (2 of 2) Result of line 28 Result of line 37 Result of line 23 Result of line 18

 2005 Pearson Education, Inc. All rights reserved. 62 An ArrayIndexOutOfBoundsException is thrown if the number of points specified in the third argument to method drawPolygon or method fillPolygon is greater than the number of elements in the arrays of coordinates that specify the polygon to display. Common Programming Error 12.1

 2005 Pearson Education, Inc. All rights reserved Java 2D API Provides advanced two-dimensional graphics capabilities for detailed and complex graphical manipulations Features for processing line art, text and images Accomplished with Graphics2D class

 2005 Pearson Education, Inc. All rights reserved. 64 Lines, Rectangles, Round Rectangles, Arcs and Ellipses Java 2D shapes specified with double-precision floating- point values – Line2D.Double, Rectangle2D.Double, RoundRectangle2D.Double, Arc2D.Double, Ellipse2D.Double Painting with Graphics2D object – Method setPaint – sets color for Graphics2D object when shapes are drawn as Paint object – Paint object can be a predeclared Color object, or an instance of GradientPaint, SystemColor or TexturePaint classes – GradientPaint used for drawing with a gradient – gradients can be cyclic or acyclic – TexturePaint used for painting by replicating a stored image

 2005 Pearson Education, Inc. All rights reserved. 65 Lines, Rectangles, Round Rectangles, Arcs and Ellipses Graphics2D method fill used to draw a filled Shape object – an object that implements interface Shape Graphics2D method draw used to draw a Shape object Setting stroke of a line or border – Graphics2D method setStroke – requires argument that implements interface Stroke – BasicStroke class – can specify width of line, end caps, line joins Arc2D.Double constants – Arc2D.PIE – arc should be closed by two lines—one from starting point to center, one from center to ending point – Arc2D.CHORD – draws a line from the starting point to the ending point – Arc2D.OPEN – arc should not be closed

 2005 Pearson Education, Inc. All rights reserved. 66 Outline ShapesJPanel.java (1 of 3) Java 2D API shape classes Creating Graphics2D reference

 2005 Pearson Education, Inc. All rights reserved. 67 Outline ShapesJPanel.java (2 of 3) Set Graphics2D object to draw using a gradient from blue to yellow Draw ellipse filled using gradientSet width of border to 10 pixels Create image to be used for TexturePaint object

 2005 Pearson Education, Inc. All rights reserved. 68 Outline ShapesJPanel.java (3 of 3) Create TexturePaint object from image Draw rounded rectangle, filled with repeating image Draw arc using white border, 6 pixels wide Draw solid green lineSet stroke to use dashes Draw dashed yellow line

 2005 Pearson Education, Inc. All rights reserved. 69 Outline Shapes.java

 2005 Pearson Education, Inc. All rights reserved. 70 General Paths A general path is a shape constructed from straight lines and complex curves Class GeneralPath – Method moveTo specifies the first point in a general path – Method lineTo draws a line to the next point in the path – Method closePath completes the general path Graphics2D method translate – used to move the drawing origin Graphics2D method rotate – used to rotate the next displayed shape

 2005 Pearson Education, Inc. All rights reserved. 71 Outline Shapes2JPanel.java (1 of 2) Set starting point of GeneralPath object Create GeneralPath object

 2005 Pearson Education, Inc. All rights reserved. 72 Outline Shapes2JPanel.java (2 of 2) Add lines of general pathDraw line from last point to first pointRotate approximately 18 degreesDraw star at current angle around origin

 2005 Pearson Education, Inc. All rights reserved. 73 Outline Shapes2.java (1 of 2)

 2005 Pearson Education, Inc. All rights reserved. 74 Outline Shapes2.java (2 of 2)