Graphics and Java 2D Introduction OBJECTIVES. One picture is worth ten thousand words.

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

Part II: Creating Visio Drawings

Graphics and Painting

Work with Shapes. Concepts CHAPTER. Concepts, page 3-1 Procedures, page 3-5

A QUICK TOUR OF ADOBE ILLUSTRATOR CC (2018 RELEASE)

INKSCAPE BASICS. 125 S. Prospect Avenue, Elmhurst, IL (630) elmhurstpubliclibrary.org. Create, Make, and Build

Adobe Illustrator CS Design Professional CREATING TEXT AND GRADIENTS

Paint/Draw Tools. Foreground color. Free-form select. Select. Eraser/Color Eraser. Fill Color. Color Picker. Magnify. Pencil. Brush.

Corel Draw 11. What is Vector Graphics?

How to draw and create shapes

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

L E S S O N 2 Background

InDesign Tools Overview

Create a Swirly Lollipop Using the Spiral Tool Philip Christie on Jun 13th 2012 with 12 Comments

Drawing shapes and lines

Creating Special Effects with Text

Word 2007: Flowcharts Learning guide

Adding Objects Creating Shapes Adding. Text Printing and Exporting Getting Started Creating a. Creating Shapes Adding Text Printing and Exporting

12 APPLYING EFFECTS. Lesson overview

How to...create a Video VBOX Gauge in Inkscape. So you want to create your own gauge? How about a transparent background for those text elements?

Ai Adobe. Illustrator. Creative Cloud Beginner

LOGO & BRAND STANDARDS GUIDE

ADOBE ILLUSTRATOR CS3

INKSCAPE INTRODUCTION COMPONENTS OF INKSCAPE MENU BAR

Software System Components 1 Graphics

Paint Tutorial (Project #14a)

Guidelines for Legible and Readable Text, page 2-1 Visual Density Transparent, Translucent, or Opaque?, page 2-3

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

12 APPLYING EFFECTS. Lesson overview

Chapter 10. Creating 3D Objects Delmar, Cengage Learning

Quick Guide for Photoshop CC Basics April 2016 Training:

Generating Vectors Overview

Graphics Applets. By Mr. Dave Clausen

Scalable Vector Graphics (SVG) vector image World Wide Web Consortium (W3C) defined with XML searched indexed scripted compressed Mozilla Firefox

Building Java Programs

2 Solutions Chapter 3. Chapter 3: Practice Example 1

JavaFX Technology Building GUI Applications With JavaFX - Tutorial Overview

Adobe InDesign CS6 Tutorial

MET 107 Drawing Tool (Shapes) Notes Day 3

Using Microsoft Excel

On the Web sun.com/aboutsun/comm_invest STAROFFICE 8 DRAW

Creating a Flyer. Open Microsoft Publisher. You will see the list of Popular Publication Types. Click the Blank Page Sizes.

Points and lines. x x 1 + y 1. y = mx + b

How to Make a Poster Using PowerPoint

Java Programming Lecture 6

EXAMINATIONS 2016 TRIMESTER 2

S206E Lecture 3, 5/15/2017, Rhino 2D drawing an overview

Adobe Illustrator. Quick Start Guide

IN DESIGN. A review of the overview

Word Processing Graphics

Step 1: Create A New Photoshop Document

Xyron Wishblade Software Manual (Mac)


Krita Vector Tools

Chapter 5 Managing Graphic Objects

Transforming Objects in Inkscape Transform Menu. Move

HOW TO. In this section, you will find. miscellaneous handouts that explain. HOW TO do various things.

Shape and Line Tools. tip: Some drawing techniques are so much easier if you use a pressuresensitive

Topic 8 graphics. -mgrimes, Graphics problem report 134

Keyboard Shortcuts. Command Windows Macintosh

Adobe Illustrator CS5 Part 2: Vector Graphic Effects

Chapter 6 Formatting Graphic Objects

User Manual Version 1.1 January 2015

EDITING SHAPES. Lesson overview

Cudacountry Radial. Fig. 2. Point. Fig. 4. Mastercam 2017 Cudacountry Radial Page 19-1

ITEC185. Introduction to Digital Media

Draw Guide. Chapter 4 Changing Object Attributes

Geometry: Semester 2 Practice Final Unofficial Worked Out Solutions by Earl Whitney

Christmas Card Final Image Preview Coach Christian s Example Ctrl+N 1920px 1200px RGB 72 pixels/inch Rectangle Tool (U)

We will start our journey into Processing with creating static images using commands available in Processing:

Introduction To Inkscape Creating Custom Graphics For Websites, Displays & Lessons

3D Body. Modified by Jason Howie on 8-Oct Parent page: PCB Dialogs. Other Related Resources 3D Body (Object)

Chapter 1 Introducing Draw

Adobe Indesign for Beginner - Session Structure

Using Microsoft Word. Working With Objects

Band Editor User Guide Version 1.3 Last Updated 9/19/07

After completing each task, be sure to save the file in the My Documents folder on your computer using the suggested name.

Lesson 6 Adding Graphics

This section provides an overview of the features available within the Standard, Align, and Text Toolbars.

Xyron Wishblade Software Manual (PC)

@Override public void start(stage primarystage) throws Exception { Group root = new Group(); Scene scene = new Scene(root);

Guide to WB Annotations

ILLUSTRATOR TUTORIAL-1 workshop handout

Topic 8 Graphics. Margaret Hamilton

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

Pen Tool, Fill Layers, Color Range, Levels Adjustments, Magic Wand tool, and shadowing techniques

CREATING THE FUNKY BUSINESS CARD

SUPPLEMENTARY FILE S1: 3D AIRWAY TUBE RECONSTRUCTION AND CELL-BASED MECHANICAL MODEL. RELATED TO FIGURE 1, FIGURE 7, AND STAR METHODS.

Part 1: Basics. Page Sorter:

This page intentionally left blank

Creating Digital Illustrations for Your Research Workshop III Basic Illustration Demo

INSTRUCTORS: A. SANPHAWAT JATUPATWARANGKUL A. NATTAPOL SUPHAWONG A. THEEPRAKORN LUNTHOMRATTANA COMPUTER AIDED DESIGN I AUTOCAD AND ILLUSTRATOR CS

CISC 1600 Lecture 3.1 Introduction to Processing

SETTINGS AND WORKSPACE

Do Now # 1 Label the Photoshop Interface.

Ornamental Pro 2010 OP2010 Instruction Manual Table of Contents Objects... 3 Bar... 3 Description... 3 Creating New... 3 Creating a Curved Bar...

The Racket Drawing Toolkit

Adobe Illustrator CC 2018 Tutorial

Exercise III: Creating a Logo with Illustrator CS6

CSC207H: Software Design Lecture 11

Transcription:

1 2 12 Graphics and Java 2D 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 1992-2007 Pearson Education Inc. All rights reserved. OBJECTIVES In this chapter you will learn: To understand graphics contexts and graphics objects. To manipulate colors. To manipulate fonts. To use methods of class to draw lines, rectangles, rectangles with rounded corners, threedimensional rectangles, ovals, arcs and polygons. To use methods of class from the API to draw lines, rectangles, rectangles with rounded corners, ellipses, arcs and general paths. To specify and characteristics of shapes displayed with. 3 12.1 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 4 12.1 Introduction 5 6 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,, Java 2D shape classes Fig. 12.1 Classes and interfaces used in this chapter from Java s original graphics capabilities and from the Java 2D API. [Note: Class! appears here because it is the superclass of the Java class hierarchy.] 1

12.1 Introduction 7 8 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. Fig. 12.2 Java coordinate system. Units are measured in pixels. 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. 9 12.2 Graphics Contexts and Graphics Objects A Java graphics context enables drawing on the screen Class Manages a graphics context and draws pixels on the screen An class contributes to Java s portability Method " Used to draw graphics Member of class ", subclass of " object passed to " by the system when a lightweight Swing component needs to be repainted If programmer needs to have " execute, a call is made to method 10 12.3 Color Control 11 12 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 constant Color RGB value $ $ %& %& red ''()() $ $ %&&* %&&* green )( ''() $ $ +,& +,& blue )()( '' $ $ %-*& %-*& orange ''( ))() $ $.*/.*/ pink ''(01'(01' $ $ 2-* 2-* cyan )( ''( '' $ $ -&*- -&*- magenta ''()( '' $ $ 2&++3 2&++3 yellow ''( ''() $ $ +-/ +-/ black )()() $ $ 34.& 34.& white ''( ''( '' $ $ %-2 %-2 gray 0 5(0 5(0 5 5 $ $ +.46%-2 +.46%-2 light gray 07 (07 (07 $ $ -%/6%-2 -%/6%-2 dark gray 89(89(89 Fig. 12.3 Color constants and their RGB values. 2

Method Description constructors and methods : : ( ( ( ( ; Creates a color based on red, green and blue components expressed as integers from 0 to 255. : :$ $( ($ $( ($ $ ; Creates a color based on red, green and blue components expressed as floating-point values from 0.0 to 1.0. %:; %:; Returns a value between 0 and 255 representing the red content. :; Returns a value between 0 and 255 representing the green content. :; :; Returns a value between 0 and 255 representing the blue content. methods for manipulating :; Returns object representing current color for the graphics context. :; Sets the current color for drawing with the graphics context. Fig. 12.4 Color methods and color-related Graphics methods. 13 0 <<=0 ='>=! <<"=? "!=@=A!=@=A 9 "!=@=A!=@=A ' "!=@=A!=@=A 8 1 5 B Method " 7 <<@$$ <<@$$ paints 0) ":; ":; 00B B 0 =": =":<<C" <<C" 0? 09 =: =:=34.& =34.&;A 0' 08 <<@@ <<@@ 01 =: =:@ @: ''( ()( ();;A ; 05 =$%: =$%:0' 0'( '(0)) 0))( ) 07=@: =@:D%>D D%>DE E=:; =:;( (0?) 0?)( (9) 9);A ) 0 <<@@$ <<@@$ =: =:@ @: :)=')$ )=')$()=1'$ )=1'$( ()=)$ )=)$;;A ;?=$%: =$%:0' 0'( (') ')( (0)) 0))( ) 9=@: =@:D%>D D%>DE E=:; =:;( (0?) 0?)( (8' 8';A ' 8 <<@@! 1 =: =:=+,& =+,&;A 5=$%: =$%:0' 0'( (1' 1'( (0)) 0))( ) 7=@: =@:D%>D D%>DE=:;( E=:;(0?) 0?)( (7) 7);A?) Set current drawing color with method Draw filled rectangle using current color Draw text value of current color Set current drawing color, specify $ arguments to constructor Set current drawing color using constant 14?0 <<% <<%? F F=-&*- =-&*-A?? =:;A =:?9=$%: =$%:0' 0'( (0)) 0))( (0)) 0))( )?'=@: =@:D%>D D%>DE E=%:; =%:;E ED(D D(DE E?8 =:; =:;E ED(D D(DE E=:; =:;( (0?) 0?)( (00' 00';A?1G G<<"" <<""?5G G<< << Retrieving RGB values using methods %, and 15 0 <<=0 =8>@=! <<"=? "!=@="A!=@="A 9 ' @ @ 8 B 1 << << 5 ":HI; ":HI; 7 B 0) <<$"$ <<$"$ 00"$"F "$"F@ @": ":D,D D,D;A 0 $"=$:"=&J.6*6+& "=&J.6*6+&;A 0? 09F F@ @:;A :;A<< << 0'$"=: $"=:<< <<$" $" 08$"=K: $"=K:9)) 9))( (05) 05) <<$"K <<$"K 01$"=L $"=L : <<$" <<$" 05G G<<" <<" 07G G<<@ <<@ 16 17 18 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. Software Engineering Observation 12.1 To change the color, you must create a new object (or use one of the predeclared constants). Like objects, objects are immutable (not modifiable). 3

12.3 Color Control M GUI component enables application users to select colors Method @ creates a 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 19 0 <<=0 =1>@ "=! <<@=? "!=@=+A!=@=+A 9 "!=@=A!=@=A ' "!=@==-&A!=@==-&A 8 "!=@==-+A!=@==-+A 1 "!=@=A!=@=A Import class 5 "!=@="A!=@="A 7 "!=@=A!=@=A 0)" "!=@=A!=@=A 00 0 @ " " " 0?B 09 A A 0' F F=+.46%- =+.46%-2A 08 A A 01 05 <<,. <<,. 07 @ ":; )B B 0: :D,D D,D;A?<<$ <<$ 9F F@ @:;A :;A ' =:;A =: 8 1 << << 5F F@ @: :DD DD;A 7=-+: =-+:?) 20?0 @ @-+:; -+:;<<" <<"? B?? <<@?9 $":-&; $":-&; Display dialog?'b B?8 F F=@: =@:?1 @ "= ( (DD DD(;A (?5?7 <<$($ <<$($ 9) $ Reference to parent $:FF :FF component ; ; Title bar text 90 F F=+.46%-2 =+.46%-2A Initial selected color 9 9? <<C 99 =:;A =: 21 0 <<=0 =5>@ =! <<@=? "!=@="A!=@="A 9 ' @ 8 B 1 << << 5 ":HI; ":HI; 7 B 0)@ @ "F@ @@ ":;A 00=$: =$:"=&J.6*6+& "=&J.6*6+&;A ;A 0 G<<" <<" 0?G G<<@ 22 9'G G<<"$" <<"$" 98G G<<" <<" 91;A <<-+ <<-+ 95 Change background color of 97:( :(+=&*&% +=&*&% << << '):( :(+=,4 +=,4 << '0 ' K:9)) 9))( (0?) 0?) <<$"K <<$"K '?L L : <<$" <<$" '9G G<<@ " ''G G<<@ " 23 24 Select a color from one of the color swatches. Sliders to select the red, green and blue color components Fig. 12.9 HSB and RGB tabs of the dialog. 4

12.4 Font Control 25 26 Class 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 =+-.* =+-.*, =.-+. or =+. Font styles can be used in combination Font sizes measured in points. A point is 1/72 of an inch. Methods *", and K retrieve information about object methods and retrieve and set the current font, respectively Method or constant Description constants, constructors and methods $ $ +-.* +-.* A constant representing a plain font style. $ $ + + A constant representing a bold font style. $ $.-+..-+. A constant representing an italic font style. :"( :"( ( ( K; Creates a object with the specified font name, style and size. :; Returns an integer value indicating the current font style. K:; Returns an integer value indicating the current font size. Fig. 12.10 Font-related methods and constants. (Part 1 of 2) 27 28 Method or constant Description *":; Returns the current font name as a string. ":; Returns the font s family name as a string. :; Returns if the font is plain, else $. :; Returns if the font is bold, else $..:; Returns if the font is italic, else $. methods for manipulating s :; :; Returns a object reference representing the current font. :$; Sets the current font to the font, style and size specified by the object reference $. Portability Tip 12.2 The number of fonts varies greatly across systems. Java provides five logical font names $ $,, $, and. 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. Fig. 12.10 Font-related methods and constants. (Part 2 of 2) 0 <<=0 =00>=! 29 <<$$$=? "!=@=A!=@=A 9 "!=@=A!=@=A ' "!=@=A!=@=A 8 "!=@=A!=@=A 1 5 7 B 0) <<$$$ <<$$$ 00 ":; ":; 0 B B Font name Font style 0?=": =":<<C <<C 09 Font size 0' <<$$:";( (0 @ 08 =: =:@ @: :D$D D$D( (=+ =+( (0 ; 01=@: =@:D$0 =D( )( (') ');A 05 07 <<$:;(( 9@ ) =: =:@ @: :D DD D( (=.-+. =.-+.( 9;;A ; Creating and setting objects 0=@: =@:D 9=D( )( (1) 1);A? <<$$:4;((09@ <<$$:4;((09@ 9 =: =:@ @: :D$D D$D( (=+-.* =+-.*( (09 09;;A ; '=@: =@:D$09=D D$09=D( )( (7) 7);A 8 1 <<$$:";( <(05@ Combining styles 5=: =:=%& =%&;A 7 =: =:@ @: :D$D D$D( (=+ =+E E=.-+. =.-+.( (05 05;;A ;?)=@: =@:=:;=*":; =:;=*":;E EDD DDE E=:;= =:;=K:; K:;E E?0 D =D( )( (00) 00);A? G G<<"" <<""??G G<< << Retrieve font name and size of object s current 30 5

0 <<=0 =0 >=! <<,$= 31 32? "!=@="A!=@="A 9 ' 8 B 1 << << Software Engineering Observation 12.2 5 ":HI; ":HI; 7 B 0) <<$"$ <<$"$ 00 "$"F "$"F@ @": ":D,$D D,$D;A 0 $"=$:"=&J.6*6+& "=&J.6*6+&;A 0? 09$F $F@ @:;A :;A<< << 0'$"=:$ $"=:$<<$$" <<$$" 08$"=K: $"=K:9 9 )( ) (01) 01) <<$"K <<$"K 01$"=L $"=L : <<$" <<$" To change the font, you must create a new object. objects are immutable class has no set methods to change the characteristics of the current font. 05G G<<" <<" 07G G<< << Font Metrics 33 34 M class methods " returns name of font family to which the current font belongs,,. 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 declares several methods for obtaining font metrics Fig. 12.13 Font metrics. Method Description 35 0 <<=0 =0'>=! <<"$$ $"=? "!=@=A!=@=A 9 "!=@=A!=@=A ' "!=@=A!=@=A 8 "!=@=A!=@=A 1 36 " " -:; -:; Returns the ascent of a font in points. :; :; Returns the descent of a font in points. +:; +:; Returns the leading of a font in points. 4:; 4:; Returns the height of a font in points. "$ "$ N N :; :; Returns the object for the current drawing. :$; :$; Returns the!$$ argument. 5 7 B 0) <<$" <<$" 00 ":; ":; 0 B 0? =": =":<<C" <<C" 09 Retrieve object of 0'=: =:@ @: :D$D D$D( (= =+ +( (0 ; current 08 "F=:;A "F=:;A 01=@: =@:D$>D D$>DE E=:; =:;( (0) 0)( (9) 9);A 05=@: =@:D->D D->DE E"=-:; "=-:;( (0) 0)( ('' '';A 07=@: =@:D>D D>DE E"= "=:; :;( (0) 0)( (1) 1);A Retrieve font metric values )=@:D4>D D4>DE E"=4:; "=4:;( (0) 0)( (5' 5';A 0=@: =@:D+>D D+>DE E"=+:; "=+:;( (0) 0)( (0)) 0));A Fig. 12.14 and methods for obtaining font metrics. 6

?$F $F@ @: :D$D D$D( (=.-+. =.-+.( (09 09;A 9 "F=:$;A "F=:$ 37 0 <<=0 =08>=! <<$"= 38 '=:$;A =:$? "!=@="A!=@="A 8=@: =@:D$>D D$>DE$( E$(0) 0)( (0?) 0?);A 9 1=@: =@:D->D D->DE E"=-:; "=-:;(0) 0)( (09' 09';A ' 5=@: =@:D>D D>DE E"=:; "=:;( (0) 0)( (08) 08);A 8 B 7=@: =@:D4>D D4>DE E"=4:; "=4:;( (0) 0)( (01' 01';A 1 << <<?)=@: =@:D+>D D+>DE E"=+:; "=+:;( (0) 0)( (07) 07);A 5 ":HI; ":HI;?0G G<<" <<"" " 7 B? G G<< << 0) <<$"$ <<$"$ 00"$"F "$"F@ @": ":D"D D"D;A 0 $"=$: $"=$:"=&J.6*6+& "=&J.6*6+&;A 0? 09"F "F@ @:;A :;A 0'$"=:" $"=:"<< <<"$" "$" 08$"=K: $"=K:'0) '0)( ') <<$"K <<$"K 01$"=L $"=L : <<$" <<$" 05G G<<" <<" 07G G<< << 12.5 Drawing Lines, Rectangles and Ovals 39 Method Description @+: @+: 0( 0( 0( 0( ( ; Draws a line between the point (0 0, 0) and the point (, ). @%: @%: ( ( ( ( @( @( ; 40 M methods for drawing lines, rectangles and ovals $%% and @%% draw rectangles with rounded corners bounding rectangle the area in which a rounded rectangle or oval will be drawn @?% and $?% draw a 3D rectangle that is either raised or lowered @ and $ draw ovals Draws a rectangle of the specified @ and. The top-left corner of the rectangle has the coordinates (, ). Only the outline of the rectangle is drawn using the object s color the body of the rectangle is not filled with this color. $%: $%: ( ( ( ( @( @( ; ; Draws a filled rectangle with the specified @ and. The top-left corner of the rectangle has the coordinate (, ). The rectangle is filled with the object s color. %: %: ( ( ( ( @( @( ; Draws a filled rectangle with the specified @ and in the current background color. The top-left corner of the rectangle has the coordinate (, ). This method is useful if the programmer wants to remove a portion of an image. @% @%%: %: ( ( ( ( @( @( ( ( 3( 3( 4; Draws a rectangle with rounded corners in the current color with the specified @ and. The 3 and 4 determine the rounding of the corners (see Fig. 12.20). Only the outline of the shape is drawn. Fig. 12.17 Graphics methods that draw lines, rectangles and ovals. (Part 1 of 2) Method Description $%%: $%%: ( ( ( ( @( @( ( ( 3( 3( 4; Draws a filled rectangle with rounded corners in the current color with the specified @ and. The 3 and 4 determine the rounding of the corners (see Fig. 12.20). @?%: @?%: ( ( ( ( @( @( ( ; Draws a three-dimensional rectangle in the current color with the specified @ and. The top-left corner of the rectangle has the coordinates (, ). The rectangle appears raised when is true and lowered when is false. Only the outline of the shape is drawn. $?%: $?%: ( ( ( ( @( @( ( ; Draws a filled three-dimensional rectangle in the current color with the specified @ and. The top-left corner of the rectangle has the coordinates (, ). The rectangle appears raised when is true and lowered when is false. 41 0 <<=0 =05>+%=! <<@(=? "!=@=A!=@=A 9 "!=@=A!=@=A ' "!=@=A!=@=A 8 1 +% +% 5 B 7 << <<( ( 0) ":; ":; 00B B 0 =": =":<<C" <<C" 0? 09=: =:=34.& =34.&;A 0' Draw a straight line 08=: =: =%& =%&;A 42 @: @: ( ( ( ( @( @( ; Draws an oval in the current color with the specified @ and. The bounding rectangle s top-left corner is at the coordinates (, ). The oval touches all four sides of the bounding rectangle at the center of each side (see Fig. 12.21). Only the outline of the shape is drawn. $: $: ( ( ( ( @( @( ; Draws a filled oval in the current color with the specified @ and. The bounding rectangle s top-left corner is at the coordinates (, ). The oval touches all four sides of the bounding rectangle at the center of each side (see Fig. 12.21). 01 =@+: =@+:'( (?)?)( (?5)?5)( (?)?);A 05 07=: =:=+,& =+,&;A )=@%: =@%:'( (9) 9)( (7) 7)( ('' '';A 0 =$%: =$%:0)) 0))( (9) 9)( (7) 7)( ('' '';A Draw an empty rectangle Draw a filled rectangle Fig. 12.17 Graphics methods that draw lines, rectangles and ovals. (Part 2 of 2) 7

?=: =:=2-* =2-*;A 9 =$%%: =$%%:07' 07'( (9) 9)( (7) 7)( ('' ''( (') ')( (') ');A '=@%%: 7)( (9) 9)( (7) 7)( ('' ''( )( ) 8 1=: =:=2&++3 =2&++3 5 =@?%: =@?%:'( (0)) 0))( (7) 7)( ('' ''( ( ;A 7 =$?%: =$?%:0)) 0))( (0)) 0))( (7) 7)( ('' ''( ($ $;A?)?0=: =:=-&*- =-&*-;A ;A? =@: =@:07' 07'( (0)) 0))( (7) 7)( ('' '';A?? =$: 7)( (0)) 0))( (7) 7)( ('' '';A?9G G<<"" <<""?'G G<<+% <<+% Draw a filled rectangle with rounded corners Draw an empty rectangle with rounded corners Draw an empty rectangle that is raised Draw a filled rectangle that is lowered Draw an empty oval Draw a filled oval 43 0 <<=0 =07>+%=! <<@(=? "!=@=A!=@=A 9 "!=@="A!=@="A ' 8 +% +% 1 B 5 << << 7 ": ":HI; HI; 0)B B 00 <<$"$+% <<$"$+% 0 "$"F "$"F 0? @ @": ":D@(D D@(D;A 09$"=$: $"=$:"=&J.6*6+& "=&J.6*6+&;A 0' 44 08+ +%%F %%F 01 @ @+%:;A +%:;A 05%=: %=:=34.& =34.& 07$"=:% $"=:%<<$" <<$" )$"=K:9)) 9))( 0);A <<$"K <<$"K 0$"=L $"=L : <<$" <<$" G<<" <<"?G<<+% 45 46 $%% @+ @%% @% $% @?% $?% @ $ Fig. 12.20 Arc width and arc height for rounded rectangles. 47 12.6 Drawing Arcs 48 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 M methods @- and $- are used to draw arcs Fig. 12.21 Oval bounded by a rectangle. 8

49 50 Method Description @-: @-: ( ( ( ( @( @( ( ( -( -( -; Draws an arc relative to the bounding rectangle s top-left and coordinates with the specified @ and. The arc segment is drawn starting at - and sweeps - degrees. $-: $-: ( ( ( ( @( @( ( ( -( -( -; Draws a filled arc (i.e., a sector) relative to the bounding rectangle s top-left and coordinates with the specified @ and. The arc segment is drawn starting at - and sweeps - degrees. Fig. 12.22 Positive and negative arc angles. Fig. 12.23 Graphics methods for drawing arcs. 0 <<=0 = 9>-=! <<@= 51 8 <<)@ <<)@O 1) 1=: =:=%& =%&;A 52? "!=@=A!=@=A 5=@%: =@%:05' 05'( (?'?'( (5) 5)( (5) 5);A 9 "!=@=A!=@=A 7=: =:=+-/ =+-/;A ' "!=@=A!=@=A?) =@-: =@-:05' 05'( (?'?'( (5) 5)( (5) 5)( ()( (O 1);A 8?0 1 - - 5 B x- and y-coordinates for upper left 7 <<@ <<@ corner of bounding rectangle 0) ":; ":; 00B B Width and height of bounding 0 =":<<C" <<C" 0? rectangle? <<)@?8 <<)@?8) )?? =$-: =$-:0' 0'( (0 0 )( ) (5) 5)( (9) 9)( ()( (?8)?8);A?9?' << 1)@O7) 7)?8 =$-: =$-:0)) 0))( (0 0 )( ) (5) 5)( (9) 9)( 1)( (O7) 7);A?1?5 <<)@ <<)@O 1) Draw filled arcs 09 <<)@?8) <<)@?8) 0'=: =:=%& =%&;A 08=@%: =@%:0' 0'( (?'?'( (5) 5)( (5) 5);A Starting angle Sweep angle?7 =$- =$-: :05' 05'( (0 0 )( ) (5) 5)( (9) 9)( ()( (O 1);A 9)G G<<"" <<"" 90G G<<- <<- 01= =: :=+-/ =+-/;A ;A 05 =@-: =@-:0' 0'( (?'?'( (5) 5)( (5) 5)( ()( (?8)?8);A 07 )<<)@00) <<)@00) 0=: =:=%& =%&;A Draw empty arcs Negative values indicate arc should sweep clockwise =@%:0)) 0))( (?'?'( (5) 5)( (5) 5);A?=:=+-/ =+-/;A 9 = =@-: @-:0)) 0))( (?'?'( (5) 5)( (5) 5)( ()( (00) 00);A ' 0 <<=0 = '>@-=! <<@= 53 09-F -F@ @-:;A -:;A<<- <<- 0'$"=: $"=:<<$" <<$" 54? "!=@="A!=@="A 08$"=K: $"=K:?))?))( 0) <<$"K <<$"K 9 01$"=L $"=L : <<$" <<$" ' @- @- 05G G<<" <<" 8 B 07G G<<@- <<@- 1 << << 5 ":HI; ":HI; 7 B 0) <<$"$- <<$"$- 00"$"F "$"F@ @": ":D@-D D@-D;A 0 $"=$:"=&J.6*6+& "=&J.6*6+&;A 0? 9

55 56 12.7 Drawing Polygons and Polylines Method Description Polygons Closed multisided shapes composed of straight line segments methods @ and $ to display polygons Polygons can be represented using class class contains method to add points to a Polylines Sequences of connected points Graphics method @ to display polylines methods for drawing polygons @: @: HI( HI( HI( HI( ; Draws a polygon. The x-coordinate of each point is specified in the array, and the y- coordinate of each point in the array. The last argument specifies the number of. This method draws a closed polygon. If the last point is different from the first, the polygon is closed by a line that connects the last point to the first. @: @: HI( HI( HI( HI( ; Draws a sequence of connected lines. The x-coordinate of each point is specified in the array, and the y-coordinate of each point in the array. The last argument specifies the number of. If the last point is different from the first, the polyline is not closed. @:; Draws the specified polygon. $: $: HI( HI( HI( HI( ; Draws a filled polygon. The x-coordinate of each point is specified in the array, and the y-coordinate of each point in the array. The last argument specifies the number of. This method draws a closed polygon. If the last point is different from the first, the polygon is closed by a line that connects the last point to the first. Fig. 12.26 Graphics methods for polygons and class Polygon methods. (Part 1 of 2) Method Description $:; Draws the specified filled polygon. The polygon is closed. constructors and methods :; Constructs a new polygon object. The polygon does not contain any points. : : LHI( LHI( LHI( LHI( " $; Constructs a new polygon object. The polygon has " $ sides, with each point consisting of an x-coordinate from L and a y-coordinate from L. : : ( ( ; Adds pairs of x- and y-coordinates to the. 57 0 <<=0 = 1>=! <<@=? "!=@=A!=@=A 9 "!=@=A!=@=A ' "!=@=A!=@=A 8 1 5 B 7 <<@ <<@ 0) ":; ":; 00B B 0 =":<<C" <<C" 0? 09 <<@@! 0' LHIFB )(9) 9)( (') ')( (?)?)( )(0' 0'GA GA 08 LHIFB LHIFB') ')( (') ')( (8) 8)( (5) 5)( (5) 5)( (8) 8)GA GA 01 0F 0F@ @:L(L( :L(L(8;A 05 =@:0;A =@:0 Draw an empty 07 )<<@@@ <<@@@ 0 L HIFB1) 1)( (7) 7)( (0)) 0))( (5) 5)( (1) 1)( (8' 8'( (8) 8)GA GA Create object from sets of x- and y-coordinates L HIFB0)) 0))( (0)) 0))( (00) 00)( (00) 00)( (0?) 0?)( (00) 00)( (7) 7)GA GA Draw polyline from sets of x- and?=@:l (L (1;A 9 y-coordinates 58 Fig. 12.26 Graphics methods for polygons and class Polygon methods. (Part 2 of 2) ' <<$@@ <<$@@ 8 L?HIFB L?HIFB0 0 )( ) (09) 09)( (0') 0')( (07) 07)GA GA 59 0 <<=0 = 5>@=! <<@= 60 1 L?HIFB L?HIFB9) 9)( (1) 1)( (5) 5)( (8) 8)GA GA 5 =$:L?(L?( =$:L?(L?(9;A 7 Draw polygon from sets of x- and y-coordinates, without creating object? "!=@="A!=@="A 9 ' @ @?) <<@$@ <<@$@! 8 B?0 F@ @:;A :;A 1 << <<? =:08' 08'( (0?' 0?';A 5 ":HI; ":HI;?? =:01' 01'( (0') 0');A?9 =: 1)( ));A?' =: ))( );A Add coordinates to with method 7 B 0) <<$"$ <<$"$ 00"$"F "$"F@ @": ":D@D D@D;A?8 =:0?) 0?)( (05) 05);A 0 $"=$: $"=$:"=&J.6*6+& "=&J.6*6+&;A?1 =$: 0??5G G<<"" <<""?7G G<< << 10

09F F@ @:;A :;A 0'$"=: $"=:<<$" <<$" 61 62 08$"=K: $"=K: 5)( 1) <<$"K <<$"K 01$"=L $"=L : <<$" <<$" 05G G<<" <<" 07G G<<@ <<@ Common Programming Error 12.1 Result of line 18 Result of line 23 Result of line 28 Result of line 37 An -.$& is thrown if the number of points specified in the third argument to method @ or method $ is greater than the number of elements in the arrays of coordinates that specify the polygon to display. 12.8 Java 2D API 63 Lines, Rectangles, Round Rectangles, Arcs and Ellipses 64 Provides advanced two-dimensional graphics capabilities for detailed and complex graphical manipulations Features for processing line art, text and images Accomplished with class Java 2D shapes specified with double-precision floatingpoint values + =, % =, %% =, - =, & = Painting with object Method sets color for object when shapes are drawn as object Paint object can be a predeclared object, or an instance of, " or classes used for drawing with a gradient gradients can be cyclic or acyclic used for painting by replicating a stored image Lines, Rectangles, Round Rectangles, Arcs and Ellipses M method $ used to draw a filled object an object that implements interface M method @ used to draw a object Setting stroke of a line or border method requires argument that implements interface 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 65 0 <<=0 = 7>=! <<"" =? "!=@=A!=@=A 9 "!=@=A!=@=A ' "!=@=A!=@=A 8 "!=@=A!=@=A 1 "!=@=A!=@=A 5 "!=!=@=%A @=%A 7 "!=@= A 0)" "!=@="=& A 00" "!=@="=% A 0 " "!=@="=%% A Java 2D API shape classes 0?" "!=@="=- A 09" "!=@=!=@="=+ A 0'" "!=@="=$$."A!=@="=$$."A 08" "!=@=A!=@=A 01 05 07B ) <<@@ -. 0 ":; ":; Creating a reference B B?=": =":<<C" <<C" 9 ' F: ;A<< 8 66 11

1 <<@ $@ O@ @ 67 5 =:@ @: :'( (?)?)( (=+,& =+,&( (?'?'( (0)) 0))( 7 =2&++3 =2&++3( ( ;;A ;?) =$:@ @& = : :'( (?)?)( (8' 8'( (0)) 0));;A ; Draw ellipse filled using gradient?0? <<@ Set object to draw using?? =:=%& =%&;A?9 =:@ @: :0)=)$ 0)=)$;;A ; a gradient Set width from of border blue to to yellow 10 pixels?' =@:@ @% = :5) 5)( (?)?)( (8' 8'( (0)) 0));;A ;?8?1 <<@ @ $$?5 $$." $$."F@ @$$.": $$.":0) 0)( (0) 0)(?7 $$."=2&6.*6% $$."=2&6.*6%;A 9) 90 << $" $$."@ 9 F $$."=:;A Create image to be used for 9?=: =:=2&++3 =2&++3 <<@@ <<@@ object 99=$%: =$%:)( ()( (0) 0)( (0) 0) <<@$ <<@$ 9' =: =:=+-/ =+-/ <<@ 98=@%: =@%:0( (0( (8( (8 <<@ <<@ 91=: =:=+,& =+,& <<@ 95=$%: =$%:0( (0( (?( (? <<@$ <<@$ 97=: =:=%& =%& <<@ <<@ ')=$%: =$%:9( (9( (?( (? <<@$ <<@$ '0 ' << $$."" '? =:@ @: $$."( '9 @ @%: %:0) 0)( (0) 0);;;A ;; '' =$: Create object from '8 @ @%% = :0'' 0''( (?)?)( (1' 1'( (0)) 0))( (') ')( (') ');;A ; '1 image '5 <<@ O@ @ Draw rounded rectangle, filled with '7 =:=34.& =34.&;A repeating image 8) =:@ @: :8=)$ 8=)$;;A ; Draw arc using white 80 =@: border, 6 pixels wide 8 @ @- = : 9)( (?)?)( (1' 1'( (0)) 0))( ()( 1)( (- =.&;;A ; 8? 89 <<@ @ @ 8' =:=%&&* =%&&*;A 88 =@:@ @+ = :?7'?7'( (?)?)( (?? )( ) (0') 0');;A ; 81 85 <<@ 87 $ $HIFB HIFB0) 0)GA GA<<$ <<$ 1) =:=2&++3 =2&++3;A ;A 10 =:@ @: :9( (=-6%,* =-6%,*( 1 =.*6%,* =.*6%,*( (0) 0)(( (();;A ; Set stroke to use dashes 1? =@:@ @+ = :?? )(?)?)( (?7'?7'( (0') 0');;A ; 19G G<<"" <<"" 1'G G<< << Draw solid green line Draw dashed yellow line 68 0 <<=0 =?)>=! <<"" =? "!=@="A!=@="A 9 ' 69 General Paths 70 8 B 1 << << 5 ":HI; ":HI; 7 B 0) <<$"$ <<$"$ 00"$"F "$"F@ @": ":D@ D;A ;A 0 $"=$:"=&J.6*6+& "=&J.6*6+&;A 0? 09 << << 0'F F@ @:;A :;A 08 01 $"=: $"=:<<$" <<$" 05$"=K: $"=K:9 9 '( ' )) <<$"K <<$"K 07$"=L $"=L : <<$" <<$" )G<<" <<" 0G G<< << A general path is a shape constructed from straight lines and complex curves Class Method " specifies the first point in a general path Method draws a line to the next point in the path Method completes the general path M method used to move the drawing origin M method used to rotate the next displayed shape 0 <<=0 =?0> =! <<"=? "!=@=A!=@=A 9 "!=@=A!=@=A ' "!=@= A 8 "!=@="=A!=@="=A 1 "!==%"A!==%"A 5 "!=@=!=@=A A 7 0) 00B 0 <<@ <<@ 0? ":; ":; 09B B 0'=": =":<<C" <<C" 08%""F %""F@ @%":;A %":;A<<"" 01 05 HIFB HIFB'' ''( (81 81( (0)7 0)7( (1? 1?( (5? 5?( ('' ''( 1( (?1?1( (0( Create (9? 9?GA GA object 07 HIFB HIFB)( (?8?8( (?8?8( ('9 '9( (78 78( (1 (78 78( ('9 '9( (?8?8( (?8?8GA GA ) 0 F: ;A F F@ @:;A :;A<<!? 9 <<$ <<$ '=":H =":H)I(H I(H)I;A I 8 Set starting point of object 71 1 << <<OO OO@ @ 5 $ $: : F F0AP=AEE; AP=AEE; 7 =:HI(HI;A =:HI(HI?)?0 =:;A =:;A<< <<??? =: ))( )) <<: ))( )); Add lines of general path?9?' <<@" <<@" Draw line from last point to first point?8 $ $: : F F0APF )AEE; AEE;?1B B?5 =:=. =.< <0)=) 0)=) <<" <"?7 9) <<"@ <<"@ 90 =:@ @:"=.: '8;( ;( 9 "=.:"=.: '8;("=.: '8;;;A ;; Rotate approximately 18 degrees 9? 99 =$:<<@$ <<@$ 9'G G<<$ <<$ 98G G<<"" <<"" 91G G<< Draw star at current angle around origin 72 12

0 <<=0 =? > =! <<"= 73 0' F@ @ :;A 08$"=: $"=: << $" 74? "!=@=A!=@=A 01$"=: $"=:=34.& =34.& <<$" 9 "!=@="A!=@="A 05$"=K: $"=K:9)) 9))( (9)) 9)) <<$ <<$"K "K ' 07$"=L $"=L : <<$" <<$" 8 )G G<<" <<" 1 B 0G G<< 5 << << 7 ":HI; ":HI; 0)B B 00 <<$"$ 0 "$"F "$"F@ @": ":D@ D;A 0?$"=$: $"=$:"=&J.6*6+& "=&J.6*6+&;A 09 13