Java 简单小程序游戏错误

Java 简单小程序游戏错误,java,swing,applet,keylistener,repaint,Java,Swing,Applet,Keylistener,Repaint,我的小程序有问题。它的目的是创建一个游戏,在这个游戏中,你可以移动屏幕底部的一个积木,以穿过积木上从上面掉下来的洞。我已经实现了块,但当我按下一个键时,我无法使块移动。 以下是我的源代码: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MathewBorumGame extends JApplet { public void init() { Dimen

我的小程序有问题。它的目的是创建一个游戏,在这个游戏中,你可以移动屏幕底部的一个积木,以穿过积木上从上面掉下来的洞。我已经实现了块,但当我按下一个键时,我无法使块移动。 以下是我的源代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MathewBorumGame extends JApplet
{
    public void init()
    {
        Dimension dim = getSize();
        MathewBorumGamePanel avoidance = new MathewBorumGamePanel(dim);
        add(avoidance);
        Thread game = new Thread(avoidance);
        game.start();
    }
}// end of the extended JApplet class

class MathewBorumGamePanel extends JPanel implements Runnable{
    protected int x = 225;
    protected int y = 450;
    private int speed = 0;
    private boolean isPlaying = false;
    private boolean hasHit = false;
    private String instructions = "Press 'Enter' to start survive as long as"
        + " as possible.";
    private String keyInstructions = "Use arrow keys to move.";
    private String test;
    private Dimension dim = null;
    private Thread game = null;
    private Timer timer = new Timer(1000, new TimerListener());
    public MathewBorumGamePanel(Dimension paramDimension) {
        this.dim = paramDimension;
        setBackground(new Color(255, 255, 204));
        setForeground(Color.black);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                        switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        if(x >= 10) x = x - 10;
                        repaint();
                        break;
                    case KeyEvent.VK_RIGHT:
                        if(x <= 490) x = x + 10;
                        repaint();
                        break;
                    default:
                        repaint();
                        break;
                }
            }
        });
    }
    public void run() {
        for (;;) {
            repaint();
        }
    }
    public void paintComponent(Graphics paramGraphics) {
        super.paintComponent(paramGraphics);
        paramGraphics.fillRect(x, y, 50, 15);
        paramGraphics.setFont(new Font("Serif", Font.BOLD, 15));
        paramGraphics.drawString(this.instructions, 30, 30);
        paramGraphics.drawString(this.keyInstructions, 30, 50);
        test = test.valueOf(x);
        paramGraphics.drawString(test, 30, 70);
    }

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    }
}//end of the extended JPanel class
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类MathewBorumGame扩展了JApplet
{
公共void init()
{
维度dim=getSize();
MathewBorumGamePanel回避=新的MathewBorumGamePanel(dim);
添加(避免);
线程游戏=新线程(回避);
game.start();
}
}//扩展JApplet类的结束
类MathewBorumGamePanel扩展了JPanel实现可运行{
保护整数x=225;
受保护整数y=450;
私用整数速度=0;
私有布尔isplay=false;
私有布尔hasHit=false;
private String instructions=“按“回车”开始生存,只要”
+“尽可能。”;
private String keyInstructions=“使用箭头键移动。”;
私有字符串测试;
私有维度dim=null;
私有线程game=null;
私人定时器=新定时器(1000,新定时器侦听器());
public MathewBorumGamePanel(维度参数维度){
this.dim=参数维度;
挫折背景(新颜色(255、255、204));
设置前景(颜色为黑色);
addKeyListener(新的KeyAdapter(){
按下公共无效键(按键事件e){
开关(如getKeyCode()){
case KeyEvent.VK_左:
如果(x>=10)x=x-10;
重新油漆();
打破
case KeyEvent.VK_RIGHT:

如果(x请参阅代码中的注释以了解更多提示

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// <applet code='MathewBorumGame' width=400 height=400></applet>
public class MathewBorumGame extends JApplet
{
    public void init()
    {
        Dimension dim = getSize();
        MathewBorumGamePanel avoidance = new MathewBorumGamePanel(dim);
        add(avoidance);
        /*
        Thread game = new Thread(avoidance);
        game.start();*/
    }
}// end of the extended JApplet class

class MathewBorumGamePanel extends JPanel { //implements Runnable{
    protected int x = 225;
    protected int y = 450;
    private int speed = 0;
    private boolean isPlaying = false;
    private boolean hasHit = false;
    private String instructions = "Press 'Enter' to start survive as long as"
        + " as possible.";
    private String keyInstructions = "Use arrow keys to move.";
    private String test;
    private Dimension dim = null;
    private Thread game = null;
    private Timer timer = new Timer(1000, new TimerListener());
    public MathewBorumGamePanel(Dimension paramDimension) {
        this.dim = paramDimension;
        setBackground(new Color(255, 255, 204));
        setForeground(Color.black);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                        switch (e.getKeyCode()) {
                    case KeyEvent.VK_LEFT:
                        System.out.println("Go left");
                        if(x >= 10) x = x - 10;
                        repaint();
                        break;
                    case KeyEvent.VK_RIGHT:
                        System.out.println("Go right");
                        if(x <= 490) x = x + 10;
                        repaint();
                        break;
                    default:
                        //repaint();
                        break;
                }
            }
        });
        // Must be focusable!
        setFocusable(true);
        // very important!  Must have focus..
        requestFocusInWindow();
        timer.start();
    }

    /*  THIS IS NOT HOW TO DO ANIMATION!
    @Override
    public void run() {
        for (;;) {
            repaint();
        }
    }*/

    public void paintComponent(Graphics paramGraphics) {
        super.paintComponent(paramGraphics);
        paramGraphics.fillRect(x, y, 50, 15);
        paramGraphics.setFont(new Font("Serif", Font.BOLD, 15));
        paramGraphics.drawString(this.instructions, 30, 30);
        paramGraphics.drawString(this.keyInstructions, 30, 50);
        test = test.valueOf(x);
        paramGraphics.drawString(test, 30, 70);
    }

    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    }
}//end of the extended JPanel class
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
// 
公共类MathewBorumGame扩展了JApplet
{
公共void init()
{
维度dim=getSize();
MathewBorumGamePanel回避=新的MathewBorumGamePanel(dim);
添加(避免);
/*
线程游戏=新线程(回避);
game.start()*/
}
}//扩展JApplet类的结束
类MathewBorumGamePanel扩展了JPanel{//implements Runnable{
保护整数x=225;
受保护整数y=450;
私用整数速度=0;
私有布尔isplay=false;
私有布尔hasHit=false;
private String instructions=“按“回车”开始生存,只要”
+“尽可能。”;
private String keyInstructions=“使用箭头键移动。”;
私有字符串测试;
私有维度dim=null;
私有线程game=null;
私人定时器=新定时器(1000,新定时器侦听器());
public MathewBorumGamePanel(维度参数维度){
this.dim=参数维度;
挫折背景(新颜色(255、255、204));
设置前景(颜色为黑色);
addKeyListener(新的KeyAdapter(){
按下公共无效键(按键事件e){
开关(如getKeyCode()){
case KeyEvent.VK_左:
System.out.println(“向左走”);
如果(x>=10)x=x-10;
重新油漆();
打破
case KeyEvent.VK_RIGHT:
System.out.println(“向右走”);

如果(x)覆盖公共无效绘制(图形参数图形)
而不是
paintComponent()
。此外,我不确定您的方法是否正确,因为我发现您正在无限循环地重新绘制,但也有一个计时器,每秒都会触发,而真正做的是…重新绘制。但是,我看不到您从何处启动计时器,所以让我们认为这是您最后工作的一个非常原始的版本:)让我知道我的提示的结果。@Noe“重写public void
paint(Graphics paramGraphics)
而不是
paintComponent()
”错误!特别是,“Swing程序应该重写
paintComponent()
而不是重写
paint()
”-