See:
// Shapes.java
// Demonstrating some Java2D shapes
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
public class Shapes extends JPanel {
public void paint( Graphics g )
{
super.paint(g);
g.translate(0,-20);
// create 2D by casting g to Graphics2D
Graphics2D g2d = ( Graphics2D ) g;
// draw 2D ellipse filled with a blue-yellow gradient
g2d.setPaint(
new GradientPaint( 5, 30, // x1, y1
Color.blue, // initial Color
35, 100, // x2, y2
Color.yellow, // end Color
true ) ); // cyclic
g2d.fill( new Ellipse2D.Double( 5, 30, 65, 100 ) );
// draw 2D rectangle in red
g2d.setPaint( Color.red );
g2d.setStroke( new BasicStroke( 10.0f ) );
g2d.draw(
new Rectangle2D.Double( 80, 30, 65, 100 ) );
// draw 2D rounded rectangle with a buffered background
BufferedImage buffImage =
new BufferedImage(
10, 10, BufferedImage.TYPE_INT_RGB );
Graphics2D gg = buffImage.createGraphics();
gg.setColor( Color.yellow ); // draw in yellow
gg.fillRect( 0, 0, 10, 10 ); // draw a filled rectangle
gg.setColor( Color.black ); // draw in black
gg.drawRect( 1, 1, 6, 6 ); // draw a rectangle
gg.setColor( Color.blue ); // draw in blue
gg.fillRect( 1, 1, 3, 3 ); // draw a filled rectangle
gg.setColor( Color.red ); // draw in red
gg.fillRect( 4, 4, 3, 3 ); // draw a filled rectangle
// paint buffImage onto the JFrame
g2d.setPaint(
new TexturePaint(
buffImage, new Rectangle( 10, 10 ) ) );
g2d.fill(
new RoundRectangle2D.Double(
155, 30, 75, 100, 50, 50 ) );
// draw 2D pie-shaped arc in white
g2d.setPaint( Color.white );
g2d.setStroke( new BasicStroke( 6.0f ) );
g2d.draw(
new Arc2D.Double(
240, 30, 75, 100, 0, 270, Arc2D.PIE ) );
// draw 2D lines in green and yellow
g2d.setPaint( Color.green );
g2d.draw( new Line2D.Double( 395, 30, 320, 150 ) );
float dashes[] = { 10 };
g2d.setPaint( Color.yellow );
g2d.setStroke(
new BasicStroke( 4,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND,
10, dashes, 0 ) );
g2d.draw( new Line2D.Double( 320, 30, 395, 150 ) );
}
public static void main( String args[] )
{
JFrame frame = new JFrame("Drawing 2D shapes" );
JPanel panel = new Shapes();
frame.setSize( 425, 160 );
Container c = frame.getContentPane();
c.add(panel);
frame.show();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
// Shapes2.java
// Demonstrating a general path
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
public class Shapes2 extends JPanel {
public void paint( Graphics g )
{
super.paint(g);
int xPoints[] =
{ 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
int yPoints[] =
{ 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
Graphics2D g2d = ( Graphics2D ) g;
// create a star from a series of points
GeneralPath star = new GeneralPath();
// set the initial coordinate of the General Path
star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
// create the star--this does not draw the star
for ( int k = 1; k < xPoints.length; k++ )
star.lineTo( xPoints[ k ], yPoints[ k ] );
// close the shape
star.closePath();
// translate the origin to center of panel
Dimension d = getSize();
g2d.translate( d.width/2, d.height/2 );
// rotate around origin and draw stars in random colors
for ( int j = 1; j <= 20; j++ ) {
g2d.rotate( Math.PI / 10.0 );
g2d.setColor(
new Color( ( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ),
( int ) ( Math.random() * 256 ) ) );
g2d.fill( star ); // draw a filled star
}
}
public static void main( String args[] )
{
JFrame frame = new JFrame("Drawing General Path" );
JPanel panel = new Shapes2();
frame.setSize( 300, 300 );
Container c = frame.getContentPane();
c.setBackground( Color.yellow );
c.add(panel);
frame.show();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
// Shapes2B.java
// Demonstrating a general path
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
public class Shapes2B extends JPanel {
public void paint( Graphics g )
{
super.paint(g);
int xPoints[] =
{ 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 };
int yPoints[] =
{ 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 };
Graphics2D g2d = ( Graphics2D ) g;
// create a star from a series of points
GeneralPath star = new GeneralPath();
// set the initial coordinate of the General Path
star.moveTo( xPoints[ 0 ], yPoints[ 0 ] );
// create the star--this does not draw the star
for ( int k = 1; k < xPoints.length; k++ )
star.lineTo( xPoints[ k ], yPoints[ k ] );
// close the shape
star.closePath();
// translate the origin
Dimension d = getSize();
g2d.translate( 10, 0 );
// draw the outline
g2d.draw(star);
g2d.translate(150,0);
g2d.fill(star);
g2d.translate(-150,150);
g2d.setPaint( Color.orange );
Stroke stroke = new BasicStroke( 4,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
g2d.setStroke(stroke);
g2d.draw(star);
g2d.translate(150,0);
g2d.setPaint( Color.yellow );
g2d.fill(star);
g2d.setPaint( Color.blue );
g2d.draw(star);
}
public static void main( String args[] )
{
JFrame frame = new JFrame("General Path Examples" );
JPanel panel = new Shapes2B();
frame.setSize( 300, 300 );
Container c = frame.getContentPane();
c.setBackground( Color.yellow );
c.add(panel);
frame.show();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
// Lines.java
// Demonstrating various line drawing methods
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
public class Lines extends JPanel {
public void paint( Graphics g )
{
super.paint(g);
Graphics2D g2d = ( Graphics2D ) g;
Stroke stroke = new BasicStroke( 8,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
Stroke old_stroke = g2d.getStroke();
g2d.setStroke(stroke);
g2d.drawLine(10,70,70,10);
g2d.drawString("using drawLine",10,90);
g2d.translate(150,0);
Line2D.Double line = new Line2D.Double(10,70,70,10);
g2d.draw(line );
g2d.drawString("using Line2D shape",10,90);
g2d.translate(-150,150);
GeneralPath p = new GeneralPath();
p.append(line, false);
g2d.draw( p );
g2d.drawString("using GeneralPath",10,90);
g2d.translate(150,0);
g2d.setStroke(old_stroke);
g2d.draw( stroke.createStrokedShape(line));
g2d.drawString("using StrokedShape",10,90);
}
public static void main( String args[] )
{
JFrame frame = new JFrame("Graphics2D Lines" );
JPanel panel = new Lines();
frame.setSize( 300, 300 );
Container c = frame.getContentPane();
panel.setBackground( Color.white );
c.add(panel);
frame.show();
frame.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
Maintained by John Loomis, last updated 1 June 2000