How to Use Repaint in Java

Save

You can use the repaint method in Java when you want the screen graphics to update as soon as possible. Repaint does not necessarily force an immediate redraw. The program treats the repaint method as a suggestion. Once called, it will try to execute it at a good time, depending on what else is happening at the moment in the program. Repaint can be given a time parameter and can be used to repaint the entire component or a specified rectangular region.

  • Open the software development environment which you use to edit Java, such as Eclipse, Netbeans or JBuilder X.

  • Open the Java source file in which you want to use the repaint method.

  • Select which of the four overloaded versions of the repaint method you want to use. There is "repaint()," "repaint(long tm)," "repaint(int x, int y, int width, int height)" and "repaint(long tm, int x, int y, int width, int height)." The "long tm" parameter gives a maximum time at which you desire a repaint to occur. The "int x, int y, int width, int height" parameters specify a rectangular region of the component you want redrawn.

  • Insert the repaint code at the location where you want the component to be redrawn. For example, you could call repaint when a component's color changes and you want it to be redrawn, using the code:

    class GraphicsArea extends JPanel {

    private Color current_color;

    public void setColor(Color c) {

    current_color = c;

    repaint();

    }

    }

References

Promoted By Zergnet

Comments

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!