1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

24
Chapter 15 Basic Figures Colors Fonts and Other Text Details Graphics Objects
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

Page 1: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

1

Chapter 15

Basic Figures Colors Fonts and Other Text Details

Graphics Objects

Page 2: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

2

Earlier chapters explain how to make graphical user interfaces (GUIs).

This chapter explains a different way of using graphics in Java: drawing pictures.

We will learn how to basic figures such as lines, ovals, and rectangles.

Basic figures can be combined to create elaborate pictures.

An optional section explains how to draw polygons.

Basic Figures

Page 3: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

3

Screen Coordinate System

(0, 0)

origin

Y-coordinate is positive and increasing in the downward direction.

X-coordinate is positive and increasing to the right.

All coordinates are normally positive.

Page 4: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

4

Screen Coordinate System

(100, 75)

(0, 0)

origin

X-coordinate:100 pixels from left edge of screen

Y-coordinate:75 pixels from top edge of screen

Location of a rectangle is specified by coordinates of upper left corner.

Page 5: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

5

Screen Coordinate System

(100, 75)

(0, 0)

origin

Location of an oval is specified by a tightly fitting rectangle.

Page 6: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

6

The paint Method Most Swing and Swing related components

have a method named paint. The paint method draws the component

on the screen. To draw basic figures such as ovals and

rectangles, you need to redefine the paint method.

The paint method is called automatically and should not be invoked in the programmer’s code.

Page 7: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

7

public void paint(Graphics g)

{

super.paint(g);

g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);

//Draw Nose:

g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER);

//Draw Eyes:

g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);

g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);

//Draw eyebrows:

g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW,

X2_LEFT_BROW, Y2_LEFT_BROW);

g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW,

X2_RIGHT_BROW, Y2_RIGHT_BROW);

//Draw Mouth:

g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,

MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);

}

The complete paint method from Madeleine.java

The program draws this picture.

Page 8: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

8

public void paint(Graphics g)

{

super.paint(g);

g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);

//Draw Nose:

g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER);

//Draw Eyes:

g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);

g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);

//Draw eyebrows:

g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW,

X2_LEFT_BROW, Y2_LEFT_BROW);

g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW,

X2_RIGHT_BROW, Y2_RIGHT_BROW);

//Draw Mouth:

g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,

MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);

}

The face, nose, and eyes are ovals. The eyebrows are lines, and the mouth is an arc.

The method for drawing each shape has numeric parameters telling where to draw the shape and how big to make it.

Page 9: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

9

public void paint(Graphics g)

{

super.paint(g);

g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);

//Draw Nose:

g.fillOval(X_NOSE, Y_NOSE, NOSE_DIAMETER, NOSE_DIAMETER);

//Draw Eyes:

g.fillOval(X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);

g.fillOval(X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);

//Draw eyebrows:

g.drawLine(X1_LEFT_BROW, Y1_LEFT_BROW,

X2_LEFT_BROW, Y2_LEFT_BROW);

g.drawLine(X1_RIGHT_BROW, Y1_RIGHT_BROW,

X2_RIGHT_BROW, Y2_RIGHT_BROW);

//Draw Mouth:

g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,

MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);

}

The paint method receives one parameter, which is an object of the Graphics class.

The Graphics parameter is the calling object for all of the methods that draw lines and shapes.

Calling the paint method of the parent class is a good programming practice.

Page 10: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

10

The Graphics class

Contains methods used to draw basic shapes and lines Is part of the AWT package (or library) so the AWT must be

imported:

import java.awt.*; Most methods for painting shapes have two versions:

» A draw version which only draws the outline of the shape

drawOval» A fill version which fills in the shape

fillOval

Page 11: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

11

Drawing Rectangles and Ovals

(100, 120)

(0, 0)

g.fillOval(100, 120, 40, 20);

(100, 50)

width

height40

20

Graphics object

20

40

g.fillRect(100, 50, 40, 20);

X and Y coordinates of upper left corner

Page 12: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

12

Drawing Arcs An arc is specified by giving an oval and then specifying

what portion of the oval will be used for the arc. To tell which part of the oval will be used, specify the

beginning angle and the degrees of the sweep.

positive direction

0 degrees

width

height

(x,y)

gdrawArc(x, y, width, height, 0, 360);

Page 13: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

13

Arc Examples

positive direction

0 degrees

negative direction

0 degrees

180 degrees

gdrawArc(x, y, width, height, 0, -90);

gdrawArc(x, y, width, height, 0, 360);

gdrawArc(x, y, width, height, 0, 90);

gdrawArc(x, y, width, height, 180, 90);

plus 90 degrees

Page 14: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

14

Round Rectangles

Round rectangle is a rectangle where corners have been replaced with arcs.

Specify by giving rectangle information and then height and width of corner arcs.

arcWidth

width

height

(x,y)

gdrawRoundRect(x, y, width, height, arcWidth, arcHeight);

arcHeight

Page 15: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

15

PolygonsdrawPolygon allows a program to draw shapes with any number of

sides.

public void drawPolygon(int[] x, int[] y, int point)

Each point in the polygon will have an x coordinate from the first parameter and a y coordinate from the corresponding element of the second parameter.

The third parameter tells how many points the polygon will have. Always draws a closed polygon. If first and last points are not equal,

draws a line from last to first. drawPolyline is similar but can draw an open figure. fillPolygon is similar but fills with color instead of drawing outline.

Page 16: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

16

Action Drawings and repaint The SadMadeleine program demonstrates how a

program can change a picture. In the original picture, the face has a frown. When the user clicks on the button, the picture

changes to a smiling face and moves up on the screen.

Page 17: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

17

Action Drawings and repaint The actionPerformed method changes the smile variable that the paint method uses.

When the smile variable is changed to true, the paint method knows to draw a smile instead of a frown.

public void actionPerformed(ActionEvent e)

{

if (e.getActionCommand().equals("Click for a Smile."))

smile = true;

else

System.out.println("Error in button interface.");

repaint();

}

Page 18: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

18

Action Drawings and repaint Unless the repaint method is called, the change will not

be shown on screen. The repaint method calls paint to update the picture. The paint methd should not be called directly.

public void actionPerformed(ActionEvent e)

{

if (e.getActionCommand().equals("Click for a Smile."))

smile = true;

else

System.out.println("Error in button interface.");

repaint();

}

Page 19: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

19

Colors Draw and fill methods like fillOval will use the current

color. You can use the setColor method of a Graphics

object to change the current color. To draw a red mouth you could change the paint method

of the Madeleine program to include these lines:

//Draw Mouth:

g.setColor(Color.red);

g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,

MOUTH_START_ANGLE, MOUTH_ARC_SWEEP);

Page 20: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

20

Defining Colors

The Color class mixes amounts of red, green, and blue to produce a new color.

Since colors are made by combing different amounts of red, green, and blue, this is called the RGB color system.

The parameters to the Color constructor must be type int or float.

When integers are used as parameters to the Color constructor they should be in the range 0 to 255.

When floats are used as parameters to the Color constructor they should be in the range 0.0 to 1.0.

Color brown = new Color(204, 102, 0);

Page 21: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

21

Fonts and Other Text Details A font is a style of text. A program can use the drawString method to

display text on the screen. The first parameter tells which characters to

display. The last two parameters to drawString are

coordinates that tell where on the screen to put the text.

g.drawString(theText, X_START, Y_START);

Page 22: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

22

Setting the Font The method setFont in a Graphics

object can be used to change the current font.

Java guarantees that at least three fonts will be available:

» Monospaced

» SanSerif

» Serif

Page 23: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

23

Using the Font Constructor To specify a font with a particular name, style, and size,

use the Font constructor.

Size of the font is specified in points.Each point is 1/72 of an inch, but point sizes are only

approximate.

Font f = new Font(“Serif”, Font.BOLD|Font.ITALIC,

POINT_SIZE);

g.setFont(f);

Name of the font Style of the font: bold and italic

Size of the font in points (an integer)

Page 24: 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.

24

Summary You can draw figures such as lines, ovals, and rectangles

using methods in the class Graphics. You can specify the color of each figure drawn using the

method setColor of the Graphics class. You can define your own colors using the class Color. Colors are defined using the RGB (Red/Green/Blue)

system. You can add text to a a graphics drawing by using the Graphics method drawString.

You can set the font and point size for a text written using drawString with the Graphics method setFont.