|
||
|
|||
|
Hello,
I just recently started a Java programming class and the current assignment is to create a program that asks the user if they are happy. Depending on the response either a smiley face or sad face appears in a window. I have been struggling to figure out how to set up an if-else statement, and am also unsure of when to use: public static void main(String[] args) I know this is probably very basic level stuff but I've tried messing around with the code for over 2 hours now. This is how far I've gotten.. Java Code
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JApplet;
/**
*This is a program that displays different faces
*based off of different responses.
*2/5/2010
*/
public class Program3
extends JApplet {
public static final int FACE_DIAMETER = 200;
public static final int X_FACE = 100;
public static final int Y_FACE = 50;
public static final int EYE_WIDTH = 10;
public static final int EYE_HEIGHT = 20;
public static final int X_RIGHT_EYE = 155;
public static final int Y_RIGHT_EYE = 100;
public static final int X_LEFT_EYE = 200;
public static final int Y_LEFT_EYE = Y_RIGHT_EYE;
public static final int MOUTH_WIDTH = 100;
public static final int MOUTH_HEIGHT = 200;
public static final int X_MOUTH = 150;
public static final int Y_MOUTH = 160;
public static final int MOUTH_START_ANGLE = 180;
public static final int MOUTH_EXTENT_ANGLE = 180;
public void init()
{
setSize(500, 500);
}
public void happyFace (Graphics canvas)
{
//Draw face outline
canvas.drawOval (X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
//Draw eyes
canvas.fillOval (X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
canvas.fillOval (X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
//Draw mouth
canvas.drawArc (X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE);
}
public void sadFace (Graphics canvas)
{
}
private static final long serialVersionUID = 1L;
public static final int FACE_DIAMETER2 = 200;
public static final int X_FACE2 = 100;
public static final int Y_FACE2 = 50;
public static final int EYE_WIDTH2 = 10;
public static final int EYE_HEIGHT2 = 20;
public static final int X_RIGHT_EYE2 = 155;
public static final int Y_RIGHT_EYE2 = 100;
public static final int X_LEFT_EYE2 = 200;
public static final int Y_LEFT_EYE2 = Y_RIGHT_EYE;
public static final int MOUTH_WIDTH2 = 100;
public static final int MOUTH_HEIGHT2 = 200;
public static final int X_MOUTH2 = 150;
public static final int Y_MOUTH2 = 150;
public static final int MOUTH_START_ANGLE2 = 150;
public static final int MOUTH_EXTENT_ANGLE2 = -120;
public void paint(Graphics canvas)
{
//Draw face outline:
canvas.drawOval(X_FACE2, Y_FACE2, FACE_DIAMETER2, FACE_DIAMETER2);
//Draw eyes:
canvas.fillOval(X_RIGHT_EYE2, Y_RIGHT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
canvas.fillOval(X_LEFT_EYE2, Y_LEFT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
//Draw mouth:
canvas.drawArc(X_MOUTH2, Y_MOUTH2, MOUTH_WIDTH2, MOUTH_HEIGHT2,
MOUTH_START_ANGLE2, MOUTH_EXTENT_ANGLE2);
happyFace (canvas);
sadFace (canvas);
//The following code deals with if-else and asking the user if they are happy
System.out.println("Are you happy?");
if (answer == 'y') || (answer == 'yes')
}
{
}
}
Last edited by Json; 09-02-2010 at 12:06 PM. Reason: Please use code tags |
|
|||
|
Hi,
i will try to help you out a bit ![]() The method "public static void main(String[] args)" is basically only needed as entry point for standard java programs, if your applet is run from a browser you wont need that function. And about the if,i would better write it in a way like if ((answer.equalsIgnoreCase("y") || (answer.equalsIgnoreCase("yes")) { .... this ensures String comparison is used, and if the user types Y or YES it will be also valid. |
|
|||
|
Okay everything is working as far as I can tell, BUT...when the program loads it won't allow me to add input using the keyboard. Is it the braces? Or is there some coding wrong?
Java Code
import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JApplet;
/**
*This is a program that displays different faces
*based off of different responses.
*2/5/2010
*/
public class Program3 extends JApplet
{
public static void main(String[] args)
{System.out.println("Are you happy?");
} public static final int FACE_DIAMETER = 200;
public static final int X_FACE = 100;
public static final int Y_FACE = 50;
public static final int EYE_WIDTH = 10;
public static final int EYE_HEIGHT = 20;
public static final int X_RIGHT_EYE = 155;
public static final int Y_RIGHT_EYE = 100;
public static final int X_LEFT_EYE = 200;
public static final int Y_LEFT_EYE = Y_RIGHT_EYE;
public static final int MOUTH_WIDTH = 100;
public static final int MOUTH_HEIGHT = 200;
public static final int X_MOUTH = 150;
public static final int Y_MOUTH = 160;
public static final int MOUTH_START_ANGLE = 180;
public static final int MOUTH_EXTENT_ANGLE = 180;
public void happyFace (Graphics canvas)
{happyFace (canvas);
//Draw face outline
canvas.drawOval (X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
//Draw eyes
canvas.fillOval (X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);
canvas.fillOval (X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);
//Draw mouth
canvas.drawArc (X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,
MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE);
}
public void sadFace (Graphics canvas)
{sadFace (canvas);}
private static final long serialVersionUID = 1L;
public static final int FACE_DIAMETER2 = 200;
public static final int X_FACE2 = 100;
public static final int Y_FACE2 = 50;
public static final int EYE_WIDTH2 = 10;
public static final int EYE_HEIGHT2 = 20;
public static final int X_RIGHT_EYE2 = 155;
public static final int Y_RIGHT_EYE2 = 100;
public static final int X_LEFT_EYE2 = 200;
public static final int Y_LEFT_EYE2 = Y_RIGHT_EYE;
public static final int MOUTH_WIDTH2 = 100;
public static final int MOUTH_HEIGHT2 = 200;
public static final int X_MOUTH2 = 150;
public static final int Y_MOUTH2 = 150;
public static final int MOUTH_START_ANGLE2 = 150;
public static final int MOUTH_EXTENT_ANGLE2 = -120;
@SuppressWarnings({ "unused", "null" })
public void paint(Graphics canvas)
{
//Draw face outline:
canvas.drawOval(X_FACE2, Y_FACE2, FACE_DIAMETER2, FACE_DIAMETER2);
//Draw eyes:
canvas.fillOval(X_RIGHT_EYE2, Y_RIGHT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
canvas.fillOval(X_LEFT_EYE2, Y_LEFT_EYE2, EYE_WIDTH2, EYE_HEIGHT2);
//Draw mouth:
canvas.drawArc(X_MOUTH2, Y_MOUTH2, MOUTH_WIDTH2, MOUTH_HEIGHT2,
MOUTH_START_ANGLE2, MOUTH_EXTENT_ANGLE2);
{
Object answer;
{Scanner keyboard = new Scanner(System.in);
answer = keyboard.nextLine();}
if ((answer.equals("y") || (answer.equals("yes")))) Statement: {
happyFace (canvas);
}
else sadFace (canvas);}
}
{
}
}
|
|
|||
|
Well i guess it is cause you can't use "System.in" for applets
I have found this link explaining how to get key input in applets: Applet Tutorial: Keyboard Input I hope you can use it for JApplet too |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Having trouble with an if statement. please help!! | humdinger | Loops & Control Statements | 11 | 21-01-2010 06:49 PM |
| If Statement Java Need help | Keno888 | Loops & Control Statements | 5 | 25-10-2009 05:53 AM |
| another unreachable statement.. | chronoz13 | Loops & Control Statements | 3 | 02-10-2009 05:15 PM |
| unreachable statement? | chronoz13 | Loops & Control Statements | 1 | 11-09-2009 03:06 PM |
| If statement | Dave | Loops & Control Statements | 2 | 27-08-2009 03:11 PM |
|
100 most searched terms
Search Cloud
|
| 2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps |