Graphics DrawingCanvas canvas = new DrawingCanvas( ); Note: - - PowerPoint PPT Presentation
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
DrawingCanvas canvas = new DrawingCanvas( );
Note: DrawingCanvas is not a standard API class; it must be downloaded from the class webpage.
made up
- f pixels
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
Shapes
Graphics object
To draw in Java we must first get a Graphics
- bject 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);
(x,y) width
void drawRect (int x, int y, int width, int height)
height
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)
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)
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)
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
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)
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)
red green blue R G B
values
0..255
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
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)
// 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)
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)
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( ); 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