Results 1 to 5 of 5
  1. #1
    dmollica is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default 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 MultiplyApplet
    Last edited by Fubarable; 12-10-2009 at 03:44 PM. Reason: Code tags added to aid readability

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    dmollica is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    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

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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:
    Java Code:
    int number1;
    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:
    number1 = Integer.parseInt(firstNumber);
    Also, and again, please use code tags. Please edit your code above and add tags. See my signature below for details.

  5. #5
    dmollica is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank You so much this worked perfectly and more importantly now I understand.

Similar Threads

  1. Rectangle and Colors
    By urbim in forum Java Applets
    Replies: 0
    Last Post: 07-11-2009, 04:03 PM
  2. non-rectangle JPanel
    By itaipee in forum AWT / Swing
    Replies: 4
    Last Post: 05-01-2009, 12:58 AM
  3. How to Fill a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:10 AM
  4. How to Draw a Rectangle in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:09 AM
  5. Rectangle expands with applet frame
    By Godzilla in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •