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
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); } }