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);
    }
}

No comments:

Post a Comment