作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为这个类创建这个游戏,我的代码应该可以正常工作,并且x和y正在苹果图像上更新,但是图像在屏幕上没有移动...除屏幕更新外,其他所有方法都有效,我有repaint();我需要的每一个地方,但它没有更新。这些都是我使用的所有方法,并且都可以正常工作,只是我的代码没有更新屏幕
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (starting==true) {
try // load the images
{
imgMain=ImageIO.read(new File("MainMenu.png"));//Load the main menu image
imgRules=ImageIO.read(new File("Rules.png"));//load the rules image
imgLost=ImageIO.read(new File("LostMenu.png"));//load lost image
imgKid=ImageIO.read(new File("Kid.png"));//load kid image
imgBG=ImageIO.read(new File("Background.png"));//load background image
imgApple=ImageIO.read(new File("Apple.png"));//load apple image
imgBomb=ImageIO.read(new File("bomb.png"));//load bomb image
}
catch(IOException e)//if image is not found
{
System.out.println("could not find image");
g.drawString("Could not find images",100,100);
}
starting=false;
xBomb=(int) ((Math.random()*1100)+1);
xApple=(int) ((Math.random()*1100)+1);
}
if (Scene==1)//MainMenu Image
{
g.drawImage(imgMain, 0,0, 1100,700,this);
Life=5;
Score=0;
yApple=0;
yBomb=0;
}//end of MainMenu Scene
if (Scene==2)//Rules Image
{
g.drawImage(imgRules, 0,0, 1100,700,this);
}//End Of Rules Scene
if (Scene==3)//Game Image
{
g.drawImage(imgBG, 0,0, 1100,700,this);//show background
//start up values for character
g.drawString ("Lives = "+Life,1000,50);//Draw Lives left
g.drawString ("Score = "+Score,900,50);//Draw Score
g.drawImage(imgKid, x,y, 200,200,this);//draw character
if (Random==1)//if Random is = to bomb(2)
{
g.drawImage(imgBomb, xBomb,yBomb, 20,20,this);//show bomb
}
if (Random==2)//if Random is = to apple(1)
{
g.drawImage(imgApple, xApple,yApple, 20,20,this);//show apple
}
if(Life<=0)
{
Scene=4;
}
}//End Of Game
if (Scene==4)//Game Image
{
g.drawImage(imgLost, 0,0, 1100,700,this);
}//End Of Game
}
public void mouseClicked(MouseEvent e)
{
Graphics g = getGraphics();
int x=e.getX(); // get the x and y location of the mouse
int y=e.getY();
if (Scene==1)
{
if (x>=350 & x<=655 & y>=325 & y<=365)//Go To the rules
{
Scene=2;
repaint();
}
if (x>=348 & x<=626 & y>=192 & y<=223)// go to the game
{
Scene=3;
repaint();
}
}//end of Scene 1
if (Scene==2)
{
if (x>=330 & x<=640 & y>=390 & y<=413)//go back to MainMenu
{
Scene=1;
repaint();
}
}//end of Scene 2
if (Scene==4)
{
if (x>=380 & x<=550 & y>=250 & y<=280 )//go back to MainMenu
{
Scene=1;
repaint();
}
}//end of Scene 2
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
@Override
public void keyPressed(KeyEvent e) {
if (Scene==3)
{
int code = e.getKeyCode();
switch(code)
{
case KeyEvent.VK_RIGHT:
if (Score>=1500){
x=x+20;//slow down the character
}
if (Score<1500) {
x=x+40;
}
break;
case KeyEvent.VK_LEFT:
if (Score>=1500){
x=x-20;//slow down the character
}
if (Score<1500) {
x=x-40;
}
break;
}
}
}
public void actionPerformed(ActionEvent arg0) {
yApple++;
yBomb++;
if (Random==2) {
System.out.println(xApple+ " "+yApple);
if (yApple>=550)//if apple hits the ground
{
yApple=0;//bring apple back to the top
xApple=(int) ((Math.random()*1000)+1);//randomize new place for apple
Random=(int) ((Math.random()*2)+1);//randomize between apple and bomb
System.out.println(Random);
Life=Life-1;//remove 1 life
}
int HitDetection;
HitDetection = (int)Math.sqrt( Math.pow(xApple-x, 2.0)+Math.pow(yApple-y, 2.0));
if (HitDetection<=110)//if the apple hits the kid
{
yApple=0;//bring apple back to the top
xApple=(int) ((Math.random()*1000)+1);//reset apple place
Random=(int) ((Math.random()*2)+1);//randomize between apple and bomb
System.out.println(Random);
Score=Score+100;//add 100 to the score
}
}
if (Random==1) {
if (yBomb>=550)
{
yBomb=0;
xBomb=(int) ((Math.random()*1000)+1);
Random=(int) ((Math.random()*2)+1);
System.out.println(Random);
}
int HitDetection2 = 0;
HitDetection2 = (int)Math.sqrt( Math.pow(xBomb-x, 2.0)+Math.pow(yBomb-y, 2.0));
if (HitDetection2<=110)
{
yBomb=0;
xBomb=(int) ((Math.random()*1000)+1);
Random=(int) ((Math.random()*2)+1);
System.out.println(Random);
Life=Life-1;
}
repaint();
}
}
}
最佳答案
您会在使用revalidate()方法之前尝试repaint()吗?
像这样;
revalidate();
repaint();
关于java - 无法重绘Java中的场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59843148/
我是一名优秀的程序员,十分优秀!