Results 1 to 5 of 5
- 12-10-2009, 02:04 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
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.
Java 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 MultiplyAppletLast edited by Fubarable; 12-10-2009 at 02:44 PM. Reason: Code tags added to aid readability
-
Declare the variables in the class, call your JOptionPanes in the init method, not the paint method, and use the numbers generated to paint in the paint method.
As an aside, it's generally frowned upon to paint directly in the applet itself. Usually we paint in a JPanel's paintComponent method (similar to the paint method) and then use that JPanel as the japplet's contentPane.
Also, I added code tags to your post to improve readability. Please see my signature to learn how to do this yourself.
Oh, and welcome to our forum!
- 12-10-2009, 04:45 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Thanks for your really quick reply. I modified the code as follows but am still getting compilation errors they are cannot find symbol variable number 1-4 in the g.Draw line any ideas.
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()
{
// 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 );
} // 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
// draw rectangle
g.drawRect( number1, number2, number3, number4 );
} // end method paint
} // end class MultiplyApplet
-
You're not declaring the ints in the class but rather are declaring them in the init method. Thus they are only visible from within the init method, not the class (the declaration part is this:
To fix it, declare these variables in the class outside of the init method, and then within the init class, fill the variables with values as you are doing, but don't declare them:Java Code:int number1;
Also, and again, please use code tags. Please edit your code above and add tags. See my signature below for details.Java Code:number1 = Integer.parseInt(firstNumber);
- 12-10-2009, 08:45 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Rectangle and Colors
By urbim in forum Java AppletsReplies: 0Last Post: 07-11-2009, 03:03 PM -
non-rectangle JPanel
By itaipee in forum AWT / SwingReplies: 4Last Post: 04-30-2009, 11:58 PM -
How to Fill a Rectangle in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:10 PM -
How to Draw a Rectangle in Java
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:09 PM -
Rectangle expands with applet frame
By Godzilla in forum New To JavaReplies: 1Last Post: 07-10-2007, 07:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks