Thobias Nordgaard
Thobias Nordgaard

Reputation: 295

java adding jpanels from other classes

I am developing a game in java and I am trying to load different levels from different class files. Each level class file is a jpanel. The idea was that when level1 is done it loads a method that removes the jpanel level1 and adds the jpanel level2. But when I am trying to load level2 from level1 I get a java.lang.OutOfMemoryError: unable to create new native thread. Error

I have 3 class files:

this is the method in the main class:

public static void levelChanger(int currentMap){
    map1 map1 = new map1(null);
    map2 map2 = new map2(null);

    if(currentMap == 1){
        frame.add(map1);
        frame.validate();
    }else if(currentMap == 2){
        frame.remove(map1);
        frame.add(map2);
        frame.validate();
    }
}

And this is how i call it from level1:

mainScreen.levelChanger(2);

Hope I provided enough information. Thanks!


This is the entire main class:

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;

public class mainScreen{

    static JFrame frame = new JFrame("Tile System");

    //Key variables
    public static int keyUp = KeyEvent.VK_UP;
    public static int keyDown = KeyEvent.VK_DOWN;

    public static void main(String[] args) {
        levelChanger(1);
        frame.setSize ( 800, 600 );
        frame.setResizable ( false );
        frame.setLocationRelativeTo ( null );
        frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
        frame.setVisible ( true );
        frame.setBackground(new Color(135, 206, 250));//RGB color code
        frame.setFocusable(true);

        new map1(null);
    }
    public static void exit(){
        WindowEvent wev = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
    }

    public static void levelChanger(int currentMap){
        public static map1 map1 = new map1(null);
            public static map2 map2 = new map2(null);
        if(currentMap == 1){
            frame.add(map1);
            frame.validate();
        }else if(currentMap == 2){
            frame.remove(map1);
            frame.add(map2);
            frame.validate();
        }
    }
}

Upvotes: 1

Views: 401

Answers (2)

Autar
Autar

Reputation: 1589

I can't be sure with the code you provide but your error is an OutOfMemoryError, did you increase the default java memory ? Try -Xms512m -Xmx1024m command line options (or put in there whatever memory size you want).

Upvotes: 0

dacwe
dacwe

Reputation: 43504

First, the OutOfMemoryError doesn't really have anything with this code, probably map1 and map2 are allocating lots of memory, maybe this answer will help you relasing some of that memory.

  1. Each time you are calling levelChanger you are allocating a new map1 and map2 object. Even though you will only use one of these objects

  2. You are not removing the same map1 object when changing to level 2.

  3. (You could let levelChanger take a map parameter instead)


An improved levelChanger method could look like this:

// allocate maps once
static map1 map1 = new map1(null);
static map2 map2 = new map2(null);

public static void levelChanger(int currentMap){

    if (currentMap == 1){
        frame.add(map1);
        frame.validate();
    } else if (currentMap == 2) {
        frame.remove(map1);
        frame.add(map2);
        frame.validate();
    }
}

A better way would be create an interface Map and let map1 and map2 implement Map, also creating an own panel (mapPanel) for the map. Something like this:

public static void levelChanger(Map map){
    mapPanel.removeAll();
    mapPanel.add(map);
}

Upvotes: 3

Related Questions