Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Wont display the rectangle.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Wont display the rectangle.

    allows for user input of the 4 values (x, y, width, height) but it wont draw the rectangle with those values.
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
     
    public class drawRectangle extends JApplet
    {
    	public int x;
     
    	public int y;
     
    	public int height;
     
    	public int width;
     
    	public void init()
    	{
    		String x = JOptionPane.showInputDialog( "Enter the x value");
     
    		String y = JOptionPane.showInputDialog( "Enter the y value");
     
    		String height = JOptionPane.showInputDialog( "Enter the height value");
     
    		String width = JOptionPane.showInputDialog( "Enter the width value");
     
    	}
    public void setWidth( int width )
    {
    	width = width;
    }
    public void setHeight( int height )
    {
    	height = height;
    }	
    public void setX( int x )
    {
        x = x;
    }
    public void setY( int y )
    {
        y = y;
    }
    public void paint( Graphics g )
    {
    	super.paint(g);
     
    	g.drawRect(x, y, width, height );
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Wont display the rectangle.

    Where do the variables that are being used for drawing the rectangle get assigned values?
    They will start with a value of 0 and will stay 0 if the code doesn't change them.

    You have two definitions for many of the variables. The code assigns values to one set of variables and then uses the other variable(still 0) for doing the drawing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Wont display the rectangle.

    how would i go about assigning the values for the rectangle in this particular instance?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Wont display the rectangle.

    Remove the definitions for the variables from the init() method. Use those defined in the class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Wont display the rectangle.

    like this?
    import java.awt.Graphics;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;
     
    public class drawRectangle extends JApplet
    {
    	public int x;
     
    	public int y;
     
    	public int height;
     
    	public int width;
     
    	public void init()
    	{
    		 JOptionPane.showInputDialog( "Enter the x value");
     
    		 JOptionPane.showInputDialog( "Enter the y value");
     
    		 JOptionPane.showInputDialog( "Enter the height value");
     
    		 JOptionPane.showInputDialog( "Enter the width value");
     
    }
    public void setWidth( int width )
    {
    	width = width;
    }
    public void setHeight( int height )
    {
    	height = height;
    }	
    public void setX( int x )
    {
        x = x;
    }
    public void setY( int y )
    {
        y = y;
    }
    public void paint( Graphics g )
    {
    	super.paint(g);
     
    	g.drawRect(x, y, width, height );
    }
    }

    i think its still not assigning the values to the variables but im unsure of how to assign them.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Wont display the rectangle.

    unsure of how to assign them.
    Use an assignment statement to assign a value to a variable:
    theVar = <the new value here>; //  assign a value to theVar

    The code in post #1 assigned values to the variables. The code in post#5 removed all the assigning of values??? The assignments need to be restored to the code in post#5.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to display a Rectangle object in JFrame (Problem).
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 15th, 2012, 07:22 PM
  2. Need help my values wont return
    By sajeed in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 7th, 2012, 08:46 AM
  3. WHY WONT THIS COMPILE!
    By usmc0311 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 12th, 2011, 02:42 PM
  4. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM
  5. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM