Paint, Repainting, Paint Component

Excuse me, I am looking a lot to find how these 3 functions (paint, repaint, paintComponent) interact between them, but I have no idea. Can you explain to me when they are called (because sometimes I call it without me asking him) what they do exactly and what is the difference between them. thank you

+3
source share
1 answer

I'm not sure about "paint", but I can explain the relationship between repaint () and paintComponent ().

In my limited experience with java, the paintComponent () method is a method of the JPanel class and is a member of the "swing".

The paintComponent () method handles the entire painting. Essentially, it draws everything you want in JPanel, which is a Graphic object.

repaint () is the inherited instance method for all JPanel objects. Calling [your_JPanel_object] .repaint () calls the paintComponent () method.

Every time you want to change the look of your JPanel, you must call repaint ().

Some actions automatically call the repaint () method:

  • Window resizing
  • Window minimization and maximization

to name a few.

In SHORT, paintComponent () is a method defined in JPanel or your own custom class that extends JPanel. repaint () is a method called in another class (like JFrame) that ultimately calls paintComponent ().

here is an example:

public class MyPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); g.draw([whatever you want]); ... ... } } public class MyFrame extends JFrame{ public MyFrame(){ MyPanel myPanel = new MyPanel(); myPanel.repaint(); } } 
+5
source

Source: https://habr.com/ru/post/1492822/


All Articles