Graphics and painting

(Chapter 15)

paint() methods

So, what can we DO in the paint() or paintComponent() methods? Pretty much anything that's available in the Graphics class, and then some.
 

Using JPanel and painting

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyPanel extends JPanel {

   // This method is inherited from Component.
   // We over-write this method so we can
   // define our own graphics that we wish to paint on the the panel.
   public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.drawLine(20, 20, 300, 20);
   }

   public static void main(String[] args) {
      // Create a JFrame object
      JFrame window = new JFrame("Hello world");
      MyPanel panel = new MyPanel();

      // This takes the panel component and places it in the
      // JFrame ... it is transparent so can't really be seen.
      window.add(panel);
      
      // When the user clicks exit to terminate the window.
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // How big should the window be.
      window.setSize(400, 400);

      // Make the windows render to the screen.
      window.setVisible(true);
   }
}

class Graphics and other useful helper classes

JPanel and painting: self adjusting circles.

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyCircles extends JPanel {

   /**
    *  This method is inherited from Component.
    *  We over-write this method so we can
    *  define our own graphics that we wish to paint on the the panel.
    *  
    *  When we resize the window this method will be called automatically
    *  again, this allows us to resize the circle and draw it so that it 
    *  keeps its shape with the chaning size of the window.
    */
   public void paintComponent(Graphics g) {
      super.paintComponent(g);

      // Aske the panel what its height currently is.
      int height = this.getHeight();
      int width = this.getWidth();
      
      /**
       * Use drawArch() method
       * 
       * g.drawArc(x, y, width, height, startAngle, arcAngle)
       * 
       * x - the x coordinate to draw the arc at.
       * y - the y coordinate to draw the arc at.
       * width - how wide to make the circle.
       * height - how high to make the circle.
       * startAngle - where to start drawing the arc at, this 
       *    is an angle from 0 - 360 degrees.
       * arcAngle - how much of the arc to draw, for a full circle
       *  this means we need a total of 360 degrees.
       *  
       *  width-20: this will give us 10px on either size of the circle.
       *  height-20: this will give us 10px on either size of the circle.
       */
      
      
      g.drawArc(10, 10, width-20, height-20, 0, 360);

   }

   public static void main(String[] args) {
      // Create a JFrame object
      JFrame window = new JFrame("Adjustable circle");
      MyCircles panel = new MyCircles();

      // This takes the panel component and places it in the
      // JFrame ... it is transparent so can't really be seen.
      window.add(panel);

      // When the user clicks exit to terminate the window.
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      // How big should the window be.
      window.setSize(400, 400);

      // Make the windows render to the screen.
      window.setVisible(true);
   }

}

Examples of common constructs

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

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

public class ExPanel extends JPanel {
   // This method is inherited from Component.
      // We over-write this method so we can
      // define our own graphics that we wish to paint on the panel.
      public void paintComponent(Graphics g) {
         super.paintComponent(g);
         /*Draw a regular line.*/
         g.drawLine(20, 20, 300, 20);
         
         Color defaultColor = g.getColor();
         /* Set pen color to red.*/
         g.setColor(Color.RED);
         /*Draw a red line.*/
         g.drawLine(20, 30, 300, 30);
         
         /* Create my own custom color*/
         Color myColor = new Color(28, 123, 98);
         g.setColor(myColor);
         g.drawLine(20, 40, 300, 40);
         
         /*Restore the color.*/
         g.setColor(defaultColor);
         
         g.drawString("Hello world", 20, 60);
         
         Font defaultFont = g.getFont();
         
         /*Using a logical font, make font bold, set size to 14 point font.*/
         Font myFont = new Font("SansSerif", Font.BOLD, 14);
         g.setFont(myFont);
         g.drawString("Hello world", 20, 80);
         
         /* Make font bold and italic as well as 20 point font*/
         myFont = new Font("SansSerif", Font.BOLD+Font.ITALIC, 20);
         g.setFont(myFont);
         
         g.drawString("Hello world", 20, 100);
         g.setFont(defaultFont);
         
         /*Polygon using array.*/
         int [] xPoints = { 10, 20, 35, 55, 80, 105, 125, 140, 150};
         int [] yPoints = { 150, 140, 130, 120, 110, 120, 130, 140, 150};
         int nPoints = xPoints.length;
         g.drawPolygon(xPoints, yPoints, nPoints);
         
         Polygon myShape = new Polygon();
         myShape.addPoint(10, 250);
         myShape.addPoint(20, 240);
         myShape.addPoint(35, 230);
         myShape.addPoint(55, 220);
         myShape.addPoint(80, 210);
         myShape.addPoint(105, 220);
         myShape.addPoint(125, 230);
         myShape.addPoint(140, 240);
         myShape.addPoint(150, 250);
         
         g.drawPolygon(myShape);
         
      }

      public static void main(String[] args) {
         // Create a JFrame object
         JFrame window = new JFrame("Hello world");
         ExPanel panel = new ExPanel();

         // This takes the panel component and places it in the
         // JFrame ... it is transparent so can't really be seen.
         window.add(panel);
         
         // When the user clicks exit to terminate the window.
         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         // How big should the window be.
         window.setSize(400, 400);

         // Make the windows render to the screen.
         window.setVisible(true);
      }

}


Java2D

The Java2D API provides advanced graphics capabilities, for more detailed and complex two-dimensional drawing.

Java 2D API -- for an overview, see this page on the Java web site.

A Java-2D Tutorial

See the last two chapter 15 examples -- these illustrate some sample uses of Graphics2D methods draw() and fill() , along with some of the Java2D shape types and drawing options.

Deitel examples