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

时间:2014-09-23 01:33:00

标签: java swing graphics ascii

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);
        }
    }

这是游戏类中的draw方法

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

    display.highlightOnScreen(x, y);

    map.draw(display);
}

如何制作它以便画出每一帧并且不会闪烁?

1 个答案:

答案 0 :(得分:3)

How to Use Root Panes

enter image description here

感兴趣的是玻璃窗:

  

隐藏,默认情况下。如果您可以看到玻璃窗格,那么它就像是在根窗格的所有其他部分上的一块玻璃。它是完全透明的,除非你实现了玻璃窗格的paintComponent方法,以便它做某事,...