gpt4 book ai didi

java - 如何使用以下 Pen 类和 canvas 类在 Java 中绘制螺旋和多边形

转载 作者:行者123 更新时间:2023-12-01 21:55:38 32 4
gpt4 key购买 nike

我正在尝试绘制一个多边形,其中用户使用以下类指定边数和螺旋线。我有一个正方形正在工作,但不知道如何绘制多边形。这个类创建了一个可用于绘制东西的 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;
}*

这是我从 Pen 类中调用方法的类,这也是我试图创建一个方法的类,它绘制一个多边形,其中边数由用户指定,并且是一个螺旋,是这样的正方形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 是边数。

螺旋:

(最后有视觉解释图)

您需要一个初始长度i(您绘制的第一条线的长度)和w(空格)行与行之间。

从左上角开始。执行以下操作,直到 i 小于或等于 0:

  • 移动i
  • 旋转90°
  • 移动i
  • 旋转90°
  • i = i - w

Explained by picture

关于java - 如何使用以下 Pen 类和 canvas 类在 Java 中绘制螺旋和多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34419435/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com