Results 1 to 12 of 12
  1. #1
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default need help with using custom JPanels

    Alright so my friend and i are making a game and the main class for the game creates a JFrame that has two major sections, a jPanel on top where the user views the game world, and secondly a bottom jPanel broke into two parts for things like inventory and spells. Now where my problem lies is that i have a class classed Grid which creates the game world which is basically a grid (duh) divided into multiple cells and places it into a JPanel in the class. What i want to do is take the JPanel that the Grid class creates and place it in the jPanel in the main class. Grid extends JPanel and this is what my code looks like so far:

    Grid class:
    Java Code:
    public class Grid extends JPanel implements KeyListener, MouseListener, ActionListener
    {
    	private Cell[][] cells;
    	private JFrame frame; //not used anymore
    	private int lastKeyPressed;
    	private Location lastLocationClicked;
    	private Color lineColor;
    	private JProgressBar health; //alos not used anymore
    	private JPanel gameFrame = new JPanel();
    
    
    	public Grid(int numRows, int numCols)
    	{
    		init(numRows, numCols);
    	}
    Grid init method:
    Java Code:
    	private void init(int numRows, int numCols)
    	{
    		setToolTipText("");
    		lastKeyPressed = -1;
    		lastLocationClicked = null;
    		setLineColor(new Color(0,0,0));
    
    		cells = new Cell[numRows][numCols];
    		for (int row = 0; row < numRows; row++)
    		{
    			for (int col = 0; col < numCols; col++)
    				cells[row][col] = new Cell();
    		}
    
    		gameFrame.addKeyListener(this);
    		int cellSize = Math.max(Math.min(500 / getNumRows(), 500 / getNumCols()), 1);    
    		setPreferredSize(new Dimension(cellSize * numCols, cellSize * numRows));
    		gameFrame.addMouseListener(this);
    		gameFrame.add(this);
    	}
    main class main method:
    Java Code:
    	
    public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try{
    					world = new Grid(10,10);
    					GameWindow frame = new GameWindow();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    where i try and put the JPanel from grid into the main class:
    Java Code:
         public void GameWindow(){
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(700, 700, 750, 700);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		GridBagLayout gbl_contentPane = new GridBagLayout();
    		gbl_contentPane.columnWidths = new int[]{243, 0, 0};
    		gbl_contentPane.rowHeights = new int[]{433, 0, 0};
    		gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    		gbl_contentPane.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    		contentPane.setLayout(gbl_contentPane);
    		JPanel gameAreaView = world; //gameAreaView is the JPanel in the main class and world is the JPanel from the Grid class
    		GridBagConstraints gbc_gameAreaView = new GridBagConstraints();
    		gbc_gameAreaView.gridwidth = 2;
    		gbc_gameAreaView.insets = new Insets(0, 0, 5, 0);
    		gbc_gameAreaView.anchor = GridBagConstraints.NORTH;
    		gbc_gameAreaView.fill = GridBagConstraints.BOTH;
    		gbc_gameAreaView.gridx = 0;
    		gbc_gameAreaView.gridy = 0;
    		contentPane.add(gameAreaView, gbc_gameAreaView);
    		gameAreaView.setLayout(new GridLayout(1, 1, 0, 0));

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: need help with using custom JPanels

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: need help with using custom JPanels

    My question is how would i go about doing it because that code above doesn't seem to work.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: need help with using custom JPanels

    Your code confuses me in that I see a variable, world, in the "static" universe, your main method, but also in your GameWindow constructor suggesting that world is a static variable. If so, don't do that. Instead why not simply create a new Gird object in the GameWindow constructor and then add that Grid object to the GameWindow GUI somehow.

    For more and better help, consider creating and posting an SSCCE

  6. #6
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: need help with using custom JPanels

    okay here is a SSCCE that contains the main class GameWindow with just a jPanel containing the contents of the Grid class which I moved to the constructor of GameWindow like you said. what i would like it to do is the fill the whole JPanel of the GameWindow class with the contents of the Grid class. Hope this helps.

    Grid class:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class Grid extends JPanel
    {
    	private Cell[][] cells;
    	private JPanel gameFrame = new JPanel();
    	public Grid(int numRows, int numCols)
    	{
    		init(numRows, numCols);
    	}
    
    	private void init(int numRows, int numCols)
    	{
    		setToolTipText("");
    		lastKeyPressed = -1;
    		lastLocationClicked = null;
    		setLineColor(new Color(0,0,0));
    
    		cells = new Cell[numRows][numCols];
    		for (int row = 0; row < numRows; row++)
    		{
    			for (int col = 0; col < numCols; col++)
    				cells[row][col] = new Cell();
    		}
    
    		gameFrame.addKeyListener(this);
    		int cellSize = Math.max(Math.min(500 / numRows, 500 / numCols), 1);    
    		setPreferredSize(new Dimension(cellSize * numCols, cellSize * numRows));
    		gameFrame.addMouseListener(this);
    		gameFrame.add(this);
    	}
    }
    GameWindow class:
    Java Code:
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    
    public class GameWindow extends JFrame {
    
    	private JPanel contentPane;
    	private Grid world;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try{
    					GameWindow frame = new GameWindow();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public GameWindow() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(700, 700, 750, 700);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		GridBagLayout gbl_contentPane = new GridBagLayout();
    		gbl_contentPane.columnWidths = new int[]{243, 0, 0};
    		gbl_contentPane.rowHeights = new int[]{433, 0, 0};
    		gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    		gbl_contentPane.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    		contentPane.setLayout(gbl_contentPane);
    		JPanel gameAreaView = world;
    		GridBagConstraints gbc_gameAreaView = new GridBagConstraints();
    		gbc_gameAreaView.gridwidth = 2;
    		gbc_gameAreaView.insets = new Insets(0, 0, 5, 0);
    		gbc_gameAreaView.anchor = GridBagConstraints.NORTH;
    		gbc_gameAreaView.fill = GridBagConstraints.BOTH;
    		gbc_gameAreaView.gridx = 0;
    		gbc_gameAreaView.gridy = 0;
    		contentPane.add(gameAreaView, gbc_gameAreaView);
    		gameAreaView.setLayout(new GridLayout(1, 1, 0, 0));
    		
    	}
    
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: need help with using custom JPanels

    Hm, that's not an SSCCE since I cannot compile it nor can I run it. It is missing dependencies. Can't help you yet. Sorry.

    Consider creating a true SSCCE, one that's short -- has no unnecessary code that's not related to your problem, but is compilable and runnable and demonstrates your problem.

    By the way, what happens wrong when you try to run your code?
    Last edited by Fubarable; 05-22-2012 at 05:08 AM.

  8. #8
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: need help with using custom JPanels

    o dang just saw the Cell reference there..and when i run it it doesn't fill the whole jPanel like i want and none of the methods of the Grid class seem to work like when I try and put a person onto the map and repainting the Grid JPanel it doesn't seem to work.

    Here is the Cell class:
    Java Code:
    public class Cell
    {
    	private Color color;
    	private String imageFileName;
    	private Person peep;
    
    	public Cell()
    	{
    		color = new Color(255, 255, 255);
    		imageFileName = null;
    	}
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: need help with using custom JPanels

    Quote Originally Posted by StaticFlow View Post
    o dang just saw the Cell reference there..and when i run it it doesn't fill the whole jPanel like i want and none of the methods of the Grid class seem to work like when I try and put a person onto the map and repainting the Grid JPanel it doesn't seem to work.

    Here is the Cell class:
    Java Code:
    public class Cell
    {
    	private Color color;
    	private String imageFileName;
    	private Person peep;
    
    	public Cell()
    	{
    		color = new Color(255, 255, 255);
    		imageFileName = null;
    	}
    }
    It's missing more than just the Cell class. The classes that you've posted above use variables that are never declared, never initialized, treat the class as if it were a KeyListener and a MouseListener (is this necessary for an SSCCE?).

    If you still need our help, please clean up the code so that it is a valid SSCCE, both short and complete. And also please give a more detailed description of the bad things that happen with this code. Do you see any exceptions? Does something show up that shouldn't show up? Does something not show up that should show up? All these details and more are important.

    Do you have to do the things I request? No of course not, and we won't respect you any less if you don't, but if you do comply with my requests, you'll have a better chance of getting decent help here in the forum. So it's up to you.

    Best of luck.

  10. #10
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: need help with using custom JPanels

    Sorry i guess im just not used to making those for forum posts, what I am wanting to happen is for the Grid class to fill up the entire JPanel in the GameWindow class. What is happening is that only part of the grid is being shown as if there is more but its being cut off.

    here are the better classes that should (hopefully) compile just fine:

    Grid:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class Grid extends JPanel
    {
    	private Cell[][] cells;
    	private JPanel gameFrame = new JPanel();
    	public Grid(int numRows, int numCols)
    	{
    		init(numRows, numCols);
    	}
    	private void init(int numRows, int numCols)
    	{
    		cells = new Cell[numRows][numCols];
    		for (int row = 0; row < numRows; row++)
    		{
    			for (int col = 0; col < numCols; col++)
    				cells[row][col] = new Cell();
    		}
    
    		int cellSize = Math.max(Math.min(500 / numRows, 500 / numCols), 1);    
    		setPreferredSize(new Dimension(cellSize * numCols, cellSize * numRows));
    		gameFrame.add(this);
    	}
    }
    GameWindow:
    Java Code:
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.GridBagLayout;
    import java.awt.GridBagConstraints;
    import java.awt.Insets;
    import java.awt.GridLayout;
    
    public class GameWindow extends JFrame {
    
    	private JPanel contentPane;
    	private Grid world;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try{
    					GameWindow frame = new GameWindow();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the frame.
    	 */
    	public GameWindow() {
    		world = new Grid(10,10);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(700, 700, 750, 700);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		GridBagLayout gbl_contentPane = new GridBagLayout();
    		gbl_contentPane.columnWidths = new int[]{243, 0, 0};
    		gbl_contentPane.rowHeights = new int[]{433, 0, 0};
    		gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    		gbl_contentPane.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
    		contentPane.setLayout(gbl_contentPane);
    		JPanel gameAreaView = world;
    		GridBagConstraints gbc_gameAreaView = new GridBagConstraints();
    		gbc_gameAreaView.gridwidth = 2;
    		gbc_gameAreaView.insets = new Insets(0, 0, 5, 0);
    		gbc_gameAreaView.anchor = GridBagConstraints.NORTH;
    		gbc_gameAreaView.fill = GridBagConstraints.BOTH;
    		gbc_gameAreaView.gridx = 0;
    		gbc_gameAreaView.gridy = 0;
    		
    		contentPane.add(gameAreaView, gbc_gameAreaView);
    		gameAreaView.setLayout(new GridLayout(1, 1, 0, 0));
    		
    	}
    
    }
    and finally Cell:
    Java Code:
    public class Cell //needed for Grid class so it knows how big to make itself empty because contents of  Cell class are irrelevant
    {
    	public Cell()
    	{
    	}
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: need help with using custom JPanels

    Your gameAreaView is being added to the GUI just fine. I'm not sure what exactly your problem with this code is right now. sorry.

    Edit: if you want the gameAreaView to fill its container, then don't have the container use GridBagLayout. instead why not give it GridLayout(1, 1) or BorderLayout? No need for GridBagConstraints.

  12. #12
    StaticFlow is offline Member
    Join Date
    May 2012
    Posts
    17
    Rep Power
    0

    Default Re: need help with using custom JPanels

    gameAreaView uses a GridLayout(1,1,0,0) its at the bottom of the GameWindow Constructor the GridBagConstraints are for gameAreaViews container the contentPane. But now my code seems to be working just fine the reason i asked this in the first place was because gameAreaView's panel covers the entire JFrame but no matter how many columns i added to the constructor of Grid it never would grow horizontally to fill the space but now it does....odd

Similar Threads

  1. JPanels ?
    By santa in forum New To Java
    Replies: 5
    Last Post: 02-25-2012, 07:10 PM
  2. Replies: 0
    Last Post: 09-12-2011, 01:06 PM
  3. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  4. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 08:33 PM
  5. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 11:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •