Originally Posted by
copeg
Tthe process of calling removeAll may be time consuming because you need to repack everything (although it shouldn't take too long - not something I'd recommend to redraw things, but just to load a new game shouldn't be a problem). Rather than calling getContentPane().removeAll(); you may wish to add your JPanel (x) to a JPanel (y), and then add 'y' to the content pane. Then if you wish to remove things call y.removeAll(). You may also wish to not remove things at all, but redesign so you can just change a parameter of the JPanel which causes to to paint differently
thx for the answer copeg, i tried what u suggested by adding JPanel(x) to JPanel(y)
however, the JPanel(y) did not draw it self in JPanel(x)
, so im still stuck with my old way of calling removeAll()
and i still having this slowdown in performance every time i start a new game, is it because of memory leak?
this is the code that i use(for testing purpose), i just hope u get what my problem is
cause im having difficulty to explain it in English
hero.java
package SaveTheVillages;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Hero {
private String hero = "pic/hero.png";
private int dx;
private int x;
private int y;
private int width;
private int height;
private boolean visible;
private Image image;
private ArrayList peluru1;
public String message;
public Hero() {
ImageIcon ii = new ImageIcon(hero);
image = ii.getImage();
width = image.getWidth(null);
height = image.getHeight(null);
peluru1 = new ArrayList();
visible = true;
x = 300;
y = 500;
}
public void gerak() {
x += dx;
if (x < 3) {
x = 3;
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public ArrayList getPeluru1() {
return peluru1;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVisible() {
return visible;
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
menembak();
}
if (key == KeyEvent.VK_A) {
dx = -3;
if (x <=10){
x +=10;
}
}
if (key == KeyEvent.VK_D) {
dx = 3;
if (x >=500) {
x =500;
}
}
else message = " Press A for Move Left, Press D for Move Right, Press Spacebar to Shoot ";
}
public void menembak() {
//peluru1.add(new Peluru(x + width, y + height/2));
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_A) {
dx = 0;
}
if (key == KeyEvent.VK_D) {
dx = 0;
if (x >=500) {
x =500;
}
}
}
}
Stage.java
package SaveTheVillages;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Stage extends JPanel implements ActionListener {
public Timer timer;
public Hero hero;
public boolean ingame;
public int B_WIDTH;
public int B_HEIGHT;
public int y;
public String message2 = "Hero Died!";
Random objectrand = new Random();
public Stage() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLUE);
setDoubleBuffered(true);
ingame = true;
setSize(500, 600);
hero = new Hero();
timer = new Timer(15, this);
timer.start();
}
public void addNotify() {
super.addNotify();
B_WIDTH = getWidth();
B_HEIGHT = getHeight();
}
public void paint(Graphics g) {
super.paint(g);
if (ingame) {
g.drawLine(0,550,600,550); //Floor Line
g.drawLine(517,510,591,510);//Line up1 Villages
g.drawLine(517,512,589,512);//Line up2 Villages
g.drawLine(589,512,589,550);//Line right1 Villages
g.drawLine(591,510,591,550);//Line right2 Villages
Graphics2D g2d = (Graphics2D)g;
if (hero.isVisible())
g2d.drawImage(hero.getImage(), hero.getX(),hero.getY(), this);
ArrayList ms = hero.getPeluru1();
g2d.setColor(Color.WHITE);
g2d.drawString("Life : ", 5, 15);
g2d.setColor(Color.WHITE);
g2d.drawString("Time : ", 250, 15);
g2d.setColor(Color.WHITE);
g2d.drawString("Score : ", 530, 15);
} else {
String msg = "Villages Died";
Font small = new Font("Helvetica", Font.BOLD, 15);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
hero.gerak();
repaint();
}
public class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
hero.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
hero.keyPressed(e);
}
}
}
Layar_Pembuka.java (the main class)
package SaveTheVillages;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static java.lang.Thread.*;
public class Layar_Pembuka extends JFrame implements ActionListener {
public JPanel mainPanel;
public JMenuBar menuBar;
public JMenu fileMenu, helpMenu;
public JMenuItem newAction, exitAction, tombolAction;
public JButton newBtn;
public Stage st;
public Layar_Pembuka() {
/*********************** game menubar ************************/
menuBar = new JMenuBar();
setJMenuBar(menuBar);
fileMenu = new JMenu("File");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(helpMenu);
newAction = new JMenuItem("New Game");
newAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
exitAction = new JMenuItem("Exit");
exitAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
tombolAction = new JMenuItem("Tombol");
fileMenu.add(newAction);
fileMenu.addSeparator();
fileMenu.add(exitAction);
helpMenu.add(tombolAction);
newAction.addActionListener(this);
exitAction.addActionListener(this);
tombolAction.addActionListener(this);
/*************************************************************/
/******************** game title screen **********************/
mainPanel = new JPanel();
newBtn = new JButton("New Game");
newBtn.addActionListener(this);
mainPanel.add(newBtn);
add(mainPanel);
/*************************************************************/
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 600);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource()==newAction || event.getSource()==newBtn) {
System.out.println("You have clicked on the new action/button");
remove(mainPanel);
mainPanel = new JPanel();
if(st != null) {
remove(st);
System.out.println("x");
}
st = new Stage();
//getContentPane().removeAll();
//mainPanel = new JPanel();
//Stage st = new Stage(music, se, this);
//st = new Stage();
//mainPanel.add(st);
add(st);
show();//pack();
st.requestFocus();
}
if(event.getSource()==exitAction) {
System.exit(0);
}
}
public static void main(String[] args) {
Layar_Pembuka y = new Layar_Pembuka();
}
}
my main problem is that i need to make a new game without losing the game speed performance