java repaint闪烁_java中paint,repaint,update之间的关系(闪烁问题的解决)

最近总结了一下java中的paint,repaint和updata三者之间的关系,首先咱们都知道用paint方法来绘图,用repaint重绘,用update来写双缓冲。但是他们之间是怎么来调用的呢,咱们来分析一下(想直接看结果,请跳过分析过程):

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

1.首先咱们画在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);//调用super.paint(g)去清除运动的痕迹

g.setColor(Color.RED);

g.fillOval(x, y, 20, 20);

y++;

repaint();//重画

try {

Thread.sleep(10);//在此处睡眠一会,要不运动太快

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

运行之后的界面

c63c3af33601c66309c0c001088bb81c.png

但是你仔细观察一下,会发现有闪烁现象,怎么办呢?我第一时间想到的就是加个双缓冲。那么咱们来试一下!

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) { //双缓冲

if(offScreenImage == null) {

offScreenImage = this.createImage(800, 600);

}

Graphics gOffScreen = offScreenImage.getGraphics();

Color c = gOffScreen.getColor();

gOffScreen.setColor(Color.GREEN);

gOffScreen.fillRect(0, 0, 800, 600);

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, 20, 20);

y++;

repaint();

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

运行:仔细看一下,发现还是闪烁,这是什么鬼,难道双缓冲加错了么?咱们再用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) { //双缓冲

if(offScreenImage == null) {

offScreenImage = this.createImage(800, 600);

}

Graphics gOffScreen = offScreenImage.getGraphics();

Color c = gOffScreen.getColor();

gOffScreen.setColor(Color.GREEN);

gOffScreen.fillRect(0, 0, 800, 600);

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, 20, 20);

y++;

repaint();

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

运行:发现不闪烁了,说明刚才加的双缓冲是没有问题的,然后我在JFrame的update方法里和Frame的update方法里都加个输出语句

56a4a837c7a1a347d2b4e291bb02bcf6.png

结果发现,JFrame运行后并没有输出0,而Frame在不断输出0;

看来JFrame压根没有调用update方法!!!

然后咱们用JPanel再试一下,把小球画在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( 800, 600);

frame.setLayout(null);

this.setBounds(0, 0, 800, 700);

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, 20, 20);

y++;

repaint();

try {

Thread.sleep(10);

} catch (InterruptedException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

}

}

运行后,竟然神奇的发现小球不闪烁了!!!并且没有加双缓冲!

为什么呢?

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

我查了API和其他资得出如下结果:

0138902fb88704f2e688e42031268b0c.png

首先repaint()方法在重量级组件的时候会调用update方法,在轻量级组件的时候会调用paint方法,(重量级和轻量级的概念自查)

恰恰Frame是重量级组件,JFrame是轻量级组件,这样就能解释JFrame不运行update方法的原因了!

那JPanel为什么就不会闪烁呢?

其实是因为JPanel中的paint方法和JFrame中的paint方法不太一样

2f4691f937a02220eabb228f835ed317.png

914afed2987c1c27aa3073cce439cdfc.png

JFrame的paint方法是继承Container类中的,而JPanel的paint方法是继承JComponent类中的,看看他俩之间方法的差异:

94e17685e3bf50c0690a9f2e07e6a57b.png

这样就明白了吧,JFrame的paint方法与JPanel中的paint方法并不一样!JPanel的paint是按顺序画的,因为Frame已经过时了,以后咱们就可以把用paint方法把东西画在JPanel(或者JLabel)上面,这样不用加双缓冲就不闪烁!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值