-2

I am trying to write a code the asks the user: Which shape you want to draw?

and based on the choice, the user enters the parameters of the shape and it appears on the screen.

example: if user choose Rectangle, then they have to key in the height and width.

Is that possible to do ?

Yassin Hajaj
  • 20,020
  • 9
  • 41
  • 81
Mohammed
  • 103
  • 10
  • 6
    Yes, it is possible. – Zoltán Nov 18 '15 at 21:03
  • What have you tried so far? [Click here](http://stackoverflow.com/help/mcve) – Yassin Hajaj Nov 18 '15 at 21:09
  • @Yassin I just know how to add the values by myself, I just change the values from this code "g.drawRect(200, 100, 100, 200);" but am wondering how to make the user enter the values. – Mohammed Nov 18 '15 at 21:15
  • Post what you've coded already. It makes it generally easier to answer and you do not break one of Stack Overflow's rule. – Yassin Hajaj Nov 18 '15 at 21:16
  • @ItssMohammed So use a `Scanner` or some other input method and store the entered values into variables, then use the variables as parameters instead of your own hard-coded numbers. – Mage Xy Nov 18 '15 at 21:16
  • @ItssMohammed, `but am wondering how to make the user enter the values` - Read the [Swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html). You could use JOptionPanes to prompt for the values. You could use a JDialog with text fields. – camickr Nov 18 '15 at 21:40

1 Answers1

1

Yes it's possible.

This is how you use scanner:

How can I read input from the console using the Scanner class in Java?

Intro to content in URL above:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

System.out.println("Enter your username: ");
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();

EDIT:

Here's a program that applies the concept above and takes radius from user and draws a circle on a frame:

import java.awt.Frame;
import java.awt.Graphics;
import java.util.Scanner;

 public class Painting extends Frame{

 int num=0;

Painting(){
   super("Paint");
   setSize(300,300);
   setVisible(true);

   Scanner myScanner = new Scanner(System.in);
   System.out.println("Enter Radius");
   num = myScanner.nextInt();

  repaint();
      }

    public void paint(Graphics g){
        g.drawOval(50, 50, (2*num), (2*num));
     }

      public static void main(String[] args)
       {
          new Painting();
         }
        }    
Community
  • 1
  • 1
Perdomoff
  • 921
  • 2
  • 7
  • 24