HELP! Cant pass my input values to the paint component
I am trying to write a program that accepts user input to draw shapes. Like It would ask what shape do you want , if it was square it would calculate perimeter and give to you along with a pop up window drawing of a square with a drawstring saying this is a square with a perimeter of..... What is below is just a short version to find out how to pass the parameters(a test version). When I can do it I will add a loop to keep asking for shapes and sizes to draw until they hit like 5-To Exit.
I am new to JAVA and have read and watched an untold number of videos but can't find anything on why I can't pass over the parameters, I think if I get that the rest will be easy. Thanks
Code :
import javax.swing.*;
public class InputTest{
public static final double PI=3.14159;
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Square object = new Square();
frame.add(object);
object.drawing();
// Gets the first number
String firstnum = JOptionPane.showInputDialog("Enter a number");
// Gets the second number
String secnum = JOptionPane.showInputDialog("Enter another number");
int num1 = Integer.parseInt(firstnum);
int num2 = Integer.parseInt(secnum);
Square SquareObject = new Square();
SquareObject.circumf(num1,num2);
}}
import javax.swing.*;
import java.awt.*;
public class Square extends JPanel{
public void circumf(int newx1, int newx2){
int circumf = newx1 * newx2;
JOptionPane.showMessageDialog(null,"You selected Rectangle" +
"\n Length is = " + newx1 +"cm "+" Width is " +newx2 +"cm" +
"\n Perimeter is = " + circumf +"cm","RECTANGLE",JOptionPane.PLAIN_MESSAGE);
}
public void drawing(){
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(10, 15,newx1,newx2);
}
}
Re: HELP! Cant pass my input values to the paint component
Seriously, no one has any ideas? Does anyone even know if its possible?
Re: HELP! Cant pass my input values to the paint component
Quote:
Seriously, no one has any ideas? Does anyone even know if its possible?
We are not a 24/7 service and there are others who have their own questions that contributors must address as well, so please be patient.
Based upon the title, you don't pass values to paintComponent - use instance variables. See Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Re: HELP! Cant pass my input values to the paint component
I read it and was able to add a "public static int" with a value, to the class the paint component is in and pass the variable to the paint component in same class, but I still can't get the input which is in a different class to pass into the class the contains the paint component. Is there another swing draw method that would
Quote:
Originally Posted by
copeg
Re: HELP! Cant pass my input values to the paint component
Provide setters to the values of the component you wish to paint (no need to make it static unless it needs to be). For example:
Code java:
public void MyComponent extends JPanel{
private int myValue = 0;
public void setMyValue(int value){
myValue = value;
}
@Override
public void paintComponent(Graphics g){
//you can use myValue for whatever the need may be
}
}
The other class would then call setMyValue with the appropriate value.
Re: HELP! Cant pass my input values to the paint component
yes that works ,but "myvalue" is only what you set it to. How do you get myvalue to be whatever the user inputs? Like if it was in a loop and said enter length of one side of square:, whatever was entered would become myvalue(say 10) and the paintcomp would draw it 10px by 10px, then loop would ask again til it ends.
Quote:
Originally Posted by
copeg
Provide setters to the values of the component you wish to paint (no need to make it static unless it needs to be). For example:
Code java:
public void MyComponent extends JPanel{
private int myValue = 0;
public void setMyValue(int value){
myValue = value;
}
@Override
public void paintComponent(Graphics g){
//you can use myValue for whatever the need may be
}
}
The other class would then call setMyValue with the appropriate value.
Re: HELP! Cant pass my input values to the paint component
Write something out and see. Use a loop, prompt the user, then set the value...if/when you have a problem post the code, error messages, misbehavior, etc...
Re: HELP! Cant pass my input values to the paint component
Doing something wrong
Code :
import java.awt.*;
import javax.swing.*;
public class myTest extends JPanel{
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myTest object = new myTest();
frame.add(object);
object.drawing();
// Gets the first number
String firstnum = JOptionPane.showInputDialog("Enter a number");
int myValue = Integer.parseInt(firstnum);
Square SquareObject = new Square();
SquareObject.perim(myValue);
}}
class Square extends JPanel{
private int myValue=0;
public void setmyValue(int select){
myValue=select;
}
public void perim(int newx1){
int perim = 4*newx1;
JOptionPane.showMessageDialog(null,"You selected Square" +
"\n Side Length is = " + newx1 +"cm "+
"\n Perimeter is = " + perim +"cm","SQUARE",JOptionPane.PLAIN_MESSAGE);
}
public void drawing(){
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillRect(10, 15,myValue,myValue);
}
}