2

I was trying to make a board game in java and i am stuck with the following problem.

I want to create a object that can be painted in the jpanel. Let's name this object Board. This board i want it to be a composite object of other boards. Hence when i call the (let's call it) paint() method of the board , in order to be correctly painted it will have to call paint it self and also call the paint() methods of its sub-boards etc. I know this is like an already implemented behavior (someone could say.. just make a Jpanel in a Jpanel) but the difference is that for the board object i want to be able to drag/ drop it anywhere in the Jpanel. Plus, the Jpanel may contain many boards which could be dragged/dropped anywhere in it (the can also overlap). So this kind of behavior restrict me from using Java swing components because they all use layouts which is not what i want.

Finally when i add a board object to a board the coords i use want to be relative to the parent board and not to the Jpanel's. (i.e when i add a board B to the board A, and i want to add the board B to (x,y)=(10,10) , these coords are relative to the location of A )

Is this any way to do it? I 'm not looking for complete example, just for general ideas (preferable clean!)

PS . i have implemented something that almost works. The problem in my implementation is that Graphics g , class Board uses to be painted is taken from the JPanel, and the x,y coords are relative to the JPanel for all the Boards (and not the the parent board in case the board is added to a parent board and not directly to JPanel).

i don't know if this is fusible but i am imaging that the leaf board is acutally painting it self with starting coords (0,0) which somehow in the JPanel are traslated to (x,y) depending on the actual location in the Jpanel of the board.

Thank you!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
C.LS
  • 1,319
  • 2
  • 17
  • 35

1 Answers1

2

I think you could consider using MovingWindows and ResizingComponents. I did something similar in the past using a null LayoutManager but this is a bad idea.

There are several problems when you need to absolute positioning Components or explicitly set their size.

Here's some 2 threads you might consider intersting:

  1. The first discussing a similar problem.
  2. The second explains why you can't directly use setXXXSize methods on Component.

The key for understanding those solutions is that you should never set from the application code, the size of your components. If you need to resize something you have to do that on frames.

Community
  • 1
  • 1
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • @mKorbel: I learnt that just a few days ago. Before I didn't know any suitable solution without using null layout. – Heisenbug Nov 01 '11 at 17:15
  • Thank you for the prompt answer. I was wondering though, using the already made swing components and altering the way they should behave to match my standards isn't a bad practice? I had a bad experience when i used a JButton as a tile in another board game i made (and that is why i decided to go and write my own components). BTW does the aforementioned solutions you propose in the links, support overlapping of the components? – C.LS Nov 02 '11 at 09:18
  • Altering the way they should behave to match your standards is often a bad practice. 2 way of altering the standard are: 1)use a null layout 2)try to explicitly set the dimension of a component (only the layoutmanager is responsible for that). – Heisenbug Nov 02 '11 at 09:25