circle.java

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

JAVA
73
字号
import java.awt.*;
import java.awt.geom.*;

// This will draw a circle on the Canvas, used for the pieces.

public class Circle
{
    private int diameter;
	private int xPosition;
	private int yPosition;
	private String color;
	private boolean isVisible;
	
    // Creates a single circle at the default setup / variables.
    public Circle()
    {
		diameter = 30;
		xPosition = 0;
		yPosition = 0;
		color = "red";
		isVisible = true;
    }
	
    // Move the circle HORIZONTALLY by the 'distance' pixels.
    public void moveHorizontal(int distance)
    {
		erase();
		xPosition += distance;
		draw();
    }

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

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

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

    // Erase the circle 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 + -
显示快捷键?