-
parseInt
hi i need some guidance on this coding...
Code:
package q2;
import java.applet.Applet;
import java.awt.*;
import javax.swing.JOptionPane;
//import javax.swing.JFrame;
public class DrawObjects extends Applet {
public void init() {
setSize(400,200);
setBackground( Color.black );
}
public void paint(Graphics g) {
g.setColor(Color.green);
//Object[] possibleValues = { "1", "2", "3" };
//Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
String inp = JOptionPane.showInputDialog(null, "Enter 1 to draw lines\nEnter 2 to draw rectangles\nEnter 3 to draw circles","Draw Objects", JOptionPane.CANCEL_OPTION);
int num = Integer.parseInt(inp);
if (num != int)
{
JOptionPane.showMessageDialog(null, "Please enter a valid number\nPlease try again", "Invalid number!", JOptionPane.ERROR_MESSAGE);
}
else
{
switch (num) {
case 1: {
for(int i = 1; i <=20; i++){
g.drawLine(10, 10, 400, (10*i));
}
}break;
case 2: {
for(int i = 1; i <=20; i++){
g.drawRect(10*i, 10*i, 10*i+40, 10*i+40);
}
}break;
case 3: {
for(int i = 1; i <=20; i++){
g.drawOval(10*i, 10*i, 10*i+40, 10*i+40);
}
}break;
default: {
JOptionPane.showMessageDialog(null, "You have entered an Invalid number\nPlease try again", "Invalid number!", JOptionPane.ERROR_MESSAGE);
//JOptionPane.showMessageDialog(null, "Thank You");
//System.exit(0);
break;
//System.out.println("Invalid value.");
}
} JOptionPane.showMessageDialog(null, "Thank You");
}
}
}
First of all, I am trying to dissappear the JOptionPane.showInputDialog but it keeps looping twice.
2ndly, I am wondering how do i put if (inp != int) the int, is there other term to put it as numbers or anyother than numbers.
Thanks and appreciate in advance.
-
Oh my. You appear to be calling your methods from within the applet's paint method which is a big no-no as the paint method is just for painting. I advise you to go through the Sun applet tutorial to learn how their supposed to be coded.
Also, if you want the user to "go again" you'll need to use a loop of some sort, perhaps a while loop or a do/while loop.
-
oh, this is just a switch case tutorial to do.
is there a shorter way to this?
-
dear debuggers, i have managed to solve this by declaring strings and integer in the class instead in graphics object.
thanks for viewing.
-
So you are no longer drawing anything?