JFrame使用方式太多内存

时间:2014-02-20 21:12:09

标签: java swing memory jframe memory-leaks

我目前正在开发2D游戏。我有一个添加JPanel的JFrame。出于某种原因,该程序每秒使用越来越多的内存。它开始时大约50,000K并越来越高,直到我的电脑无法处理它。我认为这是在我的JPanel中调用的paint()方法,但是当我不添加我的JPanel时,它仍然在上升。这是我的JFrame的代码:

package main;

import java.awt.Component;

import javax.swing.JFrame;

import level.Level;

public class LostCloud {

public static int width = 300;
public static int height = 300;
public static int scale = 2;
public static String name = "Lost Cloud: Adventure 2D";
public static JFrame frame;
public static JFrame levelFrame;
public static Component center = null;
public static Menu menu = new Menu();
public static Level level = new Level();

public static void main(String[] args) {

    frame = new JFrame(name);
    levelFrame = new JFrame(name);

    // Set size and settings
    frame.setSize(width * 2, height * 2);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLocationRelativeTo(center);
    // Set visible
    frame.setVisible(true);
    // Draw main menu panel

    frame.add(menu);
    menu.requestFocusInWindow();
    frame.validate();
}

public void play() {

    //Close main menu frame
    frame.dispose();

    // Set size and settings
            levelFrame.setSize(width * 3, height * 3);
            levelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            levelFrame.setResizable(false);
            levelFrame.setLocationRelativeTo(center);
            // Set visible
            levelFrame.setVisible(true);
            // Draw level panel

            levelFrame.add(level);
            level.requestFocusInWindow();
            levelFrame.validate();
        }
}

菜单:

package main;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import entity.PlayerEntity;

public class Menu extends JPanel {

// Animations

public int animating = 0;
public int animatingType = 0;

// Selected

public int selectedOption = 0;

//Images

public BufferedImage background;
public String backgroundLocation;

private static final long serialVersionUID = 1L;
private PlayerEntity player;
public LostCloud main = new LostCloud();

public Menu() {
    player = new PlayerEntity(400, 200);
    addKeyListener(new MenuKeyListener());


    // Data file, stores name in it

    File file = new File("data.txt");

    if (!file.exists()) {
        try {
            file.createNewFile();
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("Cloud");
            bw.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

}

public void updatePaint() {
    this.repaint();
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    updatePaint();
    final Graphics2D g2 = (Graphics2D) g;

    backgroundLocation = "res/background/main/main_background.png";
    try {
        background = ImageIO.read(new File(backgroundLocation));
    } catch (IOException e) {
        System.out.println("Unable to find file " + backgroundLocation + ",      printing stack trace!");
        e.printStackTrace();
    }

    g2.drawImage(background, 0, 0, null);

    Font font = null;

    try {
        font = Font.createFont(Font.TRUETYPE_FONT, new File(
                "res/font/BitTrip7(sRB).TTF"));
    } catch (FontFormatException | IOException e) {
        e.printStackTrace();
    }

    font = font.deriveFont(40F);
    g.setFont(font);



    if (selectedOption == 0) {
        g.setColor(Color.BLUE);
        g.drawString("Play", LostCloud.width / 2, 350);
        g.setColor(Color.BLACK);
        g.drawString("Quit", LostCloud.width / 2, 400);
    } else if (selectedOption == 1) {
        g.setColor(Color.BLACK);
        g.drawString("Play", LostCloud.width / 2, 350);
        g.setColor(Color.BLUE);
        g.drawString("Quit", LostCloud.width / 2, 400);
    }


    if (animatingType == 0) {
        g2.drawImage(player.sprite[0].getScaledInstance(128, 128,
                Image.SCALE_SMOOTH), player.x, player.y, null);
    } else if (animatingType == 1) {
        g2.drawImage(player.sprite[1].getScaledInstance(128, 128,
                Image.SCALE_SMOOTH), player.x, player.y, null);
    }
    if (animating == 0) {
        g2.drawImage(player.sprite[1], 10, 10, null);
        animating = 1;

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                if (animatingType == 0) {
                    animatingType = 1;
                } else if (animatingType == 1) {
                    animatingType = 0;
                }
            }
        }, 0, 2 * 1000);
    }
}

private class MenuKeyListener implements KeyListener {

    public void keyPressed(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.VK_UP) {
            if (selectedOption == 1) {
                selectedOption = 0;
            }
        }

        if (event.getKeyCode() == KeyEvent.VK_DOWN) {
            if (selectedOption == 0) {
                selectedOption = 1;
            }
        }

        if (event.getKeyCode() == KeyEvent.VK_ENTER) {
            if (selectedOption == 0) {
                main.play();
            }
        }


    }

    public void keyReleased(KeyEvent arg0) {

    }

    public void keyTyped(KeyEvent arg0) {

    }

}
}

等级:

package level;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JPanel;
import javax.swing.Timer;

import entity.PlayerEntity;

public class Level extends JPanel implements ActionListener {

private static final long serialVersionUID = 1L;
public Timer time;

// Player

public PlayerEntity player;

// Collisions

public static Rectangle bottom = new Rectangle(0, 500, 1000, 200);

public Level() {
    this.setVisible(true);
    this.setBackground(Color.DARK_GRAY);
    player = new PlayerEntity(200, 200);
    addKeyListener(new LevelKeyListener());

    time = new Timer(5, this);
    time.start();
}

public void updatePaint() {
    this.repaint();
}

public void paint(Graphics g) {
    super.paint(g);
    updatePaint();

    final Graphics2D g2 = (Graphics2D) g;

    g2.fill(bottom);
    if (player.attacking == 0) {
        g2.drawImage(player.sprite[1].getScaledInstance(64, 64,
                Image.SCALE_SMOOTH), player.x, player.y, null);
    } else if (player.attacking == 1) {
        g2.drawImage(player.sprite[2].getScaledInstance(64, 64,
                Image.SCALE_SMOOTH), player.x, player.y, null);
    }



    g2.setColor(Color.BLACK);
    Font font = null;

    try {
        font = Font.createFont(Font.TRUETYPE_FONT, new File(
                "res/font/BitTrip7(sRB).TTF"));
    } catch (FontFormatException | IOException e) {
        e.printStackTrace();
    }

    font = font.deriveFont(30F);
    g2.setFont(font);
    g2.drawString(String.valueOf(player.getCharge()) + "%", 250, 100);
    g2.drawString(String.valueOf(player.getHealth()) + "%", 250, 50);

    g2.drawImage(player.getStatImage().getScaledInstance(256, 128, Image.SCALE_SMOOTH), 0, 0, null);

    Rectangle chargeRect = new Rectangle(28, 74, player.getCharge() * 2 - 1, 24);
    g2.setColor(Color.YELLOW);
    g2.fill(chargeRect);

    Rectangle healthRect = new Rectangle(28, 26, player.getHealth() * 2 - 1,      24);
    g2.setColor(Color.RED);
    g2.fill(healthRect);
}

private class LevelKeyListener implements KeyListener {

    public void keyPressed(KeyEvent event) {
        player.keyPressed(event);

    }

    public void keyReleased(KeyEvent key) {
        player.keyReleased(key);

    }

    public void keyTyped(KeyEvent arg0) {

    }

}

@Override
public void actionPerformed(ActionEvent event) {
    player.move();
    updatePaint();
    revalidate();
}
}

1 个答案:

答案 0 :(得分:0)

选中添加ActionListeners。如果由于某些事件或基于时间而添加具有new的动作侦听器(因为它在没有您做任何事情的情况下从它增加),那么您可以每次为侦听器分配内存。这会造成内存泄漏,从而导致内存需求逐渐增加,直至出现故障。尝试将动作侦听器添加到实例变量,以便它们只需实例化一次。我没有看到你所有的代码,所以我只是从项目经验和遇到类似问题来说这个。