views:

156

answers:

4

Continuing my quest of learning Java by doing a simple game, i stumbled upon a little issue. My gameboard extends JPanel as well as each piece of the board. Now, this presents some problems:

  1. Cant set size of each piece, therefore, each piece JPanel ocupy the whole JFrame, concealing the rest of the pieces and the background (gameboard).

  2. Cant set the position of the pieces.

I have the default flow manager. Tried setbounds and no luck.

Perhaps i should make the piece to extend other JComponent? Added image: That's the piece, now the greyed area is also the piece! Checked that by making a mousePressed listener and assigning some stuff to it. Below the grey area, is the gameboard (or at least, should be!), another JPanel.

alt text

Some code:

package TheProject;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GameWindow extends JFrame {
public GameWindow() {
    setSize(800, 600);
    setLocationRelativeTo(null);
    Map map = new Map(800, 600, 2);
    add(map);

    MilitaryUnit imperialRussia = new MilitaryUnit(30, Color.BLACK, Color.YELLOW, Color.WHITE);
    imperialRussia.setPreferredSize(new Dimension(30, 30));
    add(imperialRussia);

}
 }

This happens when i apply the pack() method: alt text

Packs around the Unit, not the map which is bigger and fills the JFrame.

A: 

Have you tried setPreferredSize(Dimension d)?

Edit: You need to call pack() on your JFrame, after you've added your components:

JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(800,600));
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(200,200));
frame.add(panel);
frame.pack();
frame.setvisible(true);

Also, you add a Map to your JFrame, which has the same dimensions as your JFrame - fine. But then afterwards, you add another component, to the default flowlayout. This cannot fit into your frame, as the Map already occupies 100% of the space.

Frederik Wordenskjold
Just did, did not work.
Gabriel A. Zorrilla
I think we're missing some details. Post some code, so we can see what you've already tried.
Frederik Wordenskjold
A: 

Have you tried NetBeans' visual editor? You can use it to drag, drop, and resize to your convenience Swing objects in Design View, and can then switch to Code View to see the generated code. You can learn that way.

JRL
+1  A: 

For a game that has random movement of pieces you would probably use a "null layout".

Read the section from the Swing tutorial on Absolute Positioning for more information.

camickr
A: 

I wrote a few games using JPanel. Basically the way I use JPanel is like I'm using a Canvas, viz I draw directly on it by overriding the paint() method. The reason why I use JPanel is because I can determine the size of my game world, then use the setPreferredSize() to set the size of the panel. I then add the panel to a JScrollPane. So this will take care of the panning, etc.

Say I'm writing a 2D game. This is how I use JPanel. I have a logical map (a 2D array) which holds my game map. Say each location is 32x32 pixel. So you start drawing the ground and what is on that ground in that location. eg in x=1, y=2 which is screen location x=32, y=64, you draw the ground first, then draw what is on the ground. So a rough outline of the render loop would be something like this

for (int y = 0; y < map.length; y++)
   for (int x = 0; x < map[y].length; x++) {
      drawGround(map[y][x])
      for very element on on map[y][x] - render them
}      

You set a MouseListener listener to the JPanel, so every mouse click you translate back to the map eg. mouse click x=54, y=72 would correspond to x=1, y=2. The calculation is a bit tricky if you have an isometric view.

The thing you have to be careful here is that everytime when you scroll the panel via the scroll panel, paint() will be called. So it is good to render your game board on a BufferedImage and then in the paint() method just draw the BufferedImage otherwise it'll be too slow.

Chuk Lee
You should be overriding paintComponent() NOT paint() for custom painting.
camickr
I figure that since I'm drawing everything and not customizing the component, I'll use paint(). The only thing maybe I should do is call paintBorder() in paint(). Anyway thanks for commenting on it.
Chuk Lee
Aye, but what if you want to click one unit/piece? How do you locate the piece? That's why i used this approach, with a class for the map/gameboard and another class for units/pieces.
Gabriel A. Zorrilla
When you translate the screen-to-map, you will get a reference to your game world structure; in my example this is the array. So every element in the array will contain a reference to what units are on that location. I assume that only 1 unit can occupy a space. So you know exactly which unit is selected. If you game do not have the concept of 'floor' like a side scroller, then you can apply similar concept. You have all these things flying around on your screen, when you click on them, scan your data structure to see which object is selected.
Chuk Lee
There are lots of Java game framework that you can use and learn Java at the same timehttp://www.13thmonkey.org/~boris/jgame/http://goldenstudios.or.id/products/GTGE/http://www.interactivepulp.com/pulpcore/I have used GTGE and pulpcore. They are open source so you can look at how they are written. Most of them use Canvas though.You can also look at a game which I'm wrote (and abandoned, don't ask)https://carcassonne.dev.java.net/
Chuk Lee