What are the differences between paint() method and repaint() method in Java?



In this article, we will learn about the differences between the paint() method and the repaint() method in Java. In the AWT and Swing frameworks, rendering graphical components is done in two different roles by the two methods paint() and repaint().

The paint() Method

This method holds instructions to paint this component. In Java Swing, we can change the paintComponent() method instead of paint() method as paint calls paintBorder(), paintComponent() and paintChildren() methods. We cannot call this method directly instead we can call repaint().

Syntax

The following is the syntax:

public void paint(Graphics g) { // paint() method
            super.paint(g);
            g.setColor(Color.black);
}

The following are the key features of the paint() method:

  • Automatically Invoked: Whenever there is a requirement of painting a component, the AWT painting system invokes it.
  • Receive a Graphics Context: It collects an object of type Graphics for drawing operations.
  • Complete Rendering Handle: Manages the complete visual representation of the component.
  • Do not call explicitly: Should only be called through the AWT/Swing system.

The Repaint() Method

This method cannot be overridden. It controls the update() -> paint() cycle. We can call this method to get a component to repaint itself. If we have done anything to change the look of the component but not the size then we can call this method.

Syntax

The following is the syntax:

repaint();

The following are the key features of the repaint() method:

  • Invoke request: It postpones the execution of the paint operation as per the schedule.
  • Has overloaded variants: It has multiple versions according to different parameters.
  • Smart scheduling: When possible, it combines several repaints into a single repaint.
  • Thread-safe: It can be safely called from any thread.

Example of paint() and Repaint()

Below is an example of the paint() and the repaint() methods in Java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class PaintRepaintTest extends JPanel implements MouseListener {
   private Vector v;
   public PaintRepaintTest() {
      v = new Vector();
      setBackground(Color.white);
      addMouseListener(this);
   }
   public void paint(Graphics g) { // paint() method
      super.paint(g);
      g.setColor(Color.black);
      Enumeration enumeration = v.elements();
      while(enumeration.hasMoreElements()) {
         Point p = (Point)(enumeration.nextElement());
         g.drawRect(p.x-20, p.y-20, 40, 40);
      }
   }
  public void mousePressed(MouseEvent me) {
      v.add(me.getPoint());
      repaint(); // call repaint() method
   }
   public void mouseClicked(MouseEvent me) {}
   public void mouseEntered(MouseEvent me) {}
   public void mouseExited(MouseEvent me) {}
   public void mouseReleased(MouseEvent me) {}
   public static void main(String args[]) {
      JFrame frame = new JFrame();
      frame.getContentPane().add(new PaintRepaintTest());
      frame.setTitle("PaintRepaint Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
      frame.setSize(375, 250);
      frame.setVisible(true);
   }
}

Output

If we click on the screen able to draw squares. In the mousePressed() method, we can call the repaint() method.

Difference Table

The following are the key differences between the paint() method and the repaint() method in Java:

Feature paint() repaint()
Invocation Called by the system Called by a programmer
Purpose Performs actual drawing Requests drawing to be performed
Thread Safety Must run on EDT Safe to call from any thread
Direct Use Should not be called directly Meant to be called directly
Performance Heavy operation Lightweight request
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-04-15T19:15:15+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements