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;
}
}
Implement
KeyListenerOne problem might be that you implemented only one of the three methods required by the
java.awt.event.KeyListenerinterface. You didkeyPressedbut omittedkeyTypedandkeyReleased.Imports
You might need to add an explicit
importfor theColorclass to recognize its constants.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 Gameshould start with a lowercase letter. Also, booleans are often named withisprefix, soboolean isGame. I suspect you could devise a more descriptive wording.By the way, for brevity, shorten
while ( isGame == true )towhile ( isGame ).A more serious problem: This
isGamevariable used in yourwhileloop never changes its state. So yourwhileloop 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.
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.…and…
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.