0

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.

masky007
  • 608
  • 1
  • 6
  • 20

2 Answers2

3

Main problem is that you never add instance of MyDrawPanel to frame you created in go() method, so it remains empty.

One way to fix it is to explicitly add current (this) instance of MyDrawPanel in go method like

public void go() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.add(this);       //  <-- add this line, 
                           //  you can also use frame.setContentPane(this)
    frame.setVisible(true);
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Thank you.. and this was definitely left out in the book :/ (edit: the book actually never explained exactly what "this" means yet..) – masky007 Apr 30 '17 at 13:05
  • `this` inside method represents object on which method was invoked. So if we execute code like `someInstance.method()` then from inside `method()` `this` will hold same reference as `someInstance`. So here since we invoked `pan.go();` inside `go()` method `this` returns same reference as `pan` which holds `MyDrawPanel` instance, which allows us to add this panel to frame via `frame.add(this);`. – Pshemo Apr 30 '17 at 13:35
  • @masky007 But there are also other meanings of `this` depending on context. Like inside constructor we can use it like `this(argument1, argument2)` to invoke code from another constructor of same class. – Pshemo Apr 30 '17 at 13:41
1

You always should call super.paintComponent(g);

Also, you should choose a different design to create your frames and panels. Personally I would use this:

public class MyGui extends JFrame {
    public MyGui() {
        DrawPanel myDrawPanel = new DrawPanel();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(myDrawPanel);
        this.pack();
        this.setVisible(true);
    }
}

public class MyDrawPanel extends JPanel {
    public MyDrawPanel() {
        this.setPreferredSize(new Dimension(300, 300));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.orange);
        g.fillRect(20,50,100,100);
    }
}

Useful answer to understand what super.paintComponent(g) does: What does super.paintComponent(g) do?

Community
  • 1
  • 1
Bas
  • 4,423
  • 8
  • 36
  • 53
  • Prefer composition over inheritance, don't extend frame if you don't really have to. More info: [Extends JFrame vs. creating it inside the program](http://stackoverflow.com/questions/22003802/extends-jframe-vs-creating-it-inside-the-program) – Pshemo Apr 30 '17 at 12:06
  • @Pshemo interesting, this is how they learned it to me at university. Thanks! – Bas Apr 30 '17 at 12:08