22
Graphics and Java 2D

Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

Embed Size (px)

Citation preview

Page 1: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

Graphics and Java 2D

Page 2: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

2

Introduction

• Java’s graphics capabilities– Drawing 2D shapes– Controlling colors– Controlling fonts

• Java 2D API– More sophisticated graphics capabilities

• Drawing custom 2D shapes• Filling shapes with colors and patterns

Page 3: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

3

Introduction:Classes and Interfaces

Classes and interfaces from the Java2D API that appear in package java.awt

Object

Color

Component

Font

FontMetrics

Graphics

Polygon

Graphics2D interface java.awt.Paint

interface java.awt.Shape

interface java.awt.Stroke

Classes from the Java2D API that appear in package java.awt.geom

GradientPaint

BasicStroke

TexturePaint

RectangularShape

GeneralPath

Line2D

RoundRectangle2D

Arc2D

Ellipse2D

Rectangle2D

Page 4: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

4

Introduction

• Java’s coordinate system– Scheme for identifying

all points on screen– Upper-left corner has

coordinates (0,0)– Coordinate point

composed of x-coordinate and y-coordinate

X axis

Y axis

(0, 0)

(x, y)

+x

+y

Page 5: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

5

Graphics Contexts and Graphics Objects• Graphics context

– Enables drawing on screen– Graphics object manages graphics context

• Controls how information is drawn

– Class Graphics is abstract• Cannot be instantiated• Contributes to Java’s portability

– Class Component method paint takes Graphics objectpublic void paint( Graphics g )

– Called through method repaint

Page 6: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

6

Color Control

• Class Color– Defines methods and constants for

manipulating colors– Colors are created from red, green and

blue components• RGB values

Page 7: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

7

Color constant and valueColor constant Color RGB value public final static Color ORANGE orange 255, 200, 0 public final static Color PINK pink 255, 175, 175

public final static Color CYAN cyan 0, 255, 255

public final static Color MAGENTA magenta 255, 0, 255

public final static Color YELLOW yellow 255, 255, 0

public final static Color BLACK black 0, 0, 0

public final static Color WHITE white 255, 255, 255

public final static Color GRAY gray 128, 128, 128

public final static Color LIGHT_GRAY light gray 192, 192, 192

public final static Color DARK_GRAY dark gray 64, 64, 64

public final static Color RED red 255, 0, 0

public final static Color GREEN green 0, 255, 0

public final static Color BLUE blue 0, 0, 255

Page 8: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

8

Color methods & color-related Graphic methods

Method Description

Color constructors and methods

public Color( int r, int g, int b ) Creates a color based on red, green and blue components expressed as integers

from 0 to 255.

public Color( float r, float g, float b ) Creates a color based on red, green and blue components expressed as

floating-point values from 0.0 to 1.0.

public int getRed() Returns a value between 0 and 255 representing the red content.

public int getGreen() Returns a value between 0 and 255 representing the green content.

public int getBlue() Returns a value between 0 and 255 representing the blue content.

Graphics methods for manipulating Colors

public Color getColor() Returns a Color object representing the current color for the graphics

context. public void setColor( Color c ) Sets the current color for drawing with the graphics context.

Page 9: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

9

1 // ShowColors.java2 // Choosing colors with JColorChooser.3 import java.awt.*;4 import java.awt.event.*;5 import javax.swing.*;6 7 public class ShowColors extends JFrame {8 private JButton changeColorButton;9 private Color color = Color.LIGHT_GRAY;10 private Container container;11 12 // set up GUI13 public ShowColors()14 {15 super( "Using JColorChooser" );16 17 container = getContentPane();18 container.setLayout( new FlowLayout() );19 20 // set up changeColorButton and register its event handler21 changeColorButton = new JButton( "Change Color" );22 changeColorButton.addActionListener(23

Page 10: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

10

24 new ActionListener() { // anonymous inner class25 26 // display JColorChooser when user clicks button27 public void actionPerformed( ActionEvent event )28 {29 color = JColorChooser.showDialog( 30 ShowColors.this, "Choose a color", color );31 32 // set default color, if no color is returned 33 if ( color == null )34 color = Color.LIGHT_GRAY;35 36 // change content pane's background color37 container.setBackground( color );38 }39 } // end anonymous inner class40 ); // end call to addActionListener41 42 container.add( changeColorButton );43 44 setSize( 400, 130 );45 setVisible( true );46 } // end ShowColor2 constructor

JColorChooser allows user to choose from

among several colors

static method showDialog displays the color chooser dialog

Page 11: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

11

47 // execute application48 public static void main( String args[] )49 {50 ShowColors application = new ShowColors();51 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );52 }53 } // end class ShowColors2

Page 12: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

12

Font Control

• Class Font– Contains methods and constants for font

control– Font constructor takes three arguments

• Font name– Monospaced, SansSerif, Serif, etc.

• Font style– Font.PLAIN, Font.ITALIC and Font.BOLD

• Font size– Measured in points (1/72 of inch)

Page 13: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

13

Font-related methods and constants

Method or constant Description

Font constants, constructors and methods for drawing polygons

public final static int PLAIN A constant representing a plain font style.

public final static int BOLD A constant representing a bold font style.

public final static int ITALIC A constant representing an italic font style.

public Font( String name, int style, int size ) Creates a Font object with the specified font, style and size.

public int getStyle() Returns an integer value indicating the current font style.

public int getSize() Returns an integer value indicating the current font size.

public String getName() Returns the current font name as a string.

public String getFamily() Returns the font’s family name as a string.

public boolean isPlain() Tests a font for a plain font style. Returns true if the font is plain.

public boolean isBold() Tests a font for a bold font style. Returns true if the font is bold.

public boolean isItalic() Tests a font for an italic font style. Returns true if the font is italic.

Page 14: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

14

// set 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 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 ); // set 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 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 );

Font-related methods and constantsMethod or constant Description

Graphics methods for manipulating Fonts

public Font getFont() Returns a Font object reference representing the current font.

public void setFont( Font f )

Sets the current font to the font, style and size specified by the Font object reference f.

Page 15: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

15

Java 2D API

• Java 2D API– Provides advanced 2D graphics capabilities

• java.awt• java.awt.image• java.awt.color• java.awt.font• java.awt.geom• java.awt.print• java.awt.image.renderable

– Uses class java.awt.Graphics2D• Extends class java.awt.Graphics

Page 16: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

16

Java 2D API

• when using Swing overide paintComponent instead of paint.

Page 17: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

17

Java 2D API

• Java 2D shapes– Package java.awt.geom

•Ellipse2D.Double•Rectangle2D.Double•RoundRectangle2D.Double•Arc3D.Double•Lines2D.Double

Page 18: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

18

Java 2D API• The Java 2D API introduces java.awt.Graphics2D

, a new type of Graphics object. • Graphics2D extends the Graphics class to

provide access to the enhanced graphics and rendering features of the Java 2D API.

• To use Java 2D API features, you cast the Graphics object passed into a component's rendering method to a Graphics2D object.

public void Paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; ...}

Page 19: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

19

Java 2D Drawing Process: Step 1• Cast Graphics object to Graphics 2D

public void paintComponent(Graphics g) { super.paingComponent(g); // Typical Swing Graphics2d g2d = (Graphics2d) g; g2d.doSomeStuff(...); ...}

• Note– All methods that return Graphics in Java 1.1

return Graphics2D in Java 2• paint, paintComponent• getGraphics

Page 20: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

20

Java 2D Drawing Process: Step 2• Set pen parameters

– g2d.setPaint(fillColorOrPattern);– g2d.setStroke(penThicknessOrPattern);– g2d.setComposite(someAlphaComposite);– g2d.setFont(someFont);– g2d.translate(...);– g2d.rotate(...);– g2d.scale(...);– g2d.shear(...);– g2d.setTransform(someAffineTransform);

Page 21: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

21

Java 2D Drawing Process: Step 3• Create a Shape object.

Rectangle2D.Double rect = ...;Ellepse2D.Double ellipse = ...;Polygon poly = ...;GeneralPath path = ...;// satisfies Shape interfaceSomeShapeYouDefined shape = ...;

• Note– Most shapes are in the java.awt.geom package– There is a corresponding Shape class for most

of the drawXxx methods of Graphics

Page 22: Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated

22

Java 2D Drawing Process: Step 4• Draw an outlined or filled version of

the Shapeg2d.draw(someShape);g2d.fill(someShape);

• The legacy methods are still supported– drawString– drawLine, drawRect, fillRect