我正在开展一个大学项目,但在将组件与电路板网格对齐时遇到困难。该板将始终保持 1:1 的宽高比,但尺寸可能会有所不同。这是我尝试过的结果:
为什么百分比与我计算的实际图像尺寸 1200x1200 的百分比不一致?
这是我的布局代码:
public class BoardPanel extends ViewComponent {
public static final int ROWS = 27;
public static final int COLS = 23;
private Board board = new Board();
private BufferedImage background = ResourceLoader.openImage("images/board.jpg");
public BoardPanel() {
this.add(board, "pos 4.5% 4.5%, width 76.5%, height 91%");
}
@Override
public void paintContent(Graphics2D g) {
g.drawImage(background, 0, 0, getHeight(), getHeight(), null);
}
private class Board extends ViewComponent {
public Board() {
setLayout(new GridLayout(ROWS, COLS, 1, 1));
for (int row = 0; row < ROWS; row++)
for (int col = 0; col < COLS; col++)
this.add(new Location(row, col));
}
}
}
最佳答案
作为替代方案,请使用 BufferedImage#getSubimage()
将图像分成一致的部分,如图 here .
关于java - 在具有基于网格的背景的网格中排列组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12143989/