7 Replies - 1164 Views - Last Post: 04 November 2012 - 04:29 AM Rate Topic: -----

#1 DWTsend  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 12

Tile-based adventure game / Switching from Applet to JFrame

Posted 07 October 2012 - 12:31 PM

Greetings all,

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

Is This A Good Question/Topic? 0
  • +

Replies To: Tile-based adventure game / Switching from Applet to JFrame

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 865
  • View blog
  • Posts: 2,657
  • Joined: 29-July 11

Re: Tile-based adventure game / Switching from Applet to JFrame

Posted 07 October 2012 - 04:49 PM

First things first, get rid of thet setSize() on your JFrame. In the applet's init, do

setPreferredSize(new Dimension(800,600));



so then in your JFrame you can just pack() after you add the applet to the JFrame, so you don't have to worry about the frame's titlebar taking up real estate in your applet.

Have you tried running this an an applet locally on your machine? I have never been able to get getImage() to load any image from anywhere but a web server. Maybe I am doing something wrong there, but make sure those images are not null, when you run it in the JFrame locally.
Was This Post Helpful? 0
  • +
  • -

#3 DWTsend  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 12

Re: Tile-based adventure game / Switching from Applet to JFrame

Posted 08 October 2012 - 06:02 AM

View Postfarrell2k, on 07 October 2012 - 04:49 PM, said:

Have you tried running this an an applet locally on your machine? I have never been able to get getImage() to load any image from anywhere but a web server. Maybe I am doing something wrong there, but make sure those images are not null, when you run it in the JFrame locally.


As of right now, I have it embedded in a simple HTML file and it works fine. getCodeBase() is just pointing to the src directory, and images is a sub-folder in the src directory. When I've attempted to convert it to an application, I swap it out to Toolkit.getDefaultToolkit().getImage().

I guess I could have clarified a little better. Rather than post the program in some nasty, halfway state, I decided to post it as just a pure applet so I could get advice from the ground up regarding switching it over to an application. The code I've posted (with the exception of the main method) comes from the complete applet version.

I took the setSize out of the JFrame, and read the documentation on pack. Thanks for the heads up, mate.

Dave
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2833
  • View blog
  • Posts: 12,000
  • Joined: 20-September 08

Re: Tile-based adventure game / Switching from Applet to JFrame

Posted 08 October 2012 - 09:31 AM

Your current problems then, are what exactly?
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Tile-based adventure game / Switching from Applet to JFrame

Posted 08 October 2012 - 02:33 PM

Holy steaming static! Small wonder you're having issues.

Step one, take everything and move it out of Applet. I mean everything. Your main should not extend an Applet, JAppet, or JFrame. Rather, it should extend JPanel. Then, your Applet and JFrame implementation need only include the JPanel.

Honestly, without being able to compile it and see what you're seeing, there's not a lot anyone will be able to offer. Not that you may get that many takers. With nearly 100 instance variables and static methods and variables all over the place, this looks like a nightmare.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Tile-based adventure game / Switching from Applet to JFrame

Posted 08 October 2012 - 03:24 PM

Converting from JApplet to an application is usual;y a piece of cake
but just to keep a backup in case you change your mind again, just change it to a class that can be run as an Applt or as an Application
not more complicated
Was This Post Helpful? 0
  • +
  • -

#7 DWTsend  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-October 12

Re: Tile-based adventure game / Switching from Applet to JFrame

Posted 08 October 2012 - 05:56 PM

View Postbaavgai, on 08 October 2012 - 02:33 PM, said:

Holy steaming static! Small wonder you're having issues.

Step one, take everything and move it out of Applet. I mean everything. Your main should not extend an Applet, JAppet, or JFrame. Rather, it should extend JPanel. Then, your Applet and JFrame implementation need only include the JPanel.

Honestly, without being able to compile it and see what you're seeing, there's not a lot anyone will be able to offer. Not that you may get that many takers. With nearly 100 instance variables and static methods and variables all over the place, this looks like a nightmare.

Good luck.


You make a valid point. I guess I thought I could just start this project and figure things out as I go along. Judging from your response I have a lot of junk that's totally out of place. I'll just put this on the back burner until I have a better grasp of the foundations.

Sorry for burdening the board with a giant mess. Thanks to everyone who responded, and hopefully next time I come back to the forums it can be with a better idea of what I'm doing...

Regards,

Dave
Was This Post Helpful? 0
  • +
  • -

#8 jabermony  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 12

Re: Tile-based adventure game / Switching from Applet to JFrame

Posted 04 November 2012 - 04:29 AM

Hey this is a little bit late but would you consider simply adding your applet directly to a JFrame ? (Say if this isn't what your looking for or if it simply wouldn't work ).
I did some little mockups doing this and they seem to run fine after a few ajustments (namely having an extra class to hold resources ect outside the applet,though I think the reason I needed to do this was down to my own fault).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1