So After a few days on Google and a lot of head scratching, I thought it might be time to consult the pros...Here's the issue:
I'm working on a Tile-Based adventure game. I've developed the game as an applet and it works great, but I've decided that I would rather have it as an application. I've done numerous searches for converting an applet to an application, but none of them seem to go as smooth as I would like (translation: It doesn't work).
I've tried changing extends Applet to extends JPanel and inserting that panel into a new JFrame.
I've tried converting the init() method to a constructor and then calling the constructor in the main method.
My main issues seem to stem from my conversion of paint to paintComponent. My current applet uses a "manual" double buffering method where I declare an offscreen image to draw to. In my previous attempts, I've gone through an removed any references to my self-declared buffering images and replaced them with "g" since I've found out that JPanel handles double buffering automatically.
The biggest problem comes when my paintComponent method hits my tile array, which I use to draw the levels. I think that it has something to do with the way I'm trying to utilize init() and start() in my application because I get various NPE's, and if I place a print statement right before the paintComponent gets to the array, it'll print out the array as NULL, even though I've initialized it and populated it in init().
Other issues I've had are typically just related to painting. Even if I don't get a NPE, the application will just sit there with a white screen or a black screen.
I'm trimming down my code a little bit to the parts that are giving me problems. I don't know if you need all 2000 lines worth, but if someone is willing to help then I will more than happily post anything else you need.
This first part is from the game.java file. This is the file that currently runs the applet, and as of pasting this code it works perfectly as an applet.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Game extends Applet implements KeyListener, ActionListener
{
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////DECLARATIONS////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
//////////
//APPLET//
//////////
Graphics bufferGraphics;
Image offscreen;
final int WIDTH = 800;
final int HEIGHT = 600;
long currentTime;
///////////
//CLASSES//
///////////
//HERO
Hero bigHero;
/////////////
//COLLISION//
/////////////
boolean validTile;
////////////
//ENTITIES//
////////////
boolean drawEntity1;
Image entity1;
int entity1X;
int entity1Y;
boolean drawEntity2;
Image entity2;
int entity2X;
int entity2Y;
boolean drawEntity3;
Image entity3;
int entity3X;
int entity3Y;
boolean drawEntity4;
Image entity4;
int entity4X;
int entity4Y;
boolean drawEntity5;
Image entity5;
int entity5X;
int entity5Y;
//////////
//IMAGES//
//////////
//COLORS
Color gold = new Color(255, 201, 14);
//ENVIRONMENT
Image bridgeHor;
Image grass1;
Image grass2;
Image grass3;
Image grass4;
Image mountain;
Image mushroomWater;
Image pathNorth;
Image pathSouth;
Image pathEast;
Image pathWest;
Image pathHor;
Image pathVert;
Image pathNW;
Image pathNE;
Image pathSW;
Image pathSE;
Image path4;
Image tree1;
Image tree2;
Image tree3;
Image tree4;
Image tree5;
Image water1;
//PROPS
Image axeProp;
Image cabin1;
Image cabin2;
Image cabin3;
Image cabin4;
Image cabin5;
Image cabin6;
Image fallenTreeTop;
Image fallenTreeMid;
Image fallenTreeBot;
Image mushroomProp;
Image woodPile;
//ITEMS
Image mushrooms;
Image axe;
//ENTITIES
Image entityToLoad;
Image grudd;
Image sleepingGrudd;
//HERO
Image theHero;
Image livingHero;
Image heroDeath1;
/////////
//INPUT//
/////////
int actionKey;
int toggleAction;
static boolean lockMovement;
/////////////
//INVENTORY//
/////////////
static int inventoryKey;
static int toggleInventory;
static boolean inventoryStatus;
boolean drawInventory;
int inventoryX;
int inventoryY;
String itemSlot1;
String itemSlot2;
String itemSlot3;
String itemSlot4;
String itemSlot5;
String itemSlot6;
int itemCount;
static String itemUsed;
static String correctItem;
static boolean allowItems;
static boolean cantUseItem;
static boolean cantUseEmpty;
///////
//MAP//
///////
private String[][] currentLevel = new String [10] [10];
private int currentMap;
private String zoneDirection;
final private int[][] tileArray = { {0, 80, 160, 240, 320, 400, 480, 560, 640, 720},
{0, 60, 120, 180, 240, 300, 360, 420, 480, 540} };
////////////
//MESSAGES//
////////////
Font myFont;
boolean drawMessage;
boolean drawShortMessage;
int messageID;
int eventCount;
int startLine;
int endLine;
int line;
int lineCount;
/////////
//DEBUG//
/////////
String testX;
String testY;
String testZ;
String testW;
String selectMap;
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////INITIALIZATION//////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void init()
{
//////////
//APPLET//
//////////
setSize(800, 600);
addKeyListener(this);
offscreen = createImage(WIDTH,HEIGHT);
bufferGraphics = offscreen.getGraphics();
/////////
//AUDIO//
/////////
///////////
//CLASSES//
///////////
bigHero = new Hero();
/////////////
//COLLISION//
/////////////
validTile = false;
////////////
//ENTITIES//
////////////
drawEntity1 = false;
entity1X = 0;
entity1Y = 0;
drawEntity2 = false;
entity2X = 0;
entity2Y = 0;
drawEntity3 = false;
entity3X = 0;
entity3Y = 0;
drawEntity4 = false;
entity4X = 0;
entity4Y = 0;
drawEntity5 = false;
entity5X = 0;
entity5Y = 0;
//////////
//IMAGES//
//////////
//ENVIRONMENT
bridgeHor = getImage(getCodeBase(), "images\\environment\\bridgeHor.png");
grass1 = getImage(getCodeBase(), "images\\environment\\grass1.png");
grass2 = getImage(getCodeBase(), "images\\environment\\grass2.png");
grass3 = getImage(getCodeBase(), "images\\environment\\grass3.png");
grass4 = getImage(getCodeBase(), "images\\environment\\grass4.png");
mountain = getImage(getCodeBase(), "images\\mountain.jpg");
mushroomWater = getImage(getCodeBase(), "images\\environment\\mushroomWater.png");
pathNorth = getImage(getCodeBase(), "images\\environment\\pathNorth.png");
pathSouth = getImage(getCodeBase(), "images\\environment\\pathSouth.png");
pathEast = getImage(getCodeBase(), "images\\environment\\pathEast.png");
pathWest = getImage(getCodeBase(), "images\\environment\\pathWest.png");
pathHor = getImage(getCodeBase(), "images\\environment\\pathHor.png");
pathVert = getImage(getCodeBase(), "images\\environment\\pathVert.png");
pathNW = getImage(getCodeBase(), "images\\environment\\pathNW.png");
pathNE = getImage(getCodeBase(), "images\\environment\\pathNE.png");
pathSW = getImage(getCodeBase(), "images\\environment\\pathSW.png");
pathSE = getImage(getCodeBase(), "images\\environment\\pathSE.png");
path4 = getImage(getCodeBase(), "images\\environment\\path4.png");
tree1 = getImage(getCodeBase(), "images\\environment\\tree1.png");
tree2 = getImage(getCodeBase(), "images\\environment\\tree2.png");
tree3 = getImage(getCodeBase(), "images\\environment\\tree3.png");
tree4 = getImage(getCodeBase(), "images\\environment\\tree4.png");
tree5 = getImage(getCodeBase(), "images\\environment\\tree5.png");
water1 = getImage(getCodeBase(), "images\\water1.jpg");
//PROPS
axeProp = getImage(getCodeBase(), "images\\props\\axeProp.png");
cabin1 = getImage(getCodeBase(), "images\\props\\cabin1.png");
cabin2 = getImage(getCodeBase(), "images\\props\\cabin2.png");
cabin3 = getImage(getCodeBase(), "images\\props\\cabin3.png");
cabin4 = getImage(getCodeBase(), "images\\props\\cabin4.png");
cabin5 = getImage(getCodeBase(), "images\\props\\cabin5.png");
cabin6 = getImage(getCodeBase(), "images\\props\\cabin6.png");
fallenTreeTop = getImage(getCodeBase(), "images\\props\\fallenTreeTop.png");
fallenTreeMid = getImage(getCodeBase(), "images\\props\\fallenTreeMid.png");
fallenTreeBot = getImage(getCodeBase(), "images\\props\\fallenTreeBot.png");
mushroomProp = getImage(getCodeBase(), "images\\props\\mushroomProp.png");
woodPile = getImage(getCodeBase(), "images\\props\\woodPile.png");
//ITEMS
mushrooms = getImage(getCodeBase(), "images\\items\\mushrooms.png");
axe = getImage(getCodeBase(), "images\\items\\axe.png");
//ENTITIES
grudd = getImage(getCodeBase(), "images\\entities\\grudd.png");
sleepingGrudd = getImage(getCodeBase(), "images\\entities\\sleepingGrudd.png");
//HERO
livingHero = getImage(getCodeBase(), "images\\hero\\hero.png");
heroDeath1 = getImage(getCodeBase(), "images\\hero\\heroDeath1.png");
theHero = livingHero;
/////////
//INPUT//
/////////
actionKey = 0;
toggleAction = 0;
lockMovement = false;
/////////////
//INVENTORY//
/////////////
inventoryStatus = false;
toggleInventory = 0;
inventoryKey = 0;
drawInventory = false;
inventoryX = 100;
inventoryY = 100;
Inventory.emptyBags();
itemCount = 0;
itemUsed = "none";
correctItem = "none";
allowItems = false;
cantUseItem = false;
cantUseEmpty = false;
///////
//MAP//
///////
currentMap = 1;
zoneDirection = null;
////////////
//MESSAGES//
////////////
myFont = new Font("Serif", Font.BOLD + Font.ITALIC, 25);
drawMessage = false;
drawShortMessage = false;
messageID = 0;
eventCount = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////START GAME LOOP//////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
@Override
public void start()
{
this.setFocusable(true);
requestFocus();
int endGame = 0;
//set the current time in milliseconds
currentTime = System.currentTimeMillis();
//using timer to perform tasks every 15 milliseconds (67 FPS)
Timer time = new Timer(15, this);
//this is the "game loop"
time.start();
while(endGame != 1)
{
//DEBUG DISPLAY
testX = Hero.heroX() + "";
testY = Hero.heroY() + "";
testZ = eventCount + "";
testW = currentMap + "";
//Load the first map
changeMap(currentMap, zoneDirection);
//Enable inventory
itemSlot1 = Inventory.getCurrentSlot(1);
itemSlot2 = Inventory.getCurrentSlot(2);
itemSlot3 = Inventory.getCurrentSlot(3);
itemSlot4 = Inventory.getCurrentSlot(4);
itemSlot5 = Inventory.getCurrentSlot(5);
itemSlot6 = Inventory.getCurrentSlot(6);
Inventory.setInventoryCursorPosition(inventoryX, inventoryY);
if (inventoryKey == 1)
{
drawInventory = true;
inventoryStatus = true;
Hero.heroPos(Hero.heroX(), Hero.heroY());
if (toggleInventory == 1)
{
inventoryKey = 0;
toggleInventory = 0;
inventoryStatus = false;
}
}
/////////
//INTRO//
/////////
while (eventCount < 1)
{
lockHero();
displayMessage(1);
if (actionKey == 1)
{
eventCount++;
actionKey = 0;
unlockHero();
}
}
while (eventCount < 2)
{
lockHero();
validTile = false;
displayMessage(2);
if (actionKey == 1)
{
eventCount++;
actionKey = 0;
unlockHero();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////GAME LEVELS//////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
I'm omitting the game logic and utility functions for the sake of brevity. This next portion is from the same file, game.java, and my overridden paint component for the applet.
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// PAINT COMPONENT ///////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
//PAINT METHOD
@Override
public void paint(Graphics g)
{
//clear the image
bufferGraphics.clearRect(0, 0, WIDTH, HEIGHT);
//Draw the map
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
switch (currentLevel[i][j])
{
//ENVIRONMENT
case "g1":
bufferGraphics.drawImage(grass1, tileArray[0][j], tileArray[1][i], this);
break;
case "g2":
bufferGraphics.drawImage(grass2, tileArray[0][j], tileArray[1][i], this);
break;
case "g3":
bufferGraphics.drawImage(grass3, tileArray[0][j], tileArray[1][i], this);
break;
case "g4":
bufferGraphics.drawImage(grass4, tileArray[0][j], tileArray[1][i], this);
break;
case "w1":
bufferGraphics.drawImage(water1, tileArray[0][j], tileArray[1][i], this);
break;
case "m1":
bufferGraphics.drawImage(mountain, tileArray[0][j], tileArray[1][i], this);
break;
case "p1":
bufferGraphics.drawImage(pathNorth, tileArray[0][j], tileArray[1][i], this);
break;
case "p2":
bufferGraphics.drawImage(pathSouth, tileArray[0][j], tileArray[1][i], this);
break;
case "p3":
bufferGraphics.drawImage(pathEast, tileArray[0][j], tileArray[1][i], this);
break;
case "p4":
bufferGraphics.drawImage(pathWest, tileArray[0][j], tileArray[1][i], this);
break;
case "p5":
bufferGraphics.drawImage(pathHor, tileArray[0][j], tileArray[1][i], this);
break;
case "p6":
bufferGraphics.drawImage(pathVert, tileArray[0][j], tileArray[1][i], this);
break;
case "p7":
bufferGraphics.drawImage(pathNW, tileArray[0][j], tileArray[1][i], this);
break;
case "p8":
bufferGraphics.drawImage(pathNE, tileArray[0][j], tileArray[1][i], this);
break;
case "p9":
bufferGraphics.drawImage(pathSW, tileArray[0][j], tileArray[1][i], this);
break;
case "pa":
bufferGraphics.drawImage(pathSE, tileArray[0][j], tileArray[1][i], this);
break;
case "pb":
bufferGraphics.drawImage(path4, tileArray[0][j], tileArray[1][i], this);
break;
case "t1":
bufferGraphics.drawImage(tree1, tileArray[0][j], tileArray[1][i], this);
break;
case "t2":
bufferGraphics.drawImage(tree2, tileArray[0][j], tileArray[1][i], this);
break;
case "t3":
bufferGraphics.drawImage(tree3, tileArray[0][j], tileArray[1][i], this);
break;
case "t4":
bufferGraphics.drawImage(tree4, tileArray[0][j], tileArray[1][i], this);
break;
case "t5":
bufferGraphics.drawImage(tree5, tileArray[0][j], tileArray[1][i], this);
break;
case "b1":
bufferGraphics.drawImage(bridgeHor, tileArray[0][j], tileArray[1][i], this);
break;
case "mw":
bufferGraphics.drawImage(mushroomWater, tileArray[0][j], tileArray[1][i], this);
break;
//PROPS
case "ap":
bufferGraphics.drawImage(axeProp, tileArray[0][j], tileArray[1][i], this);
break;
case "c1":
bufferGraphics.drawImage(cabin1, tileArray[0][j], tileArray[1][i], this);
break;
case "c2":
bufferGraphics.drawImage(cabin2, tileArray[0][j], tileArray[1][i], this);
break;
case "c3":
bufferGraphics.drawImage(cabin3, tileArray[0][j], tileArray[1][i], this);
break;
case "c4":
bufferGraphics.drawImage(cabin4, tileArray[0][j], tileArray[1][i], this);
break;
case "c5":
bufferGraphics.drawImage(cabin5, tileArray[0][j], tileArray[1][i], this);
break;
case "c6":
bufferGraphics.drawImage(cabin6, tileArray[0][j], tileArray[1][i], this);
break;
case "ft":
bufferGraphics.drawImage(fallenTreeTop, tileArray[0][j], tileArray[1][i], this);
break;
case "fm":
bufferGraphics.drawImage(fallenTreeMid, tileArray[0][j], tileArray[1][i], this);
break;
case "fb":
bufferGraphics.drawImage(fallenTreeBot, tileArray[0][j], tileArray[1][i], this);
break;
case "mp":
bufferGraphics.drawImage(mushroomProp, tileArray[0][j], tileArray[1][i], this);
break;
case "wp":
bufferGraphics.drawImage(woodPile, tileArray[0][j], tileArray[1][i], this);
break;
}
}
}
//Draw other entities
if (drawEntity1 == true)
{
bufferGraphics.drawImage(
entity1,
tileArray[0][entity1X],
tileArray[1][entity1Y],
this);
}
if (drawEntity2 == true)
{
bufferGraphics.drawImage(
entity2,
tileArray[0][entity2X],
tileArray[1][entity2Y],
this);
}
if (drawEntity3 == true)
{
bufferGraphics.drawImage(
entity1,
tileArray[0][entity3X],
tileArray[1][entity3Y],
this);
}
if (drawEntity4 == true)
{
bufferGraphics.drawImage(
entity1,
tileArray[0][entity4X],
tileArray[1][entity4Y],
this);
}
if (drawEntity5 == true)
{
bufferGraphics.drawImage(
entity1,
tileArray[0][entity5X],
tileArray[1][entity5Y],
this);
}
//Draw our hero
bufferGraphics.drawImage(
theHero,
tileArray[0][Hero.heroX()],
tileArray[1][Hero.heroY()],
this);
//MESSAGE SECTION
if (drawMessage == true)
{
startLine = 0;
endLine = 0;
line = 150;
lineCount = 1;
//Background box
bufferGraphics.setColor(Color.BLACK);
bufferGraphics.fillRect(100, 100, 560, 300);
//Text
bufferGraphics.setColor(Color.white);
bufferGraphics.setFont(myFont);
endLine = Messages.getMessage(messageID).indexOf("\n");
while (lineCount < 10)
{
bufferGraphics.drawString(
Messages.getMessage(messageID).substring(startLine, endLine),
125, line);
line += 25;
startLine = (endLine + 1);
endLine = Messages.getMessage(messageID).indexOf("\n", startLine);
lineCount++;
}
drawMessage = false;
}
if (drawShortMessage == true)
{
startLine = 0;
endLine = 0;
line = 150;
lineCount = 1;
//Background box
bufferGraphics.setColor(Color.BLACK);
bufferGraphics.fillRect(100, 100, 560, 100);
//Text
bufferGraphics.setColor(Color.white);
bufferGraphics.setFont(myFont);
endLine = Messages.getMessage(messageID).indexOf("\n");
while (lineCount < 10)
{
bufferGraphics.drawString(
Messages.getMessage(messageID).substring(startLine, endLine),
125, line);
line += 25;
startLine = (endLine + 1);
endLine = Messages.getMessage(messageID).indexOf("\n", startLine);
lineCount++;
}
drawShortMessage = false;
}
//INVENTORY SECTION
if (drawInventory == true)
{
//Background box
bufferGraphics.setColor(Color.BLACK);
bufferGraphics.fillRect(25, 25, 750, 550);
//Text
bufferGraphics.setColor(Color.white);
bufferGraphics.setFont(myFont);
bufferGraphics.drawString("INVENTORY", 325, 50);
bufferGraphics.drawString("Press action button to use item", 235, 75);
bufferGraphics.drawString(Inventory.getItemName(Inventory.getCurrentSlot()), 300, 520);
bufferGraphics.drawString(Inventory.getItemDescription(Inventory.getCurrentSlot()), 100, 550);
//Selection box
bufferGraphics.setColor(gold);
bufferGraphics.drawRect(inventoryX, inventoryY, 150, 150);
//draw items
drawItem(itemSlot1, 110, 110);
drawItem(itemSlot2, 335, 110);
drawItem(itemSlot3, 560, 120);
drawItem(itemSlot4, 110, 335);
drawItem(itemSlot5, 335, 335);
drawItem(itemSlot6, 560, 335);
drawInventory = false;
}
if (cantUseItem == true)
{
inventoryKey = 0;
toggleInventory = 0;
inventoryStatus = false;
lockHero();
//Background box
bufferGraphics.setColor(Color.BLACK);
bufferGraphics.fillRect(100, 100, 560, 100);
//Text
bufferGraphics.setColor(Color.white);
bufferGraphics.setFont(myFont);
bufferGraphics.drawString("You can't use that item right now...", 125, 150);
}
if (cantUseEmpty == true)
{
inventoryKey = 0;
toggleInventory = 0;
inventoryStatus = false;
lockHero();
//Background box
bufferGraphics.setColor(Color.BLACK);
bufferGraphics.fillRect(100, 100, 560, 100);
//Text
bufferGraphics.setColor(Color.white);
bufferGraphics.setFont(myFont);
bufferGraphics.drawString("There's no item there to use...", 125, 150);
}
//DEBUG COORDINATES
bufferGraphics.setColor(Color.white);
bufferGraphics.setFont(myFont);
bufferGraphics.drawString("X: " + testX, 10, 25);
bufferGraphics.drawString("Y: " + testY, 10, 50);
bufferGraphics.drawString("eventCount: " + testZ, 10, 75);
bufferGraphics.drawString("CurrentMap: " + testW, 10, 100);
//draw offscreen image to applet
g.drawImage(offscreen, 0, 0, this);
//make sure everything is up to date before proceeding
Toolkit.getDefaultToolkit().sync();
}//End method paint
@Override
public void update(Graphics g)
{
paint(g);
}
No other parts of the applet are giving me problems (yet) so these should be the only relevant sections.
Lastly, this is what I was using for main.java when attempting to convert to an application:
import javax.swing.JFrame;
public class Main extends JFrame
{
public static void main (String[] args)
{
JFrame window = new JFrame();
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(800, 600);
window.setLocationRelativeTo(null);
window.setVisible(true);
Game game = new Game();
game.setVisible(true);
window.add(game);
game.init();
game.start();
}
}
I know that's a rather large code bomb I just dropped, but hopefully it's organized enough that you can discern the functional parts.
So to summarize:
-Trying to convert from applet to application
-Getting NPE's from currentLevel when converting from paint() to paintComponent()
-Might need specific help on how to organize init() and start() when converting from applet to JFrame
Thanks in advance to anyone who feels like tackling this. I'm also grateful to anyone who would be able to give me some general programming advice if there's anything in my code that looks super weird or wrong. Looking forward to a response!
Regards,
Dave

New Topic/Question
Reply



MultiQuote






|