0

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

example

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
joyu
  • 1
  • 3
  • It's generally recommend to override `paintComponent` over `paint`, also, you should be calling the paint methods `super` method in order to preserve the paint chain – MadProgrammer Nov 15 '17 at 19:31
  • Now it looks better, the background color appeared but still haven't found what i'm looking for – joyu Nov 15 '17 at 23:27
  • Okay, so basics - you need a reference to the panel, your panel needs someway to allow others to change the properties of the panel - maybe have a look at [Passing information](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – MadProgrammer Nov 15 '17 at 23:55
  • For [example](https://stackoverflow.com/a/20266619/230513). – trashgod Nov 16 '17 at 00:00

1 Answers1

0

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

}

}
joyu
  • 1
  • 3