Java – Topic 27

(Simple Graphics in Applets)

 

 

LESSON NOTE

 

 

GRAPHICS2D

 

The Graphics object that you get in the paint method knows how to paint itself on a the screen. However, it is a little limited.  We therefore create a Graphics2D object using the Graphics object.  This is done in one line.

 

Graphics2D g2d = (Graphics2D) g;

 

            From the Java documentation:

 

This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform.

 

DRAWING OR FILLING SHAPES

 

            The Graphics2D class has the following instance methods:

 

            draw(Shape s)

            fill (Shape s)

           

            We can therefore create different shapes and simply draw them on the Graphics2D object.

 

POINTS

 

In graphics with a lot of lines, it is often useful to first create points and then use those point to create lines.  To do this, we use the Point2D class or the Point2D.Double class.  The later simply allows you to specify coordinates that are double values instead of integers.  This is useful when doing math in computer graphics.

 

            CONSTRUCTORS

 

            Point2D.Double (double x, double y)

 

            EXAMPLE

 

            Create a Point2D.Double object named p1 with (4.4, 8.1) as coordinates.

 

                        Point2D.Double p1 = new Point2D.Double(4.4, 8.1);

 

SHAPES

 

There are many classes that extend the Shapes class.  Remember that any object from such a subclass is a Shape. 

 

            Line2D.Double

 

The Line2D class extends the Shape object.  Any such object can therefore be drawn on the Graphics2D object.

 

We will use the class Line2D.Double instead.  It is the same as above but allows for double values for coordinates.  This turns out to be useful when doing mathematics for graphics.

 

            CONSTRUCTORS

 

            Line2D.Double(double x1, double y1, double x2, double y2)

            Line2D.Double(Point2D p1, Point2D p2)

 

            EXAMPLE

 

            Create a line from (0,0) to (100, 100) and draw it.

 

      Line2D.Double diagonal = new Line2D.Double(0, 0, 100, 100);

      g2D.draw(diagonal);

 

            Ellipse2D.Double

 

The Ellipse2D.Double allows us to create ellipses and circles.  You simply need to specify the point of the bounding box’s top left corner as well as the width and height of the ellipse.

 

            CONSTRUCTORS

 

                        Ellipse2D.Double(double x, double y, double w, double h)

 

            EXAMPLE

 

            Create a circle with the top left corner (of bounding box) at (50,50) and a radius of 75. Fill the ellipse.

 

                        Ellipse2D.Double circle = new Ellipse2D.Double(50, 50, 75, 75);

            g2D.fill(circle);

 

            Rectangle2D.Double

 

This class allows you to create rectangles by specifying the upper left corner and the width and height of the rectangle.

 

            CONSTRUCTORS

 

                        Rectangle2D.Double(double x, double y, double w, double h)

 

            EXAMPLE

 

            Create a rectangle with top left corner (150, 90) and a width of 55 and a height of 82. Draw it.

 

                        Rectangle2D.Double r = new Rectangle2D.Double(150, 90, 55, 82);

            g2D.draw(r);


 

            RoundRectangle2D.Double

 

This class allows you to create rectangles with rounded corners.  You need to specify the same information as the regular rectangle as well as specify the x and y rounding amounts.

 

            CONSTRUCTOR

 

            RoundRectangle2D.Double(double x, double y, double w, double h, double arcw, double arch)

 

            EXAMPLE

 

Create a rounded rectangle with top left (bounding box) corner (45, 65), a width of 55 and a height of 80.  The width’s arc should be 10 and the height’s arc should be 10.  Fill the rectangle.

 

            RoundRectangle2D.Double rr = RoundRectangle2D.Double(45, 65, 55, 80, 10, 10);

      g2D.fill(rr);

           

OTHER SHAPES

 

You might want to check out the following shapes that you can also work with:

 

            Arc2D.Double

            CubicCurve2D.Double

            Path2D.Double

            QuadCurve2D.Double

 

FULL CODE EXAMPLES

 

EXAMPLE 1

 

Click here to see an applet display a rectangle with an X in it.  The code can be seen here.   

 

EXAMPLE 2

 

Click here to see a bad robot head.  The code can be seen here.