graphics & java 2d drawing two dimensional shapes controlling fonts controlling colors

8
Graphics & Java 2D • Drawing Two Dimensional Shapes • Controlling Fonts • Controlling Colors

Upload: aubrey-atkins

Post on 03-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Graphics & Java 2D

• Drawing Two Dimensional Shapes

• Controlling Fonts

• Controlling Colors

Page 2: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Java’s Coordinate System

• The upper-left coordinate (0,0) is behind the title bar of the window.

• Drawing coordinates should be adjusted to draw inside the borders of a window

• Java’s Container Class (superclass of all windows in Java) has method getInsets() that determine the drawing area for a window.

Page 3: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Color Control• Constants (ORANGE, PINK, CYAN, MAGENTA,

YELLOW, BLACK, WHITE, GRAY, RED, GREEN, BLUE)

• Example– g.setColor(Color.MAGENTA);

• RGB (Red Green Blue Values)– 0 to 255 ( 0 no color, 255 full color)– 0.0 to 1.0

• Example– g.setColor(new Color(255, 0 ,0));– g.setColor (new Color (0.0f, 1.0f, 0.0f)

Page 4: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Font Control

• Java Font Styles (PLAIN, BOLD, ITALIC)

• Java Fonts– Serif (Times)– Monospaced (Courier)– SanSerif (Helvetica)

• Examples:– g.setFont(new Font (“Serif”, Font.BOLD, 12));– g.setFont(new Font(“Monospaced”, Font.ITALIC + Font.BOLD, 24)

Page 5: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Drawing Lines, Rectangles, Ovals

• Basic Drawing Methods– drawLine (x1, y1, x2, y2)– drawRect (x, y, width, height)– drawOval (x, y, width, height)

• (x,y) is top-left corner of bounding box of oval

• Fill Shapes– Call setColor method before filling shapes– fillRect (x, y, width,height)– fillOval(x, y, width, height)

Page 6: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Drawing Polygons and Polylines

• Polygons -Closed multisided shapes consisting of straight line segments

• Polylines – a sequence of connected points.

Page 7: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Drawing Polygons and Polylines• Graphics Methods for drawing Polygons

drawPolygon( int xValues[], int yValues[], int numPoints)

drawPolyline (int xValues[], int yValues[], int numPoints)

fillPolygon (int xValues[], int yValues[], int numPoints)

• Example:

int xValues[] = {20,30,40};

int yValues[] = {50,50, 60};

g.drawPolygon (xValues, yValues, 3);

g.drawPolyline (xValeus, yValues, 3);

Page 8: Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors

Drawing Polygons and Polylines• Graphics Methods for drawing PolygonsdrawPolygon(Polygon p)fillPolygon (Polygon p)

• Polygon Constructors and MethodsPolygon() – creates a new polygon that does not contain any pointsaddPoint (int x, int y) – adds point to polygonPolygon (int xValues[], int yValues[], int numPoints)

int xValues[] = {20,30,40};int yValues[] = {50,50, 60};Polygon p = new Polygon (xValues, yValues, 3);p.addPoint(30,80);g.fillPolygon (p);