2 Replies - 273 Views - Last Post: 27 October 2012 - 05:44 AM Rate Topic: -----

#1 AlaGondA  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-October 12

call/generate shapes in jframe from console =input.next("") in

Posted 26 October 2012 - 10:29 PM

I am in Java 1 and my assignment is to ask the user to input a choice between (S)quare (T)riangle and ©ircle. The correct shape would then appear in ASCII art form. i was allowed to try something else. I want to draw the shape outside of the console using Graphics.

Erros = Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.next(Scanner.java:1463)
at drawingshapes.DrawingShapes.main(DrawingShapes.java:52)

I am new to java and i think my reach extends my grasp on this. I have searched for example codes via google and honestly i dont understand what im looking for.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drawingshapes;
import java.awt.*;
import java.applet.*;
import java.util.Scanner;
import javax.swing.*;

/**
 *
 * @author AlaGondA
 */
public class DrawingShapes extends JApplet{
    
    public static void drawRect1(Graphics gr){
        drawRect(40, 40, 120, 120);
    }

    private static void drawOval(int i, int i0, int i1, int i2) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void drawRect(int i, int i0, int i1, int i2) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void drawLine(int i, int i0, int i1, int i2) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
                    
                DrawingShapes canvas = new DrawingShapes();              //Define the Frame
                JFrame frame = new JFrame();
                frame.setSize(400, 400);                        
                frame.getContentPane().add(canvas);
                frame.setVisible(true);
                        
        Scanner input = new Scanner(System.in);
        String choiceContinue = "y";
        while (choiceContinue.equalsIgnoreCase("y")){ //create loop
    
            {
              System.out.print("Enter c, s, or t\r(C)ircle\r(S)quare\r(T)riangle\r\rEnter: ");
              String drawType = input.next("");
                
              if (drawType.equals("c")) {
                        frame.setSize(400, 400);                        
                        frame.getContentPane().add(canvas);
                        frame.setVisible(true);
                            drawOval(100, 100, 60, 60);
              }
                            else if (drawType.equals("s")){
                        
                        frame.setSize(400, 400);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.getContentPane().add(canvas);
                        frame.setVisible(true);
                            drawRect(40, 40, 120, 120);
                            }
                            else if(drawType.equals("t")){
                  
                        frame.setSize(400, 400);
                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        frame.getContentPane().add(canvas);
                        frame.setVisible(true);
                            drawLine(40, 40, 5, 75);
                            drawLine(5, 75, 75, 75);
                            drawLine(40,40, 75, 75);
                            }
                              else{
                                System.out.print("No Megusta");
                            }
                                               
                }
                       
        System.out.println(); // end loop
        System.out.println("Continue? (y/n) ");
        choiceContinue = input.next();
        System.out.println();
            
           }
            
}
    }

  
}



Is This A Good Question/Topic? 0
  • +

Replies To: call/generate shapes in jframe from console =input.next("") in

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: call/generate shapes in jframe from console =input.next("") in

Posted 27 October 2012 - 01:42 AM

The statement causing the error message at line 52 is wrong but easily fixed. Change line 52 to:

String drawType = input.next();

You have a good start on this. Don't give up and good luck!

This post has been edited by GregBrannon: 27 October 2012 - 01:43 AM

Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • Joined: 05-April 11

Re: call/generate shapes in jframe from console =input.next("") in

Posted 27 October 2012 - 05:44 AM

You do not want an applet AND a frame

Quote

A Java applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run.


I suggest you change your JApplet to a JFrame :)

Normally you would add a JPanel to the JFrame, then on your JPanel you override the paintComponent method. The paintComponent method allows you to do all of your custom paint

I'm sure there are a couple of tutorials on this site on how to do this
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1