Java: Example - Drawing a Face v2

Main ideas in this example:

Create a window by subclassing JFrame, putting main() in this class, and building the GUI in the constructor. This style is very popular for one-window GUIs.

Subclass JComponent and override the paintComponent() method to do the drawing. This is a good way to draw graphics.

One class per source file - a very good practice.

The main program and GUI

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
// File   : GUI-lowlevel/face2/Face2.java - Example app with graphics component.
// Description: Application which simply displays Ms Paclady.
// Illustrates: 1. Basic JFrame subclass style of creating window.
//              2. Subclassing JComponent and overriding JComponent to
//                 do graphics.
// Author : Fred Swartz - 2007-01-20 - Placed in public domain.

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

///////////////////////////////////////////////////////////////////////// Face
public class Face2 extends JFrame {
    // ================================================================== main
    public static void main(String[] args) {
        Face2 window = new Face2();
        window.setVisible(true);
    }
    
    // ============================================================ constructor
    Face2() {
        JPanel content = new JPanel();              // Create content panel.
        content.setLayout(new BorderLayout());
        
        PacLady drawing = new PacLady();            // Create a PacLady
        content.add(drawing, BorderLayout.CENTER);  // Put in expandable center.
        
        this.setContentPane(content);
        this.setTitle("Ms Paclady");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();                                // Finalize window layout
        this.setLocationRelativeTo(null);           // Center window on screen.
    }
}

The graphical component that draws a face

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
// File   : PacLady.java
// Purpose: Graphics component to display Ms Paclady.
// Author : Fred Swartz - 1996 ... 2006-12-17 - Placed in public domain.

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

///////////////////////////////////////////////////////// class PacLady
    // This class extends JComponent and overrides paintComponent to
    // create a component for drawing - in this case a face.
    // It doesn't do anything useful.

public class PacLady extends JComponent {
    //====================================================== constants
    private static final int BORDER = 8;  // Border in pixels.
    
    //===================================================== constructor
    PacLady() {
        this.setPreferredSize(new Dimension(400, 400));  // size
    }
    
    //================================================== paintComponent
    @Override public void paintComponent(Graphics g) {
        int w = getWidth();
        int h = getHeight();
        
        //... Draw background.
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, w, h);
        
        //... Draw the head with open mouth and 8 pixel border
        g.setColor(Color.PINK);
        g.fillArc(BORDER, BORDER, w - 2*BORDER, h - 2*BORDER, 30, 300);  
        
        //... Draw the eye
        g.setColor(Color.MAGENTA);
        int eyeSize = w / 7;
        g.fillOval(w / 2, h / 5, eyeSize, eyeSize);
    }
}