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.
Code Java:
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 );
}
}
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.
Re: Wont display the rectangle.
how would i go about assigning the values for the rectangle in this particular instance?
Re: Wont display the rectangle.
Remove the definitions for the variables from the init() method. Use those defined in the class.
Re: Wont display the rectangle.
like this?
Code Java:
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.
Re: Wont display the rectangle.
Quote:
unsure of how to assign them.
Use an assignment statement to assign a value to a variable:
Code :
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.