[Solved] why does this program not work? [closed]


While your code is really messy (and not easy to read) your problem is that you are adding a different instance of Sc to the Pane rather than adding the one you were drawing on:

frame.getContentPane().add(new Sc());

Instead you have to add “this” which you can’t do from a static method, but you can create an instance of Sc and initialize it with a method:

public static void main(String[] args) {

    Sc sc = new Sc();
    MainClass mc = new MainClass();
    mc.fun(sc);
    sc.initFrame();

}

public void initFrame() {
    JFrame frame = new JFrame();
    frame.getContentPane().add(this);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
}

You also have to take sc as an argument in your “fun” method (which then calls the “method” method from the sc object… again this could be done in only one class which would be a lot less confusing):

public void fun(Sc sc){
    sc.method(x,y,npoints);
}

This is still really messy, but at least it works now 😉

solved why does this program not work? [closed]