Results 1 to 12 of 12
- 05-22-2012, 01:10 AM #1
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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:
Grid init method: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); }
main class main 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); }
where i try and put the JPanel from grid into the main class: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(); } } }); }
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));
-
Re: need help with using custom JPanels
Do you have an actual question?
- 05-22-2012, 03:58 AM #3
Re: need help with using custom JPanels
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-22-2012, 04:12 AM #4
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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.
-
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
- 05-22-2012, 04:43 AM #6
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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:
GameWindow 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); } }
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)); } }
-
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.
- 05-22-2012, 05:25 AM #8
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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; } }
-
Re: need help with using custom JPanels
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.
- 05-22-2012, 06:34 AM #10
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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:
GameWindow: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); } }
and finally Cell: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)); } }
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() { } }
-
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.
- 05-22-2012, 02:22 PM #12
Member
- Join Date
- May 2012
- Posts
- 17
- Rep Power
- 0
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
-
JPanels ?
By santa in forum New To JavaReplies: 5Last Post: 02-25-2012, 07:10 PM -
Public API for Custom XML Parts & Custom Parts, Graphics Rendering
By sherazam in forum Java SoftwareReplies: 0Last Post: 09-12-2011, 01:06 PM -
Switching JPanels with a button inside one of the JPanels
By Beastly in forum AWT / SwingReplies: 2Last Post: 04-26-2011, 02:50 PM -
Problems regarding JPanels in JPanels
By ColtonPhillips in forum AWT / SwingReplies: 2Last Post: 07-19-2010, 08:33 PM -
HELP with jPanels
By maverik_vz in forum AWT / SwingReplies: 1Last Post: 03-12-2009, 11:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks