0

I've got a beginner question here that I was hoping someone with some Java experience could help me with. Currently taking an intro to OOP course focused on Java. My instructor is currently covering awt and swing, specifically the need to override the paint method so that graphics are redrawn when a window is resized, etc. I like to do as much outside reading as possible, and my concern is that the examples that my professor gives involve things that I've read are not best practices. To get to the point...

I understand that it's necessary to override the paint method, but I don't know the best way to do so. My professor's examples are all similar to the following:

class Example extends JFrame {
  public void paint(Graphics g) {
    super.paint(g);
    g.drawString("Blah, blah");
  }

  public static void main(String[] args) {
    Example a = new Example();
    a.setDefaultCl...
    \\Etc...
  }
}

This bothers me because it doesn't seem right to include everything for the GUI in the same class as my main method. Also, I read on a different thread here that you shouldn't extend JFrame, but there wasn't an explanation as to why. My solution was to create a class handling the gui and instantiate JFrame within the constructor. However, unless I'm mistaken, doing so won't let me override the paint method. I feel compelled to extend JFrame to allow me to override paint, but again I read that was the wrong thing to do.

Any help would be sincerely appreciated, I know I can just model my code off of what he has but I really want to understand this and know the best way to handle it.

Joseph Morgan
  • 210
  • 2
  • 8
  • Have a look at Have a look at [How can I set in the midst?](http://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319) and [How to get the EXACT middle of a screen, even when re-sized](http://stackoverflow.com/questions/13457237/how-to-get-the-exact-middle-of-a-screen-even-when-re-sized/13460914#13460914) for some of the reasons why you shouldn't override `paint` of top level containers like `JFrame`. – MadProgrammer Dec 02 '15 at 06:55
  • `JFrame` also has a `JRootPane` and `contentPane` which resides ontop if it, because of the nature of the way painting works, these can be painted independently of the frame, causing what ever was painted to the frame to be painted over, without you ever knowing about it. You're also locking your self into a single use case, making your code difficult to reuse – MadProgrammer Dec 02 '15 at 06:56
  • I would agree with your assumption about separating the main class from the UI, but that shouldn't stop you from extending from other UI classes, like `paintComponent`. This then allows you to re-use these classes in any other container you see fit – MadProgrammer Dec 02 '15 at 06:57

1 Answers1

3

I understand that it's necessary to override the paint method

No you should not override the paint() method.

You should override the paintComponent() method of a JPanel and then add the panel to the frame.

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

The tutorial will also show you how to better structure your code so that the GUI is created on the Event Dispatch Thread (EDT). The tutorial also has a section on Concurrency which will explain why this is important.

camickr
  • 321,443
  • 19
  • 166
  • 288