1 / 20

Graphics Class

Graphics Class Graphics class is a class in java.awt package. contains methods for creating line drawings, rectangles, ovals, arcs, polygons. control color and fonts A Graphics object represents a particular drawing surface.

adamdaniel
Download Presentation

Graphics Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphics Class • Graphics class is a class in java.awt package. • contains methods for creating line drawings, rectangles, ovals, arcs, polygons. • control color and fonts • A Graphics object represents a particular drawing surface. • We cannot directly call the constructor of Graphics class to create a Graphics object. • A Graphics object is created indirectly. For example, each applet is associated with a Graphics object, and we can access this Graphics object. • Any methods called with the Graphics object associated with an applet will affect that applet. • An object of Graphics class represents a particular drawing surface, and Graphics class contains methods for drawing shapes on that surface. COP 3330 Object Oriented Programming

  2. An Applet’s Graphics Context • An applet has a graphics context which is automatically passed to the paint method when it is invoked. import java.applet.Applet; import java.awt.*; public class AnApplet extends Applet { public void paint(Graphics g) { … … // using this Graphics object g, … … // we can draw shapes on this applet. } } COP 3330 Object Oriented Programming

  3. Coordinate System 0,0 width drawing surface of an applet height width-1,height-1 • Each point on the coordinate system represents a single pixel. • Anything drawn outside of this area will not be visible. COP 3330 Object Oriented Programming

  4. An Example import java.applet.Applet; import java.awt.*; public class AnApplet extends Applet { public void paint(Graphics g) { g.drawRect(10,10,100,50); top left corner width and height of the rectangle g.drawLine(10,10,110,60); source coordinate destination coordinate g.drawString(“This a rectangle”,10,150); } } top left corner COP 3330 Object Oriented Programming

  5. Drawing Shapes • Graphics class directly supports the drawing of: • lines • ovals (circles are special forms of ovals) • rectangles • arcs • polygons – triangles, hexagons, … • polylines – a series of line segments • Most shapes (except polylines) can be drawn filled or unfilled. • We can see the other graphics objects under an unfilled object • The foreground color is used to fill shapes. The other graphics objects under a filled object cannot be seen. • Thickness cannot be specified (always 1 pixel). Thicker lines can be drawn by multiple lines. g.drawLine(10,10,200,10); g.drawLine(10,11,200,11); COP 3330 Object Oriented Programming

  6. Rectangles drawRect(xsrc,ysrc,width,height) fillRect(xsrc,ysrc,width,height) top left corner COP 3330 Object Oriented Programming

  7. Ovals • A bounding rectangle is used to define the position of an oval. drawRect(xsrc,ysrc,width,height) fillRect(xsrc,ysrc,width,height) top left corner of the bounding rectangle bounding rectangle is not seen • if width and height are equal  a circle COP 3330 Object Oriented Programming

  8. Some Other Rectangle Related Methods clearRect(x,y,width,height) draws a (filled) rectangle in the current background color. cleans that rectangle area. drawRoundRect(x,y,width,height,arcwidth,archeight) fillRoundRect(x,y,width,height,arcwidth,archeight) draws a rectangle with rounded corners. archeight arcwidth COP 3330 Object Oriented Programming

  9. Arcs • An arc is a segment of an oval. • The segment begins at a specific angle, and extends for a distance specified by the arc angle. drawArc(x,y,width,height,startangle,arcangle) fillArc(x,y,width,height,startangle,arcangle) -270,90 counter-clockwise (positive) 180,-180 0,360,-360 clockwise (negative) 270,-90 COP 3330 Object Oriented Programming

  10. Arcs (cont.) • startangle is in degrees – • a value between 0 and 360, or between 0 and -360 • arcangle • positive (counter clockwise) • negative (clockwise) drawArc(20,20,50,50,90,90); drawArc(20,20,50,50,-270,90); drawArc(20,20,50,50,180,-90); drawArc(20,20,50,50,-180,-90); same arc is drawn by all of them fillArc(20,20,50,50,90,90); COP 3330 Object Oriented Programming

  11. Polygons • A polygon is a multi-sided figure • A polygon is defined using a series of <x,y> points which indicates the end points of the sides of that polygon. • Polygons are closed. It forms a line segment from the last point to the first. • End points can be specified by: • two integer arrays or • an object of Polygon class indicating the end points. drawPolygon(int[] xpoints, int[] ypoints, int numofpoints) fillPolygon(int[] xpoints, int[] ypoints, int numofpoints) drawPolygon(Polygon poly) fillPolygon(Polygon poly) COP 3330 Object Oriented Programming

  12. Polygons (cont.) int[] xs = {50,100,150,100}; 100,50 int[] ys = {100,50,100,150}; drawPolygon(xs,ys,4); 50,100 150,100 Polygon p = new Polygon(); p.addPoint(50,100); p.addPoint(100,50); 100,150 p.addPoint(150,100); p.addPoint(100,150); drawPolygon(p); COP 3330 Object Oriented Programming

  13. Polyline • A polyline is a similar to a polygon except it is not closed. • There is no line segment from the last point to the first one. • Polylines cannot be filled. int[] xs = {50,100,150,100}; int[] ys = {100,50,100,150}; drawPolyline(xs,ys,4); COP 3330 Object Oriented Programming

  14. Color Class • Color class is used to define and manage the color in which shapes are drawn. • We can control the color of the shapes we draw. • A color is defined by an R G B value (red,green,blue) that specifies the relative contribution of these three primary colors red, green, blue (values between 0 to 255). • Color class contains several final static Color objects to define basic colors. • blue Color.blue 0,0,255 • green Color.green 0,255,0 • red Color.red 255,0,0 • black Color.black 0,0,0 • white Color.white 255,255,255 • yellow Color.yellow 255,255,0 • gray Color.gray 128,128,128 COP 3330 Object Oriented Programming

  15. Color Class (cont.) • In addition to basic colors defined in Color class, we can define our own color by setting these three values (2563 – almost 16 million different colors). Color mycolor = new Color(100,100,100); • We can change foreground (colors of shapes) and background (general color of the applet) colors. In paint method, setBackground(Color.white) changes the background color to white. setBackground is a method of Applet class. g.setColor(Color.red) changes the foreground color to red. setColor is a method of Graphics class. COP 3330 Object Oriented Programming

  16. Color Class - Example public void paint (Graphics g) { setBackground(Color.white);  background is white g.setColor(Color.yellow);  color of pen is yellow g.drawString(“This is yellow”,20,20); This is yellow g.setColor(Color.blue);  color of pen is blue g.drawLine(20,50,200,50);  g.setColor(Color.green);  color of pen is green g.drawRect(20,100,200,100);  } COP 3330 Object Oriented Programming

  17. Fonts • A font defines the look of each character when it is printed or drawn. • Font class provides methods for specifying fonts in a Java program. • There is a specific set of fonts in each system. We can use one of the fonts from this list. • Using the constructor of Font class we can create a Font object. • Then we can set the current font to this Font object using setFont method in Graphics class. new Font(fontname,style,size) “TimesRoman” Font.PLAIN 12 14 16 18 20 … “Helvetica” Font.BOLD Font.ITALIC COP 3330 Object Oriented Programming

  18. Fonts -- Example import java.applet.Applet; import java.awt.*; public class FontTest extends Applet { public void paint(Graphics g) { g.setFont(new Font("TimesRoman",Font.PLAIN,16)); g.drawString("Font is TimesRoman-PLAIN-16",10,30);  Font is TimesRoman-PLAIN-16 g.setFont(new Font("TimesRoman",Font.ITALIC,20)); g.drawString("Font is TimesRoman-ITALIC-20",10,60);  Font is TimesRoman-ITALIC-20 g.setFont(new Font(“Courier",Font.BOLD+Font.ITALIC,20)); g.drawString("Font is Courier-BOLD-ITALIC-20",10,90);  Font is Courier-BOLD-ITALIC-20 g.setFont(new Font("Courier",Font.PLAIN,16)); g.drawString("Font is Courier-PLAIN-16",10,120);  Font is Courier-PLAIN-16 } } COP 3330 Object Oriented Programming

  19. GraphicsTest import java.applet.Applet; import java.awt.*; public class GraphicsTest extends Applet { public void paint(Graphics g) { resize(600,400); g.drawRect(10,10,200,100); g.drawLine(10,10,210,110); g.drawLine(10,110,210,10); g.drawString("This is a rectangle",10,130); g.setColor(Color.red); g.drawString("A red string",10,150); g.setColor(Color.blue); g.drawString("A blue string",10,170); g.fillRect(300,10,100,50); g.drawOval(300,100,100,50); g.fillOval(300,200,100,50); g.drawOval(300,300,40,40); g.fillOval(350,300,40,40); } } COP 3330 Object Oriented Programming

  20. Output of GraphicsTest COP 3330 Object Oriented Programming

More Related