如何使用以下笔 class 和 canvas class 在 Java 中绘制螺旋和多边形
How to draw a spiral and polygon in Java using the following Pen class and canvas class
我正在尝试绘制一个多边形,用户可以使用以下 classes 指定边数和螺旋线。我有一个正方形可以工作,但无法弄清楚如何绘制多边形。
这是class,它创建了一个可以用来画东西的Pen
import java.awt.Color;
import java.util.Random;
public class Pen
{
// constants for randomSquiggle method
private static final int SQIGGLE_SIZE = 40;
private static final int SQIGGLE_COUNT = 30;
private int xPosition;
private int yPosition;
private int rotation;
private Color color;
private boolean penDown;
private Canvas canvas;
private Random random;
/**
* Create a new Pen with its own canvas. The pen will create a new canvas for
* itself to draw on, and start in the default state (centre of canvas, direction
* right, color black, pen down).
*/
public Pen()
{
this (280, 220, new Canvas("My Canvas", 560, 440));
}
/**
* Create a new Pen for a given canvas. The direction is initially 0 (to the right),
* the color is black, and the pen is down.
*
* @param xPos the initial horizontal coordinate of the pen
* @param yPos the initial vertical coordinate of the pen
* @param drawingCanvas the canvas to draw on
*/
public Pen(int xPos, int yPos, Canvas drawingCanvas)
{
xPosition = xPos;
yPosition = yPos;
rotation = 0;
penDown = true;
color = Color.BLACK;
canvas = drawingCanvas;
random = new Random();
}
/**
* Move the specified distance in the current direction. If the pen is down,
* leave a line on the canvas.
*
* @param distance The distance to move forward from the current location.
*/
public void move(int distance)
{
double angle = Math.toRadians(rotation);
int newX = (int) Math.round(xPosition + Math.cos(angle) * distance);
int newY = (int) Math.round(yPosition + Math.sin(angle) * distance);
moveTo(newX, newY);
}
/**
* Move to the specified location. If the pen is down, leave a line on the canvas.
*
* @param x The x-coordinate to move to.
* @param y The y-coordinate to move to.
*/
public void moveTo(int x, int y)
{
if (penDown) {
canvas.setForegroundColor(color);
canvas.drawLine(xPosition, yPosition, x, y);
}
xPosition = x;
yPosition = y;
}
/**
* Turn the specified amount (out of a 360 degree circle) clockwise from the current
* rotation.
*
* @param degrees The amount of degrees to turn. (360 is a full circle.)
*/
public void turn(int degrees)
{
rotation = rotation + degrees;
}
/**
* Turn to the specified direction. 0 is right, 90 is down, 180 is left, 270 is up.
*
* @param angle The angle to turn to.
*/
public void turnTo(int angle)
{
rotation = angle;
}
/**
* Set the drawing color.
*
* @param newColor The color to use for subsequent drawing operations.
*/
public void setColor(Color newColor)
{
color = newColor;
}
/**
* Lift the pen up. Moving afterwards will not leave a line on the canvas.
*/
public void penUp()
{
penDown = false;
}
/**
* Put the pen down. Moving afterwards will leave a line on the canvas.
*/
public void penDown()
{
penDown = true;
}*
这是 class 我正在从 Pen class 中调用方法,这也是 class 我正在尝试创建一个绘制多边形的方法其中边数由用户和螺旋线指定,它是方形的 http://vector.me/browse/291037/square_spiral
import java.awt.Color;
import java.util.Random;
public class DrawDemo
{
private Canvas myCanvas;
private Random random;
/**
* Prepare the drawing demo. Create a fresh canvas and make it visible.
*/
public DrawDemo()
{
myCanvas = new Canvas("Drawing Demo", 500, 400);
random = new Random();
}
/**
* Draw a square on the screen.
*/
public void drawSquare()
{
Pen pen = new Pen(320, 260, myCanvas);
pen.setColor(Color.BLUE);
square(pen);
}
/**
* Draw a Triangle on the screen.
*/
public void drawTriangle()
{
Pen pen = new Pen(320, 260, myCanvas);
pen.setColor(Color.GREEN);
triangle(pen);
}
/**
* Draw a Pentagon on the screen.
*/
public void drawPentagon()
{
Pen pen = new Pen(250, 200, myCanvas);
pen.setColor(Color.GREEN);
pentagon(pen);
}
/**
* Draw a wheel made of many squares.
*/
public void drawWheel()
{
Pen pen = new Pen(250, 200, myCanvas);
pen.setColor(Color.RED);
for (int i=0; i<36; i++) {
square(pen);
pen.turn(10);
}
}
/**
* Draw a square in the pen's color at the pen's location.
*/
private void square(Pen pen)
{
for (int i=0; i<4; i++) {
pen.move(100);
pen.turn(90);
}
}
/**
* Draw a triangle in the pen's color at the pen's location.
*/
private void triangle(Pen pen)
{
for (int i=0; i<3; i++) {
pen.move(100);
pen.turn(120);
}
}
private void pentagon(Pen pen)
{
for (int i=0; i<5; i++) {
pen.move(100);
pen.turn(-72);
}
}
/**
* Draw some random squiggles on the screen, in random colors.
*/
public void colorScribble()
{
Pen pen = new Pen(250, 200, myCanvas);
for (int i=0; i<10; i++) {
// pick a random color
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
pen.setColor(new Color(red, green, blue));
pen.randomSquiggle();
}
}
/**
* Clear the screen.
*/
public void clear()
{
myCanvas.erase();
}
}
Polygen:你是说正多边形吗?您需要转动的角度将是 180°-(((n-2)×180)°)/n
,减少到 (360°)/n
,其中 n
是边数。
螺旋:
(图解在文末)
你需要一个initial length
,i
,这是你画的第一条线的长度,w
,这是线与线之间的space。
从左上角开始。执行以下操作直到 i
小于或等于 0
:
- 移动
i
- 转
90°
- 移动
i
- 转
90°
i = i - w
我正在尝试绘制一个多边形,用户可以使用以下 classes 指定边数和螺旋线。我有一个正方形可以工作,但无法弄清楚如何绘制多边形。 这是class,它创建了一个可以用来画东西的Pen
import java.awt.Color;
import java.util.Random;
public class Pen
{
// constants for randomSquiggle method
private static final int SQIGGLE_SIZE = 40;
private static final int SQIGGLE_COUNT = 30;
private int xPosition;
private int yPosition;
private int rotation;
private Color color;
private boolean penDown;
private Canvas canvas;
private Random random;
/**
* Create a new Pen with its own canvas. The pen will create a new canvas for
* itself to draw on, and start in the default state (centre of canvas, direction
* right, color black, pen down).
*/
public Pen()
{
this (280, 220, new Canvas("My Canvas", 560, 440));
}
/**
* Create a new Pen for a given canvas. The direction is initially 0 (to the right),
* the color is black, and the pen is down.
*
* @param xPos the initial horizontal coordinate of the pen
* @param yPos the initial vertical coordinate of the pen
* @param drawingCanvas the canvas to draw on
*/
public Pen(int xPos, int yPos, Canvas drawingCanvas)
{
xPosition = xPos;
yPosition = yPos;
rotation = 0;
penDown = true;
color = Color.BLACK;
canvas = drawingCanvas;
random = new Random();
}
/**
* Move the specified distance in the current direction. If the pen is down,
* leave a line on the canvas.
*
* @param distance The distance to move forward from the current location.
*/
public void move(int distance)
{
double angle = Math.toRadians(rotation);
int newX = (int) Math.round(xPosition + Math.cos(angle) * distance);
int newY = (int) Math.round(yPosition + Math.sin(angle) * distance);
moveTo(newX, newY);
}
/**
* Move to the specified location. If the pen is down, leave a line on the canvas.
*
* @param x The x-coordinate to move to.
* @param y The y-coordinate to move to.
*/
public void moveTo(int x, int y)
{
if (penDown) {
canvas.setForegroundColor(color);
canvas.drawLine(xPosition, yPosition, x, y);
}
xPosition = x;
yPosition = y;
}
/**
* Turn the specified amount (out of a 360 degree circle) clockwise from the current
* rotation.
*
* @param degrees The amount of degrees to turn. (360 is a full circle.)
*/
public void turn(int degrees)
{
rotation = rotation + degrees;
}
/**
* Turn to the specified direction. 0 is right, 90 is down, 180 is left, 270 is up.
*
* @param angle The angle to turn to.
*/
public void turnTo(int angle)
{
rotation = angle;
}
/**
* Set the drawing color.
*
* @param newColor The color to use for subsequent drawing operations.
*/
public void setColor(Color newColor)
{
color = newColor;
}
/**
* Lift the pen up. Moving afterwards will not leave a line on the canvas.
*/
public void penUp()
{
penDown = false;
}
/**
* Put the pen down. Moving afterwards will leave a line on the canvas.
*/
public void penDown()
{
penDown = true;
}*
这是 class 我正在从 Pen class 中调用方法,这也是 class 我正在尝试创建一个绘制多边形的方法其中边数由用户和螺旋线指定,它是方形的 http://vector.me/browse/291037/square_spiral
import java.awt.Color;
import java.util.Random;
public class DrawDemo
{
private Canvas myCanvas;
private Random random;
/**
* Prepare the drawing demo. Create a fresh canvas and make it visible.
*/
public DrawDemo()
{
myCanvas = new Canvas("Drawing Demo", 500, 400);
random = new Random();
}
/**
* Draw a square on the screen.
*/
public void drawSquare()
{
Pen pen = new Pen(320, 260, myCanvas);
pen.setColor(Color.BLUE);
square(pen);
}
/**
* Draw a Triangle on the screen.
*/
public void drawTriangle()
{
Pen pen = new Pen(320, 260, myCanvas);
pen.setColor(Color.GREEN);
triangle(pen);
}
/**
* Draw a Pentagon on the screen.
*/
public void drawPentagon()
{
Pen pen = new Pen(250, 200, myCanvas);
pen.setColor(Color.GREEN);
pentagon(pen);
}
/**
* Draw a wheel made of many squares.
*/
public void drawWheel()
{
Pen pen = new Pen(250, 200, myCanvas);
pen.setColor(Color.RED);
for (int i=0; i<36; i++) {
square(pen);
pen.turn(10);
}
}
/**
* Draw a square in the pen's color at the pen's location.
*/
private void square(Pen pen)
{
for (int i=0; i<4; i++) {
pen.move(100);
pen.turn(90);
}
}
/**
* Draw a triangle in the pen's color at the pen's location.
*/
private void triangle(Pen pen)
{
for (int i=0; i<3; i++) {
pen.move(100);
pen.turn(120);
}
}
private void pentagon(Pen pen)
{
for (int i=0; i<5; i++) {
pen.move(100);
pen.turn(-72);
}
}
/**
* Draw some random squiggles on the screen, in random colors.
*/
public void colorScribble()
{
Pen pen = new Pen(250, 200, myCanvas);
for (int i=0; i<10; i++) {
// pick a random color
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
pen.setColor(new Color(red, green, blue));
pen.randomSquiggle();
}
}
/**
* Clear the screen.
*/
public void clear()
{
myCanvas.erase();
}
}
Polygen:你是说正多边形吗?您需要转动的角度将是 180°-(((n-2)×180)°)/n
,减少到 (360°)/n
,其中 n
是边数。
螺旋:
(图解在文末)
你需要一个initial length
,i
,这是你画的第一条线的长度,w
,这是线与线之间的space。
从左上角开始。执行以下操作直到 i
小于或等于 0
:
- 移动
i
- 转
90°
- 移动
i
- 转
90°
i = i - w