【问题标题】:Active rendering on a JPanel with ImageIcon tiles? #Java使用 ImageIcon 磁贴在 JPanel 上进行主动渲染? #Java
【发布时间】:2015-11-03 13:47:48
【问题描述】:

所以我正在尝试编写一个基于 tile-grid 的游戏,并想出了一个非常不寻常的解决方案。我用 JLabels 填充了一个 2D JPanel 数组,其中一个 ImageIcon 作为平铺。到目前为止一切正常,但我没有找到任何方法来主动渲染它。 我尝试了一些在 Internet 上找到的主动渲染方法,但它们对我的想法不起作用。您是否有一些想法如何在不将所有内容重写为 Canvas 或类似的东西的情况下实现这一点?

这是我的代码:

窗口

public class Win extends JFrame {
private static final long serialVersionUID = 1L;
private BufferStrategy bs;

public Win(int x, int y) {

    this.setSize(x, y);
    this.setVisible(true);
    this.setResizable(false);
    this.setIgnoreRepaint(true);
    this.createBufferStrategy(2);
    setBs(getBufferStrategy());
}

public BufferStrategy getBs() {
    return bs;
}

public void setBs(BufferStrategy bs) {
    this.bs = bs;
}
}

“画”

public class Field extends JPanel {
private static final long serialVersionUID = 5257799495742189076L;

private int x = 0;
private int y = 0;
private JPanel backPanel[][] = new JPanel[19][19];
private BufferedImage images[] = new BufferedImage[100];
private JLabel image[][] = new JLabel[19][19];

public Field() {

    this.setLayout(new GridLayout(20, 20));
    this.setIgnoreRepaint(true);

}

// Creates Panel Grid & Draws floor
public void setPanels() {
    for (int h = 0; h < 19; h++) {
        for (int w = 0; w < 19; w++) {

            backPanel[h][w] = new JPanel();
            backPanel[h][w].setLayout(new GridLayout(1, 1));

            image[h][w] = new JLabel(new ImageIcon(images[0]));

            backPanel[h][w].add(image[h][w]);

            this.add(backPanel[h][w]);
        }
    }
}

// Loads the Textures
public void getTextures() throws IOException {
    for (int i = 0; i < 1; i++) {
        images[i] = ImageIO.read(new File("texture.png"));

    }
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(images[1], 0, 0, null);

}

public int getY() {
    return y;
}

public void setY(int y) {
    this.y = y;
}

public int getX() {
    return x;
}

public void setX(int x) {
    this.x = x;
}

}

游戏循环

public class GameLoop implements Runnable {

private boolean runFlag = true;

@Override
public void run() {
    Field field = new Field();
    Win window = new Win(640, 640);
    window.add(field);

    try {
        field.getTextures();
    } catch (IOException e) {

        e.printStackTrace();
    }

    while (runFlag) {

        try {
            field.setPanels();
            window.getBs().show();
            Thread.sleep(20);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    }

}

public void stop() {
    runFlag = false;
}

}

【问题讨论】:

  • (在这里搜索)使用 Swing Timer 而不是被 Thread.sleep(int) 阻塞的 Runnable

标签: java swing rendering thread-sleep game-loop


【解决方案1】:

一些替代方案:

  • 洗牌组件并执行removeAll()add()validate(),如图here

  • 随机播放内容并执行setIcon(),如here所示。

无论哪种情况,

【讨论】:

  • 重绘现在正在使用 removeAll() 和 validate() 但现在闪烁。正在努力。泰:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 2016-12-09
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多