Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: how to destroy JPanel?

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default how to destroy JPanel?

    i know maybe this is a very easy question,
    but until now i still unable to destroy a JPanel

    here is my problem, i have a Menubar, and a JPanel inside a JFrame,, what i want is everytime i click
    a menu(new game menu) from the menubar, the JPanel get destroyed and replaced by a new JPanel

    what i have done until now is using

    getContentPane().removeAll();

    to remove the JPanel, however it slowdown the game system everytime i use it


    i think this should be simple to do, oh btw i dont want to hide the JPanel,
    what i want is to completely destroy the JPanel


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to destroy JPanel?

    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

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to destroy JPanel?

    Quote Originally Posted by copeg View Post
    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

Similar Threads

  1. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  2. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM
  3. Destroy Brick
    By Ceasar in forum Java Theory & Questions
    Replies: 2
    Last Post: October 10th, 2009, 04:36 AM
  4. How to write above a JPanel
    By bruno88 in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2009, 06:16 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM