Java 如何消除重新喷漆时的屏幕闪烁?
我用java制作了一个简单的snake游戏,我遇到了一个问题,每当屏幕刷新时,屏幕就会闪烁。为了减少和/或消除这种情况,我是否应该了解/可以实施什么Java 如何消除重新喷漆时的屏幕闪烁?,java,swing,Java,Swing,我用java制作了一个简单的snake游戏,我遇到了一个问题,每当屏幕刷新时,屏幕就会闪烁。为了减少和/或消除这种情况,我是否应该了解/可以实施什么 public class SnakeTest extends JFrame { private boolean GAMEOVER=false; //Detect if user has a gameover private int score=-1;//Keeps the score ie// number of apples
public class SnakeTest extends JFrame
{
private boolean GAMEOVER=false; //Detect if user has a gameover
private int score=-1;//Keeps the score ie// number of apples eaten
MoveDown s=new MoveDown();
MoveRight q=new MoveRight();
MoveLeft r=new MoveLeft();
MoveUp t=new MoveUp();
private boolean LEFT = false;
private boolean RIGHT = false;
private boolean UP = false;
private boolean DOWN = false;
Timer downtimer = new Timer(10, s);
Timer righttimer=new Timer(10, q);
Timer lefttimer=new Timer(10, r);
Timer uptimer=new Timer(10, t);
Font gameoverfont= new Font("Helvetica", Font.BOLD, 70);
Font scorefont=new Font("Helvetica", Font.BOLD,30);
//defines the snake
int snakex=325;
int snakey=340;
Rectangle SNAKE=new Rectangle(snakex, snakey,15,15);
//Defines the tail
Rectangle[] tails=new Rectangle[999];
int[] tailx=new int[999];
int[] taily=new int[999];
//Defines the apple
int applex=400;
int appley=400;
Rectangle apple=new Rectangle(applex,appley,15,15);
Random ran=new Random();//Used for apple placement
public SnakeTest()
{
super("SNAKE! By Cody Antcliffe");
addKeyListener(new TAdapter());
}
public void paint(Graphics G)
{
if(!GAMEOVER)
{
StopAtTail();
StopAtWalls();
EatApple();
G.setColor(Color.BLACK);
G.fillRect(0, 0, 800, 700);
G.setColor(Color.WHITE);
G.fillRect(50, 75, 700, 575);
Graphics2D g2= (Graphics2D)G;
g2.setColor(Color.GREEN);
g2.fill(SNAKE);
g2.setColor(Color.RED);
g2.fill(apple);
g2.setColor(Color.DARK_GRAY);
for(int i=0; i<=score;i++)
g2.fill(tails[i]);
}
else
{
G.setColor(Color.WHITE);
G.fillRect(0, 0, 800, 700);
G.setColor(Color.BLACK);
G.fillRect(0, 0, 800, 200);
G.fillRect(0, 500, 800, 200);
G.setColor(Color.RED);
G.setFont(gameoverfont);
G.drawString("Game Over!", 200, 370);
G.setFont(scorefont);
G.setColor(Color.BLACK);
Integer.toString(score);
G.drawString("Score: "+score/2, 290, 450);
}
}//end paint()
public static void main(String[] args)
{
JFrame newframe=new SnakeTest();
newframe.setIconImage((new ImageIcon("snake.png")).getImage());
newframe.setSize(800,700);
newframe.setVisible(true);
}
//handles movement down
public class MoveDown implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//Algorithm for tail movement
tails[0]=new Rectangle(snakex, snakey,15,15);
tailx[0]=snakex;
taily[0]=snakey;
for(int i=score;i>0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
tails[i]=tails[i-1];
}
//
snakey+=5;
SNAKE=new Rectangle(snakex, snakey,15,15);
repaint();
}
}//end MoveDown
//handles movement up
public class MoveUp implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//Algorithm for tail movement
tails[0]=new Rectangle(snakex, snakey,15,15);
tailx[0]=snakex;
taily[0]=snakey;
for(int i=score;i>0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
tails[i]=tails[i-1];
}
//
snakey-=5;
SNAKE=new Rectangle(snakex, snakey,15,15);
repaint();
}
}//end MoveUp
//handles movement right
public class MoveRight implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//Algorithm for tail movement
tails[0]=new Rectangle(snakex, snakey,15,15);
tailx[0]=snakex;
taily[0]=snakey;
for(int i=score;i>0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
tails[i]=tails[i-1];
}
//
snakex+=5;
SNAKE=new Rectangle(snakex, snakey,15,15);
repaint();
}
}//MoveRight
//handles movement left
public class MoveLeft implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//Algorithm for tail movement
tails[0]=new Rectangle(snakex, snakey,15,15);
tailx[0]=snakex;
taily[0]=snakey;
for(int i=score;i>0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
tails[i]=tails[i-1];
}
//
snakex-=5;
SNAKE=new Rectangle(snakex, snakey,15,15);
repaint();
}
}//end MoveLeft
//Class for handling the arrow key presses
private class TAdapter extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
int direction = e.getKeyCode();
if ((direction == KeyEvent.VK_LEFT) && (!RIGHT)&&(!GAMEOVER))
{
LEFT = true;
UP = false;
DOWN = false;
downtimer.stop();
uptimer.stop();
lefttimer.start();
}
if ((direction == KeyEvent.VK_RIGHT) && (!LEFT)&&(!GAMEOVER))
{
RIGHT = true;
UP = false;
DOWN = false;
downtimer.stop();
uptimer.stop();
righttimer.start();
}
if ((direction == KeyEvent.VK_UP) && (!DOWN)&&(!GAMEOVER))
{
UP = true;
RIGHT = false;
LEFT = false;
righttimer.stop();
lefttimer.stop();
uptimer.start();
}
if ((direction == KeyEvent.VK_DOWN) && (!UP)&&(!GAMEOVER))
{
DOWN = true;
RIGHT = false;
LEFT = false;
righttimer.stop();
lefttimer.stop();
downtimer.start();
}
}
}//end arrow ActionHandler
//Collision Detection Methods
//Detects when you hit a wall
public void StopAtWalls()
{
if(snakex<=45)
{
lefttimer.stop();
GAMEOVER=true;
repaint();
}
else if(snakex>=735)
{
righttimer.stop();
GAMEOVER=true;
repaint();
}
else if(snakey>=635)
{
GAMEOVER=true;
repaint();
downtimer.stop();
}
else if(snakey<=70)
{
GAMEOVER=true;
repaint();
uptimer.stop();
}
} //End StopAtWalls()
public void StopAtTail()
{
for(int i=5; i<=score; i++)
if(snakex-5<=tailx[i]&&snakex+5>=tailx[i]&&snakey-5<=taily[i]&&snakey+5>=taily[i])
GAMEOVER=true;
} //End StopAtTail()
//End Collision Detection Methods
//Detects when an apple is eaten
public void EatApple()
{
if(snakex-10<=applex&&snakex+15>=applex&&snakey-10<=appley&&snakey+15>=appley)
{
applex=gen_randomx();
appley=gen_randomy();
apple=new Rectangle(applex, appley,15,15);
score+=2; //Increase by two so that the tail grows quicker
//Will be corrected by dividing the final score by 2
repaint();
}
}//End EatApple)
/* Get new apple location*/
//generates an x value to move apple around
public int gen_randomx()
{
int Low = 50;
int High = 730;
int x = ran.nextInt(High-Low) + Low;
return x;
}
//generates a y value to move apple around
public int gen_randomy()
{
int Low = 80;
int High = 630;
int y = ran.nextInt(High-Low) + Low;
return y;
}
}//end main class SnakeTest
公共类SnakeTest扩展JFrame
{
私有布尔GAMEOVER=false;//检测用户是否有GAMEOVER
private int score=-1;//保留分数,即//吃的苹果数
MoveDown s=新的MoveDown();
MoveRight q=新的MoveRight();
MoveLeft r=新的MoveLeft();
MoveUp t=新的MoveUp();
私有布尔左=假;
私有布尔右=假;
private boolean UP=false;
私有布尔向下=false;
定时器停机定时器=新定时器(10秒);
定时器righttimer=新定时器(10,q);
定时器lefttimer=新定时器(10,r);
定时器正常运行时间=新定时器(10,t);
Font gameoverfont=新字体(“Helvetica”,Font.BOLD,70);
Font scorefont=新字体(“Helvetica”,字体粗体,30);
//定义蛇
int snakex=325;
int snakey=340;
矩形蛇=新矩形(蛇,蛇,15,15);
//定义尾部
矩形[]尾部=新矩形[999];
int[]tailx=新int[999];
int[]taily=新int[999];
//苹果的定义
int applex=400;
int appley=400;
矩形苹果=新矩形(applex,appley,15,15);
Random ran=new Random();//用于放置苹果
公开考试
{
超级(“科迪·安特克利夫的蛇”);
addKeyListener(新的TAdapter());
}
公共空间涂料(图G)
{
如果(!GAMEOVER)
{
StopAtTail();
StopAtWalls();
吃苹果();
G.setColor(颜色为黑色);
G.fillRect(0,0800700);
G.setColor(颜色为白色);
G.fillRect(50,75700575);
图形2d g2=(图形2d)G;
g2.设置颜色(颜色为绿色);
g2.填充(蛇);
g2.设置颜色(颜色为红色);
g2.填充(苹果);
g2.设置颜色(颜色:深灰色);
对于(int i=0;i0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
尾[i]=尾[i-1];
}
//
snakey+=5;
SNAKE=新矩形(snakex,snakey,15,15);
重新油漆();
}
}//端部向下移动
//处理向上的移动
公共类MoveUp实现ActionListener
{
已执行的公共无效操作(操作事件)
{
//尾部运动算法
tails[0]=新矩形(snakex,snakey,15,15);
tailx[0]=蛇;
taily[0]=snakey;
对于(int i=分数;i>0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
尾[i]=尾[i-1];
}
//
snakey-=5;
SNAKE=新矩形(snakex,snakey,15,15);
重新油漆();
}
}//端部上移
//正确处理移动
公共类MoveRight实现ActionListener
{
已执行的公共无效操作(操作事件)
{
//尾部运动算法
tails[0]=新矩形(snakex,snakey,15,15);
tailx[0]=蛇;
taily[0]=snakey;
对于(int i=分数;i>0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
尾[i]=尾[i-1];
}
//
蛇x+=5;
SNAKE=新矩形(snakex,snakey,15,15);
重新油漆();
}
}//右移
//处理向左移动
公共类MoveLeft实现ActionListener
{
已执行的公共无效操作(操作事件)
{
//尾部运动算法
tails[0]=新矩形(snakex,snakey,15,15);
tailx[0]=蛇;
taily[0]=snakey;
对于(int i=分数;i>0;i--)
{
tailx[i]=tailx[i-1];
taily[i]=taily[i-1];
尾[i]=尾[i-1];
}
//
蛇-=5;
SNAKE=新矩形(snakex,snakey,15,15);
重新油漆();
}
}//左移结束
//用于处理箭头键按下的类
私有类TAdapter扩展了KeyAdapter
{
按下公共无效键(按键事件e)
{
int direction=e.getKeyCode();
如果((direction==KeyEvent.VK_LEFT)&&(!RIGHT)&(!GAMEOVER))
{
左=真;
向上=错误;
向下=假;
downtimer.stop();
uptimer.stop();
lefttimer.start();
}
如果((direction==KeyEvent.VK_RIGHT)&&(!LEFT)&(!GAMEOVER))
{
右=真;
向上=错误;
向下=假;
downtimer.stop();
uptimer.stop();
righttimer.start();
}
如果((方向==KeyEvent.VK_UP)&&&(!DOWN)&(!GAMEOVER))
{
向上=真;
右=假;
左=假;
righttimer.stop();
lefttimer.stop();
uptimer.start();
}
如果((方向==KeyEvent.VK_DOW
public class SnakeComponent extends JComponent {
...
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
}