I am having challenges calling my graphics method in my main method so that it can paint a triangle using points from an array. When I don't use an array and just use regular numbers the painting works just fine, but the idea of the program is that the user enters values into the array to be used. Any suggestions on how to make the triangle paint? Here's the code: (I have all the proper imports)
public class Summative extends JFrame{
static int[] inpoot() {
Scanner input = new Scanner(System.in);
int[] numbers = new int[6];
System.out.println("Please enter 3 sets of coordinates:");
for (int i = 0; i < 6; i++) {
numbers[i] = input.nextInt();
}
return numbers;
}
static void outpoot(int [] numbers) {
double A = Math.sqrt (Math.pow ((numbers[4] - numbers[2]), 2) + Math.pow ((numbers[5] - numbers[3]), 2));
double B = Math.sqrt (Math.pow ((numbers[4] - numbers[0]), 2) + Math.pow ((numbers[5] - numbers[1]), 2));
double C = Math.sqrt (Math.pow ((numbers[2] - numbers[0]), 2) + Math.pow ((numbers[3] - numbers[1]), 2));
double s = (A + B + C) / 2;
double area = (Math.sqrt (s * (s - A) * (s - B) * (s - C)));
System.out.println ("The area of the triangle entered is : " + area + " units squared.");
}
public static void main(String[] args) {
int[] numbers = inpoot();
outpoot(numbers);
JFrame frame = new JFrame("Triangle");
frame.setVisible(true);
frame.setSize(new Dimension(500, 500));
JPanel panel = new myPanel();
frame.add(panel);
frame.validate();
frame.repaint();
}
}
And this is the graphics class:
public class myPanel extends JPanel{
public void paint (int [] numbers, Graphics g) {
super.paint (g);
g.setColor (Color.BLACK);
int[] xTri = {numbers[0], numbers[2], numbers[4]};
int[] yTri = {500 - numbers[1], 500 - numbers[3], 500 - numbers[5]};
g.fillPolygon (xTri, yTri, 3);
}
}
myPanel
and whenmyPanel
is painted it makes it use of itmyPanel
's constructor