So far the book has been mostly clear and consistent, on occasions i had to go back and reread but in this example i am really stuck, no matter how much i reread i just don't get it. It's either something missed out in the book or i am missing the whole point. So please if you can clear this up for me, it is bugging me immensely and i don't want to continue to read the book without getting this right.
import java.awt.*;
import javax.swing.*;
public class MyDrawPanel extends JPanel {
public static void main(String[] args) {
MyDrawPanel pan = new MyDrawPanel();
pan.go();
}
private static final long serialVersionUID = 2; // this was not mentioned in the book, but i had to put it to get rid of the warning the compiler gives without it (i googled it)
public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
// In the book it's mentioned that you never call this method yourself (i don't quite get it how that works...)
public void paintComponent(Graphics g) {
g.setColor(Color.orange);
g.fillRect(20,50,100,100);
}
}
The code above compiles and runs just fine but nothing happens.
I tried to instantiate a Graphics object and play around but apparently the Graphics class in an abstract one..
Thank you!
edit: I have the 2nd edition of the book, so you can turn to page 364 chapter 12 if you have it as well (the code where i draw the JFrame is from the previous example - and i think that the paintComponent() method example just adds to the previous example.