gpt4 book ai didi

java - 在游戏中绘制已编写的 JPanel 代码

转载 作者:行者123 更新时间:2023-11-30 08:20:32 28 4
gpt4 key购买 nike

I have a third part JPanel that basically creates an ascii roguelike terminal for my game.使用它,我想创建一个游戏,但我需要能够在面板顶部绘制普通图形。我的问题是,当我尝试这样做时,ascii 看起来不错,但在顶部绘制的东西闪烁,有时不显示。

这是我的显示代码

public class Display {
private static final char TRANSPARENT_CHARACTER = ' ';

// Might break if the character is the same as TRANSPARENT_CHARACTER
private static final AsciiCharacterData HIGHLIGHTER = new AsciiCharacterData(
'H', new Color(255, 255, 0), new Color(255, 255, 0));

private final AsciiPanel displayPanel;
private final int widthInCharacters, heightInCharacters;

private final static int Z_LEVELS = DrawingLayer.values().length;

private final AsciiCharacterData[][][] characterMap;

public Display(final AsciiPanel panel) {
displayPanel = panel;
widthInCharacters = panel.getWidthInCharacters();
heightInCharacters = panel.getHeightInCharacters();

characterMap = new AsciiCharacterData[widthInCharacters][heightInCharacters][Z_LEVELS];
for (int x = 0; x < widthInCharacters; x++) {
for (int y = 0; y < heightInCharacters; y++) {
for (int z = 0; z < Z_LEVELS; z++) {
characterMap[x][y][z] = new AsciiCharacterData(
TRANSPARENT_CHARACTER,
displayPanel.getDefaultForegroundColor(),
displayPanel.getDefaultBackgroundColor());
}
}
}
}

public void setCharacterAt(final int x, final int y, final DrawingLayer z,
final AsciiCharacterData c) {
characterMap[x][y][z.layer] = c;
// if z is not the top level
if (z.layer != Z_LEVELS - 1) {
// check all levels above
for (int i = z.layer + 1; i < Z_LEVELS; i++) {
// if there is an opaque character
if (characterMap[x][y][i].character != TRANSPARENT_CHARACTER)
// we dont need to draw anything
return;
}
}

if (c.character == TRANSPARENT_CHARACTER) {
// loop through all characters under the transparent character
for (int i = z.layer - 1; i >= 0; i--) {
// if we find a non transparent character
if (characterMap[x][y][i].character != TRANSPARENT_CHARACTER) {
// display that one instead
displayPanel.write(characterMap[x][y][i].character, x, y,
characterMap[x][y][i].foregroundColor,
characterMap[x][y][i].backgroundColor);
return;
}
}
// if there were no non trasparent characters
displayPanel.write(TRANSPARENT_CHARACTER, x, y);
// if we are a highlighter, we draw the below character and then
// just draw on top
} else {
displayPanel.write(c.character, x, y, c.foregroundColor,
c.backgroundColor);
}
displayPanel.repaint();
}

public void highlightOnScreen(final int x, final int y) {
final Graphics g = displayPanel.getGraphics();
g.setColor(HIGHLIGHTER.backgroundColor);
g.fillRect(x * AsciiPanel.getCharWidth(),
y * AsciiPanel.getCharHeight(), AsciiPanel.getCharWidth(),
AsciiPanel.getCharHeight());
}

public AsciiCharacterData getCharacterAt(final int x, final int y,
final DrawingLayer z) {
return characterMap[x][y][z.layer];
}

public int getWidth() {
return widthInCharacters;
}

public int getHeight() {
return heightInCharacters;
}
}

这是 GameMap 类中的 draw 方法:

public void draw(final Display display) {
for (int x = getViewportX(); x < getViewportX() + viewportWidthInTiles; x++) {
for (int y = viewportY; y < viewportY + viewportHeightInTiles; y++) {
final char character = background[x][y].getCharacter();
final Color foreground = background[x][y].getForeground();
final Color backgroundColor = background[x][y].getBackground();
final AsciiCharacterData data = new AsciiCharacterData(
character, foreground, backgroundColor);
display.setCharacterAt(x - getViewportX(), y - viewportY,
background[x][y].getDrawingLayer(), data);
}
}
for (int i = 0; i < entities.size(); i++) {
final Entity e = entities.get(i);

final char character = e.getCharacter();
final Color foreground = e.getForeground();
final Color backgroundColor = e.getBackground();
final AsciiCharacterData data = new AsciiCharacterData(character,
foreground, backgroundColor);
display.setCharacterAt(e.getX() - getViewportX(), e.getY()
- viewportY, e.getDrawingLayer(), data);
}
}

这是Game类中的draw方法

@Override
public void draw() {
final int x = map.mapTileXToDisplayTileX(0);
final int y = map.mapTileYToDisplayTileY(0);

display.highlightOnScreen(x, y);

map.draw(display);
}

我怎样才能让它绘制每一帧并且不闪烁?

最佳答案

参见 How to Use Root Panes .

enter image description here

感兴趣的是玻璃板:

Hidden, by default. If you make the glass pane visible, then it's like a sheet of glass over all the other parts of the root pane. It's completely transparent unless you implement the glass pane's paintComponent method so that it does something, ..

关于java - 在游戏中绘制已编写的 JPanel 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25985525/

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