What is paintComponent method in Java?

Paint with Java – Window, Canvas and Variables.

SWING Swing is the set of classes, interfaces, resources, etc, for the construction of GUI (Graphical User Interface). It is part of Java Foundation Classes (JFC). The JFC contains the Swing components, a 2D API, a drag and drop API and an API to facilitate access. Swing is built on the AWT (Abstract Window Toolkit) architecture. AWT is the class library, built into Java 1.0, for performing GUI programming. AWT is platform dependent. Swing appears in Java 2, it is platform independent. With Swing the appearance of GUI elements is the same on all platforms. Today, all, or almost all, graphical programming is done in Swing. The Swing classes are contained in the javax.swing package. So, a program that uses graphic components and processes events will have the following import statements: import java.awt.*; // it will not always be necessary to import java.awt.event.*; import.javax.swing.*;

DISPLAY CONTROL(II)The repaint() method, of the Component class, is called whenever you want to redraw a component. repaint() makes a call to update(), which in its original implementation, calls paint(). repaint() is overloaded: void repaint() for the whole enclosure void repaint(int x, int y, int width, int height) limited to the rectangle The update() method, of the Component class, is normally called by the repaint() method. Its prototype: void update(Graphics g). If not redefined, it performs two actions: Redraw the component with the current background color. l Call the paint() method, passing the graphics context.


When you use paintGraphics() you are not calling paintGraphics(), but repaint(). repaint() enqueues in a java thread a request to paint() and this one makes the call to paintComponent(). But java is smart and if the window is not visible (which is the case), it doesn’t make those calls or at least it doesn’t give error, in the second case, you call paint() and you pass a Grapichs. As I said, it would help if you said which exact line gives error, but I imagine Graphics is null. You don’t have an associated Graphics until your window is visible on screen. Your sequence of calls is new Line() calls init() which calls new internal() and assign() which calls paint(). Then you build the window, so when you call assign possibly this.getGrapics() returns null.Be good.

Thanks, now I think I understand it, it is a different case because before pressing the click the component is already assembled. One last thing. In this example if instead of paint(). I put a paintComponent() and I call it with a repaint() it would also work, but if I use a paintComponents() finished in s.. it would not work. What is the difference? Best regards.

Paintcomponent java example

Java provides the Graphics class, which allows drawing ellipses, squares, lines, displaying text and also has many other drawing methods. For any programmer, an understanding of the Graphics class is essential before getting into drawing in Java.

In order to be able to paint, a program needs a graphical context.

How to use Panels with independent classes in java

In the previous article we arrived at the point where we could move the head of the snake and “eat” the eggs that were appearing randomly in the screen; it was pending that it grows with each egg that it ate and that when it collides against itself the game ends.

It will be necessary to modify the method paintComponent(Graphics g) so that it draws the snake according to the list; in passing we take advantage to incorporate some lines related to when the game is finished, the method is as follows:

There are two changes made:1. The first one consists of going through the list and drawing the points2. The second one shows the legend when the game is finished along with the score and the possible options to continue.

Now we only have to modify the update() method so that it changes the positions of the elements of the list so that it generates certain continuity with the points; and also detects if there is collision, the method is as follows:

Of course it does not have elaborated graphics, they are only rectangles of different color; but I believe that the interesting thing is some questions related to how to handle the graphics, the events of the keyboard and the rhythm of the game.