Sample Graphics Programs

The following program illustrates the drawing primitives.

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

public class TestGraphics
{
  public static void main ( String[] args )
  {
      JFrame frame = new JFrame ("Graphics Test");
      DrawingPanel dPanel = new DrawingPanel();
      frame.getContentPane().add(dPanel);
      frame.setSize (800, 600);
      frame.setLocation (40, 40);
      frame.setVisible (true);
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  }
}

class DrawingPanel extends JPanel
{
  public void paintComponent ( Graphics g )
  {
    super.paintComponent ( g );
	
    // Set the font
    g.setFont (new Font("Serif", Font.BOLD, 20));
    g.drawString ("Serif", 10, 30);
    g.setFont (new Font("SansSerif", Font.ITALIC, 20));
    g.drawString ("SansSerif", 150, 30);
    g.setFont (new Font ("Monospaced", Font.PLAIN, 20));
    g.drawString ("Monospaced", 300, 30);
	
    // Draw lines to form a T
    g.setColor (Color.red);
    g.drawLine (10, 50, 100, 50);
    g.setColor (Color.blue);
    g.drawLine (55, 50, 55, 150);

    // Draw rectangle and filled rounded square
    g.setColor (Color.orange);
    g.drawRect (150, 50, 50, 75);
    g.fillRoundRect (300, 50, 50, 75, 20, 20);	
		
    // 3D: Draw filled rectangle and square
    g.setColor (Color.green);
    g.fill3DRect (10, 200, 50, 75, true);
    g.draw3DRect (150, 200, 50, 50, true);

    // Draw an open and a filled ellipse
    g.setColor (Color.cyan);
    g.drawOval (250, 200, 50, 75);
    g.fillOval (350, 200, 50, 75);
		
    // Draw an open and a filled circle
    g.setColor (Color.magenta);
    g.drawOval (10, 300, 75, 75);
    g.fillOval (150, 300, 75, 75);

    // Draw arc to represent a Pac man
    g.setColor (Color.pink);
    g.drawArc (250, 300, 50, 50, 30, 300);
    g.fillArc (350, 300, 50, 50, 30, 300);		

    // Create a polygon
    int[] xPoints = {10, 30, 40, 50, 70, 50, 40, 30};
    int[] yPoints = {440, 430, 410, 430, 440, 450, 470, 450};
    int nPoints = xPoints.length;
    Polygon star = new Polygon (xPoints, yPoints, nPoints);
    g.setColor (Color.yellow);
    g.drawPolygon (star);
		
    // Put a box around the polygon
    g.setColor (Color.darkGray);
    Rectangle bounds = star.getBounds();
    g.drawRect (bounds.x, bounds.y, bounds.width, bounds.height);

    // Set the clipping region
    g.setClip (200, 400, 100, 100);
		
    // Draw Pac-man in a 500 x 600 area
    for (int i = 0; i < 500; i += 60)
      for (int j = 0; j < 600; j += 60)
        g.fillArc (i, j,		// top left hand corner
	           50, 50,		// width, height
	           30, 300);		// start angle, sweep angle	

  }
	
}

This program shows how you can use the primitives to draw graphs.


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

public class DrawingGraph 
{
  public static void main ( String[] args )
  {
      JFrame frame = new JFrame ("Drawing a Graph");
      DrawingPanel dPanel = new DrawingPanel();
      frame.getContentPane().add(dPanel);
      frame.setSize (800, 600);
      frame.setLocation (40, 40);
      frame.setVisible (true);
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  }
}

class DrawingPanel extends JPanel
{
  public void paintComponent ( Graphics g )
  {
    super.paintComponent ( g );
	
    // Set up a Cartesian coordinate system

    // get the size of the drawing area		
    Dimension size = this.getSize();

    // place the origin at the middle
    g.translate (size.width / 2, size.height / 2);

    // draw the x and y axes
    drawXYAxes (g);
		
    graphLine (g, 0.5, 25.2);
    graphQuadratic (g, -0.125, 0, 0);

  }
	
  private void drawXYAxes (Graphics g) {
    Dimension size = this.getSize();
    int hBound = size.width / 2;
    int vBound = size.height / 2;
    int tic = size.width / 100;
			
    // draw the x-axis
    g.drawLine (-hBound, 0, hBound, 0);

    // draw the tic marks along the x axis
    for (int k = -hBound; k <= hBound; k += 10)
      g.drawLine (k, tic, k, -tic);
			
    // draw the y-axis
    g.drawLine (0, vBound, 0, -vBound);	

    // draw the tic marks along the y axis
    for (int k = -vBound; k <= vBound; k += 10)
      g.drawLine (-tic , k, +tic, k);					
  }
	
  private void graphLine (Graphics g, double a,  double b) {
    Dimension size = this.getSize();
    g.setColor (Color.red);
		
    int x1 = size.width / 2;
    int y1 = (int) ((a * x1) + b);
    y1 = - y1;
		
    int x2 = - x1;
    int y2 = (int)((a * x2) + b);
    y2 = - y2;
		
    g.drawLine (x1, y1, x2, y2);	
  }
	
  private void graphQuadratic (Graphics g, double a, double b, double c) {
    Dimension size = this.getSize();
    g.setColor (Color.blue);
	
    int hBound = size.width / 2;
    for (int i = -hBound; i <= hBound; i++) {
      int x1 = i;
      int y1 = (int) (a * x1 * x1 + b * x1 + c);
      y1 = - y1;
		
      int x2 = x1 + 1;
      int y2 = (int) (a * x2 * x2 + b * x2 + c);
      y2 = - y2;
					
      g.drawLine (x1, y1, x2, y2);
  }	
  
  }
}