Which layout manager to use on this game?
-
LayoutManager should not be used for games. Just use a panel and draw your board on it. The 'Start' button can be placed absolute (or maybe also drawn). Since the tiles have to be placed random(?) I don't think they need any layout and should be placed absolute aswell. – kaetzacoatl Jan 24 '17 at 19:00
-
Unless you're really up to writing your own, I wouldn't (or at least I wouldn't use a component based solution). Instead, I use a custom painting solution instead. It will give you greater level of flexibility and control without some of the issues that managing multiple components can introduce – MadProgrammer Jan 24 '17 at 19:20
-
2For [example](http://stackoverflow.com/questions/33163298/dragging-image-using-mousedrag-method/33163542#33163542) and [example](http://stackoverflow.com/questions/33402446/how-to-click-and-drag-something-without-it-deselecting/33403429#33403429) – MadProgrammer Jan 24 '17 at 19:23
-
@MadProgrammer I am! But plis tell me how to start? :D To implement layoutmanager2 ? And custom painting is in paintComponent method? – getXgetY Jan 24 '17 at 19:30
-
@Jeja Custom painting is using `paintComponent`, but instead of using multiple components, you render all your shapes within the same component. The benefit is, you gain complete control. Both the linked examples above demonstrate the basic idea – MadProgrammer Jan 24 '17 at 19:31
-
@kaetzacoatl to draw board whit paintComponent method? How to place them absolute? – getXgetY Jan 24 '17 at 19:31
-
1A [component based solution](http://stackoverflow.com/questions/27915214/how-can-i-drag-images-with-the-mouse-cursor-in-java-gui/27915358#27915358) using a `JLayeredPane` - a little more complex, but a basic idea – MadProgrammer Jan 24 '17 at 19:31
-
2@Jeja *"to draw board whit paintComponent method? How to place them absolute?"* - That's the beauty of it, you have direct access to the `Graphics` context, so you paint whatever you want, wherever you want. I'd start by defining some basic concepts. You have a "piece", which has a size (width/height), which has a position (x/y) and which can be painted (it will also need to know if it can fit inside another area or not, but you can focus on that later). Most of that falls within the bounds of `java.awt.Rectangle`, which would be a good idea to use – MadProgrammer Jan 24 '17 at 19:35
-
1With that, you can define a number of different pieces (you may also want to store the color) that meet your requirements. In your "board" class, you would maintain a `List` of these pieces, based on your needs and then using the `paintComponent`, paint them – MadProgrammer Jan 24 '17 at 19:37
-
@MadProgrammer i will think about it...sry for bordering , I must ask..can I just use Image for parts of the puzzle? And then make it like 1 component? :D – getXgetY Jan 24 '17 at 19:38
-
I was thinking on using custom painting, but I was unsure if it would be the best approach, however reading your comments @MadProgrammer, I see I wasn't wrong. I like when you write tons of useful comments that actually solve OP's question w/o writing the code, because you've written a lot of similar codes before :) – Frakcool Jan 24 '17 at 19:39
-
@Jeja Yes, the basic concept remains, it has a size, location and can be painted ;) – MadProgrammer Jan 24 '17 at 19:41
-
@Frakcool Yea to having to write more code :P – MadProgrammer Jan 24 '17 at 19:41
-
You are the best @MadProgrammer :DD you give me a idea for solution! I probably will ask later on more about this...just need to study this a little. – getXgetY Jan 24 '17 at 19:51
2 Answers
IMHO, using layouts and components is not a good solution to your problem, personally, I'd lean towards a custom painting solution instead.
Start with a basic concept of a piece, it needs to know it's location, it's size, it's color, be able to paint itself and possibly be relocatable, something like...
public interface Piece {
public Rectangle getBounds();
public Color getColor();
public void setLocation(Point point);
public void paint(Graphics2D g2d);
}
From this, you can define what ever shapes you need, for example...
public abstract class AbstractPiece implements Piece {
private Rectangle bounds;
private Color color;
@Override
public void setLocation(Point point) {
bounds.setLocation(point);
}
@Override
public Rectangle getBounds() {
return bounds;
}
@Override
public Color getColor() {
return color;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public void setColor(Color color) {
this.color = color;
}
}
public class Square extends AbstractPiece {
public Square(Point location, int size, Color color) {
Rectangle bounds = new Rectangle();
bounds.setLocation(location);
bounds.setSize(size, size);
setBounds(bounds);
setColor(color);
}
@Override
public void paint(Graphics2D g2d) {
g2d.setColor(getColor());
g2d.fill(getBounds());
g2d.setColor(Color.GRAY);
Rectangle bounds = getBounds();
g2d.drawLine(bounds.x + (bounds.width / 2), bounds.y, bounds.x + (bounds.width / 2), bounds.y + bounds.height);
g2d.drawLine(bounds.x, bounds.y + (bounds.height / 2), bounds.x + bounds.width, bounds.y + (bounds.height / 2));
}
}
This is just a basic square, nothing fancy, but, it's self contained, easy to create and manage. You can create any combination of shapes you like using this simple pattern, at the end of the day, your board class won't care, it just needs to the space it occupies and how to paint it, speaking for which, you need some kind of container to manage all these shapes...
public class PuzzelPane extends JPanel {
private List<Piece> pieces;
public PuzzelPane() {
Dimension size = getPreferredSize();
pieces = new ArrayList<>(25);
Point location = new Point((size.width - 50) / 2, (size.width - 50) / 2);
pieces.add(new Square(location, 50, Color.BLUE));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Piece piece : pieces) {
Graphics2D g2d = (Graphics2D) g.create();
piece.paint(g2d);
g2d.dispose();
}
}
}
This is a really simply concept, it has a List
to maintain all the available shapes and simply loops over this to paint them in the paintComponent
method
Couple it with the idea from this example and this example and you have the ability to now drag the shapes

- 1
- 1

- 343,457
- 22
- 230
- 366
-
Really tnx you <3 ,I am tierd now,I was trying to make that like 4 days...When I get some sleep , I will make this tomorow. And probably ask somthing again..thank you very much for the eford! :D – getXgetY Jan 24 '17 at 20:04
-
1@Jeja If you have the time available to you, I would suggest having a go at using a component based solution, preferably using a custom layout manager, just so you can compare the differences, there are pros and cons to both approaches ;) – MadProgrammer Jan 24 '17 at 21:01
-
Oh, look, another, unsolicited downvote, what a surprised, not. Since every answer I've posted in the last two weeks has attracted your downvote, and no other answers have been downvoted nor any new downvotes/close votes to the question have been added, I can only assume you have a personal dislike of me and my participation on SO, of course, I'd welcome any new information you can bring to the question, maybe even providing your own unique point of view to enlighten us all about the things I'm not covering in my answers – MadProgrammer Jan 25 '17 at 03:39
-
@MadProgrammer don't you have access to whom upvotes and downvotes a question? Also, isn't SO supposed to be able to pick up on accounts that continuously vote on a specific account and either notify or ban them? I could be wrong about both of those things, still somewhat new to SO and don't know all the intricacies of it. – CraigR8806 Jan 25 '17 at 12:15
-
@MadProgrammer huh are you telling me that? I am so happy you helped me whit this...I cant give you downvote xD and there is my question: how to get an element from arraylist in java and set to jpanel? I whant to create new JPanel and that JPanel will have movement skills :D? – getXgetY Jan 25 '17 at 13:24
-
@CraigR8806 No, you can't tell who is voting on your answers/questions. Since I'm only posting 1-2 answers a day, it's not getting picked up as a serial voter, time period between votes is to long – MadProgrammer Jan 25 '17 at 22:27
-
@Jeja No, this is a problem which is been occurring to me over the last couple of weeks ;). `ArrayList#get` is how you access elements – MadProgrammer Jan 25 '17 at 22:28
-
@MadProgrammer huh xD ..so I know to access elemetn..but I confuze to make a container for elements. I kind stuck there...so how to make container for elements that will moving and 1 element will not(the board) :)? – getXgetY Jan 26 '17 at 07:28
-
@Jeja Based on the example above, you could have a single reference to the border element and a `List` of pieces and manage them seperatly – MadProgrammer Jan 26 '17 at 07:30
-
@MadProgrammer yes I was thinking in that why, but I am confuse how to make container that will hold segments and board? :S Sry to lay up on you :/ – getXgetY Jan 26 '17 at 07:34
-
@Jeja This will get complicated fast, my "gut" feeling is to use the `Shape` API to generate compound shapes which make up your pieces, this avoids you some more functionality, as you can combine the pieces into a large piece and perform intersection/contains operations within the supplied API, which would make it easier to determine if pieces fit or not. That, unfortunately, is some what complicated to get started with. The above example would allow you to get started, as you have the bases for the pieces and the board is just a speical piece – MadProgrammer Jan 26 '17 at 08:02
To expand on kaetzacoatl's comment, you should not use a LayoutManager for this at all, for several reasons:
- They do not have the flexibility for more complex setups.
- They are quite hard to work with for non-trivial movement of elements.
- They mostly make sense for elements that can be resized and maybe wrapped or stretched, which I assume does not make sense for tiles in a puzzle game.
Instead, I would advise to use something like a canvas and draw your puzzle pieces with coordinates.

- 1,316
- 2
- 15
- 28
-
1Oh [really](http://stackoverflow.com/questions/21247833/how-to-prevent-jlabel-positions-from-resetting/21248274#21248274), [really](http://stackoverflow.com/questions/27974966/moving-jpasswordfield-to-absolute-position/27975101#27975101), [really](http://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601), [really](http://stackoverflow.com/questions/13698217/how-to-make-draggable-components-with-imageicon/13700490#13700490) – MadProgrammer Jan 24 '17 at 19:18
-
1[really](http://stackoverflow.com/questions/15844070/components-disappear-after-resizing-jpanel/15844981#15844981), [really](http://stackoverflow.com/questions/33883330/cant-move-jlabel-on-jpanel/33884907#33884907), [really](http://stackoverflow.com/questions/14635952/java-layout-proportions-creating-a-scalable-square-panel/14636828#14636828), [really](http://stackoverflow.com/questions/17847816/position-image-in-any-screen-resolution/17848635#17848635)? My personal gut feeling would be to use a `JPanel` (it's more flexible the `Canvas`) and use a custom painting approach as well though ;) – MadProgrammer Jan 24 '17 at 19:19
-
:P Well, I'm not saying you cannot use a LayoutManager for that (althoug null layout doesn't really count for me), it's just my experience that it's more complicated for cases like this. – Silverclaw Jan 24 '17 at 19:23
-
1Agreed, `null` layout is generally just a bad idea, custom layouts are probably to complex for the problem, custom painting becomes an easier proposition ;) – MadProgrammer Jan 24 '17 at 19:24