Relationship between paint repaint update in Java

Source: Internet
Author: User
Tags getcolor

Recently summed up the relationship between Paint,repaint and Updata in java, first we all know to use the Paint method to draw, with repaint redraw, with update to write double buffering. But how do they call each other, let's analyze (to see the results directly, skip the analysis process):

--------------------------------------------------------------------------------------------------------------- --------------

1. First of all, let's draw on Jframe.

Import java.awt.Color;
Import java.awt.Graphics;
Import java.awt.Image;
Import javax.swing.JFrame;

public class Jframe extends jframe{
int x = 40,y=50;
Jframe () {
this.setsize (800,700);
This.setlocationrelativeto (null);
this.getcontentpane (). setbackground (color.green);
this.setvisible (true);
this.setdefaultcloseoperation (jframe.exit_on_close);
}
public static void main (string[] Args) {
New Jframe ();
}
public void Paint (Graphics g) {
super.paint (g);//call super.paint (g) to remove traces of motion
G.setcolor (color.red);
g.filloval (x, y, +);
y++;
Repaint ();//repaint
try {
Thread.Sleep (10);//sleep here for a while, or move too fast
} catch (interruptedexception E) {
//TODO auto-generated Catch block
e.printstacktrace ();
}
}
}

After running the interface

But if you look closely, you will find a flicker phenomenon , how to do? My first thought was to add a double Cushion. Then let's try it!

Import java.awt.Color;
Import java.awt.Graphics;
Import java.awt.Image;
Import javax.swing.JFrame;


public class Jframe extends jframe{
int x = 40,y=50;
Jframe () {
this.setsize (800,700);
This.setlocationrelativeto (null);
this.getcontentpane (). setbackground (color.green);
this.setvisible (true);
this.setdefaultcloseoperation (jframe.exit_on_close);
}
public static void main (string[] Args) {
New Jframe ();
}
Image offscreenimage = null;
public void update (Graphics g) {//double buffering
if (offscreenimage = = Null) {
offscreenimage = this.createimage (+-);
}
Graphics goffscreen = offscreenimage.getgraphics ();
Color C = Goffscreen.getcolor ();
Goffscreen.setcolor (color.green);
goffscreen.fillrect (0, 0, +);
Goffscreen.setcolor (c);
Paint (goffscreen);
g.drawimage (offscreenimage, 0, 0, null);
}
public void Paint (Graphics g) {
Super.paint (g);
G.setcolor (color.red);
g.filloval (x, y, +);
y++;
Repaint ();
try {
Thread.Sleep (ten);
} catch (interruptedexception E) {
//TODO auto-generated Catch block
e.printstacktrace ();
}
}
}

Run: look carefully, discover or blink , this is what ghost, is double buffer added wrong? Let's try it again with Frame.

Import java.awt.Color;
Import java.awt.Frame;
Import java.awt.Graphics;
Import java.awt.Image;
Import javax.swing.JFrame;


public class Jframe extends frame{
int x = 40,y=50;
Jframe () {
this.setsize (800,700);
This.setlocationrelativeto (null);
This.setbackground (color.green);
this.setvisible (true);
//this.setdefaultcloseoperation (jframe.exit_on_close);
}
public static void main (string[] Args) {
New Jframe ();
}
Image offscreenimage = null;
public void update (Graphics g) {//double buffering
if (offscreenimage = = Null) {
offscreenimage = this.createimage (+-);
}
Graphics goffscreen = offscreenimage.getgraphics ();
Color C = Goffscreen.getcolor ();
Goffscreen.setcolor (color.green);
goffscreen.fillrect (0, 0, +);
Goffscreen.setcolor (c);
Paint (goffscreen);
g.drawimage (offscreenimage, 0, 0, null);
}
public void Paint (Graphics g) {
Super.paint (g);
G.setcolor (color.red);
g.filloval (x, y, +);
y++;
Repaint ();
try {
Thread.Sleep (ten);
} catch (interruptedexception E) {
//TODO auto-generated Catch block
e.printstacktrace ();
}
}
}

Run: The discovery does not blink, indicating that the double buffering is not a problem, and then I in the JFrame Update method and Frame Update method to add an output statement

It is found that thejframe is not output 0 after running, and the frame is continuously outputting 0.

It seems jframe didn't call the Update method at all!!!

Then we'll try again with jpanel, and draw the ball on the Jpanel.

Import java.awt.Color;
Import java.awt.Graphics;
Import javax.swing.JFrame;
Import javax.swing.JPanel;


public class Jpanel extends jpanel{
int x=40,y=40;
Jpanel () {
JFrame frame = new JFrame ();
frame.setsize (+);
frame.setlayout (null);
this.setbounds (0, 0, +);
Frame.setlocationrelativeto (null);
frame.setvisible (true);
This.setbackground (color.green);
Frame.add (this);
frame.setdefaultcloseoperation (jframe.exit_on_close);
}
public static void main (string[] Args) {
New Jpanel ();
}
public void Paint (Graphics g) {
Super.paint (g);
G.setcolor (color.red);
g.filloval (x, y, +);
y++;
Repaint ();
try {
Thread.Sleep (ten);
} catch (interruptedexception E) {
//TODO auto-generated Catch block
e.printstacktrace ();
}
}
}

After the operation, unexpectedly magically found that the ball does not blink!!! And no double buffering!

Why is it?

--------------------------------------------------------------------------------------------------------------- --------------

I checked the API and other assets to produce the following results:

first the Repaint () method calls the Update method at the heavyweight component, and the paint method is called when the lightweight component is used (heavyweight and lightweight concept Self-examination)

The exact frame is a heavyweight component, JFrame is a lightweight component, which explains why JFrame does not run the update method!

Why wouldn't the JPanel blink?

In fact, because the paint method in JPanel is not the same as the paint method in JFrame

Jframe's Paint method inherits from the container class, and Jpanel's paint method inherits from the JComponent class , looking at the difference between the two methods:

This makes sense, Jframe's paint method is not the same as the paint method in jpanel! JPanel paint is in order, because frame is obsolete, we can use the paint method to draw things on the JPanel (or jlabel), so that no double buffering does not blink!

Relationship between paint repaint update in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.