32
Java Programming Graphics Chapter 16

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?

Embed Size (px)

Citation preview

Page 1: 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 ProgrammingGraphics

Chapter 16

Page 2: 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

If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?

Page 3: 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 Coordinate System

(0, 0)

(x, y)

x

y

x-axis

y-axis

Java Coordinate System

(0, 0)

Conventional Coordinate System

Page 4: 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?

Creating a Window

A rectangular area Part of a user interface Called a frame Contains title bar

Page 5: 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?

Preparing to Draw

Cannot draw directly on JFrame object This is a container Contains other objects

Create a panel upon which to draw

Page 6: 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?

Drawing Geometric Figures

Drawing Strings Drawing Lines Drawing Rectangles Drawing Ovals Drawing Arcs Drawing Polygons

Page 7: 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?

Graphic Methods

paint() method Write own method to override default Method header

public void paint(Graphics g)

Graphics object Preconfigured with the appropriate state for drawing

on the component repaint() method

Use when window needs to be updated Calls paint() method Creates Graphics object

Page 8: 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?

Drawing Strings

drawString() method Draw String in JFrame

window Requires three arguments

String x-axis coordinate y-axis coordinate

Member of Graphics class

Page 9: 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?

Drawing Strings

setFont() method Requires Font object Instruct Graphics object to use a font

somegraphicsobject.setFont(someFont);

setColor() method Designate Graphics color Use 13 Color class constants as arguments Create any Color object

Color someColor = new Color(r, g, b); Values range from 0 to 255

Page 10: 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?

Drawing Strings

setColor() method Designate Graphics

color Use 13 Color class

constants as arguments Create any Color object

Color someColor = new Color(r, g, b);

Values range from 0 to 255

Page 11: 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?

Drawing Strings

drawString(String s, int x, int y);

(0,0) (getWidth(),0)

(0, getHeight()) (getWidth(),getHeight())

(x, y) → String goes here

Page 12: 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?

Drawing Lines

drawLine() method Draw straight line between any two points Takes four arguments

x- and y-coordinates of line’s starting point x- and y-coordinates of the line’s ending point

Page 13: 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?

Drawing Lines

drawLine(int x1, int y1, int x2, int y2);

(0,0) (getWidth(),0)

(0, getHeight()) (getWidth(),getHeight())

(x1, y1)

(x2, y2)

Page 14: 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?

Drawing Shapes

drawRect() method Draw outline of rectangle

fillRect() method Draw solid or filled rectangle

Both require four arguments x- and y-coordinates of upper-left corner of

rectangle Width and height of rectangle

Page 15: 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?

Drawing RectanglesdrawRect(int x, int y, int w, int h);

fillRect(int x, int y, int w, int h);

(x,y)

h

w

(x,y)

h

w

Page 16: 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?

Drawing Shapes

clearRect() method Draws rectangle Requires four arguments

x- and y-coordinates of upper-left corner of rectangle Width and height of rectangle

Appears empty or “clear” drawRoundRect() method

Create rectangles with rounded corners Requires six arguments

Page 17: 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?

Drawing Rounded Rectangles

drawRoundRect(int x, int y, int w, int h, int aw, int ah);

fillRoundRect(int x, int y, int w, int h, int aw, int ah);

(x,y)

h

w

aw/2

ah/2

Page 18: 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?

Drawing Ovals

drawOval() and fillOval() methods Draw ovals using the same four arguments that

rectangles use

drawOval(int x, int y, int w, int h);

fillOval(int x, int y, int w, int h);(x,y)

h

w

Page 19: 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?

Drawing Arcs

drawArc() method arguments x-coordinate of upper-left corner of imaginary

rectangle that represents bounds of imaginary circle that contains arc

y-coordinate of same point Width of imaginary rectangle that represents

bounds of imaginary circle that contains arc Height of same imaginary rectangle Beginning arc position Arc angle

Page 20: 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?

Drawing Arcs

Page 21: 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?

Drawing Arcs

fillArc() method Create solid arc

(x,y)

h

w

drawArc(int x, int y, int w, int h, int angle1, int angle2);fillArc(int x, int y, int w, int h, int angle1, int angle2);

Page 22: 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?

Shadowed Rectangles

draw3DRect() method Minor variation on drawRect() method Draw rectangle that appears to have

“shadowing” on two edges Contains Boolean value argument

true if rectangle darker on right and bottom false if rectangle darker on left and top

fill3DRect() method Create filled three-dimensional rectangles

Page 23: 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?

Drawing Polygons

drawPolygon() method Draw complex shapes Requires three arguments

Integer array holds series of x-coordinate positions Second array holds series of corresponding y-

coordinate positions Number of pairs of points to connect

Page 24: 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?

Drawing Polygons

fillPolygon() method Draw solid shape If beginning and ending points not identical

Two endpoints connected by straight line before polygon filled with color

addPoint() method Add points to polygon indefinitely

Page 25: 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?

Drawing Polygons

int[] x = {40, 70, 60, 45, 20};int[] y = {20, 40, 80, 45, 60};g.drawPolygon(x, y, x.length);

(x[0], y[0])

(x[1], y[1])

(x[2], y[2])

(x[3], y[3])

(x[4], y[4])

Page 26: 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?

Drawing Exampleimport java.awt.Graphics;import javax.swing.JPanel;

public class DrawingPanel extends JPanel{ public void paintComponent (Graphics g) { g.drawRect (50, 50, 20, 20); // square g.drawRect (100, 50, 40, 20); // rectangle g.drawOval (200, 50, 20, 20); // circle g.drawOval (250, 50, 40, 20); // oval g.drawString ("Square", 50, 90); g.drawString ("Rectangle", 100, 90); g.drawString ("Circle", 200, 90); g.drawString ("Oval", 250, 90);

// continued on next slide

Page 27: 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?

Drawing Example

g.fillRect (50, 100, 20, 20); // square g.fillRect (100, 100, 40, 20); // rectangle g.fillOval (200, 100, 20, 20); // circle g.fillOval (250, 100, 40, 20); // oval g.drawLine (50, 150, 300, 150); // line g.drawArc (50, 150, 250, 100, 0, 180); // arc g.fillArc (100, 175, 200, 75, 90, 45); // arc } // end paintComponent} // end DrawingPanel

Page 28: 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?

Drawing Example

import javax.swing.JFrame;

public class DrawingSamples { public static void main (String [ ] args) { JFrame aWindow = new JFrame (); aWindow.setSize (350, 300); // width x height aWindow.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); aWindow.setTitle ("Samples of shapes we can draw"); DrawingPanel panel = new DrawingPanel (); aWindow.add (panel); aWindow.setVisible (true); }}

Page 29: 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?

Font Statistics

You can display a string at any location in a panel.

Can you display it centered? To do so, you need to use the FontMetrics class

to measure the exact width and height of the string for a particular font.

A FontMetrics can measure the following attributes:

Page 30: 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?

Font Statistics

Leading Amount of space between baselines

Ascent Height of uppercase character from baseline to top of

character Descent

Measures part of characters that “hang below” baseline

Height of font Sum of leading, ascent, and descent

Page 31: 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?

Font Statistics

getFontMetrics() method Discover font’s height Returns FontMetrics object Use FontMetrics class methods with object

to return Font’s statistics public int getLeading() public int getAscent() public int getDescent() public int getHeight()

Page 32: 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?

Font Statistics

stringWidth() method Returns integer width of a String Requires name of String Member of FontMetrics class

getHeight()

getLeading()

getAscent()

getDescent()