在使用JFrame的绘画时,我的显示闪烁很多。我不能让它停止,我很想知道如何让它停止闪烁。我也尝试在这里添加swing.timer,但它仍然没有做任何事情。 包游戏;
public class Play extends JFrame implements MouseMotionListener, MouseListener, KeyListener, ActionListener {
private int _xPad,_yPad,_xdir,_ydir;
private Brick[][] _bricks;
private Ball _gameBall;
private Timer _timer;
public Play(int[][] board){
super("Elemental Brick Breaker");
_xdir = 0;
_ydir = 0;
_xPad = 350;
_yPad = 500;
_timer = new Timer(1000,this);
_timer.start();
_gameBall = new FireBall(_xPad,_yPad);
_bricks = new Brick[8][10];
int x = 75, y = 40;
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 10 ; j++) {
_bricks[i][j] = new FireBrick(x,y);
x = x + 65;
}
y = y + 25;
x = 75;
}
}
public void paint(Graphics g) {
super.paint(g);
g.fillRect(_xPad, _yPad, 120, 10);
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 10 ; j++) {
g.fillRect(_bricks[i][j].getX(), _bricks[i][j].getY() , 60, 20);
}
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
请勿覆盖paint
或update
。绘画应使用轻量级组件完成并覆盖其paintComponent
方法。例如:
public class MyPanel extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
//custom painting
}
}
//elsewhere in your code - add the JPanel to the JFrame
MyPanel myPanel = new MyPanel();
JFrame frame = new JFrame();
frame.add(myPanel );
如果您希望重新绘制某种类型的事件,请在实例上调用重绘。例如,在ActionListener的实现中:
@Override
public void actionPerformed(ActionEvent e){
myPanel.repaint();
}
答案 1 :(得分:-1)
在你的绘画方法中,做
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
答案 2 :(得分:-1)
您可以尝试这种方法:
有一个单独的图像来绘制内容:
im=createImage(getWidth(), getHeight());
在构造函数中。
然后在油漆中:
Graphics g2=im.getGraphics();
并在g2上应用所有重命名为g的命令:
g2.fillRect etc
然后在g:
上绘制图像g.drawImage(im, 0, 0, this);
简而言之:
public void paint(Graphics g) {
Graphics g2=im.getGraphics();
g2.fillRect(_xPad, _yPad, 120, 10);
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 10 ; j++) {
g2.fillRect(_bricks[i][j].getX(), _bricks[i][j].getY() , 60, 20);
}
g.drawImage(im, 0, 0, this);
}
}
并有更新方法:
public void update(Graphics g) { paint(g); }
-
我已经简化了你的课程如下: - 它不会产生闪烁 - 如果我把它放回原处就会产生闪烁。
class Play extends JFrame implements ActionListener {
private int _xPad,_yPad,_xdir,_ydir;
private Rectangle[][] _bricks;
// private Ball _gameBall;
private javax.swing.Timer _timer;
Image im;
public Play(){
super("Elemental Brick Breaker");
setSize(1000, 500);
_xdir = 0;
_ydir = 0;
_xPad = 350;
_yPad = 500;
_timer = new javax.swing.Timer(1000,this);
_timer.start();
// _gameBall = new FireBall(_xPad,_yPad);
_bricks = new Rectangle[8][10];
int x = 75, y = 40;
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 10 ; j++) {
_bricks[i][j] = new Rectangle(x,y, 60, 20);
x = x + 65;
}
y = y + 25;
x = 75;
}
setVisible(true);
im=createImage(getWidth(), getHeight());
}
public void paint(Graphics g) {
Graphics g2=im.getGraphics();
g2.fillRect(_xPad, _yPad, 120, 10);
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 10 ; j++) {
g2.fillRect(_bricks[i][j].x, _bricks[i][j].y , 60, 20);
}
}
g.drawImage(im, 0, 0, this);
}
public void update(Graphics g) { paint(g); }
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
repaint();
System.out.println("t");
}
}