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
Java’s (wacky) coordinate system x axis (0,0) the larger the y the farther (x,y) down the pixel is on the DrawingCanvas y axis
Shapes
Graphics object To draw in Java we must first get a Graphics object from the DrawingCanvas: import java.awt.Graphics; . . . Graphics g = canvas.getGraphics();
Graphics methods Examples void drawLine (int x1, int y1, int x2, int y2) g.drawLine(50, 25, 180, 220); (x1,y1) (x2,y2)
Graphics methods Examples void drawLine (int x1, int y1, int x2, int y2) g.drawRect(30, 70, 150, 100); void drawRect (int x, int y, int width, int height) width (x,y) height
Graphics methods Examples void drawLine (int x1, int y1, int x2, int y2) 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) width (x,y) height
Graphics methods Examples void drawLine (int x1, int y1, int x2, int y2) g.drawOval(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) width (x,y) height
Graphics methods Examples void drawLine (int x1, int y1, int x2, int y2) g.fillOval(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) width void fillOval (int x, int y, int width, int height) (x,y) height
Graphics methods Examples void drawLine (int x1, int y1, int x2, int y2) g.drawArc(30, 70, 150, 100, 45, 180); 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) width void fillOval (int x, int y, int width, int height) (x,y) void drawArc (int x, int y, int width, int height, startAngle int startAngle, int arcAngle) arcAngle height
Graphics methods Examples void drawLine (int x1, int y1, int x2, int y2) g.drawString(“I love java!”, 100, 50); 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) (x,y) void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) void drawString (String s, int x, int y)
Graphics methods Examples import java.awt.Polygon; void drawLine (int x1, int y1, int x2, int y2) // create Polygon object void drawRect (int x, int y, int width, int height) Polygon p = new Polygon(); void fillRect (int x, int y, int width, int height) p.addPoint(150,150); void drawOval (int x, int y, int width, int height) p.addPoint(250,100); void fillOval (int x, int y, int width, int height) p.addPoint(325,125); p.addPoint(375,225); void drawArc (int x, int y, int width, int height, p.addPoint(450,250); int startAngle, int arcAngle) p.addPoint(275,375); void drawString (String s, int x, int y) g.drawPolygon(p); // draw Polygon void drawPolygon (Polygon p)
red green blue R G B values 0..255
Java predefined colors java.awt.Color Color.black Color.red Color.green Color.blue (0, 0, 0) (255, 0, 0 ) (0, 255, 0) (0, 0, 255) Color.magenta Color.yellow Color.cyan Color.white (255, 0, 255) (255, 255, 0) (0, 255, 255) (255,255,255) Color.lightGray Color.gray Color.darkGray Color. Color. orange pink
Graphics methods g.setColor(Color.red); void drawLine (int x1, int y1, int x2, int y2) 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 drawString ( String s, int x, int y) void setColor (Color c)
Graphics methods // Create Color Object using RGB value Color mycolor = new Color(255,0,0); void drawLine (int x1, int y1, int x2, int y2) // Create Color Object using floats. [0.0,1.0] //Color mycolor = new Color(1.0, 0.0, 0.0); void drawRect (int x, int y, int width, int height) void fillRect (int x, int y, int width, int height) g.setColor(mycolor); g.fillRect(30, 70, 150, 100); 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)
Graphics methods Examples canvas.setBackground(Color.yellow); g.setColor(Color.red); void drawLine (int x1, int y1, int x2, int y2) 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 drawString ( String s, int x, int y) void setcolor (Color c) DrawingCanvas method void setBackground (Color c)
How do we access the Graphics object?
import java.awt.Graphics ; import java.awt.Color ; class DrawingExample { public static void main (String[] args) { instantiate DrawingCanvas object DrawingCanvas canvas = new DrawingCanvas( ); get Graphics object for drawing Graphics g = canvas.getGraphics( ); call drawing g.drawLine(10, 50, 25, 100); methods } Let’s try it… } download DrawingCanvas.java
Recommend
More recommend