使用Java中的JTextField值绘制抛物线

时间:2017-11-15 19:21:56

标签: java swing

该程序包括每次按下按钮时使用A,B和C jtextfields中的值绘制抛物线: example

它也必须在两个独立的类上,View显示菜单,Controller接收第一类的输入并绘制抛物线。

我的实际代码:

public static void main(String[] args) {
    JFrame frame = new JFrame("Parabola");
    frame.getContentPane().setLayout(new BorderLayout());

    JPanel panel1 = new JPanel();
    panel1.setPreferredSize(new Dimension(50, 50));

    JLabel labelA = new JLabel();
    labelA.setText("a");
    JTextField textA = new JTextField("0",3);
    JLabel labelB = new JLabel();
    labelB.setText("b");
    JTextField textB = new JTextField("0",3);
    JLabel labelC = new JLabel();
    labelC.setText("c");
    JTextField textC = new JTextField("0",3);

    JButton draw = new JButton();
    draw.setText("Draw");
    draw.addActionListener( new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            textA.getText();
            textB.getText();
            textC.getText();

        }
    });

    panel1.add(labelA);
    panel1.add(textA);
    panel1.add(labelB);
    panel1.add(textB);
    panel1.add(labelC);
    panel1.add(textC);
    panel1.add(draw);

    JPanel panel2 = new JPanel(){

        double a=2, b=1, c=0;

        public void section (Graphics g){
            g.setColor(Color.blue);
            g.drawLine(200,0,200,400);
            g.drawLine(0,200,400,200);
            for (int x=0; x<=400; x= x +40){
                g.drawLine(x,195,x,205);
            }
            for (int y=0; y<=400; y=y+40){
                g.drawLine(195,y,205,y);
            }
        }

        public void graphic(Graphics g) {
            g.setColor(Color.red);
            for (double x=-100;x<=100;x = x+0.1){
            double y = a * x * x + b * x + c;
            int X = (int)Math.round(200 + x*20);
            int Y = (int)Math.round(200 - y*20);
            g.fillOval(X-2,Y-2,4,4);
            }   
        }

        public void paint (Graphics g){
            section(g);
            graphic(g);
        }
    };
    panel2.setBackground(Color.WHITE);

    frame.getContentPane().add(panel1, BorderLayout.PAGE_START);
    frame.getContentPane().add(panel2, BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);
    frame.setSize(420,490);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

我设法在没有文本字段工作的一个类中完成它,并且不知道如何将图形分成另一个类,以便它可以执行操作并再次将它们发送回视图类。

1 个答案:

答案 0 :(得分:0)

解决了它:

班级视图

public class View extends JFrame {

public View() {

    JFrame frame = new JFrame("Equation");
    frame.getContentPane().setLayout(new BorderLayout());

    JPanel panel1 = new JPanel();
    panel1.setPreferredSize(new Dimension(50, 50));

    JLabel labelA = new JLabel();
    labelA.setText("a");
    JTextField textA = new JTextField("0",3);
    JLabel labelB = new JLabel();
    labelB.setText("b");
    JTextField textB = new JTextField("0",3);
    JLabel labelC = new JLabel();
    labelC.setText("c");
    JTextField textC = new JTextField("0",3);

    JButton draw = new JButton();
    draw.setText("Draw");
    draw.addActionListener( new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            Controller.a = Double.parseDouble(textA.getText());
            Controller.b = Double.parseDouble(textB.getText());
            Controller.c = Double.parseDouble(textC.getText());

            repaint();
            frame.pack();
            frame.setSize(420,490);
        }
    });

    panel1.add(labelA);
    panel1.add(textA);
    panel1.add(labelB);
    panel1.add(textB);
    panel1.add(labelC);
    panel1.add(textC);
    panel1.add(draw);

    JPanel panel2 = new JPanel(){

        public void paint(Graphics g){
            super.paint(g);
            Controller.grid(g);
            Controller.Graphic1(g);
        }                                  
    };

    panel2.setBackground(Color.WHITE);

    frame.getContentPane().add(panel1, BorderLayout.PAGE_START);
    frame.getContentPane().add(panel2, BorderLayout.CENTER);

    frame.setVisible(true);
    frame.setSize(420,490);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                View frame = new View();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
}

班长控制器

public class Controller {

static double a=2, b=1, c=0;

public static void grid (Graphics g){
            g.setColor(Color.blue);
            g.drawLine(200,0,200,400);
            g.drawLine(0,200,400,200);
            for (int x=0; x<=400; x= x +40){
                g.drawLine(x,195,x,205);
            }
            for (int y=0; y<=400; y=y+40){
                g.drawLine(195,y,205,y);
            }
}

public static void Graphic1(Graphics g) {
            g.setColor(Color.red);
            for (double x=-100;x<=100;x = x+0.1){
                double y = a * x * x + b * x + c;
                int X = (int)Math.round(200 + x*20);
                int Y = (int)Math.round(200 - y*20);
                g.fillOval(X-2,Y-2,4,4);
            }   

}

}