Start: applet not initialized error. How do I resolve this issue?

904 Views Asked by At

I was trying to make an applet as a project for school but encountered this error I had never seen before. Any advice would be extremely welcome, I've struggled for hours to no avail. Also, this is my first big project in Java so any other advice regarding coding and style is very welcome. My IDE is blueJ and I ran it in the applet viewer if it matters. Cheers!

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();
            }

        }

    }

}

Edit: Here is the player class as well

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

There are 1 best solutions below

5
Basil Bourque On

Implement KeyListener

One problem might be that you implemented only one of the three methods required by the java.awt.event.KeyListener interface. You did keyPressed but omitted keyTyped and keyReleased.

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

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

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

Imports

You might need to add an explicit import for the Color class to recognize its constants.

import java.awt.Color;

Constants in Java are named with all uppercase, by convention. So, Color.BLUE.

Naming conventions

Not a compiler or applet problem, but you should follow Java naming conventions for the sake of other programmers reading your code. So boolean Game should start with a lowercase letter. Also, booleans are often named with is prefix, so boolean isGame. I suspect you could devise a more descriptive wording.

By the way, for brevity, shorten while ( isGame == true ) to while ( isGame ).

A more serious problem: This isGame variable used in your while loop never changes its state. So your while loop is infinite.

Runs

After making the above changes, I see your applet launch using IntelliJ IDE and Java 8 (Zulu JVM from Azul Systems) via AppletViewer app version 1.0 on my MacBook Pro with macOS Mojave.

enter image description here

By the way, it looks to me like you are sleeping the main GUI thread with your line Thread.sleep(sleepTime);. I am no expert on applets or AWT, but as I recall you should never sleep the main GUI thread.

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;
    }

}

…and…

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.
    }

}

I’ll add the obligatory caution that Java Applet technology is being phased out by web browser makers and by Oracle and the Java community. This project is fine for learning. But for real work, you’ll likely want to learn about OpenJFX and using jlink to bundle a Java runtime.