graphics drawingcanvas canvas new drawingcanvas
play

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


  1. Graphics

  2. DrawingCanvas canvas = new DrawingCanvas( ); Note: DrawingCanvas is not a standard API class; it must be downloaded from the class webpage.

  3. made up of pixels

  4. 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

  5. Shapes

  6. Graphics object To draw in Java we must first get a Graphics object from the DrawingCanvas: import java.awt.Graphics; . . . Graphics g = canvas.getGraphics();

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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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)

  14. 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)

  15. red green blue R G B values 0..255

  16. 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

  17. 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)

  18. 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)

  19. 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)

  20. How do we access the Graphics object?

  21. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend