Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Using scanner and if/else statement to draw one of two graphics.

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Using scanner and if/else statement to draw one of two graphics.

    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:

     
    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. I need the graphics to pop up in a Java applet, but I don't know how to do that. I think I'm supposed to use "such-and-such class extends JApplet", but I'm only using one class (HappyWild) in the program. How would I make Java applets pop up with the correct graphics?


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Using scanner and if/else statement to draw one of two graphics.

    you're passing a Graphics object as a parameter in your definition but you didn't follow the rules of that syntax in your main method... its says to you need to delete something in your face methods in the main basically

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Using scanner and if/else statement to draw one of two graphics.

    You will want to read a tutorial on how to create a GUI with Java to gain some insights because you can't guess at this stuff. First though before you do anything else, you have to decide if you want to create a JApplet or a JFrame. Then check out the appropriate tutorial on how to construct this GUI because their structures are very different. Then you'll want to read up on how to draw with Swing applications. Usually this is done by overriding the paintComponent method of a JPanel or similar class and using the Graphics object passed into this method. For the details I again refer you to the tutorials which Google can help you find. Much luck!

    --- Update ---

    Edit: it appears that you are supposed to create a JApplet, and so yes, your class must extend JApplet, and you should not have a main method since applets don't use main methods (at least none directly created by us). Again, please read the tutorials as they will explain much that will help you. Also, don't use a Scanner or System.out.println statements (except perhaps to debug). This is a JApplet GUI and should be a GUI-only program.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    10
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Using scanner and if/else statement to draw one of two graphics.

    Thank you gentlemen, I appreciate your brilliance and time. I finished it and the code is here:
    import javax.swing.JApplet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Scanner;
     
    public class HappyWild extends JApplet {
     
    	public void init() {
    		setSize(500,500);
    	}
     
    	public void paint (Graphics canvas) {
    		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.nextLine();
    		System.out.println("You entered: \"" + answer + "\"");
    		if (answer.equalsIgnoreCase("wild"))
    		{
    			//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);
     
    		}
    		else if (answer.equalsIgnoreCase("happy")) 
    		{
    			// 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);
    		}
    		else 
    			System.out.println("incorrect input.");
     
    	}
    }

Similar Threads

  1. [SOLVED] Graphics Object Won't Draw To The Correct JFrame
    By NickNumero in forum AWT / Java Swing
    Replies: 7
    Last Post: October 27th, 2012, 01:07 PM
  2. Draw labyrinth ( I need help)
    By ramin in forum Loops & Control Statements
    Replies: 2
    Last Post: January 3rd, 2012, 06:27 PM
  3. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  4. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  5. help me draw a triangle....
    By beandip408 in forum Object Oriented Programming
    Replies: 10
    Last Post: October 28th, 2010, 05:49 PM