8/29/2012

Example of Java 2D Graphics

easywayprogramming.com example of 2D graphics in java 

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

/** An example of drawing/filling shapes with Java 2D in
* Java 1.2 and later.
*/

public class ShapeExample extends JPanel
{
    private Ellipse2D.Double circle =
    new Ellipse2D.Double(10, 10, 350, 350);
    private Rectangle2D.Double square =
    new Rectangle2D.Double(10, 10, 350, 350);

    public void paintComponent(Graphics g)
    {
        //clear(g);
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(Color.black);
        g2d.fill(circle);
        g2d.draw(square);
    }

    /*protected void clear(Graphics g)
    {
        super.paintComponent(g);
    }*/

    protected Ellipse2D.Double getCircle()
    {
        return(circle);
    }

    public static void main(String[] args)
    {
        ShapeExample se=new ShapeExample();
        JFrame frm=new JFrame("Shape");
        Container c=frm.getContentPane();
        c.add(se);
        frm.setContentPane(c);
        frm.setSize(380,400);
        frm.setVisible(true);
    }
}

Java 2D graphics in Java

easywayprogramming.com 2D graphics in java 

public void paintComponent(Graphics g)
{
         // Clear background if opaque
          super.paintComponent(g);

        // Cast Graphics to Graphics2D
         Graphics2D g2d = (Graphics2D)g;

        // Set pen parameters
         g2d.setPaint(fillColorOrPattern);
         g2d.setStroke(penThicknessOrPattern);
         g2d.setComposite(someAlphaComposite);
         g2d.setFont(anyFont);
         g2d.translate(...);
         g2d.rotate(...);
         g2d.scale(...);
         g2d.shear(...);
         g2d.setTransform(someAffineTransform);

        // Allocate a shape
        SomeShape s = new SomeShape(...);

       // Draw shape
        g2d.draw(s); // outline
        g2d.fill(s); // solid
}

Drawing Shapes in 2D Graphics:
            With the AWT, you generally drew a shape by calling the drawXxx or fillXxx
method of the Graphics object. In Java 2D, you generally create a Shape object,
then call either the draw or fill method of the Graphics2D object, supplying the
Shape object as an argument. For example:
public void paintComponent(Graphics g)
{
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D)g;
        // Assume x, y, and diameter are instance variables.
       Ellipse2D.Double circle = new Ellipse2D.double(x, y, diameter, diameter);
       g2d.fill(circle);
...
}

Most of the Shape classes define both a Shape.Double and a Shape.Float version
of the class. Depending on the version of the class, the coordinate locations are
stored as either double precision numbers (Shape.Double) or single precision
numbers (Shape.Float). The idea is that single precision coordinates might be
slightly faster to manipulate on some platforms. You can still call the familiar
drawXxx methods of the Graphics class if you like; the Graphics2D object inherits
from the Graphics object. This approach is necessary for drawString and
drawImage and possibly is convenient for draw3DRect.
Shape Classes

public Ellipse2D.Float(float left, float top, float width,
float height)
public Ellipse2D.Double(double left, double top,
double width, double height)

These constructors create an ellipse bounded by a rectangle of dimension
width by height. The Ellipse2D class inherits from the Rectangular-
Shape class and contains the same methods as common to Rectangle2D and
RoundRectangle2D.

public GeneralPath()
A GeneralPath is an interesting class because you can define all the line segments
to create a brand-new Shape. This class supports a handful of methods
to add lines and Bézier (cubic) curves to the path: closePath, curveTo,
lineTo, moveTo, and quadTo. Appending a path segment to a General-
Path without first performing an initial moveTo generates an IllegalPath-
StateException. An example of creating a GeneralPath follows:
GeneralPath path = new GeneralPath();
path.moveTo(100,100);
path.lineTo(300,205);
path.quadTo(205,250,340,300);
path.lineTo(340,350);
path.closePath();

public Line2D.Float(float xStart, float yStart, float xEnd,
float yEnd)
public Line2D.Double(double xStart, double yStart,
double xEnd, double yEnd)

These constructors create a Line2D shape representing a line segment from
(xStart, yStart) to (xEnd, yEnd).

public QuadCurve2D.Float(float xStart, float yStart,
float pX, double pY,
float xEnd, float yEnd)
public QuadCurve2D.Double(double xStart, double yStart,
double pX, double pY,
double xEnd, double yEnd)

These constructors create a Shape representing a curve from (xStart,
yStart) to (xEnd, yEnd). The point (pX, pY) represents a control point
impacting the curvature of the line segment connecting the two end points.

public Rectangle2D.Float(float top, float left, float width,
float height)
public Rectangle2D.Double(double top, double left,
double width, double height)

These constructors create a Rectangle2D shape with the upper-left corner
located at (top, left) and a dimension of width by height.

public RoundRectangle2D.Float(float top, float left,
float width, float height,
float arcX, float arcY)
public RoundRectangle2D.Double(double top, double left,
double width, double height,
double arcX, double arcY)

These two constructors create a RectangleShape with rounded corners. The
upper-left corner of the rectangle is located at (top, left), and the dimension
of the rectangle is width by height. The arguments arcX and arcY represent
the distance from the rectangle corners (in the respective x direction and y
direction) at which the rounded curve of the corners start.

Most of the code examples throughout this chapter are presented as Java applications.
To convert the examples to applets, follow the given template:
import java.awt.*;
import javax.swing.*;
public class YourApplet extends JApplet {
public void init() {
JPanel panel = new ChapterExample();
panel.setBackground(Color.white);
getContentPane().add(panel);
}
}


Click here:
Example of java 2D graphics