Java 我的计时器设置了镜头移动到帧顶部的动画,我的逻辑错误在哪里?

Java 我的计时器设置了镜头移动到帧顶部的动画,我的逻辑错误在哪里?,java,swing,jpanel,paintcomponent,keylistener,Java,Swing,Jpanel,Paintcomponent,Keylistener,我正在做一个简单的太空入侵者类型的游戏,当我试图使用计时器来设置向上发射的子弹的动画时,我犯了一个错误。我是否应该重新编写代码,以便将快照添加到单独的面板,然后将该面板添加到帧中的主面板?或者我可以保持原样,只是清理代码并稍微修改一下逻辑 这是我完成的代码: public static void main(String[] args) throws IOException { GameTest t = new GameTest(); } public static class G

我正在做一个简单的太空入侵者类型的游戏,当我试图使用计时器来设置向上发射的子弹的动画时,我犯了一个错误。我是否应该重新编写代码,以便将快照添加到单独的面板,然后将该面板添加到帧中的主面板?或者我可以保持原样,只是清理代码并稍微修改一下逻辑

这是我完成的代码:

   public static void main(String[] args) throws IOException {
    GameTest t = new GameTest();
}

public static class GameTest extends JFrame {

    private static final int WINDOW_WIDTH = 800;
    private static final int WINDOW_HEIGHT = 500;
    public static GamePanel gamePanel;

    public GameTest() throws IOException {
        super("Deep Fried Freedom");
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setLayout(new BorderLayout());
        gamePanel = new GamePanel();
        add(gamePanel);
        center(this);
        setVisible(true);
        this.addKeyListener(new aKeyListener());
        this.setFocusable(true);

    }

    public void center(JFrame frame) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point center = ge.getCenterPoint();

        int w = frame.getWidth();
        int h = frame.getHeight();

        int x = center.x - w / 2, y = center.y - h / 2;
        frame.setBounds(x, y, w, h);
        frame.validate();
    }//end of center method  

    public class aKeyListener implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
        }//end empty keyTyped method

        @Override
        public void keyPressed(KeyEvent e) {
            if (Launcher.lxCoord == 0 || Launcher.lxCoord == 735) {   //ensure the launcher can't leave the frame
                Launcher.lRun *= -1;
            } else {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_A : Launcher.lRun = -5; break;
                    case KeyEvent.VK_D : Launcher.lRun = 5; break;
                    case KeyEvent.VK_ENTER :
                        gamePanel.numShots++; gamePanel.createShots();
                    break;
                    default: Launcher.lRun = 0;
                }
            }
            gamePanel.move(gamePanel);
        }//end keyPressed method

        @Override
        public void keyReleased(KeyEvent e) {
        }//end empty keyReleased method

    }//end aKeyListener class

}//end GameTest class

}// end main class


public class GamePanel extends JPanel {

Launcher launcher1;
Background bground1;
public static ArrayList<Shot> shots;
public int numShots;
public static int counter;


public GamePanel() throws IOException {
    super();
    this.shots = new ArrayList<>();
    this.numShots = 0;
    launcher1 = new Launcher();
    bground1 = new Background();
}//end constructor

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(bground1.background, 0, 0, getWidth(), getHeight(), null);
    g.drawImage(launcher1.baldEagleImage, launcher1.getLxCoord(), launcher1.lyCoord, null);//paint the launcher
    while (counter == 1) {
        for (int i = 0; i < numShots; i++) {
            g.drawImage(shots.get(i).mcDShotImage, shots.get(i).staticXLauncherCoord, shots.get(i).getSyCoord(), null);
        }
    }
}//end paintComponent method

public void move(GamePanel gamePanel) {
    launcher1.moveX();
    if (numShots > 0) {
        moveShot();
    }
    repaint();
}//end move method

public void moveShot() {
    for (int i = 0; i < numShots; i++) {//loop to move all the shots
        if (shots.get(i).getSyCoord() > 10) {
            counter = 1;
            shots.get(i).moveY();
            repaint();
        } else {
            counter = 0;
            numShots--;
            shots.remove(i);
            repaint();
        }
    }
}//end shot method

public void createShots() {
    try {
        for (int j = 0; j < numShots; j++) {
            shots.add(new Shot());
        }
    } catch (IOException | IndexOutOfBoundsException e) {
        System.out.println("caught an exception" + e);
    }
}

}//end GamePanel class


public class Shot {

public int syCoord;
public int sRise = 5;
public BufferedImage mcDShotImage;
GamePanel gPanel;
public static int staticXLauncherCoord;
private Timer timer;

public Shot() throws IOException {
    timer = new Timer(20, new TimerListener());
    staticXLauncherCoord = Launcher.getLxCoord() + 10;
    syCoord = 381;
    mcDShotImage = ImageIO.read(new File("mcdonaldsarchesshot.jpg"));
}//end constructor

public void moveY() {
    syCoord -= sRise;
    setSyCoord(syCoord);
}//end moveY method

public void setSyCoord(int syCoord) {
    this.syCoord = syCoord;
}

public int getSyCoord() {
    return this.syCoord;
}

public class TimerListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < GamePanel.shots.size(); i++) {
            moveY();
        }
    }//end actionPerformed method

}//end TimerListener class

}//end Shot class


public class Launcher {

public static int lxCoord;        //the launcher's x coordinate
public static final int lyCoord = 415;
public static int lRun = 0;           //the launcher's x change
public static BufferedImage baldEagleImage;

//Constructor
public Launcher() throws IOException {
    lxCoord = 350;
    baldEagleImage = ImageIO.read(new File("baldeagleimage.jpg"));
}

/**
 * The movement of the launcher in the x direction
 */
public void moveX() {
    lxCoord += lRun;
    setLxCoord(lxCoord);
}//end moveX method

public void setLxCoord(int lxCoord) {
    this.lxCoord = lxCoord;
}

public static int getLxCoord() {
    return lxCoord;
}

}//end Launcher class
publicstaticvoidmain(字符串[]args)引发IOException{
GameTest t=新GameTest();
}
公共静态类GameTest扩展JFrame{
专用静态最终整数窗口\u宽度=800;
专用静态最终内窗高度=500;
公共静态游戏面板;
public GameTest()引发IOException{
超级(“油炸自由”);
可设置大小(假);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置尺寸(窗宽、窗高);
setLayout(新的BorderLayout());
gamePanel=新的gamePanel();
添加(游戏面板);
中心(本);
setVisible(真);
this.addKeyListener(新的aKeyListener());
此参数为.setFocusable(true);
}
公共空隙中心(JFrame){
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
Point center=ge.getCenterPoint();
int w=frame.getWidth();
inth=frame.getHeight();
INTX=中心.x-w/2,y=中心.y-h/2;
框架立根(x,y,w,h);
frame.validate();
}//中心终止法
公共类aKeyListener实现KeyListener{
@凌驾
public void keyTyped(KeyEvent e){
}//结束空键类型化方法
@凌驾
按下公共无效键(按键事件e){
如果(Launcher.lxCoord==0 | | Launcher.lxCoord==735){//确保启动器不能离开帧
Launcher.lRun*=-1;
}否则{
开关(如getKeyCode()){
case KeyEvent.VK_A:Launcher.lRun=-5;break;
case KeyEvent.VK_D:Launcher.lRun=5;中断;
case KeyEvent.VK_输入:
gamePanel.numShots++;gamePanel.createShots();
打破
默认值:Launcher.lRun=0;
}
}
移动(游戏面板);
}//末端按键法
@凌驾
公共无效密钥已释放(密钥事件e){
}//结束空键释放方法
}//结束aKeyListener类
}//结束游戏测试类
}//结束主课
公共类游戏面板扩展了JPanel{
发射装置1;
背景1;
公共静态数组列表快照;
公共国际合照;
公共静态计数器;
public GamePanel()引发IOException{
超级();
this.shots=新的ArrayList();
this.numShots=0;
launcher1=新的启动器();
bground1=新背景();
}//端构造函数
@凌驾
受保护组件(图形g){
超级组件(g);
g、 drawImage(bground1.background,0,0,getWidth(),getHeight(),null);
g、 drawImage(launcher1.baldEagleImage,launcher1.getLxCoord(),launcher1.lyCoord,null);//绘制启动器
while(计数器==1){
对于(int i=0;i0){
moveShot();
}
重新油漆();
}//端移法
公共空间移动快照(){
对于(int i=0;i10){
计数器=1;
shots.get(i.moveY();
重新油漆();
}否则{
计数器=0;
裸照--;
删除(i);
重新油漆();
}
}
}//端射法
公共void createShots(){
试一试{
对于(int j=0;j
问题#1
while(counter==1){
中的
paintComponent
将阻止事件分派