Java 我的JFrame闪烁
我正在尝试制作一个简单的java游戏——驾驶一辆汽车穿过跑道。我的老师让我习惯于使用矩形之类的东西。我的问题是,当我使用方法Java 我的JFrame闪烁,java,swing,jframe,repaint,Java,Swing,Jframe,Repaint,我正在尝试制作一个简单的java游戏——驾驶一辆汽车穿过跑道。我的老师让我习惯于使用矩形之类的东西。我的问题是,当我使用方法repaint()时,我的帧会闪烁。我试着在汽车转向时使用重新喷漆的方法,但没有任何效果。有什么建议吗 import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.
repaint()
时,我的帧会闪烁。我试着在汽车转向时使用重新喷漆的方法,但没有任何效果。有什么建议吗
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class MAP extends JFrame
{
//the URL and Img designed for the images
URL cLeft,cRight,cUp;
Image img1,img2,img3;
//these will keep track of each player’s speed:
double p1Speed =.5, p2Speed =.5;
//constant for the screen size and used for the drawing the terrain
final int WIDTH = 900, HEIGHT = 650;
//these are ints that represent directions:
final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;
//these will keep track of the player’s directions (default = up)
int p1Direction = 0;
//this is the rectangle for player 1’s (outer) car:
Rectangle p1 = new Rectangle(WIDTH/9,HEIGHT/2, WIDTH/30,WIDTH/30);
//draw the terrain
Rectangle left = new Rectangle(0,0,WIDTH/9,HEIGHT);
Rectangle right = new Rectangle((WIDTH/9)*9,0,WIDTH/9,HEIGHT);
Rectangle top = new Rectangle(0,0,WIDTH, HEIGHT/9);
Rectangle bottom = new Rectangle(0,(HEIGHT/9)*9,WIDTH,HEIGHT/9);
Rectangle center = new Rectangle((int)((WIDTH/9)*2.5),(int)((HEIGHT/9)*2.5), (int)((WIDTH/9)*5),(HEIGHT/9)*4);
//these obstacles will obstruct the path and make navigating harder
Rectangle obstacle = new
Rectangle(WIDTH/2,(int)((HEIGHT/9)*7),WIDTH/10,HEIGHT/9);
Rectangle obstacle2 = new
Rectangle(WIDTH/3,(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle3 = new
Rectangle(2*(WIDTH/3),(int)((HEIGHT/9)*5),WIDTH/10,HEIGHT/4);
Rectangle obstacle4 = new Rectangle(WIDTH/3,HEIGHT/9,WIDTH/30,HEIGHT/9);
Rectangle obstacle5 = new Rectangle(WIDTH/2,(int)((HEIGHT/9)*1.5),WIDTH/30,HEIGHT/4);
Rectangle finish = new Rectangle(WIDTH/9,(HEIGHT/2)-HEIGHT/9,(int)((WIDTH/9)*1.5),HEIGHT/70);
Rectangle lineO=new Rectangle(WIDTH/9,HEIGHT/2,(int)((WIDTH/9)*1.5)/2,HEIGHT/140);
Rectangle lineI = new Rectangle(((WIDTH/9)+((int)((WIDTH/9)*1.5)/2)),(HEIGHT/2)+(HEIGHT/10),(int)((WIDTH/9)*1.5)/2, HEIGHT/140);
//the constructor:
public MAP() {
//the following code creates the JFrame
super("Radical Racing");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
try {
cUp = this.getClass().getResource("carUp.jpg");
cLeft = this.getClass().getResource("carLeft.jpg");
cRight = this.getClass().getResource("carRight.jpg");
} catch(Exception e) {
}
//attach the URLs to the images
img1 = Toolkit.getDefaultToolkit().getImage(cUp);
img2 = Toolkit.getDefaultToolkit().getImage(cLeft);
img3 = Toolkit.getDefaultToolkit().getImage(cRight);
repaint();
Move1 m1 = new Move1();
m1.start();
}
//this will draw the cars and the race track
public void paint(Graphics g) {
super.paint(g);
//draw the background for the racetrack
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,WIDTH,HEIGHT);
//when we draw, the border will be green
g.setColor(Color.GREEN);
//fill rectangle
g.fillRect(left.x,left.y,left.width,left.height);
g.fillRect(right.x,right.y,right.width,right.height);
g.fillRect(top.x,top.y,top.width,top.height);
g.fillRect(bottom.x,bottom.y,bottom.width,bottom.height);
g.fillRect(center.x,center.y,center.width,center.height);
g.fillRect(obstacle.x,obstacle.y,obstacle.width,obstacle.height);
g.fillRect(obstacle2.x,obstacle2.y,obstacle2.width,obstacle2.height);
g.fillRect(obstacle3.x,obstacle3.y,obstacle3.width,obstacle3.height);
g.fillRect(obstacle4.x,obstacle4.y,obstacle3.width,obstacle4.height);
g.fillRect(obstacle5.x,obstacle5.y,obstacle5.width,obstacle5.height);
//set the starting line color to white
g.setColor(Color.WHITE);
//now draw the starting line
g.fillRect(lineO.x,lineO.y,lineO.width,lineO.height);
g.fillRect(lineI.x,lineI.y,lineI.width,lineI.height);
//set the color of the finish line to yellow
g.setColor(Color.YELLOW);
//now draw the finish line
g.fillRect(finish.x,finish.y,finish.width,finish.height);
//set the color to blue for p1
g.setColor(Color.WHITE);
//now draw the actual player
g.fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//draw the images for the player
if(p1Direction==UP)
g.drawImage(img1,p1.x,p1.y,this);
if(p1Direction==LEFT)
g.drawImage(img2,p1.x,p1.y,this);
if(p1Direction==RIGHT)
g.drawImage(img3,p1.x,p1.y,this);
}
private class Move1 extends Thread implements KeyListener {
public void run() {
//now, this should all be in an infinite loop, so the process
//repeats
addKeyListener(this);
while(true) {
//now, put the code in a try block. This will let the
//program exit
//if there is an error.
try {
//first, refresh the screen:
repaint();
//increase speed a bit
// if(p1Speed<=5)
// p1Speed+=.2;
//p1.y-=(int) p1Speed;
if(p1.intersects(left) || p1.intersects(right) || p1.intersects(top) || p1.intersects(bottom) || p1.intersects(obstacle) || p1.intersects(obstacle2)|| p1.intersects(obstacle3) || p1.intersects(obstacle4) || p1.intersects(obstacle5)) {
p1Speed = -5;
}
//if the car hits the center, do the same as above
//but make the speed -2.5.
if(p1.intersects(center)) {
p1Speed = -5;
}
//increase speed a bit
if(p1Speed<=5)
p1Speed+=.2;
//these will move the player based on direction
if(p1Direction==UP) {
p1.y-=(int)p1Speed;
//repaint();
}
if(p1Direction==DOWN) {
p1.y+=(int)p1Speed;
//repaint();
}
if(p1Direction==LEFT) {
p1.x-=(int)p1Speed;
//repaint();
}
if(p1Direction==RIGHT) {
p1.x+=(int)p1Speed;
//repaint();
}
//this delays the refresh rate:
Thread.sleep(200);
} catch(Exception e) {
//if there is an exception (an error), exit the loop.
break;
}
}
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent event) {
if(event.getKeyChar()=='a') {
p1Direction = LEFT;
repaint();
}
if(event.getKeyChar()=='s') {
p1Direction = DOWN;
repaint();
}
if(event.getKeyChar()=='d') {
p1Direction = RIGHT;
repaint();
}
if(event.getKeyChar()=='w') {
p1Direction = UP;
repaint();
}
}
}
//this starts the program by calling the constructor:
public static void main (String[ ] args) {
new MAP();
}
}
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.Rectangle;
导入java.awt.Toolkit;
导入java.awt.event.KeyEvent;
导入java.awt.event.KeyListener;
导入java.net.URL;
导入javax.swing.JFrame;
@抑制警告(“串行”)
公共类映射扩展了JFrame
{
//为图片设计的URL和Img
URL谱号,cRight,cUp;
图像img1、img2、img3;
//这些将跟踪每个玩家的速度:
双p1速度=.5,p2速度=.5;
//屏幕大小恒定,用于绘制地形
最终内部宽度=900,高度=650;
//这些是表示方向的整数:
最终整数向上=0,右=1,向下=2,左=3;
//这些将跟踪玩家的方向(默认值=向上)
int p1方向=0;
//这是玩家1(外部)汽车的矩形:
矩形p1=新矩形(宽度/9,高度/2,宽度/30,宽度/30);
//绘制地形图
左矩形=新矩形(0,0,宽度/9,高度);
右矩形=新矩形((宽度/9)*9,0,宽度/9,高度);
矩形顶部=新矩形(0,0,宽度,高度/9);
矩形底部=新矩形(0,(高度/9)*9,宽度,高度/9);
矩形中心=新矩形((int)((宽度/9)*2.5),(int)((高度/9)*2.5),(int)((宽度/9)*5),(高度/9)*4);
//这些障碍物将阻碍道路,使航行更加困难
矩形障碍物=新
矩形(宽度/2,(内部)(高度/9)*7),宽度/10,高度/9);
矩形obstacle2=新建
矩形(宽度/3,(内部)(高度/9)*5),宽度/10,高度/4);
矩形obstacle3=新建
矩形(2*(宽度/3),(整数)((高度/9)*5),宽度/10,高度/4);
矩形obstacle4=新矩形(宽度/3,高度/9,宽度/30,高度/9);
矩形障碍5=新矩形(宽度/2,(整数)((高度/9)*1.5),宽度/30,高度/4);
矩形饰面=新矩形(宽度/9,(高度/2)-高度/9,(内部)(宽度/9)*1.5),高度/70);
矩形线条o=新矩形(宽度/9,高度/2,(整数)((宽度/9)*1.5)/2,高度/140);
矩形线条i=新矩形((宽度/9)+(整型)((宽度/9)*1.5)/2),(高度/2)+(高度/10),(整型)((宽度/9)*1.5)/2,高度/140);
//建造商:
公共地图(){
//下面的代码创建JFrame
超级(“激进赛车”);
设置尺寸(宽度、高度);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
可设置大小(假);
试一试{
cUp=this.getClass().getResource(“carUp.jpg”);
cLeft=this.getClass().getResource(“carLeft.jpg”);
cRight=this.getClass().getResource(“carRight.jpg”);
}捕获(例外e){
}
//将URL附加到图像
img1=Toolkit.getDefaultToolkit().getImage(cUp);
img2=Toolkit.getDefaultToolkit().getImage(cLeft);
img3=Toolkit.getDefaultToolkit().getImage(cRight);
重新油漆();
Move1 m1=新的Move1();
m1.start();
}
//这将吸引赛车和赛道
公共空间涂料(图g){
超级油漆(g);
//绘制赛马场的背景
g、 setColor(颜色:深灰色);
g、 fillRect(0,0,宽度,高度);
//当我们画图时,边界将是绿色的
g、 setColor(Color.GREEN);
//填充矩形
g、 fillRect(left.x,left.y,left.width,left.height);
g、 fillRect(right.x,right.y,right.width,right.height);
g、 fillRect(top.x,top.y,top.width,top.height);
g、 fillRect(底部.x,底部.y,底部.宽度,底部.高度);
g、 fillRect(center.x,center.y,center.width,center.height);
g、 fillRect(障碍物.x,障碍物.y,障碍物.宽度,障碍物.高度);
g、 fillRect(obstacle2.x、obstacle2.y、obstacle2.width、obstacle2.height);
g、 fillRect(obstacle3.x、obstacle3.y、obstacle3.width、obstacle3.height);
g、 fillRect(obstacle4.x、obstacle4.y、obstacle3.width、obstacle4.height);
g、 fillRect(obstacle5.x、obstacle5.y、obstacle5.width、obstacle5.height);
//将起始线颜色设置为白色
g、 setColor(Color.WHITE);
//现在画起跑线
g、 fillRect(线条x、线条y、线条宽度、线条高度);
g、 fillRect(lineI.x、lineI.y、lineI.width、lineI.height);
//将终点线的颜色设置为黄色
g、 setColor(颜色为黄色);
//现在画终点线
g、 fillRect(finish.x,finish.y,finish.width,finish.height);
//将p1的颜色设置为蓝色
g、 setColor(Color.WHITE);
//现在画出实际的玩家
g、 fill3DRect(p1.x,p1.y,p1.width,p1.height,true);
//为玩家绘制图像
如果(p1方向==向上)
g、 drawImage(img1,p1.x,p1.y,this);
如果(p1方向==左)
g、 drawImage(img2、p1.x、p1.y、this);
如果(p1方向==右侧)
g、 drawImage(img3,p1.x,p1.y,this);
}
私有类Move1扩展线程实现KeyListener{
公开募捐{
//现在,这一切都应该在一个无限循环中,所以这个过程
//重复
addKeyListener(此);
while(true){
//现在,将代码放在一个try块中
//程序退出
//如果有错误。
试一试{
//首先,刷新屏幕:
重新油漆();
//把速度提高一点
//if(p1Speed您可以通过使用双缓冲技术避免图形界面上的闪烁。这篇文章给出了双缓冲的一个示例。
本部分还提供了进一步的建议