简体   繁体   中英

Paint a parabola using JTextField values in Java

The program consists of drawing a parabola using the values from the A, B and C jtextfields every time the button is pressed:

例子

It also has to be on two separate classes, the View which displays the menu and the Controller which takes the inputs from the first class and paints the parabola.

My actual code:

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);
}

I`ve managed to do it in one class without the textfields working and have no idea how to separate the graphic into another class so it can do the operations and send them back to the view class again.

Solved it:

Class View

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();
            }
        }
    });
}
}

Class Controller

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);
            }   

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM