Graphics in Java Part II. Lecture Objectives Learn how to use Graphics in Java.

Slides:



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

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?
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 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a 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.
 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.
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™
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
 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.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
 2003 Prentice Hall, Inc. All rights reserved. Outline 12.1 Introduction 12.2 Graphics Contexts and Graphics Objects 12.3 Color Control 12.4 Font Control.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
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.
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.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
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.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
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.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java2D Outline 11.1Introduction 11.2Graphics Contexts and Graphics Objects 11.3Color.
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
(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.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
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 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
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
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
Chapter 8 Graphics.
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.
Chapter 12 - Graphics and Java 2D
Presentation transcript:

Graphics in Java Part II

Lecture Objectives Learn how to use Graphics in Java

Lecture Objectives Outline: Introduction Graphics Contexts and Graphics Objects Color Control Font Control Drawing Lines, Rectangles and Ovals Drawing Arcs Drawing Polygons and Polylines The Java2D API Java2D Shapes

In this lecture, we will learn about:  Drawing 2D shapes.  Using colors.  Using fonts. Java Graphics Support:  Has a class hierarchy for its graphics classes and 2D API classes Java Coordinate System:  (x, y) pairs x - horizontal axis y - vertical axis  Upper left corner is (0,0)  Coordinates measured in Pixels (Picture Element - Smallest unit of resolution) Introduction

Graphics context  Enables drawing on screen  Graphics object manages graphics context Controls how information is drawn Has methods for drawing, font manipulation, etc  Graphics an abstract class! Cannot instantiate objects Implementation hidden - more portable Class Component  Superclass for many classes in java.awt package  Method paint() takes Graphics object as argument Graphics Context and Objects

Class Component (Cont’d)  paint() often called in response to an event  repaint() calls update(), which forces a paint() operation update() rarely called directly! Sometimes overridden to reduce "flicker" Headers: public void repaint() public void update( Graphics g ) In this lecture:  Focus on paint() method Graphics Context and Objects (Cont’d)

Class Color  Defines methods and constants for manipulating colors  Colors created from red, green, and blue (RGB) components RGB value: 3 integers from 0 to 255 each, or three floating point values from 0 to 1.0 each Larger the value, more of that color  Color methods getRed(), getGreen(), getBlue() return an integer between 0 and 255 representing amount  Graphics method setColor() sets drawing color Takes Color object Method getColor() gets current color setting Color Control

Component JColorChooser  Displays dialog allowing user to select a color  Method showDialog() First argument: reference to parent Component (window from which dialog being displayed) –Modal dialog - user cannot interact with other dialogs while active Second argument: String for title bar Third argument: Initial selected color Returns a Color object Class Container  Method setBackground() - takes Color object  Sets background color Color Control (Cont’d)

JColorChooser Color Control (Cont’d)

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ShowColors extends Jframe { public ShowColors() { super( "Using colors" ); setSize( 400, 130 ); show(); } public void paint( Graphics g ) { // set new drawing color using integers g.setColor( new Color( 255, 0, 0 ) ); g.fillRect( 25, 25, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(), 130, 40 ); // set new drawing color using floats g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); g.fillRect( 25, 50, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(), 130, 65 ); Outline: import Class definition define paint()

Color Control (Cont’d) // set new drawing color using static Color objects g.setColor( Color.blue ); g.fillRect( 25, 75, 100, 20 ); g.drawString( "Current RGB: " + g.getColor(), 130, 90 ); // display individual RGB values Color c = Color.magenta; g.setColor( c ); g.fillRect( 25, 100, 100, 20 ); g.drawString( "RGB values: " + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue(), 130, 115 ); } public static void main( String args[] ) { ShowColors app = new ShowColors(); } Outline: define paint() main() method Program Output

Font Control Class Font  Constructor takes three arguments public Font( String name, int style, int size) name: any font supported by system ( Serif, Monospaced ) style: constants FONT.PLAIN, FONT.ITALIC, FONT.BOLD –Combinations: FONT.ITALIC + FONT.BOLD size: measured in points (1/72 of an inch)  Use similar to Color  g.setFont( fontObject );

Methods  getStyle()  getSize()  getName()  getFamily()  isPlain()  isBold()  isItalic()  getFont()  setFont(Font f) Font Control (Cont’d)

Class FontMetrics  Has methods for getting font metrics  g.getFontMetrics() - returns FontMetrics object Font Control (Cont’d) Xy1 leading ascent baseline descent height

FontMetrics (and Graphics ) methods  getAscent()  getDescent()  getLeading()  getHeight()  getFontMetrics()  getFontMetrics( Font f ) Font Control (Cont’d)

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Fonts extends Jframe { public Fonts() { super( "Using fonts" ); setSize( 420, 125 ); show(); } public void paint( Graphics g ) { // set current font to Serif (Times), bold, 12pt // and draw a string g.setFont( new Font( "Serif", Font.BOLD, 12 ) ); g.drawString( "Serif 12 point bold.", 20, 50 ); // set current font to Monospaced (Courier), // italic, 24pt and draw a string g.setFont( new Font( "Monospaced", Font.ITALIC, 24 ) ); g.drawString( "Monospaced 24 point italic.", 20, 70 ); Outline: import Class definition define paint()

Font Control (Cont’d) // set current font to SansSerif (Helvetica), // plain, 14pt and draw a string g.setFont( new Font( "SansSerif", Font.PLAIN, 14 ) ); g.drawString( "SansSerif 14 point plain.", 20, 90 ); // set current font to Serif (times), bold/italic, // 18pt and draw a string g.setColor( Color.red ); g.setFont(new Font( "Serif", Font.BOLD + Font.ITALIC, 18 ) ); g.drawString( g.getFont().getName() + " " + g.getFont().getSize() + " point bold italic.", 20, 110 ); } public static void main( String args[] ) { Fonts app = new Fonts(); } Outline: define paint() main() method Program Output

Font Control (Cont’d) import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Metrics extends JFrame { public Metrics() { super( "Demonstrating FontMetrics" ); setSize( 510, 210 ); show(); } public void paint( Graphics g ) { g.setFont( new Font( "SansSerif", Font.BOLD, 12 ) ); FontMetrics fm = g.getFontMetrics(); g.drawString( "Current font: " + g.getFont(), 10, 40 ); g.drawString( "Ascent: " + fm.getAscent(), 10, 55 ); g.drawString( "Descent: " + fm.getDescent(), 10, 70 ); g.drawString( "Height: " + fm.getHeight(), 10, 85 ); g.drawString( "Leading: " + fm.getLeading(), 10, 100 ); Outline: import Class definition define paint()

Font Control (Cont’d) Font font = new Font( "Serif", Font.ITALIC, 14 ); fm = g.getFontMetrics( font ); g.setFont( font ); g.drawString( "Current font: " + font, 10, 130 ); g.drawString( "Ascent: " + fm.getAscent(), 10, 145 ); g.drawString( "Descent: " + fm.getDescent(), 10, 160 ); g.drawString( "Height: " + fm.getHeight(), 10, 175 ); g.drawString( "Leading: " + fm.getLeading(), 10, 190 ); } public static void main( String args[] ) { Metrics app = new Metrics(); } Outline: define paint() main() method Program Output

Graphics methods for drawing shapes:  drawLine(x1, y1, x2, y2) Line from x1, y1 to x2, y2  drawRect(x1, y1, width, height) Draws rectangle with upper left corner x1, y1  fillRect(x1, y1, width, height) As above, except fills rectangle with current color  clearRect(x1, y1, width, height) As above, except fills rectangle with background color  draw3DRect(x1, y1, width, height, isRaised) Draws 3D rectangle, raised if isRaised is true, else lowered. Drawing Lines, Rectangles and Ovals

Drawing Lines, Rectangles and Ovals (Cont’d) Graphics methods for drawing shapes (Cont’d)  fill3DRect As previous, but fills rectangle with current color  drawRoundRect(x, y, width, height, arcWidth, arcHeight) Draws rectangle with rounded corners. See diagram next slide.  fillRoundRect(x, y, width, height, arcWidth, arcHeight)  drawOvalx, y, width, height) Draws oval in bounding rectangle (see diagram) Touches rectangle at midpoint of each side  fillOval(x, y, width, height)

Drawing Lines, Rectangles and Ovals (Cont’d) arc width width height (x, y) arc height (x, y) height width drawRoundRect() parameters drawOval() parameters

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LinesRectsOvals extends JFrame { private String s = "Using drawString!"; public LinesRectsOvals() { super( "Drawing lines, rectangles and ovals" ); setSize( 400, 165 ); show(); } public void paint( Graphics g ) { g.setColor( Color.red ); g.drawLine( 5, 30, 350, 30 ); g.setColor( Color.blue ); g.drawRect( 5, 40, 90, 55 ); g.fillRect( 100, 40, 90, 55 ); g.setColor( Color.cyan ); g.fillRoundRect( 195, 40, 90, 55, 50, 50 ); g.drawRoundRect( 290, 40, 90, 55, 20, 20 ); Outline: import Class definition define paint() Drawing Lines, Rectangles and Ovals (Cont’d)

g.setColor( Color.yellow ); g.draw3DRect( 5, 100, 90, 55, true ); g.fill3DRect( 100, 100, 90, 55, false ); g.setColor( Color.magenta ); g.drawOval( 195, 100, 90, 55 ); g.fillOval( 290, 100, 90, 55 ); } public static void main( String args[] ) { LinesRectsOvals app = new LinesRectsOvals(); } Outline: define paint() main() method Program Output Drawing Lines, Rectangles and Ovals (Cont’d)

Arc  Portion of an oval  Measured in degrees Starts at a starting angle and sweeps the number of degrees specified by arc angle Positive - counterclockwise Negative - clockwise  When drawing an arc, specify bounding rectangle for an oval  drawArc(x, y, width, height, startAngle, arcAngle)  fillArc - as above, but draws a solid arc (sector) Drawing Arcs

Drawing Arcs (Cont’d) import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DrawArcs extends Jframe { public DrawArcs() { super( "Drawing Arcs" ); setSize( 300, 170 ); show(); } public void paint( Graphics g ) { // start at 0 and sweep 360 degrees g.setColor( Color.yellow ); g.drawRect( 15, 35, 80, 80 ); g.setColor( Color.black ); g.drawArc( 15, 35, 80, 80, 0, 360 ); // start at 0 and sweep 110 degrees g.setColor( Color.yellow ); g.drawRect( 100, 35, 80, 80 ); g.setColor( Color.black ); g.drawArc( 100, 35, 80, 80, 0, 110 ); Outline: import Class definition define paint()

Drawing Arcs (Cont’d) // start at 0 and sweep -270 degrees g.setColor( Color.yellow ); g.drawRect( 185, 35, 80, 80 ); g.setColor( Color.black ); g.drawArc( 185, 35, 80, 80, 0, -270 ); // start at 0 and sweep 360 degrees g.fillArc( 15, 120, 80, 40, 0, 360 ); // start at 270 and sweep -90 degrees g.fillArc( 100, 120, 80, 40, 270, -90 ); // start at 0 and sweep -270 degrees g.fillArc( 185, 120, 80, 40, 0, -270 ); } public static void main( String args[] ) { DrawArcs app = new DrawArcs(); } Outline: define paint() main() method Program Output

Polygon - multisided shape Polyline - series of connected points Methods of class Polygon  drawPolygon(xPoints[], yPoints[], points) Draws a polygon, with x and y points specified in arrays. Last argument specifies number of points Closed polygon, even if last point different from first  drawPolyline(xPoints[], yPoints, points) As above, but draws a polyline Open polyline Drawing Polygons and Polylines

Methods of class Polygon  drawPolygon(myPolygon) Draws specified closed polygon  fillPolygon(xPoints[], yPoints[], points) Draws a solid polygon  fillPolygon(myPolygon) Draws specified solid polygon  Polygon(xValues[], yValues[], numberOfPoints) Constructs a new polygon object  myPolygon.addPoint(x, y) Add pairs of x-y coordinates to polygon object Drawing Polygons and Polylines (Cont’d)

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DrawPolygons extends Jframe { public DrawPolygons() { super( "Drawing Polygons" ); setSize( 275, 230 ); show(); } public void paint( Graphics g ) { int xValues[] = { 20, 40, 50, 30, 20, 15 }; int yValues[] = { 50, 50, 60, 80, 80, 60 }; Polygon poly1 = new Polygon( xValues, yValues, 6 ); g.drawPolygon( poly1 ); int xValues2[] = { 70, 90, 100, 80, 70, 65, 60 }; int yValues2[] = { 100, 100, 110, 110, 130, 110, 90 }; g.drawPolyline( xValues2, yValues2, 7 ); int xValues3[] = { 120, 140, 150, 190 }; int yValues3[] = { 40, 70, 80, 60 }; g.fillPolygon( xValues3, yValues3, 4 ); Outline: Import Class definition define paint() Drawing Polygons and Polylines (Cont’d)

Polygon poly2 = new Polygon(); poly2.addPoint( 165, 135 ); poly2.addPoint( 175, 150 ); poly2.addPoint( 270, 200 ); poly2.addPoint( 200, 220 ); poly2.addPoint( 130, 180 ); g.fillPolygon( poly2 ); } public static void main( String args[] ) { DrawPolygons app = new DrawPolygons(); } Outline: define paint() main() method Program Output Drawing Polygons and Polylines (Cont’d)

Java2D API  Advanced two dimensional graphics capabilities  Too many capabilities to cover (for overview, see demo) Drawing with Java2D API  Use instance of class Graphics2D (package java.awt ) Subclass of Graphics Has all graphics capabilities we have previously discussed  Must downcast Graphics reference passed to paint Graphics2D g2d = (Graphics2D) g;  This technique used in programs of next section Java2D API

Sample methods from Graphics2D  setPaint(paintObject) Paint object is an object of a class that implements java.awt.Paint Can be an object of class Color, GradientPaint, SystemColor, TexturePaint  GradientPaint(x1, y1, color1, x2, y2, color2, cyclic) Creates a gradient (slowly changing color) from x1, y1, to x2, y2, starting with color1 and changing to color2 If cyclic true, then cyclic gradient (keeps transitioning colors) –If acyclic, only transitions colors once Java2D Shapes

Sample methods from Graphics2D  fill ( shapeObject ) Draws a filled Shape object Instance of any class that implements Shape ( java.awt ) Ellipse2D.Double, Rectangle2D.Double Double-precision inner classes of Ellipse2D  setStroke( strokeObject ) Set a shape's borders Instance of a class that implements Stroke ( java.awt ) BasicStroke( width ) - One of many constructors –This constructor specifies width in pixels of border Java2D Shapes (Cont’d)

Sample methods from Graphics2D  draw (shapeObject) Draws specified Shape object  Class BufferedImage Can produce images in color or grayscale Can create patterns by drawing into the BufferedImage object  Class TexturePaint Constructor can take BufferedImage and shape to fill Object of class TexturePaint then drawn using setPaint() Java2D Shapes (Cont’d)

Class Arc2D.Double  Similar to normal arcs, except has another argument at end Arc2D.PIE - close arc with two lines Arc2D.CHORD - draws line from endpoints of arc Arc2D.OPEN - arc not closed Class BasicStroke  Can be used to create customized dashed lines, set how lines end and join Java2D Shapes (Cont’d)

Class GeneralPath  A general path is a shape made from lines and curves  Method moveTo() Specifies first point in a general path  Method lineTo() Draws a line to next point in general path  Method closePath() Draws line from last point to point specified in last call to moveTo() Java2D Shapes (Cont’d)

Other methods  rotate(radians) - rotate next shape around origin  translate(x, y) - translates origin to x, y Java2D Shapes (Cont’d)

import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; public class Shapes extends Jframe { public Shapes() { super( "Drawing 2D shapes" ); setSize( 425, 160 ); show(); } public void paint( Graphics g ) { // create 2D by casting g to Graphics2D Graphics2D g2d = ( Graphics2D ) g; // draw 2D ellipse filled with a blue-yellow gradient g2d.setPaint( new GradientPaint( 5, 30, // x1, y1 Color.blue, // initial Color 35, 100, // x2, y2 Color.yellow, // end Color true ) ); // cyclic g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) ); Outline: import Class definition define paint() Cast to Graphics2D Java2D Shapes (Cont’d)

// draw 2D rectangle in red g2d.setPaint( Color.red ); g2d.setStroke( new BasicStroke( 10.0f ) ); g2d.draw(new Rectangle2D.Double( 80, 30, 65, 100 ) ); // draw 2D rounded rectangle with a buffered background BufferedImage buffImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB ); Graphics2D gg = buffImage.createGraphics(); gg.setColor( Color.yellow ); // draw in yellow gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle gg.setColor( Color.black ); // draw in black gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle gg.setColor( Color.blue ); // draw in blue gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle gg.setColor( Color.red ); // draw in red gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle // paint buffImage onto the JFrame g2d.setPaint(new TexturePaint(buffImage, new Rectangle( 10, 10 ) ) ); g2d.fill(new RoundRectangle2D.Double(155, 30, 75, 100, 50, 50 ) ); // draw 2D pie-shaped arc in white g2d.setPaint( Color.white ); g2d.setStroke( new BasicStroke( 6.0f ) ); g2d.draw(new Arc2D.Double(240, 30, 75, 100, 0, 270, Arc2D.PIE ) ); Outline: paint() Program Output Java2D Shapes (Cont’d)

Code available on WebCT – Shapes2.java Outline: Program Output Java2D Shapes (Cont’d)