Search code examples
javaarraysswingnetbeansprogram-entry-point

How can I use arrays from main for a swing graph?


i have a java class, main, in net beans, where i calculate 10000 or 10000000 double values, and i store them in x[] and y[] . I want to make a simple graph of these double values in a x-y axis in blue colour points connected by What's the simplest way to do it (use the x[] y[] of the main, in paint method and loop 0-100000 to paint the points and lines)?

for example i have:

public class sth extends JFrame{


  public sthfiltra(){

    super();
  }

  @Override
    public void paint(Graphics g){
        for(int c=1; c<size; c++){
         g.drawLine(x[i-1],y[i-1],x[i],y[i]); 
        }
  }  

    public static void main(String[] args) throws IOException {
// [...]  in main i have some code. For example i want to use arrays x[i] and y[i] for visualization...

                   sth frame = new sth();
                   frame.setSize(200,200);
                   frame.setVisible(true);         

    }
}

Solution

  • You should be able to add the parameters to your paint method.

    public void paint(Graphics g, int[] x, int[] y){
       for(int i = 1; i < x.size && i < y.size; i++){
       g.drawLine(x[i-1],y[i-1],x[i],y[i]); 
    }
    

    This will give you access to them.

    I added the second condition to the for loop so that you won't run into array index out of bounds problems.