square.java

来自「Java based Snakes and Ladders game.」· Java 代码 · 共 72 行

JAVA
72
字号
import java.awt.*;

// This will draw a square on the Canvas, used to make the board.

public class Square
{
    private int size;
    private int xPosition;
    private int yPosition;
    private String color;
    private boolean isVisible;

    // Creates a single square at the default setup / variables.
    public Square()
    {
        size = 40;
        xPosition = 0;
        yPosition = 0;
        color = "black";
        isVisible = true;
    }

    // Move the square HORIZONTALLY by the 'distance' pixels.
    public void moveHorizontal(int distance)
    {
        erase();
        xPosition += distance;
        draw();
    }

    // Move the square VERTICALLY by the 'distance' pixels.
    public void moveVertical(int distance)
    {
        erase();
        yPosition += distance;
        draw();
    }

    // Change to the new size specified - Only used if board size changed.
    public void changeSize(int newSize)
    {
        erase();
        size = newSize;
        draw();
    }

    // Change the colour, only colours used will be BLACK or WHITE.
    public void changeColor(String newColor)
    {
        color = newColor;
        draw();
    }

    // Draw the square with current specs on the screen.
    private void draw()
    {
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.draw(this, color,
                        new Rectangle(xPosition, yPosition, size, size));
        }
    }

    // Erase the square on the screen at call time.
    private void erase()
    {
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.erase(this);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?