
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement PaintComponent Method of JPanel in Java
In this article, we will learn to implement the paintComponent() method of a JPanel in Java. In Swing, custom rendering of components is achieved by overriding the paintComponent() method.
What is a JPanel?
A JPanel is a lightweight container and it is an invisible component in Java. A JPanel's default layout is FlowLayout. After the JPanel has been created, other components can be added to the JPanel object by calling its add() method inherited from the Container class.
What is a paintComponent() Method?
The paintComponent() method is needed to draw something on a JPanel other than drawing the background color. This method already exists in a JPanel class, so we need to use the super declaration to add something to this method and take Graphics objects as parameters.
Syntax
The following is the syntax for the paintComponent() method declaration:
protected void paintComponent(Graphics g)
Super Keyword
The super keyword is similar to the this keyword. If you have a method that overrides a method from its parent class, you can call the original method using the super keyword.
Implementing the paintComponent() Method
For rendering a drawing inside a JPanel, first, we will create a custom class SmileyApp with the JPanel and then override the paintComponent method, which indicates that this method replaces the JPanel's default painting.
public class SmileyApp extends JPanel { @Override protected void paintComponent(Graphics g) {
The super.paintComponent(), which represents the normal paintComponent() method of the JPanel, which can only handle the background of the panel, must be called in the first line inside the SmileyApp, else incorrect rendering might appear in the drawing.
super.paintComponent(g);
Then draws the yellow face by setting the setColor() method to YELLOW and draws a circle for the face using the fillOval() method and then creates eyes and a smile for the face by setting the remaining values.
At last, in the main method, we will create a constructor for the SmileyApp, then create a JFrame titled "Smiley App", and then add the SmileyApp inside the JFrame.
SmileyApp smiley = new SmileyApp(); JFrame app = new JFrame("Smiley App"); app.add(smiley, BorderLayout.CENTER);
Example
Below is an example of implementing the paintComponent() method of a JPanel in Java:
import java.awt.*; import javax.swing.*; public class SmileyApp extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.YELLOW); g.fillOval(10, 10, 200, 200); // draw Eyes g.setColor(Color.BLACK); g.fillOval(55, 65, 30, 30); g.fillOval(135, 65, 30, 30); // draw Mouth g.fillOval(50, 110, 120, 60); // adding smile g.setColor(Color.YELLOW); g.fillRect(50, 110, 120, 30); g.fillOval(50, 120, 120, 40); } public static void main(String[] args) { SmileyApp smiley = new SmileyApp(); JFrame app = new JFrame("Smiley App"); app.add(smiley, BorderLayout.CENTER); app.setSize(300, 300); app.setLocationRelativeTo(null); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } }