Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用箭头键时,我的图像消失_Java_Animation_Applet_Keyboard_Awt - Fatal编程技术网

Java 使用箭头键时,我的图像消失

Java 使用箭头键时,我的图像消失,java,animation,applet,keyboard,awt,Java,Animation,Applet,Keyboard,Awt,问题是,当我按下向下按钮时,图像(蓝色男孩)消失,它也不会向上移动,但它可以左右移动,我不明白为什么?请帮忙。 (我正在使用RealJ)。 如果有一个网站,可以帮助我在二维侧滚游戏,也会帮助我 import java.applet.Applet; import java.awt.event.*; import java.awt.*; public class projectblue extends Applet implements ActionListener,KeyLi

问题是,当我按下向下按钮时,图像(蓝色男孩)消失,它也不会向上移动,但它可以左右移动,我不明白为什么?请帮忙。 (我正在使用RealJ)。 如果有一个网站,可以帮助我在二维侧滚游戏,也会帮助我

import java.applet.Applet;
import java.awt.event.*;     
import java.awt.*;  
public class projectblue extends Applet implements     ActionListener,KeyListener,MouseListener {

Image blueboy,trees;    //image variable
int size = 4,jump = 50, cx = 1, cy = 1;    

public void init() {

   blueboy = getImage(getDocumentBase(),"png.png"); 
   trees=     getImage(getDocumentBase(),"background.jpg");
addKeyListener( this ); 
addMouseListener( this );
}

public void paint(Graphics g)
{   
  int width = trees.getWidth(this);     
  int height = trees.getHeight(this);   
  //System.out.println("" + cx +","+ cy);   

  //The later images are drawn on top of the earlier ones.
  g.drawImage(trees, 1 ,1, width*size,height*7,this);
  g.drawImage(blueboy, cx, cy, this);
}

 public void actionPerformed(ActionEvent e) {
 repaint();

}
 public void left(){
      cx = cx -100;
      // blueboy moves left
}

public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
 System.out.println("key pressed");
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT) {
        left();
    }

    if (key == KeyEvent.VK_RIGHT) {
        cx = cx + 100;
        //blueboy moves right
    }

    if (key == KeyEvent.VK_UP) {
        while (cy >= 0 && cy <=1000)
        cy = cy - 100;
            if (cy < 0){
                cy = 1;
               //blueboy moves up, but if it goes out of bounds it goes back to the top
               }
    }

    if (key == KeyEvent.VK_DOWN) {
       while (cy >= 0 && cy <=1000)
        cy = cy + 100;
            if (cy > 1001){
                cy = 999;
               }
        //blueboy moves down, but if it goes out of bounds it goes back to the bottim
    }
    repaint();
}

public void keyReleased(KeyEvent e) {
}

public void mouseEntered( MouseEvent e ) { } 
public void mouseExited( MouseEvent e ) { }
 public void mousePressed( MouseEvent e ) { }
  public void mouseReleased( MouseEvent e ) { }
   public void mouseClicked( MouseEvent e ) {
   int x = getX();
   int y = getY();
System.out.println("clicked at (" + x + ", " + y + ")");
} }
import java.applet.applet;
导入java.awt.event.*;
导入java.awt.*;
公共类projectblue扩展小程序实现ActionListener、KeyListener、MouseStener{
Image blueboy,trees;//Image变量
int size=4,jump=50,cx=1,cy=1;
公共void init(){
blueboy=getImage(getDocumentBase(),“png.png”);
trees=getImage(getDocumentBase(),“background.jpg”);
addKeyListener(此);
addMouseListener(这个);
}
公共空间涂料(图g)
{   
int width=trees.getWidth(this);
int height=trees.getHeight(this);
//系统输出打印项次(“+cx+”,“+cy”);
//后面的图像是在前面的图像之上绘制的。
g、 drawImage(树,1,1,宽度*大小,高度*7,本);
g、 drawImage(蓝男孩、cx、cy、this);
}
已执行的公共无效操作(操作事件e){
重新油漆();
}
公共空间(左){
cx=cx-100;
//蓝男孩向左移动
}
public void keyTyped(KeyEvent e){
}
按下公共无效键(按键事件e){
System.out.println(“按键”);
int key=e.getKeyCode();
if(key==KeyEvent.VK_左){
左();
}
if(key==KeyEvent.VK_RIGHT){
cx=cx+100;
//蓝男孩向右移动
}
if(key==KeyEvent.VK_UP){
而(cy>=0&&cy=0&&cy 1001){
cy=999;
}
//蓝男孩向下移动,但如果超出边界,它会回到底部
}
重新油漆();
}
公共无效密钥已释放(密钥事件e){
}
公共无效mouseenterned(MouseEvent e){}
公共无效mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
公共无效MouseEvent e{}
公共无效mouseClicked(MouseEvent e){
intx=getX();
int y=getY();
System.out.println(“单击“+x+”,“+y+”);
} }

那些while循环将占用GUI线程,使动画变得无用。他们不应该是if blocks吗?此外,还应将所有块用大括号括起来。你的缩进在撒谎,大括号会告诉你原因

您还需要在绘制覆盖中调用
super.paint(g)
方法


此外,这是一项作业吗?如果是的话,你是否需要使用AWT/小程序而不是Swing进行编码?

我们班不教Swing。知道秋千有什么好处吗?@EdwardBezzant:啊。考虑建议你的辅导员继续到1990岁,因为这是当AWT变得过时的时候。如果将鼠标悬停在此处的Swing标记上:,请单击信息链接以获得指向优秀站点和资源的链接。这里有一个更直接的链接:.1)为什么要编写小程序?如果这是由于您的导师的规范,请参考他们。2) 为什么使用AWT组件而不是Swing?请参阅许多放弃AWT的好理由。1)一行空白总是足够的。
{
之后或
}
之前的空行通常也是多余的。2) 使用逻辑一致的代码格式样式!代码缩进旨在帮助人们遵循程序流程。