Home Java Java-tips 45examples 30graphics 20face Java: Example - Drawing a Face - v5 - Fill-in

Ask Questions?

View Latest Questions

Advertisement


 
 

Java: Example - Drawing a Face - v5 - Fill-in
Posted on: April 17, 2011 at 12:00 AM
This page discusses - Java: Example - Drawing a Face - v5 - Fill-in

Java: Example - Drawing a Face - v5 - Fill-in

Main - Setting the instance variables

  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 
// File: face/file2/PacLadyApplication.java 
// Author: Fred Swartz,  Date: July 1998 ... Oct 2004
import java.awt.*;
import javax.swing.*;

///////////////////////////////////////////////// class PacLadyApplication
public class PacLadyApplication {
    public static void main(String[] args) {
        Face aFace = new Face();
        
        JFrame window = new JFrame("Face"); // Create a new window.
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(aFace);  // Put drawing in window.
        window.pack();                 // Adjust to fit panel.
        window.setVisible(true);       // Show the window.
        
        int eyeSize = getInt("How big should the eye be?");
        aFace.setEyeDiameter(eyeSize);
    }
    
    static int getInt(String message) {
        String s = JOptionPane.showInputDialog(null, message);
        return Integer.parseInt(s);
    }
}

Face - Setting the instance variables

  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 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
// File: face/file2/Face.java 
// Author: Fred Swartz,  Date: July 1998 ... Oct 2004

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

//////////////////////////////////////////////////////////////// class Face
class Face extends JPanel {
    //... Instance (member) variables that define the "state" of the face.
    int m_mouthAngle   = 60;  // Number of degrees open the mouth is.
    int m_eyeDiameter  = 20;  // Size of the eye.
    int m_headDiameter = 200; // Size of the head.

    /** Constructor sets background color and initial size. */
    public Face() {
        this.setPreferredSize(new Dimension(300, 300));
        this.setBackground(Color.GRAY);
    }
    
    /** Set the size of the eye. */
    public void setEyeDiameter(int size) {
        m_eyeDiameter = size;
        this.repaint();        //  Call repaint after changes.
    }

    /** Override paintComponent to draw a face */
    public void paintComponent(Graphics g) {
        super.paintComponent(g);   // Required as first line.
        
        //... Get panel width and height, Compute center.
        int w = this.getWidth();
        int h = this.getHeight();
        int centerX = w / 2;  // x-coordinate of the center.
        int centerY = h / 2;  // y-coordinate of the center.
        
        //... Draw vertical lines in the background.
        g.setColor(Color.LIGHT_GRAY);
        for (int x=0; x < w; x += 10) {
            g.drawLine(x, 0, x, h);
        }

        //... Draw everything except the mouth in pink
        g.setColor(Color.PINK);
        g.fillArc(centerX - m_headDiameter / 2, centerY - m_headDiameter / 2
                 , m_headDiameter, m_headDiameter
                 , m_mouthAngle / 2, 360 - m_mouthAngle);

        //... Draw an eye
        g.setColor(Color.MAGENTA);
        g.fillOval(centerX + 20, centerY - 60, m_eyeDiameter, m_eyeDiameter);
    }
}
Copyleft 2004 Fred Swartz MIT License
Advertisement


DMCA.com