When using a GridLayout, it diveds its container's space evenly between its row and column elements. However when calcuating the cell width wouldn't turn out in an integer, the exceeding space is put between the container's edges and its content in a way, that the container's content is centered by the GridLayout.
In this picture you can see exceeding space (colored green):
Since the frame's size is dragged to 233x233 the LayoutManager would offer each component floor(233 / 20) = 11 pixels height and width. Thus 233 % 20 = 13 pixels exceed and are put at the edges.
That's the code to generate the frame in the picture:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(20, 0);
layout.setHgap(0);
layout.setVgap(0);
frame.setLayout(layout);
for (int j = 0; j < 20; j++) {
for (int i = 0; i < 20; i++) {
JPanel panel = new JPanel();
panel.setBackground((i + j) % 2 == 0 ? Color.BLACK : Color.WHITE);
frame.add(panel);
}
}
frame.pack();
frame.getContentPane().setBackground(Color.GREEN);
frame.setVisible(true);
So I wonder if there's an easy way to make the container 'clip' or align in a way, that this exceeding space doesn't show up, but instead the container is resized to fit its content perfectly.
That's right but I want to keep the advantage of a GridLayout offering every cell the same size,
Yes well you can't have it both ways.
If you want every cell to be the same size, then you will see the background. One option would be to make the panel non-opaque, so you don't see the background of the panel.
If you want to completely fill the area available with the components, then some components will need to be a different size by one pixel.
For implementing point 2, maybe this example will be easy enough for you to use:
import java.awt.*;
import javax.swing.*;
public class BoardTest
{
private static void createAndShowGUI()
{
Float constraint = new Float(1);
RelativeLayout boardLayout = new RelativeLayout(RelativeLayout.Y_AXIS);
boardLayout.setRoundingPolicy( RelativeLayout.EQUAL );
boardLayout.setFill(true);
JPanel board = new JPanel(boardLayout);
board.setBackground(Color.GREEN);
RelativeLayout rowLayout = new RelativeLayout(RelativeLayout.X_AXIS);
rowLayout.setRoundingPolicy( RelativeLayout.EQUAL );
rowLayout.setFill(true);
for (int j = 0; j < 20; j++)
{
JPanel row = new JPanel( rowLayout );
for (int i = 0; i < 20; i++)
{
JPanel square = new JPanel();
square.setBackground((i + j) % 2 == 0 ? Color.BLACK : Color.WHITE);
row.add(square, constraint);
}
board.add(row, constraint);
}
JFrame frame = new JFrame("BoardTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(board);
frame.pack();
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater( () -> createAndShowGUI() );
/*
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
*/
}
}
It uses the Relative Layout class which allows you to control how the extra pixels are allocated to each of the components.
Related
I am attempting to use Java Swing to create a Grid that allows me to access specific panels if I need to. The frame pulls up but there are no panels. I would like some advice on how to make the panels display in the frame while still allowing me to access the specific panel through the two dimensional array.
x=0;
y=0;
z=0;
JPanel[][] coordinate = new JPanel[5][5];
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(550,550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
panel.setBackground(Color.WHITE);
panel.setLayout(new GridLayout(5,5));
for(int i = 0; i<25; i++) {
if(z == 5) {
z = 0;
x = 0;
y++;
}
coordinate[x][y] = new JPanel();
coordinate[x][y].setBorder(BorderFactory.createLineBorder(Color.BLACK, 10));
panel.add(coordinate[x][y]);
z++;
x++;
}
frame.add(panel);
I am trying to use a GridBagLayout to have a JFrame that contains a JPanel that has a grid layout and a JPanel with just a large button. I want the rows to all be the same size, and the JPanel with the JButton to be the same size as one row. However, the button panel, which is currently empty, is about 1/3 of the JFrame. I'm not quite sure what's happening, but it is pretty important to me that I maintain this structure because the rest of my code uses this. Any help is appreciated, and thank you in advance.
This is my code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Minesweeper extends JPanel {
private final int SIZE = 7;
public void startGame(){
JFrame holder = new JFrame();
JPanel window = new JPanel();
JPanel pan = new JPanel();
holder.setLayout(new GridBagLayout());
GridBagConstraints con = new GridBagConstraints();
con.weightx = 1;
con.weighty = 1;
con.gridx = 0;
con.gridy = 0;
con.fill = GridBagConstraints.BOTH;
con.gridheight = SIZE;
con.gridwidth = SIZE;
holder.getContentPane().setBackground(Color.darkGray);
holder.setSize(450, 450);
holder.setResizable(false);
holder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBackground(Color.darkGray);
window.setLayout(new GridLayout(SIZE, SIZE));
for (int c=0; c<(SIZE*SIZE); c++){
int row = (c/SIZE);
int col = (c%SIZE);
JPanel p = new JPanel();
p.setBackground(Color.gray);
Border b = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
p.setBorder(b);
window.add(p);
}
holder.add(window, con);
con.gridx = 0;
con.gridy = SIZE+1;
con.gridheight = 0;
con.gridwidth = SIZE;
holder.add(pan, con);
holder.setVisible(true);
}
public static void main(String[] args){
Minesweeper start = new Minesweeper();
start.startGame();
}
}
This is what is being shown:
con.gridy = SIZE+1;
You can't specify a gridy value of 8. There are only two components added to the grid. The grid doesn't know that one of your panels happens to contain 7 rows of components. So the value should be 1.
This won't solve the problem but should clear up a misunderstanding of how GridBagLayout works.
holder.setSize(450, 450);
You are manually setting a size to the frame. Each component is originally sized at its preferred size. When there is extra space in the frame the space is distributed equally between the two components.
You should NOT be setting the size. Each component should determine its own size and then you should use pack(). So you need to use custom components that override the getPreferredSize() method to return the appropriate size for each component so pack() can do its job.
Also, the pack() is done just before the setVisible().
This question already has answers here:
Making a robust, resizable Swing Chess GUI [closed]
(2 answers)
Closed 8 years ago.
I have a simple Chess board in a JPanel with GridLayout(8,8) as layout manager.
I am trying to add panels for the fields' column name and row number.
Right now I've created another panel with BorderLayout as layout manager, and in this panel I add the board in BorderLayout.CENTER. Next to the board itself I've added a panels with GridLayout(0,8) in BorderLayout.SOUTH and a panel with GridLayout(8,0) in BorderLayout.WEST. The rows numbers is perfectly placed next to the board because the number of rows in the left JPanel matches the number of rows in the board, but the column names (A, B, C, D, E, F, G, H) in the JPanel under the board is not placed correctly because of the JPanel in BorderLayout.WEST.
What can I do to make a proper Chess board with side panels to show the field numbers/names?
I've tried setting the layout for the south panel to GridLayout(0,9) and have the first field empty, but the width of the left panel is not equal to each field in the board, so it's not a good workaround.
Note
The GUI seen here has been improved and moved to Making a robust, resizable Swing Chess GUI.
I will leave the animated GIF here (because it's cute) and the original, stripped down code (of just 125 code lines, the final code seen on the other thread is 218 LOC).
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
public class ChessBoardWithColumnsAndRows {
private final JPanel gui = new JPanel(new BorderLayout(3, 3));
private JButton[][] chessBoardSquares = new JButton[8][8];
private JPanel chessBoard;
private final JLabel message = new JLabel(
"Chess Champ is ready to play!");
private static final String COLS = "ABCDEFGH";
ChessBoardWithColumnsAndRows() {
initializeGui();
}
public final void initializeGui() {
// set up the main GUI
gui.setBorder(new EmptyBorder(5, 5, 5, 5));
JToolBar tools = new JToolBar();
tools.setFloatable(false);
gui.add(tools, BorderLayout.PAGE_START);
tools.add(new JButton("New")); // TODO - add functionality!
tools.add(new JButton("Save")); // TODO - add functionality!
tools.add(new JButton("Restore")); // TODO - add functionality!
tools.addSeparator();
tools.add(new JButton("Resign")); // TODO - add functionality!
tools.addSeparator();
tools.add(message);
gui.add(new JLabel("?"), BorderLayout.LINE_START);
chessBoard = new JPanel(new GridLayout(0, 9));
chessBoard.setBorder(new LineBorder(Color.BLACK));
gui.add(chessBoard);
// create the chess board squares
Insets buttonMargin = new Insets(0,0,0,0);
for (int ii = 0; ii < chessBoardSquares.length; ii++) {
for (int jj = 0; jj < chessBoardSquares[ii].length; jj++) {
JButton b = new JButton();
b.setMargin(buttonMargin);
// our chess pieces are 64x64 px in size, so we'll
// 'fill this in' using a transparent icon..
ImageIcon icon = new ImageIcon(
new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB));
b.setIcon(icon);
if ((jj % 2 == 1 && ii % 2 == 1)
//) {
|| (jj % 2 == 0 && ii % 2 == 0)) {
b.setBackground(Color.WHITE);
} else {
b.setBackground(Color.BLACK);
}
chessBoardSquares[jj][ii] = b;
}
}
//fill the chess board
chessBoard.add(new JLabel(""));
// fill the top row
for (int ii = 0; ii < 8; ii++) {
chessBoard.add(
new JLabel(COLS.substring(ii, ii + 1),
SwingConstants.CENTER));
}
// fill the black non-pawn piece row
for (int ii = 0; ii < 8; ii++) {
for (int jj = 0; jj < 8; jj++) {
switch (jj) {
case 0:
chessBoard.add(new JLabel("" + (ii + 1),
SwingConstants.CENTER));
default:
chessBoard.add(chessBoardSquares[jj][ii]);
}
}
}
}
public final JComponent getChessBoard() {
return chessBoard;
}
public final JComponent getGui() {
return gui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
ChessBoardWithColumnsAndRows cb =
new ChessBoardWithColumnsAndRows();
JFrame f = new JFrame("ChessChamp");
f.add(cb.getGui());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// ensures the minimum size is enforced.
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
Notes
The chess board complete with columns on the left and a row above it is provided by a 9x9 GridLayout. The first cell of the grid layout is a label with no text.
To simplify the game logic though, we maintain a separate 8x8 array of buttons.
To allow keyboard functionality we use buttons for the chess board places. This also provides inbuilt focus indication. Remove the margin of the button to allow them to shrink to the size of the icon. Add an ActionListener to the button and it will respond to both keyboard and mouse events.
The small ? in the left hand side of the GUI is meant to imply that area is 'reserved for future use'. We might use it to show lists of captured pieces, a selector for choice of piece when promoting pawns, game statistics, ...
The chess piece images were obtained from Example images for code and mark-up Q&As, which was in turn developed out of 'Fill' Unicode characters in labels.
Using images is simpler, whereas filling Unicode characters is more versatile as well as being 'lighter'. I.E. to support 4 different colors in 3 separate sizes of 3 different chess piece styles would require 36 separate sprite sheets!
i have a Jframe which has some panels as instance variables and one of the panels is a grid board (i am implementing Lines of action game). After my game ends I have a button "Play again" which i want to reinitialize my board panel. I tried a lot of things like removing my panel from the content pane and re-initializing it, but nothing worked so far. Here are some of the things i tried (i didn't try them all at once )
public class Frame extends JFrame implements MouseListener{
JLabel l = new JLabel();
Panel1 Boards;
Panel2 newGame;
Panel3 winner;
Point lastCheckerSelected;
Board game = new Board();
public Frame() {
setResizable(false);
setTitle("Lines Of Action");
setBounds(290, 350, 1000, 700);
setLayout(null);
winner=new Panel3();
winner.playAgain.addMouseListener(this);
getContentPane().add(winner);
Boards= new Panel1();
getContentPane().add(Boards);
setDefaultCloseOperation(EXIT_ON_CLOSE);
l.setIcon(new ImageIcon(
"E:\\background0213.jpg"));
l.setBounds(0 ,0 , 1000 , 700);
getContentPane().add(l);
validate();
newGame=new Panel2();
newGame.b.addMouseListener(this);
getContentPane().add(newGame);
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
Boards.x[i][j].addMouseListener(this);
Boards.y[i][j].addMouseListener(this);
}
}
}
public void mouseClicked(MouseEvent e) {
if(e.getSource().equals(newGame.b)) {
Boards.setVisible(true);
newGame.setVisible(false);
game=new Board();
}
if(this.game.getWinner()==1) {
winner.setVisible(true);
winner.whiteWins.setVisible(true);
}
if(this.game.getWinner()==2) {
winner.setVisible(true);
winner.greywins.setVisible(true);
}
if(e.getSource().equals(winner.playAgain)) {
//this.getContentPane().remove(Boards);
// this.game= new Board();
// Boards = new Panel1();
// this.getContentPane().add(Boards);
//Boards.setVisible(true);
// validate();
// Boards.repaint();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
}
I still cant make my new panel appear ( removing the Boards panel from the content pane makes it disappear which is good but the new one does not appear)
here is my panel =1 that contains the board
public class Panel1 extends JPanel {
JButton[][] x = new JButton[8][8];
JButton[][] y=new JButton[8][8];
Graphics g;
public Panel1() {
setBounds(0, 30 ,400 ,400);
setLayout(new GridLayout(8, 8));
setOpaque(false);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
x[i][j] = new JButton();
x[i][j].setSize(50, 50);
if ((i % 2 == 0 && j % 2 == 0) || (i % 2 != 0 && j % 2 != 0)) {
x[i][j].setBackground(Color.DARK_GRAY.darker());
// x[i][j].setBackground(new Color(111,89,81,150));
}
else {
// Color.OPAQUE = 2;
x[i][j].setBackground(Color.red.darker().darker());
// x[i][j].setBackground(new Color(223,37,32,150));
}
x[i][j].setEnabled(false);
add(x[i][j]);
}
}
for(int i = 0; i < 8 ;i++){
for(int j=0;j < 8;j++){
y[i][j]=new JButton();
x[i][j].add(y[i][j]);
// y[i][j].setSize(100,100);
// y[i][j].setEnabled(false);
// y[i][j].setOpaque(false);
// y[i][j].setContentAreaFilled(false);
// y[i][j].setBorderPainted(false);
y[i][j].setVisible(false);
}
}
for(int i=1;i<7;i++){
// y[0][i].setOpaque(true);
y[0][i].setBackground(Color.white);
y[0][i].setEnabled(true);
y[0][i].setVisible(true);
// y[7][i].setOpaque(true);
y[7][i].setBackground(Color.white);
y[7][i].setEnabled(true);
y[7][i].setVisible(true);
}
for(int i=1;i<7;i++){
// y[i][0].setOpaque(true);
y[i][0].setEnabled(true);
y[i][0].setBackground(new Color(102,125,153));
y[i][0].setVisible(true);
// y[i][7].setOpaque(true);
y[i][7].setEnabled(true);
y[i][7].setBackground(new Color(102,125,153));
y[i][7].setVisible(true);
}
// addMouseListener(this);
setVisible(false);
}
}
You could also try this:
if(e.getSource().equals(winner.playAgain))
{
Boards.removeAll();
revalidate();
repaint();
}
I don't think you need to create a new Panel1 instance.
i have a Jframe which has some panels as instance variables and one of
the panels is a grid board (i am implementing Lines of action game).
After my game ends I have a button "Play again" which i want to
reinitialize my board panel. I tried a lot of things like removing my
panel from the content pane and re-initializing it, but nothing worked
so far.
I think that CardLayout is best of choices
Try these four together. Need to see more of your code to make sure this work.
if(e.getSource().equals(winner.playAgain)) {
this.getContentPane().remove(Boards);
Boards = new Panel1();
this.getContentPane().add(Boards);
this.invalidate();
this.validate();
this.repaint();
}
After the components are made visible in the screen, if you remove and add components, then you have to call Component.repaint() or Component.validate() to call the repainting again. Do this inside your actionPerformed() of your playAgainButton()
The problem is when I set the background color of the square JPanel as square.setBackground(colors[j]) the square draws only the first color of the list of colors without displaying the other 3. This is my code:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
#SuppressWarnings({ "unused", "serial" })
public class RegionPartition extends JFrame
{
JLayeredPane layeredPane;
JPanel regionBoard;
JLabel regionPiece;
private static int DELAY = 200;
private Color[] colors = new Color[]{Color.PINK, Color.GREEN, Color.BLACK, Color.RED};
public RegionPartition()
{
Dimension boardSize = new Dimension(500, 500);
// Use a Layered Pane for this this application
layeredPane = new JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
regionBoard = new JPanel();
layeredPane.add(regionBoard, JLayeredPane.DEFAULT_LAYER);
regionBoard.setLayout( new GridLayout(4, 4) );
regionBoard.setPreferredSize( boardSize );
regionBoard.setBounds(0, 0, boardSize.width, boardSize.height);
Random random = new Random();
for (int i = 0; i < 16; i++) {
JPanel square = new JPanel(new BorderLayout());
square.setBorder(BorderFactory.createLineBorder(Color.black));
regionBoard.add( square );
square.setBackground(Color.green);
int j=0;
square.setBackground(colors[j]);
j++;
}
}
{
JPanel panel = new JPanel()
{
Clients[] c = new Clients[128];
Random random = new Random();
private final int SIZE = 450;
private int DELAY = 9999999;
public void paintComponent (Graphics g)
{
super.paintComponent(g);
for (int i=0; i<c.length; i++)
{
int x = ( int ) ( random.nextFloat() * SIZE ) + 10;
int y = ( int ) ( random.nextFloat() * SIZE ) + 10;
g.drawOval( x, y, 10, 10 );
g.fillOval(x, y, 10, 10);
}
for (int j=0; j<DELAY; j++)
{
repaint();
}
}
};
panel.setOpaque(false);
//Set the glass pane in the JFrame
setGlassPane(panel);
//Display the panel
panel.setVisible(true);
}
public static void main(String[] args)
{
JFrame frame = new RegionPartition();
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE );
frame.pack();
frame.setResizable(true);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
protected void paintComponent(Graphics g)
{
// TODO Auto-generated method stub
}
}
That is because you are always setting j to 0 on each iteration:
int j=0;
square.setBackground(colors[j]);
j++;
you may want to change j for an i or do a nested loop, that depends on what you really want to do here.
If you want to make all 16 squares have all four colors in a grid like manner, you might want to change your loop to something like:
for (int i = 0; i < 16; i++) {
JPanel square = new JPanel(new GridLayout(2,2));
square.setBorder(BorderFactory.createLineBorder(Color.black));
regionBoard.add( square );
for(int j=0; j<4; ++j){
JPanel insideSquare = new JPanel();
insideSquare.setBackground(colors[j]);
square.add(insideSquare);
}
}
Because you only have 4 colors in your color array, but your loop index exceeds this, you could use:
square.setBackground(colors[ i % colors.length]);
to alternate the colors of your squares.
You are instantiating int j within the scope of your for loop, so its value is not preserved across multiple iterations. You should declare it at a point in your code to allow it scope over your entire for loop.
int j = 0;
<for loop>
square.setBackground(colors[j]);
j++;
<end for>
However, your j is serving the purpose of i in this situation, where i is sufficient as an array index. It would be more correct to remove j entirely and instead do the following:
square.setBackground(colors[i]);