0

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);
  }
}
3
  • The short and long answer is, you don't. You supply the information to myPanel and when myPanel is painted it makes it use of it Commented Jun 2, 2018 at 22:49
  • So where or how do I then supply the information of the array to myPanel if its in its own class?
    – Nick Crees
    Commented Jun 2, 2018 at 23:01
  • In you case, probably myPanel's constructor Commented Jun 2, 2018 at 23:01

1 Answer 1

0

You need to supply some means to pass the information from you main method to your myPanel class. In your case, you could simply supply a custom constructor, something like...

public class myPanel extends JPanel{

  private int[] xTri;
  private int[] yTri;

  public myPanel(int[] xTri, int[] yTri) {
      this.xTri = xTri;
      this.yTri = yTri;
  }

  public void paint (int [] numbers, Graphics g) {
                super.paint (g);
                g.setColor (Color.BLACK);
                g.fillPolygon (xTri, yTri, xTri.length);
  }
}

Observations...

Don't extend from JFrame, you're creating a new instance anyway which is just confusing the issue

Because of the way that Swing works, you need to make sure that you start you UI within the context of the Event Dispatching Thread. So, in your main method, you should use...

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        // Your UI code here
    }
});

and build your UI within the run method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.