日期:2014-05-20  浏览次数:20857 次

使用了双缓冲,但动画还是闪烁,怎么修改?
[code=Java][/code]/*
 * 背景旋转正常,但eye旋转有闪烁,希望有人能指出修复办法。
 */
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
class Syaringan extends JPanel{
private ImageIcon eye;
private int x;
private int y;
private boolean ini = true;
private Graphics2D g2;
private double angle = 0;
private int dAngle = 0;
//控制旋转角度
private boolean up = true;
Syaringan(ImageIcon eye){
this.eye = eye;
setSize(eye.getIconWidth()+2,eye.getIconHeight()+2);
}
//初始化图片中心位置
private void fresh(){
if(ini){
x = this.getWidth()/2-eye.getIconWidth()/2;
y = this.getHeight()/2-eye.getIconHeight()/2;
g2 = (Graphics2D)(this.getGraphics());
ini = false;
}
}
private int kt = Environment.dT/10;
public void draw(){
fresh();
//kt—每3帧动画改变旋转加速度
if(kt==3)
{
if(dAngle<-20)
{
dAngle++;
up = true;
}
else if(dAngle>20)
{
dAngle--;
up = false;
}
else if(dAngle<=20 && dAngle>=-20)
{
if(up)
dAngle++;
else dAngle--;
}
angle += dAngle;
kt = 1;
}
else kt++;
//旋转图片
Graphics g2 = this.getGraphics();
// g2.rotate(Math.toRadians(angle),x+eye.getIconWidth()/2,y+eye.getIconHeight()/2);
g2.drawImage(eye.getImage(),x,y,null);
}
public static void main(String[] args){
JFrame frame = new JFrame();
frame.add(new Environment(),BorderLayout.CENTER);
frame.setTitle("写轮眼");
frame.setSize(400+8,400+34);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class Environment extends JPanel{
static final int dT = 10;
private Timer timer = new Timer(dT,new TimerListener());
//背景每次的旋转角度
private double angle = 0;
private Image background = Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("eyeBK.png"));
private ImageIcon eye1 = new ImageIcon("eye1.png");
private ImageIcon eye2 = new ImageIcon("eye2.png");
private ImageIcon eye3 = new ImageIcon("eye3.png");
private Syaringan Seye1 = new Syaringan(eye1);
private Syaringan Seye2 = new Syaringan(eye2);
private Syaringan Seye3 = new Syaringan(eye3);
Environment(){
setLayout(null);
timer.start();
}
boolean ini = true;
private class TimerListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
fresh();
angle += 1;
repaint();
}
}
private int x;
private int y;
private void fresh(){
if(ini){
Seye1.setLocation(getWidth()/7*2,getWidth()/7*2);
Seye2.setLocation(getWidth()/7*5,getWidth()/7*2);
Seye3.setLocation(getWidth()/2,getWidth()/7*5);
add(Seye1);
add(Seye2);
add(Seye3);
x = getWidth()/2-background.getWidth(null)/2;
y = getHeight()/2-background.getHeight(null)/2;
ini = false;
}
}
private Image iBuffer;
private Graphics gBuffer;
@Override
public void update(Graphics scr){
if(iBuffer == null)
{
iBuffer = createImage(this.getSize().width,this.getSize().height);
gBuffer = iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
paint(gBuffer);
scr.drawImage(iBuffer,0,0,null);
}
@Override
//怎么改……
public void paint(Graphics g2){
// Graphics2D g2 = (Graphics2D)scr;
Seye1.draw();
Seye2.draw();
Seye3.draw();