gpt4 book ai didi

Java NetBeans IDE - JPanel 中的动画闪烁

转载 作者:行者123 更新时间:2023-12-01 14:58:24 29 4
gpt4 key购买 nike

我目前正在 NetBeans 中学习 Java 动画和图形。

我决定从 JPanel 中的简单球运动开始。

我在修复闪烁问题时遇到一些问题。我看过很多论坛,但大多数都是使用双缓冲的 AWT,但我后来知道 SWING 组件不需要双缓冲。我尝试过 - 使用 repaint() 和 .clearRect()

在这 2 个中,我发现使用 .clearRect() 给了我更好的结果,但并不是一直无缝无闪烁动画。所以我想知道是否有更好的方法消除闪烁。

这是我的代码:

public class NewJFrame extends javax.swing.JFrame {

int x;
int y;
int xspeed = 1;
int yspeed = 1;
int width;
int height;
Graphics g;

/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
g = jp.getGraphics();
width = jp.getWidth();
height = jp.getHeight();
final Timer timerCHK = new Timer();
timerCHK.schedule(new TimerTask() {
public void run() {
move();
time();

}
}, 1000, 10);

}
void time() {
final Graphics g = jp.getGraphics();
final Timer timerCHK = new Timer();
timerCHK.schedule(new TimerTask() {
public void run() {
g.clearRect(0, 0, jp.getWidth() - 3, jp.getHeight() - 3);

}
}, 1000, 12);
}

void move() {
x = x + xspeed;
y = y + yspeed;
Graphics mk = jp.getGraphics();
if (x < 0) {
x = 0;
xspeed = -xspeed;
} else if (x > width - 20) {
x = width - 20;
xspeed = -xspeed;
}

if (y < 0) {
y = 0;
yspeed = -yspeed;
} else if (y == height - 20) {
y = height - 20;
yspeed = -yspeed;
}

mk.drawOval(x, y, 20, 20);

}
public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
}

最佳答案

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NewJFrame extends JFrame {

private JPanel jp;
private Timer timer;

public NewJFrame() {
initComponents();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationByPlatform(true);
timer.start();
}

public void initComponents() {
ActionListener al = new ActionListener() {

public void actionPerformed(ActionEvent evt) {
jp.repaint();
}
};
timer = new Timer(50,al);

jp = new JPanel() {

int x;
int y;
int xspeed = 1;
int yspeed = 1;

Dimension preferredSize = new Dimension(300, 100);

@Override
public Dimension getPreferredSize() {
return preferredSize;
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.move();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawOval(x, y, 20, 20);
}

void move() {
x = x + xspeed;
y = y + yspeed;
if (x < 0) {
x = 0;
xspeed = -xspeed;
} else if (x > getWidth() - 20) {
x = getWidth() - 20;
xspeed = -xspeed;
}

if (y < 0) {
y = 0;
yspeed = -yspeed;
} else if (y == getHeight() - 20) {
y = getHeight() - 20;
yspeed = -yspeed;
}
}
};
jp.setBackground(Color.ORANGE);

this.add(jp);
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

public void run() {
new NewJFrame().setVisible(true);
}
});
}
}

关于Java NetBeans IDE - JPanel 中的动画闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14037848/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com