Hey guys, I am coming along pretty good in getting a lot of stuff done on my game, today I’m wanting to have it so that I can have a normal window for my game instead of having it run in an applet. I’m still somewhat new to java so I’m kind of lost on how to get it working with my code. Heres what I have right now, 2 classes one for my GameLoop and one for my Display stuff, I really have no clue where and how to start getting JFrame to work with this, but if any of you are able to help out that would be awsome!
import java.awt.Graphics;
import java.awt.Image;
public class Display extends GameLoop implements State { // extends gameloop class to Display.
public Image backbuffer;
public static Graphics bb;
public void init(){ //this method opens the Applet window
setSize (257,225);
Thread th= new Thread (this);
th.start();
backbuffer=createImage(256,224); //backbuffer image size
bb= backbuffer.getGraphics();
}
//TODO: fix flickering, may be in here...
public void paint (Graphics g) { //this method paints.
//draws foreground and menus
stateStack.render();
//copy back buffer to front buffer
g.drawImage(backbuffer, 0, 0, this);
}
}
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.imageio.ImageIO;
public class GameLoop extends Applet implements Runnable, KeyListener, State {
public StateStack stateStack = new StateStack();
final double gameplayFrequency=1000/60;
public static boolean [] keysPressed = new boolean[256];
public static boolean [] keysDown = new boolean[256];
public static boolean [] keysUp = new boolean[256];
private static boolean [] keysPrevious = new boolean[256];
public boolean running=true;
//run method
public void run() {
//initializers
InventoryState.initialize();
GamePlayState.initialize();
GamePlayState.entityAdder();
//------------------------
addKeyListener(this);
double lastGameplayTime= System.currentTimeMillis();
double now;
double counting = 0;
while(running)
{
//this enables frame skipping
now = System.currentTimeMillis();
if(now>lastGameplayTime){
counting=0;
while (now>lastGameplayTime)
{
counting++;
if(counting>=10){
lastGameplayTime=System.currentTimeMillis();
break;
}
lastGameplayTime+=gameplayFrequency;
stateStack.update();
keyUpdate();
}
repaint(); //this refreshes the screen
}
}
now=System.currentTimeMillis();
if(now>lastGameplayTime){
try {Thread.sleep((long) (now-lastGameplayTime));}
catch (InterruptedException e) {e.printStackTrace();}
}
}