1

Here is a code sample:

public class MyClass extends JPanel
{
      Image img;
      public MyClass(Image img) {
      this.img = img;
      }
      ...

      void paintComponent(Graphics g) {
           g.drawImage(img, 0, 0, this);
      }

      void someFunction() {
           img = (Image) inputStream.getObject();
      }
}

Will the paintComponent() will also get automatically called if img so much as receives a brand new Image instance?

Also, is there any use to the paint()and repaint()methods in this context?

WIlopu
  • 47
  • 7
  • 2
    No, Swing will not repaint if you assign a variable a new value. I recommend checking out [custom painting in swing](https://docs.oracle.com/javase/tutorial/uiswing/painting/). In `someFunction` you should call `repaint` – Vince Oct 29 '15 at 16:55
  • 1
    Remember that the first line of code in an overridden paintComponent method *must* be `super.paintComponent(g)`. Otherwise, you will get unpredictable painting artifacts. – VGR Oct 29 '15 at 17:07
  • Thank you. I infer that `repaint()` calls `paintComponent()`internally, is that right? @VGR: Would you mind explaing why is it so? Like, what does the superclass' `paintComponent()`do specifically and where would those artifacts come from? – WIlopu Oct 29 '15 at 17:08
  • Yes, `repaint()` will go through the steps of repainting the component and will eventually get to `paintComponent()` and draw the image, make sure you call `repaint()` every time you update the data behind the rendering, so the rendering matches the data – phflack Oct 29 '15 at 17:10
  • 1
    Oracle's [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and the Java tutorial's [A Closer Look at the Paint Mechanism](https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html) explain it better than I can. At the very least, it is needed to ensure the background is properly painted. I believe it performs some painting optimizations as well, at least on some platforms. – VGR Oct 29 '15 at 17:13

1 Answers1

2

No. In particular changing a Component's variables or calling methods like someFunction() does not implicitly run paintComponent() unless the method is design to do that. For instance the someFunction() method can be made to repaint whenever it's called as follows:

void someFunction() {
   img = (Image) inputStream.getObject();
   repaint();
}

But you'd still have to call repaint() wherever you change img, which will be prone to bugs if you do it alot. A smoother approach (which allows you passes in images externally if you might do that) is to write a setter that suggest a repaint.

public void setImage(Image img){
   this.img = img;
   repaint();
}

and use use this method instead of changing the variable directly. e.g.:

void someFunction() {
   setImage((Image) inputStream.getObject());
}

Finally, repaint() may ultimately invoke paint() but you generally do not call paint() directly because repaint() is better managed. See paint vs repaint and Painting in AWT and Swing for more information.

Community
  • 1
  • 1
Linus
  • 894
  • 7
  • 13