gpt4 book ai didi

java - JLabel 图像数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:42:06 26 4
gpt4 key购买 nike

我试图将同一个 jlabel 存储的图像加载到网格布局面板中两次,但是没有创建图像的两个实例,图像只显示一次然后移动。

如何将 pieces 数组中的相同 JLabel 位置存储到 boardLabels 数组中的多个 JLabel 中。

谢谢:)

public static JPanel boardPanel = new JPanel(new GridLayout(4, 0));
public static JLabel pieces[] = new JLabel[2];
private static JLabel[] boardLabels = new JLabel[4];

public MainFrame() {
pieces[0] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece1.png"));
pieces[1] = new JLabel(new ImageIcon(System.getProperty("user.dir") + "/images/piece2.png"));

this.add(boardPanel);
displayGUIboard();
}


public static void displayGUIboard() {

//ERROR - the label in pieces[0] is not copied into both boardLabels [0] and [1]
boardLabels[0] = pieces[0];
boardLabels[1] = pieces[0];

boardPanel.add(boardLabels[0]);
boardPanel.add(boardLabels[1]);
}

public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

这行得通

    boardLabels[0] = new JLabel(pieces[1]);
boardLabels[1] = new JLabel(pieces[1]);

当使用 ImageIcons 时,但我想避免这种情况,因为要更新板我将不得不删除然后重新加载 JLabel。我宁愿只更新已经加载的标签。

编辑我之前试过这个但是它抛出一个空指针异常...

    boardLabels[0].setIcon(pieces[1]);
boardLabels[1].setIcon(pieces[1]);

boardPanel.add(boardLabels[0]);
boardPanel.add(boardLabels[1]);

最佳答案

为了比较,我重构了@HFOE 的 example以便 Ground 实现 Icon 并索引 values() 返回的数组。由于 value 是一个实现细节,int[][] MAP 可以改为 Ground[][] MAP

更新:此变体说明了 Ground[][] MAP 并添加了 TexturePaint

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;

/** @see https://stackoverflow.com/a/11556441/230513 */
public class GridExample extends JPanel {

public static final Ground[][] MAP = {
{Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.WATER, Ground.WATER},
{Ground.GRASS, Ground.DIRT, Ground.CITY, Ground.WATER, Ground.WATER},
{Ground.GRASS, Ground.DIRT, Ground.CITY, Ground.WATER, Ground.WATER},
{Ground.GRASS, Ground.DIRT, Ground.DIRT, Ground.DIRT, Ground.WATER},
{Ground.GRASS, Ground.GRASS, Ground.DIRT, Ground.WATER, Ground.WATER},
};
private JLabel[][] labelGrid = new JLabel[MAP.length][MAP[0].length];

public GridExample() {
setLayout(new GridLayout(MAP.length, MAP[0].length));
for (int r = 0; r < labelGrid.length; r++) {
for (int c = 0; c < labelGrid[r].length; c++) {
labelGrid[r][c] = new JLabel();
labelGrid[r][c].setIcon(MAP[r][c]);
add(labelGrid[r][c]);
}
}
}

private static void createAndShowGui() {
GridExample mainPanel = new GridExample();
JFrame frame = new JFrame("GridExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowGui();
}
});
}
}

enum Ground implements Icon {

DIRT(new Color(205, 133, 63)), GRASS(new Color(0, 107, 60)),
WATER(new Color(29, 172, 214)), CITY(Color.lightGray);
private static final int SIZE = 42;
private Random random = new Random();
private TexturePaint paint;

private Ground(Color color) {
this.paint = initPaint(color);
}

private TexturePaint initPaint(Color color) {
BufferedImage image = new BufferedImage(
SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, SIZE, SIZE);
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (random.nextBoolean()) {
image.setRGB(col, row, color.getRGB());
} else {
if (random.nextBoolean()) {
image.setRGB(col, row, color.darker().getRGB());
} else {
image.setRGB(col, row, color.brighter().getRGB());
}
}
}
}
return new TexturePaint(image, rect);
}

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(paint);
g.fillRect(0, 0, SIZE, SIZE);
}

@Override
public int getIconWidth() {
return SIZE;
}

@Override
public int getIconHeight() {
return SIZE;
}
}

关于java - JLabel 图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com