Java - einfache Grafiken und Rekursion

Gut, da man Leute hat mir sehr geholfen mit meinem letzten Projekt, bei dem ich dachte, ich würde etwas Hilfe mit der aktuellen 🙂

Das Projekt hat uns üben Rekursion und Objekte (gerade angefangen zu lernen, über das letztere). Also zuerst erstellen wir eine "BasicStar", später eine "Schneeflocke", dann kommt der "SuperSnowflake" und schließlich der gefürchtete "KochCurve".

Also habe ich die "BasicStar" war ganz einfach, und jetzt die Idee von der "Schneeflocke" ist rekursiv zeichnen "BasicStar"s mit kleineren Radien. Hochgeladen habe ich drei Bilder (basic star, was ich auch Tat erfolgreich, Schneeflocke, wie es sein sollte, und meine Schneeflocke), so ist es leicht zu verstehen, was ich meine. Meine rekursive Methode zeichnet etwas ganz anderes, und ich habe keine Ahnung, was ich falsch mache. Jede Hilfe wäre toll.

Dank!

(P. S. Die Main und Painter Klassen wurden von der Universität Fakultät also selbst wenn es Dinge zu verbessern gibt, wird es nicht relevant sein. Der rest wurde von mir selbst geschrieben)

Main:

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");
}

}

Painter:

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());

    }



 }

}

Pixel:

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();
}
}

BasicStar:

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);

}
}

Snowflake:

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();
        }
    }
}
}

Java - einfache Grafiken und Rekursion

Java - einfache Grafiken und Rekursion

Java - einfache Grafiken und Rekursion

  • Es sieht aus wie es ist Zeit, um einige zu testen und Debuggen. Einen debugger verwenden, um zu helfen Sie zunächst identifizieren Ihre Fehler, da hast du noch nicht getan, und dies ist ein entscheidender Schritt, der erreicht werden muss, bevor man es lösen kann.
  • Etwa, dass - ich tatsächlich versucht, aber ich lief in ein anderes problem - da habe ich nichts in meinem "main" - Methode, ich habe keine Ahnung, wie die Verwendung von "Step In" und es herauszufinden - ich habe versucht, einen Haltepunkt in meinem rekursive Methode aber alle es tut, wenn ich versuche, "Debug-Datei" ist es halt auf mein Haltepunkt, dann drücke ich "f5" (Einzelschritt), und es geht einfach weiter und zieht alles.. es geht nicht weiter, Zeile für Zeile, wie es sollte.
  • Nur als Anmerkung: Das ist ein schönes fraktal hast du da, aber es ist nicht die Koch-Schneeflocke
  • sind Sie sicher, dass Sie verwenden möchten, Reflexionen zu behandeln, die Fallunterscheidung in der paint-Methode?
  • das ist nur der reguläre Schneeflocke. Die Koch-Schneeflocke wird später in das Projekt.
  • Der Maler und die Wichtigsten Klassen geschrieben wurden, von der Fakultät und sind nicht von uns geändert werden, Studenten 🙂
  • Das sieht zu kompliziert für mich. Ist das system-design Teil der Aufgabe?
  • Nicht - nur schreiben die rekursive Methoden zum zeichnen der verschiedenen Arten von Schneeflocken.

InformationsquelleAutor Cauthon | 2013-12-21
Schreibe einen Kommentar