-2

i'm trying to draw a little drawing using some values from other class (a physics problem). What I want is translate the results of that problem to the drawing. For example, x and y for an arc symbolizing a parabolic shot.

My problem is that I don`t know how to call the paint() method to write everything translating these values. The execution shows JavaNullPointerException. This is my code:

public class Draw extends JFrame {
    private Calculate c;


    public Draw() {
        super("Title");
        setSize(1000, 500);
        setVisible(true);
    }

    public void paint(Graphics g, int x, int y, int alpha) {
        super.paint(g);

        g.setColor(Color.RED);
        g.drawLine(0, 30, 350, 30);

        g.setColor(Color.BLUE);
        g.drawRect(200, 100, 450, 250);
        g.drawString("x = 10 cm", 400, 90);
        g.drawString("y = 2 cm", 670, 200);
        //g.drawLine(200, 350, 450, 250);
        g.drawArc(200, 350, x, y, alpha, 70);
}

public static void main(String args[]) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        Draw aplication = new Draw();
        aplicacion.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

And main class:

public class Main{
    public static void main(String[] args) {
//45 means alpha
        Calculate c = new Calculate(45, (int) (5.0*(Math.pow(10, 6))));
        Graphics g = new Graphics() {..};
        Draw d = new Draw();
//After everything is calculated
        d.paint(g, c.getX(), c.getY(), c.getAlfa());
    }
}
DDN
  • 123
  • 1
  • 7
  • paint() is called automatically by Java to paint the JFrame (or other Component) when necessary. What you probably want to do is put your Calculate into Draw as a field. Then you will use the main method in Draw and get rid of your Main class. Refer to the JFrame tutorial on more information about JFrames ( http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html ) and this SO post seems to have some information about custom components: http://stackoverflow.com/a/3110836 (so you can learn about changing the paint function) – Nulano Mar 05 '17 at 11:18

1 Answers1

0

Paint( ) method is found in jpanel not jframe you extend jpanel

class name extends JPanel{}
Bc96
  • 13
  • 9