更新坐标后如何绘制jpanel

nhaq1z21  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(157)

我试着用一些线来显示一个jpanel区域。然后我想介绍一些坐标,在这个区域显示一条抛物线。问题是我不能正确地更新坐标,也不能绘制抛物线。特别是最后一部分我不知道怎么做。
这是我到目前为止的代码:
主.java

public class Main {

    public static void main(String[] args) {

        Datos datos = new Datos();
        Ventana ventana = new Ventana();
        Controlador controlador = new Controlador(ventana, datos);
        controlador.iniciar_vista();
        ventana.setVisible(true);
    }
}

数据.java

public class Datos {

    private int a, b, c;

    public Datos() {
        this.a = 0;
        this.b = 0;
        this.c = 0;
    }

    public void setA(int A) {
        this.a = A;
    }
// more setters for B and C

    public int getA() {
        return this.a;
    }

// more getters for B and C

}

文塔纳.java

// imports

public class Ventana extends JFrame {

    JTextField cuadroTexto1, cuadroTexto2, cuadroTexto3;

    JLabel etiqueta1 = new JLabel("A: ");
    JLabel etiqueta2 = new JLabel("B: ");
    JLabel etiqueta3 = new JLabel("C: ");

    JButton boton = new JButton("Calcular");

    public Ventana() {
        setSize(500, 500);
        setTitle("Ventana");
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel();
        grafica();
    }

    public void panel() {
        JPanel panel = new JPanel();

        panel.setBounds(0, 0, 500, 50);
        panel.setVisible(true);

        cuadroTexto1 = new JTextField("    ");
// same for 2 and 3

        panel.add(etiqueta1);
        panel.add(cuadroTexto1);

// more .add for the rest of JTextField and JButton

        this.getContentPane().add(panel, BorderLayout.NORTH);
        this.validate();
    }

    public void grafica() {
        JPanel panel2 = new JPanel();
        panel2.setBounds(0, 0, 500, 500);
        panel2.setVisible(true);
        DrawingArea drawingArea = new DrawingArea();
        this.getContentPane().add(drawingArea, BorderLayout.CENTER);
        this.validate();
    }

    static class DrawingArea extends JPanel {

      @Override
      public void paintComponent(Graphics g) {
        int ANCHO = getWidth();
        int LARGO = getHeight();

        super.paintComponent(g);
        g.setColor(Color.BLACK);

        g.drawLine(0, (LARGO / 2), ANCHO, (LARGO / 2));
        g.drawLine(ANCHO / 2, 0, ANCHO / 2, LARGO);
        for (int i = 0; i <= LARGO / 100; i++) {
          g.drawLine(ANCHO / 2 - 5, (LARGO / 2) - 50 * i, ANCHO / 2 + 5, (LARGO / 2) - 50 * i);
        }
        for (int i = 1; i <= LARGO / 100; i++) {
          g.drawLine(ANCHO / 2 - 5, (LARGO / 2) + 50 * i, ANCHO / 2 + 5, (LARGO / 2) + 50 * i);
        }
        for (int i = 0; i <= ANCHO / 100; i++) {
          g.drawLine(ANCHO / 2 - 50 * i, (LARGO / 2) - 5, ANCHO / 2 - 50 * i, (LARGO / 2) + 5);
        }
        for (int i = 0; i <= ANCHO / 100; i++) {
          g.drawLine(ANCHO / 2 + 50 * i, (LARGO / 2) - 5, ANCHO / 2 + 50 * i, (LARGO / 2) + 5);
        }

// FROM HERE DRAWS THE PARABOLA

        g.setColor(Color.BLUE);

        double y, yd;

        for (double i = -ANCHO / 2; i < ANCHO / 2; i++) {
          y = (a * Math.pow(i / 50, 2) + b * i / 50 + c) * 50;
          yd = (a * Math.pow((i + 1) / 50, 2) + b * ((i + 1) / 50) + c) * 50;
          g.drawLine((int) (i + ANCHO / 2), (LARGO / 2) - (int) y, (int) ((i + 1 + ANCHO / 2)), (LARGO / 2) - ((int) yd));

        }
     }
  }
}

controlador.java文件

// imports
public class Controlador implements ActionListener {

    private Ventana vista;
    private Datos modelo;

    public Controlador(Ventana vista, Datos modelo) {
        this.vista = vista;
        this.modelo = modelo;
        this.vista.boton.addActionListener(this);
    }

    public void iniciar_vista() {
        vista.cuadroTexto1.setText(String.valueOf(modelo.getA()));

// setText for B and C
    }

    public void actionPerformed(ActionEvent e) {
        modelo.setA(Integer.valueOf(vista.cuadroTexto1.getText()));

// setB and C

    }
}

所以我在这里遇到的两个问题是,第一个问题看起来我无法正确更新jtextfields(我检查了在console中打印它们的值,但是只更新了cuadrotexto3,其他的仍然是0),第二个问题是我不知道如何在ventana.java中完成在按下按钮设置值之后绘制抛物线的部分。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题