gpt4 book ai didi

Java - 简单图形和递归

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:27 25 4
gpt4 key购买 nike

<分区>

既然你们在我上一个项目中帮了我很多,我想我可能会在当前项目中找到一些帮助:)

该项目让我们练习递归和对象(刚开始学习后者)。所以我们首先创建一个“BasicStar”,然后是“Snowflake”,然后是“SuperSnowflake”,最后是可怕的“KochCurve”。

所以“BasicStar”非常简单,现在“Snowflake”的想法是递归地绘制具有较小半径的“BasicStar”。我已经上传了三张图片(基本的星星,我成功地做到了,雪花应该是这样的,还有我的雪花)所以很容易理解我的意思。我的递归方法绘制了一些非常不同的东西,我不知道我做错了什么。任何帮助都会很棒。

谢谢!

(P.S. MainPainter 类(class)是由大学教员制作的,所以即使有需要改进的地方也无关紧要。剩下的是写的由我自己)

主要:

package recursion;

import java.util.Scanner;

/*
* the class main get from the user the shape he wish to draw,
* and call the drew method of the desired shape .
*/
public class Main {


public static void main(String[] args) {


Scanner sc = new Scanner(System.in);


System.out.println("Please enter the number of the shape you wish to draw:\n" +
" 1-example\n" +
" 2-BasicStar\n" +
" 3-Snowflake\n" +
" 4-SuperSnowflake\n" +
" 5-KochCurve\n" +
" 6-KochSnowflake\n");
int shape = sc.nextInt();

// chooses which shape to draw based on the number received
switch(shape){
/*
* An example given to you so you can see how the painted works.
* This example opens a frame, and draws a red line.
*/
case 1:
drawExample();
break;
case 2:
drawBasicStar();
break;
case 3:
drawSnowflake();
break;
case 4:
drawSuperSnowflake();
break;
case 5:
drawKochCurve();
break;
case 6:
drawKochSnowflake();
break;
default: System.out.println("invalid shape");
}

sc.close();
}

// Draw the example line
public static void drawExample(){
Painter.draw("example");
}

// Draw a BasicStar
public static void drawBasicStar(){
Painter.draw("BasicStar");
}

// Draw a Snowflake
public static void drawSnowflake(){
Painter.draw("Snowflake");
}

// Draw a SuperSnowflake
public static void drawSuperSnowflake(){
Painter.draw("SuperSnowflake");
}

// Draw a KochCurve
public static void drawKochCurve(){
Painter.draw("KochCurve");
}

// Draw a KochSnowflake
public static void drawKochSnowflake(){
Painter.draw("KochSnowflake");
}

}

画家:

package recursion;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
/*
* open a frame named aShape and drew the given shape
*/

public class Painter extends Component {

private static final long serialVersionUID = 1L;
private static int SIZE = 600;
private static Painter painter;
private static Graphics g;
private static String shape = null;

// Create a frame and display it
public static void draw(String aShape) {
shape = aShape;
JFrame frame = new JFrame(shape);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
painter = new Painter();
frame.add(painter, null);
frame.pack();
frame.setVisible(true);
}

// returns the Frame's width
public static int getFrameWidth () {
return painter.getSize().width;
}

// returns the Frame's height
public static int getFrameHeight () {
return painter.getSize().height;
}

// changes the color of the lines to be drawn
public static void setColor (String color) {
if (color.equals("red")){
g.setColor(Color.red);
}
else if (color.equals("blue")){
g.setColor(Color.blue);
}
else if (color.equals("green")){
g.setColor(Color.green);
}
}

public static void drawLine (Pixel p1, Pixel p2) {
drawLine((int)Math.round(p1.getX()),(int)Math.round(p1.getY()),(int)Math.round(p2.getX()),(int)Math.round(p2.getY()));
}

// Draw a line on the frame
public static void drawLine (int x1, int y1, int x2, int y2) {
g.drawLine(x1, getFrameHeight()-y1, x2, getFrameHeight()-y2);

}

// Set the default size of the window frame to SIZE*SIZE pixels
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}

// paint the frame - draw the shape given (call the draw method in that shape object)
public void paint(Graphics g) {
Painter.g = g;
try{
Object myShape = (Class.forName("recursion." + shape)).newInstance();
Object [] objs = null;
Class [] classes = null;
(Class.forName("recursion." + shape)).getMethod("draw", classes).invoke(myShape, objs);
}
catch(Exception e)
{
System.out.println("Can't handle shape " + shape);
System.out.println(e.toString());
System.out.println(e.getCause());

}



}

}

像素:

package recursion;

public class Pixel {
private double x;
private double y;

public Pixel(){
x = 0;
y = 0;
}

public Pixel(double x, double y){
this.x = x;
this.y = y;
}

public Pixel(Pixel center){
this();
if(center != null){
this.x = center.x;
this.y = center.y;
}
}

public double getX(){
return x;
}
public double getY(){
return y;
}
public void translate(Pixel p){
this.x = this.x + p.x;
this.y = this.y + p.y;
}
public void rotateRelativeToAxesOrigin(double theta){
double tempX = this.x;
double tempY = this.y;
this.x = ((tempX)*(Math.cos(theta)) - ((tempY)*(Math.sin(theta))));
this.y = ((tempX)*(Math.sin(theta)) - ((tempY)*(Math.cos(theta))));
}
public void rotateRelativeToPixel(Pixel p1, double theta){
double tempX = this.x;
double tempY = this.y;
Pixel translatedPixel = new Pixel(tempX-p1.getX(), tempY-p1.getY());
translatedPixel.rotateRelativeToAxesOrigin(theta);
this.x = translatedPixel.getX() + p1.getX();
this.y = translatedPixel.getY() + p1.getY();
}
}

基本之星:

package recursion;

public class BasicStar {
private Pixel center;
private double radius;

public BasicStar(){
double height = Painter.getFrameHeight()/2;
double width = Painter.getFrameWidth()/2;
this.center = new Pixel (width, height);
double maxRadius = Math.min(width, height)/2;
this.radius = maxRadius/4;
}

public BasicStar(Pixel center, double radius){
this.center = new Pixel(center);
this.radius = radius;
}

public Pixel getCenter(){
return new Pixel(center);
}
public double getRadius(){
return this.radius;
}
public void draw(){
Pixel begin = new Pixel(this.center);
Pixel end = new Pixel(center.getX() + getRadius(), center.getY());

Painter.drawLine(begin, end);
end.rotateRelativeToPixel(center, (2*Math.PI)/6);
Painter.drawLine(begin, end);
end = new Pixel(center.getX() + getRadius(), center.getY());
end.rotateRelativeToPixel(center, (4*Math.PI)/6);
Painter.drawLine(begin, end);
end = new Pixel(center.getX() + getRadius(), center.getY());
end.rotateRelativeToPixel(center, (6*Math.PI)/6);
Painter.drawLine(begin, end);
end = new Pixel(center.getX() + getRadius(), center.getY());
end.rotateRelativeToPixel(center, (8*Math.PI)/6);
Painter.drawLine(begin, end);
end = new Pixel(center.getX() + getRadius(), center.getY());
end.rotateRelativeToPixel(center, (10*Math.PI)/6);
Painter.drawLine(begin, end);

}
}

雪花:

package recursion;

public class Snowflake {
private BasicStar basic;
private int depth;

public Snowflake(){
double height = Painter.getFrameHeight()/2;
double width = Painter.getFrameWidth()/2;
Pixel center = new Pixel (width, height);
double maxRadius = Math.min(width, height)/2;
double radius = maxRadius/4;
this.basic = new BasicStar(center, radius);
this.depth = 2;
}

public Snowflake(BasicStar basic, int depth){
this();
if(basic!=null){
this.basic = basic;
this.depth = depth;
}
}

public int getDepth(){
return this.depth;
}

public BasicStar getBasic(){
return this.basic;
}

public double getRadius(BasicStar basic){
return this.basic.getRadius();
}

public Pixel getBasicCenter(BasicStar basic){
return this.basic.getCenter();
}

public void draw(){
draw(this.depth, basic.getCenter(), basic.getRadius());
}

private void draw(int depth, Pixel center, double radius){
BasicStar basic = new BasicStar(center, radius);

if(depth==1){
basic.draw();
}
else{
Pixel p = new Pixel(center.getX() + radius, center.getY());
draw(depth - 1, p, (radius/3));
for(int i=0; i<6; i=i+1){
p.rotateRelativeToPixel(center, (2*Math.PI)/6);
BasicStar temp = new BasicStar(p, radius/3);
temp.draw();
}
}
}
}

BasicStar

Snowflake

My Snowflake

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