Problem to graph inside a JPanel

0

I have a problem, I want to put a graph that I did in a jframe in a jpanel inside that jframe. I'm working in Netbeans. Can someone solve it? The graphic I did was importing this class to draw a perlin noise function: link

For the graphic use this, first within my frame, and now I want to move it to the jpanel that it will have inside:

Noisy noisy = new Noisy((int) (100000 * Math.random()), 3);

public void paint(Graphics g) {

        int y1, y2 = 0, x1, x2 = 0;
        float rugosidad = 0.1f;

        for (int i = 0; i < (500 * rugosidad); i++) {

            y1 = (int) (100 * (noisy.perlinNoise((float) i / 10) + 1));
            x1 = (int) (i / rugosidad);

            g.setColor(Color.red);
            g.drawLine(x1, y1, x2, y2);

            y2 = y1;
            x2 = x1;

        }

}

Please, I need your help.

    
asked by Mati Bonino 17.11.2018 в 21:34
source

1 answer

0

The JPanel component would only need to overwrite your paint or paintComponent method in the same way that the JFrame container does.

Something like the following may be a good approximation:

//...

miPanel = new JPanel() {
  Noisy noisy = new Noisy((int) (100000 * Math.random()), 3);

  public void paint(Graphics g) {
    int y1, y2 = 0, x1, x2 = 0;
    float rugosidad = 0.1f;

    for (int i = 0; i < (500 * rugosidad); i++) {
      y1 = (int) (100 * (noisy.perlinNoise((float) i / 10) + 1));
      x1 = (int) (i / rugosidad);

      g.setColor(Color.red);
      g.drawLine(x1, y1, x2, y2);

      y2 = y1;
      x2 = x1;
    }
  }
};
getContentPane().add(miPanel);//u otra manera alternativa de agregar componentes

//...

For example, in a JPanel with LineBorder , this catch was obtained:

    
answered by 18.11.2018 / 01:36
source