Reputation: 61
Which layout manager to use on this game?
Upvotes: 3
Views: 425
Reputation: 347332
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
Upvotes: 7
Reputation: 1396
To expand on kaetzacoatl's comment, you should not use a LayoutManager for this at all, for several reasons:
Instead, I would advise to use something like a canvas and draw your puzzle pieces with coordinates.
Upvotes: 0