Java的NetBeans IDE中 - 动画在闪烁的JPanel [英] Java NetBeans IDE - Animation Flickering in JPanel
问题描述
目前我正在学习Java动画和图形在NetBeans。
我决定在JPanel的一个简单的球运动开始。
我有一些问题,固定闪烁闪烁问题。我看过很多论坛,但多数是使用双缓冲AWT,但我才知道,Swing组件不需要双缓冲。我想 - 。使用重绘()
和 clearRect()
出了2我发现,使用 clearRect()
给了我更好的结果,但不是无缝无闪烁的动画所有time.So我想知道还有就是要消除闪烁更好的方法。
下面是我的code:
公共类NewJFrame扩展javax.swing.JFrame中{INT X;
诠释Ÿ;
INT xspeed = 1;
INT yspeed = 1;
INT宽度;
INT高度;
图形克; / **
*创建新形式NewJFrame
* /
公共NewJFrame(){
的initComponents();
} 私人无效jButton2ActionPerformed(EVT java.awt.event.ActionEvent中){
G = jp.getGraphics();
宽度= jp.getWidth();
高度= jp.getHeight();
最后定时器timerCHK =新的Timer();
timerCHK.schedule(新的TimerTask(){
公共无效的run(){
移动();
时间(); }
},1000,10); }
无效时间(){
最后图形G = jp.getGraphics();
最后定时器timerCHK =新的Timer();
timerCHK.schedule(新的TimerTask(){
公共无效的run(){
g.clearRect(0,0,jp.getWidth() - 3,jp.getHeight() - 3); }
},1000,12);
}无效移动(){
X = X + xspeed;
Y = Y + yspeed;
图形MK = jp.getGraphics();
如果(X℃,){
X = 0;
xspeed = -xspeed;
}否则如果(X>宽度 - 20){
X =宽度 - 20;
xspeed = -xspeed;
} 如果(γ℃,){
Y = 0;
yspeed = -yspeed;
}否则如果(Y ==高度 - 20){
Y =身高 - 20;
yspeed = -yspeed;
} mk.drawOval(X,Y,20,20);}
公共静态无效的主要(字符串ARGS []){ 了java.awt.EventQueue.invokeLater(新的Runnable(){
公共无效的run(){
新NewJFrame()调用setVisible(真)。
}
});
}
}
进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类NewJFrame扩展的JFrame { 私人的JPanel JP;
私人定时器定时器; 公共NewJFrame(){
的initComponents(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationByPlatform(真);
timer.start();
} 公共无效的initComponents(){
ActionListener的人=新的ActionListener(){ 公共无效的actionPerformed(ActionEvent的EVT){
jp.repaint();
}
};
定时器=新定时器(50人); JP =新JPanel(){ INT X;
诠释Ÿ;
INT xspeed = 1;
INT yspeed = 1; 尺寸preferredSize =新尺寸(300,100); @覆盖
公共尺寸的get preferredSize(){
返回preferredSize;
} @覆盖
公共无效的paintComponent(图形G){
super.paintComponent方法(G);
this.move();
Graphics2D的G2 =(Graphics2D的)克;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawOval(X,Y,20,20);
} 无效移动(){
X = X + xspeed;
Y = Y + yspeed;
如果(X℃,){
X = 0;
xspeed = -xspeed;
}否则如果(X GT;的getWidth() - 20){
X =的getWidth() - 20;
xspeed = -xspeed;
} 如果(γ℃,){
Y = 0;
yspeed = -yspeed;
}否则如果(Y ==的getHeight() - 20){
Y =的getHeight() - 20;
yspeed = -yspeed;
}
}
};
jp.setBackground(Color.ORANGE); this.add(JP);
} 公共静态无效的主要(字符串ARGS []){
EventQueue.invokeLater(新的Runnable(){ 公共无效的run(){
新NewJFrame()调用setVisible(真)。
}
});
}
}
I am currently learning Java Animation and graphics in NetBeans.
I decided to start off with a simple ball movement in JPanel.
I am having some problem with fixing the flickering a flickering problem. I have looked at many forums but most were for AWT using Double Buffering,but I came to know that SWING components don't need Double Buffering. I tried - using repaint()
and .clearRect()
.
Out of the 2 I found that using .clearRect()
gave me better results, but not seamless flicker-free animation all the time.So I wanted to know if there is a better way to eliminate flickering.
Here is my code:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!