开始:小程序未初始化错误。我该如何解决这个问题?

时间:2019-03-13 23:37:27

标签: java keylistener japplet

我试图将applet作为学校项目,但遇到了我从未见过的错误。任何建议都将非常受欢迎,我已经奋斗了数小时无济于事。另外,这是我的第一个Java大项目,因此非常欢迎您提供有关编码和样式的其他建议。我的IDE是blueJ,如果需要,我可以在applet浏览器中运行它。干杯!

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

public abstract class Renderer extends JApplet implements KeyListener
{
    public PlayerShip playerShip;
    public static final int CANVAS_SIZE=500;
    public static final int FRAMES_PER_SECOND = 20;
    public static final int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
    public void initLevel(){
        playerShip= new PlayerShip(CANVAS_SIZE);
    }
    @Override 
    public void keyPressed(KeyEvent e){
        if (e.getKeyCode()== KeyEvent.VK_RIGHT){
            playerShip.moveRight();
        }
        else if (e.getKeyCode()== KeyEvent.VK_LEFT){
            playerShip.moveLeft();
        }
        repaint();
    }
    public void paint(Graphics g){
        int sleep_time = 0;
        int next_game_tick = 0;
        long sleepTime;
        boolean Game= true;
        long startTime= System.currentTimeMillis();
        setSize(CANVAS_SIZE, CANVAS_SIZE);
        while(Game== true){
            initLevel();
            int leftSide=playerShip.getLeftBound();
            int width=playerShip.getWidth();
            int topSide=playerShip.getTopBound();
            int height=playerShip.getHeight();


            g.setColor(Color.blue);
            g.fillRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
            g.setColor(Color.orange);
            g.fillRect (leftSide, topSide, width, height);
            long timeElapsed = System.currentTimeMillis() - startTime;
            next_game_tick += SKIP_TICKS;

            sleepTime= next_game_tick - timeElapsed;
            try{
                Thread.sleep(sleepTime);
            }
            catch(InterruptedException ex)
            {
                Thread.currentThread().interrupt();
            }

        }

    }

}

编辑:这也是播放器类

public class PlayerShip
{
    // instance variables - replace the example below with your own
    public static final int SIZE= 20;
    public int shipX;
    public int shipY;
    public int shipLeft;
    public int shipRight;
    public PlayerShip(int canvasSize)
    {
        shipX= canvasSize/2;
        shipY= canvasSize*3/4;
    }
    public void moveLeft(){
        shipX -=1;
    }
    public void moveRight(){
        shipX+=1;
    }
    public int getLeftBound(){
        int leftSide = Math.round(shipX - (SIZE/2));
        return (leftSide);
    }
    public int getWidth(){
        return SIZE;
    }
    public int getTopBound(){
        int topSide = Math.round(shipY - (SIZE/2));
        return (topSide);
    }
    public int getHeight(){
        return SIZE;
    }

}

1 个答案:

答案 0 :(得分:1)

实施KeyListener

一个问题可能是您仅实现了java.awt.event.KeyListener接口所需的三种方法之一。您做了keyPressed,但省略了keyTypedkeyReleased

@Override
public void keyTyped ( KeyEvent e )
{
    …
}

@Override
public void keyPressed ( KeyEvent e )
{
    …
}

@Override
public void keyReleased ( KeyEvent e )
{
    …
}

进口

您可能需要为import类添加一个显式Color才能识别其常量。

import java.awt.Color;

按照惯例,Java中的常量全部用大写字母命名。因此,Color.BLUE

命名约定

这不是编译器或applet的问题,但是您应该遵循Java命名约定,以便其他程序员阅读您的代码。因此boolean Game应该以小写字母开头。此外,布尔值通常以is前缀命名,因此boolean isGame。我怀疑您可以设计出更具描述性的措辞。

为简便起见,请将while ( isGame == true )缩短为while ( isGame )

一个更严重的问题:isGame循环中使用的while变量永远不会更改其状态。因此,您的while循环是无限的。

运行

做出上述更改后,我看到您的applet通过{em> AppletViewer 应用程序版本1.0使用IntelliJ IDE和Java 8(Azul Systems的 Zulu JVM)启动了在装有macOS Mojave的MacBook Pro上。

enter image description here

顺便说一句,在我看来,您好像正在用行Thread.sleep(sleepTime);休眠主GUI线程。我不是applet或AWT的专家,但我记得您应该从不休眠主GUI线程。

package work.basil.example;

public class PlayerShip
{
    // instance variables - replace the example below with your own
    public static final int SIZE = 20;
    public int shipX;
    public int shipY;
    public int shipLeft;
    public int shipRight;

    public PlayerShip ( int canvasSize )
    {
        shipX = canvasSize / 2;
        shipY = canvasSize * 3 / 4;
    }

    public void moveLeft ()
    {
        shipX -= 1;
    }

    public void moveRight ()
    {
        shipX += 1;
    }

    public int getLeftBound ()
    {
        int leftSide = Math.round( shipX - ( SIZE / 2 ) );
        return ( leftSide );
    }

    public int getWidth ()
    {
        return SIZE;
    }

    public int getTopBound ()
    {
        int topSide = Math.round( shipY - ( SIZE / 2 ) );
        return ( topSide );
    }

    public int getHeight ()
    {
        return SIZE;
    }

}

…和…

package work.basil.example;

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

public class Renderer extends JApplet implements KeyListener
{
    public PlayerShip playerShip;
    public static final int CANVAS_SIZE = 500;
    public static final int FRAMES_PER_SECOND = 20;
    public static final int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;

    public void initLevel ()
    {
        playerShip = new PlayerShip( CANVAS_SIZE );
    }

    public void paint ( Graphics g )
    {
        int sleep_time = 0;
        int next_game_tick = 0;
        long sleepTime;
        boolean isGame = true;
        long startTime = System.currentTimeMillis();
        setSize( CANVAS_SIZE , CANVAS_SIZE );
        while ( isGame  )
        {
            initLevel();
            int leftSide = playerShip.getLeftBound();
            int width = playerShip.getWidth();
            int topSide = playerShip.getTopBound();
            int height = playerShip.getHeight();

            g.setColor( Color.BLUE );
            g.fillRect( 0 , 0 , CANVAS_SIZE , CANVAS_SIZE );
            g.setColor( Color.ORANGE );
            g.fillRect( leftSide , topSide , width , height );
            long timeElapsed = System.currentTimeMillis() - startTime;
            next_game_tick += SKIP_TICKS;

            sleepTime = next_game_tick - timeElapsed;
            try
            {
                Thread.sleep( sleepTime );
            } catch ( InterruptedException ex )
            {
                Thread.currentThread().interrupt();
            }

        }

    }

    //----------|  Override `java.awt.event.KeyListener`  |------------------

    @Override
    public void keyPressed ( KeyEvent e )
    {
        if ( e.getKeyCode() == KeyEvent.VK_RIGHT )
        {
            playerShip.moveRight();
        } else if ( e.getKeyCode() == KeyEvent.VK_LEFT )
        {
            playerShip.moveLeft();
        }
        repaint();
    }

    @Override
    public void keyTyped ( KeyEvent e )
    {
        // No code needed here.
    }

    @Override
    public void keyReleased ( KeyEvent e )
    {
        // No code needed here.
    }

}

我要强制性地警告,Java Applet技术将被Web浏览器制造商以及Oracle和Java社区逐步淘汰。这个项目适合学习。但是对于实际工作,您可能想了解OpenJFX,并使用 jlink 捆绑Java运行时。