Hey there, I have a homework problem for us to solve. I have to make a program for my introductory programming course that displays either one picture (a "wild face") or another (a "happy face") when the user inputs their response to the question("Are you feeling happy or wild?"). The problem is that I don't know how to make the if/else statement display the appropriate graphics on screen. Here's my code so far:
Eclipse keeps telling me to delete the token "canvas" and that Graphics cannot be resolved to a variable, whatever those two statements mean. Thanks for your time and any help.
import javax.swing.JApplet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Scanner;
public class HappyWild{
public void init() {
setSize(1000,1000);
}
public void wildFace (Graphics canvas)
{
//Draw outline
canvas.setColor(new Color(237,207,159));
canvas.fillOval(100, 50, 200, 200);
canvas.setColor(Color.BLACK);
canvas.drawOval (100, 50, 200, 200);
//Draw eyebrows
canvas.fillRoundRect(150, 80, 40, 10, 8, 16);
canvas.fillRoundRect(215, 80, 40, 10, 8, 16);
//Draw mustache
canvas.drawLine (190, 184, 150, 195);
canvas.drawLine (210, 184, 250, 195);
//Draw lips
canvas.drawArc (163, 190, 32, 20, 360, 180);
canvas.drawArc (195, 190, 32, 20, 360, 180);
//Draw eyelids
canvas.setColor(Color.BLACK);
canvas.drawArc (150, 95, 40, 20, 360, 180);
canvas.drawArc (215, 95, 40, 20, 360, 180);
//Draw nose
canvas.drawLine (200, 125, 200, 165);
canvas.drawArc (192, 175, 16, 4, 180, 180);
canvas.drawArc (208, 170, 8, 4, 90, 180);
canvas.drawArc (184, 170, 8, 4, 99, -180);
//Draw mouth
canvas.drawLine (163, 200, 228, 200);
//Draw eyes
canvas.setColor(Color.WHITE);
canvas.fillOval (155, 100, 25, 20);
canvas.fillOval (220, 100, 25, 20);
canvas.setColor(Color.BLACK);
canvas.drawOval (155, 100, 25, 20);
canvas.drawOval (220, 100, 25, 20);
//Draw irises
canvas.setColor(new Color(158,104,17));
canvas.fillOval (163, 107, 10, 10);
canvas.fillOval (228, 107, 10, 10);
//Draw pupils
canvas.setColor(Color.BLACK);
canvas.fillOval (165, 109, 6, 6);
canvas.fillOval (230, 109, 6, 6);
//Draw eye shine
canvas.setColor(Color.WHITE);
canvas.fillOval (168, 109, 2, 2);
canvas.fillOval (233, 109, 2, 2);
}
public void happyFace (Graphics canvas)
{
// Draw outline
canvas.drawOval (100, 50, 200, 200);
// Draw eyes
canvas.fillOval (155, 100, 10, 20);
canvas.fillOval (230, 100, 10, 20);
// Draw mouth
canvas.drawArc (150, 160, 100, 50, 180, 180);
}
public static void main(String[] args) {
System.out.println("Are you feeling happy or wild?");
System.out.println("Type in either 'happy' or 'wild'.");
Scanner keyboard = new Scanner(System.in);
String answer = keyboard.next();
System.out.println("You entered: \"" + answer + "\"");
if (answer.equalsIgnoreCase("happy"))
happyFace (Graphics canvas);
else if (answer.equalsIgnoreCase("wild"))
wildFace (Graphics canvas);
else
System.out.println("incorrect input.");
}
}
Eclipse keeps telling me to delete the token "canvas" and that Graphics cannot be resolved to a variable, whatever those two statements mean. Thanks for your time and any help.