3

I just don't see how the paint() or paintComponent() is being called here. This is from an old youtube video back in 2011; the comment section of that video didn't help. I was expecting this code to call repaint() in the mousePressed() method, but it didn't, and it just works.

This is the youtube video: https://www.youtube.com/watch?v=PrPwCKr6WNI


import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;

public class JavaApplication17 extends JFrame {

  int GWIDTH = 800;
  int GHEIGHT = 600;
  int x, y;
  private Image dbImage;
  private Graphics dbg;

  public JavaApplication17() {
    setSize(GWIDTH, GHEIGHT);
    setTitle("Game");
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addMouseListener(new Mouse());
    x = 15;
    y = 15;
  }

  public class Mouse extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e) {
      int xCoord = e.getX();
      int yCoord = e.getY();
      x = xCoord + 7;
      y = yCoord + 7;
    }
  }

  public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
  }

  public void paintComponent(Graphics g) {
    g.fillOval(x, y, 15, 15);
    repaint();
  }

  public static void main(String[] args) {
    JavaApplication17 main = new JavaApplication17();
  }

}

shahaf
  • 4,750
  • 2
  • 29
  • 32
Noob
  • 37
  • 7

1 Answers1

1

how the paint() or paintComponent() is being called here?

paintComponent() is called by your code in paint() method.

paint() method is called by Swing framework every time when frame update is needed. By invoking repaint() method you're informing the Swing framework that your frame needs to update its content.

In IntelliJ IDEA this code looks like:

paint method screenshot

Note the letter O in blue circle. It means that the method paint() overrides some method in a parent class. In Java, it's recommended to add @Override modifier to mark overridden methods.

Your class JavaApplication17 extends class JFrame which means it inherits all methods of JFrame and its ancestors. JFrame class in its turn extends Frame and inherits all methods of Frame and its ancestors. And so on. See the class hierarchy diagram:

Swing classes hierarchy

Window and Container classes both implement their public void paint(Graphics g) methods. And in the abstract Component class paint() is called by update() method. You don't need to call the paint() method on your own.

If you want to learn more and understand how it works, read about OOP concepts in Java (abstraction, encapsulation, inheritance, and polymorphism).

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • 1
    But how does it know that frame update is needed, or what caused the `update()` method to run? Also, what is the class of `update()`? – Noob Feb 07 '20 at 01:12
  • @Noob 1. https://stackoverflow.com/questions/14341618/swing-refresh-cycle 2. https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html 3. "Java Swing" book, page 1013, "Swing painting delegation" chart. – naXa stands with Ukraine Feb 07 '20 at 10:24
  • JComponent extends Container, Container extends Component. `JComponent.update(Graphics)` -> https://docs.oracle.com/javase/10/docs/api/javax/swing/JComponent.html#update(java.awt.Graphics) `Container.update(Graphics)` -> https://docs.oracle.com/javase/10/docs/api/java/awt/Container.html#update(java.awt.Graphics) `Component.update(Graphics)` -> https://docs.oracle.com/javase/10/docs/api/java/awt/Component.html#update(java.awt.Graphics) – naXa stands with Ukraine Feb 07 '20 at 10:29
  • 1
    I read the links, but it would be really nice if you actually give a step by step summary of what causes `update()` to be called, so I can mark this question as answered. I know those links are probably good enough for the average java programmer, and I kind of get the gist of it, but I still can't get the full picture in my head. – Noob Feb 08 '20 at 10:57