Graphics%20Class - PowerPoint PPT Presentation

About This Presentation
Title:

Graphics%20Class

Description:

We can control the color of the shapes we draw. ... public void paint (Graphics g) { setBackground(Color.white); background is white ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 21
Provided by: IBMU362
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: Graphics%20Class


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.

2
An Applets 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.

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.

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

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)

6
Rectangles
  • drawRect(xsrc,ysrc,width,height)
  • fillRect(xsrc,ysrc,width,height)
  • top left corner

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

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

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

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)

11
Polygons
  • A polygon is a multi-sided figure
  • A polygon is defined using a series of ltx,ygt
    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)

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)

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)

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

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.

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) ?

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

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.BOLDFont.ITA
    LIC,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

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)

20
Output of GraphicsTest
Write a Comment
User Comments (0)
About PowerShow.com