I am trying to write the code for a face that winks. My code doesn't give me any errors however when I try to run it the window comes up as a blank window. It does have the corredt title but no face is drawn in it and I'm not sure why.
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
class Face{
private boolean wink;
private final int x, y;
private final int faceRadius;
private final Color faceColor = Color.YELLOW;
private final int faceDiameter, faceLeft, faceTop;
private final int mouthWidth, mouthHeight, mouthLeft, mouthTop;
private final int eyeDiameter, eyeTop, leftEyeLeft, rightEyeLeft;
public static final int NOSE_WIDTH = 100;
public static final int NOSE_HEIGHT = 50;
public static final int X_NOSE = 150;//Center of nose will be at 200
public static final int Y_NOSE = 130;
public static final int NOSE_START_ANGLE = 90;
public static final int NOSE_DEGREES_SHOWN = 170;
private static final Color EYE_COLOR = Color.BLACK;
private static final Color MOUTH_COLOR = Color.BLACK;
public Face(){
this(200, 100, 75, Color.YELLOW);
}
public Face(int centerX, int centerY, int radius, Color color) {
wink = true;
x = centerX;
y = centerY;
faceRadius = radius;
faceDiameter = 2 * faceRadius;
faceLeft = x - faceRadius;
faceTop = y - faceRadius;
mouthWidth = (int)(0.4 * faceDiameter);
mouthHeight = mouthWidth / 4;
mouthLeft = x - mouthWidth/2;
mouthTop = y + 2 * mouthHeight;
int mouthRight = mouthLeft + mouthWidth - 1;
int eyeRadius = (int)(0.15 * faceRadius);
eyeDiameter = 2 * eyeRadius;
eyeTop = y - (faceRadius - 2 * mouthHeight) / 3;
leftEyeLeft = mouthLeft - eyeRadius;
rightEyeLeft = mouthRight - eyeRadius;
}
public void wink(){
wink = true;
}
public void normal(){
wink = false;
}
public void paintFace(Graphics g){
//Draw face
g.setColor(faceColor);
g.fillOval(faceLeft, faceTop, faceDiameter, faceDiameter);
//Draw Eyes
g.setColor(EYE_COLOR);
g.fillOval(leftEyeLeft, eyeTop, eyeDiameter, eyeDiameter);
g.fillOval(rightEyeLeft, eyeTop, eyeDiameter, eyeDiameter);
//Draw mouth
g.setColor(MOUTH_COLOR);
int ovalTop = mouthTop - mouthHeight;
g.fillOval(mouthLeft, ovalTop, mouthWidth,2 * mouthHeight);
//Draw nose:
g.setColor(Color.BLACK);
g.drawArc(X_NOSE, Y_NOSE, NOSE_WIDTH, NOSE_HEIGHT, NOSE_START_ANGLE, NOSE_DEGREES_SHOWN);
g.setColor(faceColor);
if(wink){
g.fillOval(rightEyeLeft, eyeTop, eyeDiameter, eyeDiameter);
}else{
g.fillOval(rightEyeLeft, eyeTop, eyeDiameter, eyeDiameter);
}
}
}
class FacePanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
public static final int PANEL_WIDTH = 400;
public static final int PANEL_HEIGHT = 550;
public static final int FACE_X = PANEL_WIDTH / 2;
public static final int FACE_Y = PANEL_HEIGHT / 2;
public static final int FACE_RADIUS = PANEL_WIDTH / 4;
public static final Color FACE_COLOR = Color.YELLOW;
private final Face aFace;
public FacePanel(){
aFace = new Face(FACE_X, FACE_Y, FACE_RADIUS, FACE_COLOR);
setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
}
public void paintComponent(Graphics g){
aFace.paintFace(g);
}
public void setWink(boolean wink){
if(wink){
aFace.wink();
}else{
aFace.normal();
}
repaint();
}
}
class faceAndButtonPanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private final FacePanel facePanel;
private final JPanel buttonPanel;
private static final boolean WINK = true, NORMAL = false;
public faceAndButtonPanel(){
facePanel = new FacePanel();
buttonPanel = new JPanel();
ButtonListener listener = new ButtonListener();
JButton winkButton = new JButton("Wink");
winkButton.setActionCommand("wink-button");
winkButton.addActionListener(listener);
buttonPanel.add(winkButton);
JButton normalButton = new JButton("Normal");
normalButton.setActionCommand("normal-button");
normalButton.addActionListener(listener);
buttonPanel.add(normalButton);
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
if("wink-button".equals(event.getActionCommand())){
facePanel.setWink(WINK);
}else{
facePanel.setWink(NORMAL);
}
}
}
}
public class FaceDriver {
public static void main (String[] args){
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setTitle("Winking Face");
faceAndButtonPanel panel = new faceAndButtonPanel();
window.add(panel);
window.pack();
window.setVisible(true);
}
}