2D graphics in Java - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

2D graphics in Java

Description:

All graphics operations in Java are performed using a java. ... Plotting Points ... For example to plot a point at the 10 by 15 pixels away from the origin use: ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 13
Provided by: theunivers70
Category:

less

Transcript and Presenter's Notes

Title: 2D graphics in Java


1
2D graphics in Java
2
java.awt.Component
  • Java included basic graphics capabilities as part
    of the AWT (Abstract Windowing Toolkit).
  • All graphics operations in Java are performed
    using a java.awt.Graphics object. The Graphics
    object serves three purposes
  • it represents the drawing surface. A Graphics
    object is used to draw into a java.awt.Component
    on the screen, to draw into an off-screen
    java.awt. Image, or to send graphics to a
    printer.
  • It maintains the current state of graphics
    attributes, such as the current drawing color,
    font, and clipping region.
  • It defines methods that perform various graphics
    operations, such as drawing lines, rendering
    strings of text, and copying the content of Image
    objects onto the drawing surface.

3
Lines
  • To draw a line at a particular location on the
    screen, we use the method
  • java.awt.Graphics.drawLine(int x1, int y1, int
    x2, int y2)

4
Plotting Points
  • The Java language does not offer any way to draw
    points (pixels) but we can achieve it by using
    the drawLine() method like this
  • java.awt.Graphics.drawLine(int x1,
    int y1, int x1, int y1)
  • That is, we use the same starting point and the
    ending point for x1 and y1. For example to plot a
    point at the 10 by 15 pixels away from the origin
    use
  • java.awt.Graphics.drawLine(10, 15, 10,
    15)

5
Rectangles
  • The drawRect() method draws an outline rectangle.
    The left and right edges of the rectangle are at
    x and x  width. The top and bottom edges are at
    y and y  height.
  • java.awt.Graphics.drawRect(int x1, int
    y1, int width, int height)
  • To draw a filled rectangle we use the method
  • java.awt.Graphics.fillRect(int x1, int
    y1, int width, int height)
  • To draw a square 50 by 50 pixels away from the
    origin use
  • java.awt.Graphics.drawRect(50, 50, 100,
    100) This will give us a rectangle starting from
    the point (50, 50) and extending up to the point
    (150, 150)

6
Circles and Ovals
  • This method draws a circle or an ellipse such
    that it fits within the rectangle specified by
    the x, y, width and height arguments. The center
    of the oval is the center of the rectangle. When
    the value of width and height are equal we end up
    with a circle and when they are not equal we end
    up with an oval.
  • java.awt.Graphics.drawOval(int x, int
    y, int width, int height)
  • To draw a circle at the center of the origin with
    the radius of 50 pixels, use the following
    values
  • java.awt.Graphics.drawOval(60, 60, 50,
    50)
  • The following will draw an oval with the
    center at 100, 100 with a radius of 50 for x and
    80 for y
  • java.awt.Graphics.drawOval(50,
    20, 50, 100) )

7
Arcs and Wedges
  • To draw a circular or elliptical arc use the
    method
  • java.awt.Graphics.drawArc(int x, int y,
    int width, int height, int startAngle, int
    arcAngle)
  • The center of the arc is the center of the
    rectangle whose origin is (x, y) and whose size
    is specified by the width and height arguments.
    The two axes of the arc are given by the width
    and height arguments. The arc is drawn from
    startAngle to startAngle arcAngle.
  • The start angle and arc angle are in degrees, not
    radians. A start angle of 0 indicates the
    3-o'clock position. A positive arc angle
    indicates a counter-clockwise rotation a
    negative arc angle indicates a clockwise
    rotation.
  • To draw a filled arc use the method
  • java.awt.Graphics.fillArc(int x, int
    y, int width, int height, int startAngle, int
    arcAngle)

8
Clearing an area on the Screen
  • To wipe out and clean everything (or part of a
    rectangular area) of the screen with the
    background color use the method
  • java.awt.Graphics.clearRect(int x, int y,
    int width, int height)

9
Drawing Text
  • To write text/string on the screen we must use a
    special method to do so. The method that we used
    last week (System.out.print()) won't work as that
    method is not graphical.
  • Java offer a method to draw string in graphic
    mode, this method is
  • java.awt.Graphics.drawString(String s, int
    x, int y)
  • s is the text/string that we want to draw. x
    and y are the location on the screen where the
    text/string should be drawn. For example
  • g.drawString("Hello, Java", 290, 240)

10
Color
  • colors are represented by the java.awt.Color
    class.
  • Color such as Color.black and Color.white.
  • example
  • java.awt.Graphics.setColor(Color.blue)

11
Example
  • //program to draw vertical line
  • import java.awt.
  • import java.awt.event.
  • import java.util.
  • //this frame appears on your secreen and goes
    away when you close it
  • //use it to paint the 2d
  • public class vline extends Frame
  • public static void main(String args)new
    vline()
  • vline()
  • //name of frame
  • super("draw line")
  • addWindowListener(new WindowAdapter()
  • public void windowClosing(WindowEvent
    e)System.exit(0))
  • //size of frame
  • setSize(640, 230)
  • add("Center", new verline())
  • show()

12
Example
  • class verline extends Canvas
  • void showGrid(Graphics g)
  • g.setColor(Color.blue)
  • int x10 int y10
  • for(int i1ilt100i)
  • g.drawLine(x, y, x, y)
  • y
  • public void paint(Graphics g)
  • showGrid(g)
Write a Comment
User Comments (0)
About PowerShow.com