Need Help with Java Rectangle Applet
Hi
I have a project I am working on for school. I have to get 4 variables from the the end user and use those variables to create a rectangle. Below is the code that I have so far. I tried to declare the variables under the init method, but it would not compile. Any help would be greatly appreciated.
Code:
import java.awt.Graphics; // program uses class Graphics
import javax.swing.JApplet; // program uses class JApplet
import javax.swing.JOptionPane; // program uses class JOptionPane
public class RectangleApplet extends JApplet
{
// initialize applet by obtaining values from user
public void init()
{
} // end method init
// draw results in a rectangle on applet’s background
public void paint( Graphics g )
{
super.paint( g ); // call superclass version of method paint
// obtain first number from user
String firstNumber = JOptionPane.showInputDialog(
"Enter first value" );
// obtain second number from user
String secondNumber = JOptionPane.showInputDialog(
"Enter second value" );
// obtain third number from user
String thirdNumber = JOptionPane.showInputDialog(
"Enter third value" );
// obtain fourth number from user
String fourthNumber = JOptionPane.showInputDialog(
"Enter fourth value" );
// convert numbers from type String to type double
int number1 = Integer.parseInt( firstNumber );
int number2 = Integer.parseInt( secondNumber );
int number3 = Integer.parseInt( thirdNumber );
int number4 = Integer.parseInt( fourthNumber );
// draw rectangle
g.drawRect( number1, number2, number3, number4 );
} // end method paint
} // end class MultiplyApplet