我试图绘制一个多边形,其中用户指定的边数和螺旋使用以下类。我有一个正方形的工作,但不知道如何绘制多边形。这是一个类,它创建一个可用于绘制事物的钢笔。
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;
}*
这是我从Pen类调用方法的类,这也是我试图创建一个方法的类,它绘制一个由用户指定边数的多边形,以及一个像这个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();
}
}
发布于 2015-12-22 08:41:57
波利根:你是说正多边形吗?你需要转向的角度是180°-(((n-2)×180)°)/n
,它将减少到(360°)/n
,其中n
是边的数目。
螺旋:
(最后的视觉解释图片)
您需要一个initial length
,i
,它是您画的第一行的长度,w
是线条之间的空格。
从左上角开始。在i
小于或等于0
之前执行以下操作
i
90°
i
90°
i = i - w
https://stackoverflow.com/questions/34419435
复制