1

Hi guys i'm trying to create a draughts game in Java and am using JPanels to represent the squares, if I were to change the size of the panels how would I do so ? if I use a layout manager the squares are not big enough. At the moment i'm not using a layout manager to try and change the size, but the size doesnt seem to change - just stays at 1,1 pixel.

 private void createSquares(){
    for(int i = 0; i < 65; i++){
        squares[i] = new JPanel();
        squares[i].setLayout(null);
        squares[i].setSize(20,20);
        board.add(squares[i]);
    }
}
Robin Green
  • 32,079
  • 16
  • 104
  • 187
John Collins
  • 121
  • 2
  • 14
  • 1
    Yes, use a layout manager. No, never use a `null` layout. – Hovercraft Full Of Eels Nov 16 '13 at 16:13
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). (What @HovercraftFullOfEels said, but longer.) – Andrew Thompson Nov 16 '13 at 16:13
  • 1
    See also this [example](http://stackoverflow.com/a/2562685/230513) and [variation](http://stackoverflow.com/a/2563350/230513). – trashgod Nov 16 '13 at 16:15
  • Ok but how would I resize each JPanel ? if I delete the setLayout line the size stays the same even when I use setSize – John Collins Nov 16 '13 at 16:20
  • 1
    Please read the layout manager tutorial. If you do this, you'll find out that JPanels use FlowLayout by default and that top level windows use BorderLayout, and just what this means. – Hovercraft Full Of Eels Nov 16 '13 at 16:25
  • 1
    Override [`getPreferredSize()`](http://stackoverflow.com/q/7229226/230513) to establish the initial size, and use `GridLayout` to follow changes in the enclosing container's size. – trashgod Nov 16 '13 at 16:27

1 Answers1

0

You could always "borrow" an image or two online, and put that into your program. For example this code:

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class GetChessSquareImages {
   public static final String PATH_TO_SQUARES = "http://www.colourbox.com/preview/" +
        "4578561-622234-seamless-oak-square-chess-like-parquet-texture.jpg";
   private static final int IMG_SIDE_COUNT = 4;
   private static final double SCALE = 0.8;
   private Map<SquareColor, List<Icon>> squareColorMap = new HashMap<SquareColor, List<Icon>>(); 
   private Random random = new Random();

   public void downloadImages() throws IOException {
      URL lrgImgUrl = new URL(PATH_TO_SQUARES);
      BufferedImage largeImg = ImageIO.read(lrgImgUrl);

      int w = largeImg.getWidth() / IMG_SIDE_COUNT;
      int h = largeImg.getHeight() / IMG_SIDE_COUNT;
      for (int i = 0; i < IMG_SIDE_COUNT; i++) {
         int x = (i * largeImg.getWidth()) / IMG_SIDE_COUNT;
         for (int j = 0; j < IMG_SIDE_COUNT; j++) {
            if (j != 1 && j != 2) {
               int y = (j * largeImg.getHeight()) / IMG_SIDE_COUNT;
               extractSubImg(largeImg, i, j, x, y, w, h);
            }
         }
      }
   }

   private void extractSubImg(BufferedImage largeImg, 
         int i, int j, int x, int y, int w, int h) {
      Image subImg = largeImg.getSubimage(x, y, w, h);
      int width = (int) (w * SCALE);
      int height = (int) (h * SCALE);
      subImg = subImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);
      List<Icon> iconList = null;
      if (i % 2 == j % 2) {
         iconList = squareColorMap.get(SquareColor.LIGHT);
         if (iconList == null) {
            iconList = new ArrayList<Icon>();
            squareColorMap.put(SquareColor.LIGHT, iconList);
         }
      } else {
         iconList = squareColorMap.get(SquareColor.DARK);
         if (iconList == null) {
            iconList = new ArrayList<Icon>();
            squareColorMap.put(SquareColor.DARK, iconList);
         }
      }
      iconList.add(new ImageIcon(subImg));
   }

   public Icon getRandomIcon(SquareColor sqrColor) {
      List<Icon> iconList = squareColorMap.get(sqrColor);
      if (iconList == null) {
         return null;
      } else {
         return iconList.get(random.nextInt(iconList.size()));
      }
   }

   public static void main(String[] args) {
      GetChessSquareImages getImages = new GetChessSquareImages();
      try {
         getImages.downloadImages();
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      int side = 8;;
      JPanel panel = new JPanel(new GridLayout(side , side));
      for (int i = 0; i < side; i++) {
         for (int j = 0; j < side; j++) {
            SquareColor sqrColor = (i % 2 == j % 2) ? SquareColor.LIGHT : SquareColor.DARK;
            Icon icon = getImages.getRandomIcon(sqrColor);
            panel.add(new JLabel(icon));
         }
      }
      JOptionPane.showMessageDialog(null, panel);
   }
}

enum SquareColor {
   DARK, LIGHT
}

returns this JPanel:

enter image description here

Then your square size will be based on the sizes of your ImageIcons. For example, I have scaled my squares back with a scale factor of 0.8 (the SCALE constant above) to make the grid a more reasonable size.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373