The repaint method in java is available in java.applet.Applet class is a final method used whenever we want to call update method along with the call to paint method; call to update method clears the current window, performs an update, and afterwards calls paint method.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsSyntax:
package <packagename>; // class extending applet public class <classname> extends Applet{ public method <methodname>(<arguments>){ repaint(); // calling repaint method when required } }</arguments></methodname></classname></packagename>
The above syntax shows how a repaint method is used in java. The repaint method is a part of the java.applet.Applet class, and it cannot be overridden. Therefore repaint method can be directly called from a class extending Applet or its subclasses.
How does repaint work in Java?
The repaint method is a final method available in the Applet class, and that’s why it cannot be overridden. Whenever the repaint method is to be used, it should be directly called from the Applet class’s subclasses. The repaint method is responsible for handling update to the paint cycle of the applet. Whenever we want a component to repaint itself, we need to call the repaint method. In case we have made changes to the appearance of a component but have not made any changes to its size, then we can call the repaint method to update the new appearance of the component on the graphical user interface. The repaint method is an asynchronous method of applet class. When call to repaint method is made, it performs a request to erase and perform redraw of the component after a small delay in time.
Whenever the repaint method is invoked from a component, a request is sent to the graphical user interface, which communicates to the graphical user interface to perform some action at a future instance of time. The whole idea behind the repaint method is to avoid the call to paint () method directly.
Examples to Implement repaint Method in Java
Now we will see some java examples showing the use of the repaint method:
Example #1
Here is an example showing how the repaint method is used in java:
Code:
package com.edubca.repaintdemo; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import java.applet.Applet; // class extending applet component and implementing mouse event listener public class RepaintDemo extends Applet implements MouseListener { private Vector vector; public RepaintDemo() { vector = new Vector(); setBackground(Color.red); addMouseListener(this); } public void paint(Graphics graphics) { // paint method implementation super.paint(graphics); graphics.setColor(Color.black); Enumeration enumeration = vector.elements(); while(enumeration.hasMoreElements()) { Point p = (Point)(enumeration.nextElement()); graphics.drawRect(p.x-20, p.y-20, 40, 40); } } public void mousePressed(MouseEvent mouseevent) { vector.add(mouseevent.getPoint()); repaint(); // call repaint() method } public void mouseClicked(MouseEvent mouseevent) {} public void mouseEntered(MouseEvent mouseevent) {} public void mouseExited(MouseEvent mouseevent) {} public void mouseReleased(MouseEvent mouseevent) {} public static void main(String args[]) { JFrame frame = new JFrame(); // creating a jFrame object frame.getContentPane().add(new RepaintDemo()); // Adding Window frame.setTitle("Repaint Method Demo"); // set title of the window frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setSize(375, 250); // set size of window frame.setVisible(true); // set window as visible } }
Output:
After the mouse click event is performed, the following shapes will be visible with a black color border. Note that this is done through the repaint method, which calls the update and then paints method, due to which we are able to see visible shapes immediately after click event is performed.
Example #2
To provide more clarity about the use of the repaint method, here is another example:
Code:
import java.awt.*; import java.awt.event.*; import java.util.*; import java.applet.Applet; import java.awt.Graphics; // class extending public class RepaintDemo extends Applet { int test=2; public void paint(Graphics graphics) { super.paint(graphics); setBackground(Color.cyan); // set backgroung color of window graphics.setColor(Color.black); // set color of Text appearing on window graphics.drawString("Value of Variable test = "+test, 80, 80); try { Thread.sleep(1000); } catch(InterruptedException ex){} // increasing value of variable by 1 and update its value of GUI test++; repaint(); } }
Output:
In the above example, we have an applet and a variable called test is declared inside it. We are continuously incrementing the value of the variable test, and we want to ensure that the variable’s updated value is visible on the user interface. Therefore, we are using the repaint method that ensures to call update method before the paint method. The output of the above program. In this window value of the test, the variable is always incrementing, and the updated value is visible on GUI.
Conclusion
The above example provides a clear understanding of the repaint method and its function. We should call the repaint method when we want the applet’s update and paint cycle to be invoked. The calling repaint method performs an immediate update to the look and appearance of a component.
The above is the detailed content of repaint in Java. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function