Outline Shapes
Home ] Up ] [ Outline Shapes ] Filled Shapes ]

 

 

Here's an example of how you can draw outline shapes using the original, primitive AWT drawing support:

package swingExamples;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JFrame;
import javax.swing.JPanel;

class ShapesDisplayPanel extends JPanel
{
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    int cx = 5;
    int cy = 5;
    cy = drawPolys(g, cx, cy);
    cy = drawRects(g, cx, cy);
    cy = drawOvals(g, cx, cy);
  }
  
  private int drawPolys(Graphics g, int cx, int cy)
  {
    int r = 45;     // radius of circle
    cx += r;
    cy += r;
    int angle = 30;
    
    int dx = (int)(r * Math.cos(angle * Math.PI/180));
    int dy = (int)(r * Math.sin(angle * Math.PI/180));
    
    g.drawLine(cx, cy, cx + dx, cy + dy);
    g.drawLine(cx, cy, cx + dx, cy - dy);
    g.drawArc(cx - r, cy - r, 2 * r, 2 * r, angle, 360 - 2*angle);
    
    Polygon poly = new Polygon();
    cx += 100;
    for (int i = 0; i < 5; i++)
    {
      poly.addPoint((int)(cx + r * Math.cos(i * 2 * Math.PI/5)),
          (int)(cy + r * Math.sin(i * 2 * Math.PI/5)));
    }
    g.setColor(Color.RED);
    g.drawPolygon(poly);
    
    poly = new Polygon();
    cx += 100;
    for (int i = 0; i < 360; i++)
    {
      double t = i/360.0;
      poly.addPoint((int)(cx + r * t * Math.cos(8 * t * Math.PI)),
          (int)(cy + r * t * Math.sin(8 * t * Math.PI)));
    }
    g.setColor(Color.BLUE);
    g.drawPolygon(poly);
    
    return cy + r + 10;
  }
  
  private int drawRects(Graphics g, int cx, int cy)
  {
    int width = 80;
    int height = 30;
    
    g.setColor(Color.MAGENTA);
    g.drawRect(cx, cy, width, height);
    
    cx += 100;
    g.setColor(Color.GREEN);
    g.drawRoundRect(cx, cy, width, height, 15, 15);
    
    cx += 100;
    g.setColor(Color.PINK);
    g.draw3DRect(cx, cy, width, height, true);
    
    cx += 100;
    g.draw3DRect(cx, cy, width, height, false);
    
    return cy + height + 10;
  }
  
  private int drawOvals(Graphics g, int cx, int cy)
  {
    int width = 80;
    int height = 30;
    
    g.setColor(Color.MAGENTA);
    g.drawOval(cx, cy, width, height);
    
    cx += 100;
    height = 80;
    g.setColor(Color.BLUE);
    g.drawArc(cx, cy, width, height, 0, 360);
    
    cx += 100;
    g.setColor(Color.BLACK);
    g.drawOval(cx, cy, width, height);
    
    cx += 100;
    height = 100;
    g.setColor(Color.ORANGE);
    g.drawOval(cx, cy, width, height);
    
    return cy + height + 10;
  }
}

class ShapesDisplayFrame extends JFrame
{
  public ShapesDisplayFrame()
  {
    setTitle("ShapesDisplay");
    setSize(400, 280);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new ShapesDisplayPanel() );
  }
}

public class ShapesDisplay
{
  public static void main(String[] args)
  {
    ShapesDisplayFrame frame = new ShapesDisplayFrame();
    frame.setVisible(true);
  }
}

which produces:

 

This page was last modified on 02 October, 2007