JAVA:如何在文本部分添加X坐标

时间:2018-04-22 16:25:38

标签: java

我想将文本部分CONTROLS放在右边而不是刚刚列出,我是一个新手,我使用的是发布Java游戏代码的网站的源代码。 Y坐标存在,但我似乎无法找到X坐标,这将帮助我定位文本部分。这是我的侧面板类的代码:

/**
 * The number of rows and columns in the preview window. Set to
 * 5 because we can show any piece with some sort of padding.
 */
private static final int TILE_COUNT = 5;

/**
 * The center x of the next piece preview box.
 */
private static final int SQUARE_CENTER_X = 130;

/**
 * The center y of the next piece preview box.
 */
private static final int SQUARE_CENTER_Y = 65;

/**
 * The size of the next piece preview box.
 */
private static final int SQUARE_SIZE = (TILE_SIZE * TILE_COUNT >> 1);

/**
 * The number of pixels used on a small insets (generally used for categories).
 */
private static final int SMALL_INSET = 20;

/**
 * The number of pixels used on a large insets.
 */
private static final int LARGE_INSET = 30;

/**
 * The y coordinate of the stats category.
 */
private static final int STATS_INSET = 100;

/**
 * The y coordinate of the controls category.
 */
private static final int CONTROLS_INSET = 175;

/**
 * The number of pixels to offset between each string.
 */
private static final int TEXT_STRIDE = 25;

/**
 * The small font.
 */
private static final Font SMALL_FONT = new Font("Arial", Font.BOLD, 11);

/**
 * The large font.
 */
private static final Font LARGE_FONT = new Font("Arial", Font.BOLD, 13);

/**
 * The color to draw the text and preview box in.
 */
private static final Color DRAW_COLOR = new Color(128, 192, 128);

/**
 * The Tetris instance.
 */
private Tetris tetris;

/**
 * Creates a new SidePanel and sets it's display properties.
 * @param tetris The Tetris instance to use.
 */
public SidePanel(Tetris tetris) {
    this.tetris = tetris;

    setPreferredSize(new Dimension(200, BoardPanel.PANEL_HEIGHT));
    setBackground(Color.BLACK);
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    //Set the color for drawing.
    g.setColor(Color.WHITE);

    /*
     * This variable stores the current y coordinate of the string.
     * This way we can re-order, add, or remove new strings if necessary
     * without needing to change the other strings.
     */
    int offset;

    /*
     * Draw the "Stats" category.
     */
    g.setFont(LARGE_FONT);
    g.drawString("Stats", SMALL_INSET, offset = STATS_INSET);
    g.setFont(SMALL_FONT);
    g.drawString("Level: " + tetris.getLevel(), LARGE_INSET, offset += TEXT_STRIDE);
    g.drawString("Score: " + tetris.getScore(), LARGE_INSET, offset += TEXT_STRIDE);

    /*
     * Draw the "Controls" category.
     */
    g.setFont(LARGE_FONT);
    g.drawString("Controls", SMALL_INSET, offset = CONTROLS_INSET);
    g.setFont(SMALL_FONT);
    g.drawString(" Left- Move Left", LARGE_INSET, offset += TEXT_STRIDE);
    g.drawString("Right - Move Right", LARGE_INSET, offset += TEXT_STRIDE);
    g.drawString("Z - Rotate Anticlockwise", LARGE_INSET, offset += TEXT_STRIDE);
    g.drawString("X - Rotate Clockwise", LARGE_INSET, offset += TEXT_STRIDE);
    g.drawString("Down - Drop", LARGE_INSET, offset += TEXT_STRIDE);
    g.drawString("P - Pause Game", LARGE_INSET, offset += TEXT_STRIDE);

    /*
     * Draw the next piece preview box.
     */
    g.setFont(LARGE_FONT);
    g.drawString("Next Piece:", SMALL_INSET, 70);
    g.drawRect(SQUARE_CENTER_X - SQUARE_SIZE, SQUARE_CENTER_Y - SQUARE_SIZE, SQUARE_SIZE * 2, SQUARE_SIZE * 2);

    /*
     * Draw a preview of the next piece that will be spawned. The code is pretty much
     * identical to the drawing code on the board, just smaller and centered, rather
     * than constrained to a grid.
     */
    TileType type = tetris.getNextPieceType();
    if(!tetris.isGameOver() && type != null) {
        /*
         * Get the size properties of the current piece.
         */
        int cols = type.getCols();
        int rows = type.getRows();
        int dimension = type.getDimension();

        /*
         * Calculate the top left corner (origin) of the piece.
         */
        int startX = (SQUARE_CENTER_X - (cols * TILE_SIZE / 2));
        int startY = (SQUARE_CENTER_Y - (rows * TILE_SIZE / 2));

        /*
         * Get the insets for the preview. The default
         * rotation is used for the preview, so we just use 0.
         */
        int top = type.getTopInset(0);
        int left = type.getLeftInset(0);

        /*
         * Loop through the piece and draw it's tiles onto the preview.
         */
        for(int row = 0; row < dimension; row++) {
            for(int col = 0; col < dimension; col++) {
                if(type.isTile(col, row, 0)) {
                    drawTile(type, startX + ((col - left) * TILE_SIZE), startY + ((row - top) * TILE_SIZE), g);
                }
            }
        }
    }
}


/**
 * Draws a tile onto the preview window.
 * @param type The type of tile to draw.
 * @param x The x coordinate of the tile.
 * @param y The y coordinate of the tile.
 * @param g The graphics object.
 */

private void drawTile(TileType type, int x, int y, Graphics g) {
    /*
     * Fill the entire tile with the base color.
     */
    g.setColor(type.getBaseColor());
    g.fillRect(x, y, TILE_SIZE, TILE_SIZE);

    /*
     * Fill the bottom and right edges of the tile with the dark shading color.
     */
    g.setColor(type.getDarkColor());
    g.fillRect(x, y + TILE_SIZE - SHADE_WIDTH, TILE_SIZE, SHADE_WIDTH);
    g.fillRect(x + TILE_SIZE - SHADE_WIDTH, y, SHADE_WIDTH, TILE_SIZE);

    /*
     * Fill the top and left edges with the light shading. We draw a single line
     * for each row or column rather than a rectangle so that we can draw a nice
     * looking diagonal where the light and dark shading meet.
     */
    g.setColor(type.getLightColor());
    for(int i = 0; i < SHADE_WIDTH; i++) {
        g.drawLine(x, y + i, x + TILE_SIZE - i - 1, y + i);
        g.drawLine(x + i, y, x + i, y + TILE_SIZE - i - 1);
    }
}

}

enter image description here

0 个答案:

没有答案