Graphics DrawingCanvas canvas = new DrawingCanvas( ); Note: - - PowerPoint PPT Presentation

graphics drawingcanvas canvas new drawingcanvas
SMART_READER_LITE
LIVE PREVIEW

Graphics DrawingCanvas canvas = new DrawingCanvas( ); Note: - - PowerPoint PPT Presentation

Graphics DrawingCanvas canvas = new DrawingCanvas( ); Note: DrawingCanvas is not a standard API class; it must be downloaded from the class webpage. made up of pixels Javas (wacky) coordinate system x axis (0,0) the larger the y the


slide-1
SLIDE 1

Graphics

slide-2
SLIDE 2

DrawingCanvas canvas = new DrawingCanvas( );

Note: DrawingCanvas is not a standard API class; it must be downloaded from the class webpage.

slide-3
SLIDE 3

made up

  • f pixels
slide-4
SLIDE 4

Java’s (wacky) coordinate system

x axis y axis

(0,0) (x,y)

the larger the y the farther down the pixel is on the DrawingCanvas

slide-5
SLIDE 5

Shapes

slide-6
SLIDE 6

Graphics object

To draw in Java we must first get a Graphics

  • bject from the DrawingCanvas:

import java.awt.Graphics; . . . Graphics g = canvas.getGraphics();

slide-7
SLIDE 7

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) g.drawLine(50, 25, 180, 220);

(x1,y1) (x2,y2)

slide-8
SLIDE 8

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) g.drawRect(30, 70, 150, 100);

(x,y) width

void drawRect (int x, int y, int width, int height)

height

slide-9
SLIDE 9

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) g.fillRect(30, 70, 150, 100);

(x,y) width

void drawRect (int x, int y, int width, int height)

height

void fillRect (int x, int y, int width, int height)

slide-10
SLIDE 10

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) g.drawOval(30, 70, 150, 100);

(x,y) width

void drawRect (int x, int y, int width, int height)

height

void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height)

slide-11
SLIDE 11

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) g.fillOval(30, 70, 150, 100);

(x,y) width

void drawRect (int x, int y, int width, int height)

height

void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height) void fillOval (int x, int y, int width, int height)

slide-12
SLIDE 12

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) g.drawArc(30, 70, 150, 100, 45, 180);

(x,y) width

void drawRect (int x, int y, int width, int height)

height

void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height) void fillOval (int x, int y, int width, int height) void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle)

startAngle arcAngle

slide-13
SLIDE 13

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) g.drawString(“I love java!”, 100, 50);

(x,y)

void drawRect (int x, int y, int width, int height) void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height) void fillOval (int x, int y, int width, int height) void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) void drawString (String s, int x, int y)

slide-14
SLIDE 14

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) import java.awt.Polygon; // create Polygon object Polygon p = new Polygon(); p.addPoint(150,150); p.addPoint(250,100); p.addPoint(325,125); p.addPoint(375,225); p.addPoint(450,250); p.addPoint(275,375); g.drawPolygon(p); // draw Polygon void drawRect (int x, int y, int width, int height) void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height) void fillOval (int x, int y, int width, int height) void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) void drawString (String s, int x, int y) void drawPolygon (Polygon p)

slide-15
SLIDE 15
slide-16
SLIDE 16

red green blue R G B

values

0..255

slide-17
SLIDE 17

Java predefined colors java.awt.Color

Color.black (0, 0, 0) Color.red (255, 0, 0 ) Color.green (0, 255, 0) Color.blue (0, 0, 255) Color.magenta (255, 0, 255) Color.yellow (255, 255, 0) Color.cyan (0, 255, 255) Color.white (255,255,255)

Color.

  • range

Color. pink

Color.lightGray Color.gray Color.darkGray

slide-18
SLIDE 18

g.setColor(Color.red); g.fillRect(30, 70, 150, 100);

Graphics methods

void drawLine (int x1, int y1, int x2, int y2) void drawRect (int x, int y, int width, int height) void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height) void fillOval (int x, int y, int width, int height) void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) void drawString (String s, int x, int y) void setColor (Color c)

slide-19
SLIDE 19

// Create Color Object using RGB value Color mycolor = new Color(255,0,0); // Create Color Object using floats. [0.0,1.0] //Color mycolor = new Color(1.0, 0.0, 0.0); g.setColor(mycolor); g.fillRect(30, 70, 150, 100);

Graphics methods

void drawLine (int x1, int y1, int x2, int y2) void drawRect (int x, int y, int width, int height) void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height) void fillOval (int x, int y, int width, int height) void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) void drawString (String s, int x, int y) void setColor (Color c)

slide-20
SLIDE 20

Graphics methods Examples

void drawLine (int x1, int y1, int x2, int y2) canvas.setBackground(Color.yellow); g.setColor(Color.red); g.fillRect(30, 70, 150, 100); void drawRect (int x, int y, int width, int height) void fillRect (int x, int y, int width, int height) void drawOval (int x, int y, int width, int height) void fillOval (int x, int y, int width, int height) void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) void setcolor (Color c)

DrawingCanvas method

void setBackground (Color c) void drawString (String s, int x, int y)

slide-21
SLIDE 21

How do we access the Graphics object?

slide-22
SLIDE 22

import java.awt.Graphics ; import java.awt.Color ; class DrawingExample { public static void main (String[] args) { } }

instantiate DrawingCanvas object

DrawingCanvas canvas = new DrawingCanvas( ); Graphics g = canvas.getGraphics( ); g.drawLine(10, 50, 25, 100);

call drawing methods

Let’s try it… download DrawingCanvas.java

get Graphics object for drawing