如何使用递归重新创建图形?

时间:2016-02-20 23:51:22

标签: java user-interface recursion graphics

我有一个图形,它由一定水平的不同大小的圆圈组成。这是图片:

https://gyazo.com/e18723e99b1139d50dfa17c3f79ffced

我试图使用递归重新创建此图形,而不是我的代码中使用的if语句,但在尝试之后,我不确定如何执行此操作。希望得到一些帮助,这是我的代码:

package weekFour;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;


import javax.swing.*;

@SuppressWarnings("serial")
public class Circle extends JPanel {
    private int circX  = 10;
    private static int windowW = 1700;
    private static int windowH = 1000;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //smoothes out edges

        Color c5 = new Color(50, 50, 50);
        Color c4 = new Color(100, 100, 100);
        Color c3 = new Color(150, 150, 150);
        Color c2 = new Color(200, 200, 200);
        Color c1= new Color(250, 250, 250);

        for (int i = 0; i < 1; i++) {
            g2.setColor(c1);
            g2.fillOval(522 + 75*i, 138, 666, 666);
            g2.setColor(c1);
            g2.drawOval(522 + 75*i, 138, 666, 666);
        }

        for (int i = 0; i < 3; i++) {
            g2.setColor(c2);
            g2.fillOval(244 + 522*i, 365, 180, 180);
            g2.setColor(c2);
            g2.drawOval(244 + 522*i, 365, 180, 180);
        }

        for (int i = 0; i < 10; i++) {
            g2.setColor(c3);
            g2.fillOval(130 + 174*i, 428, 60, 60);
            g2.setColor(c3);
            g2.drawOval(130 + 174*i, 428, 60, 60);
        }

        for (int i = 0; i < 25; i++) {
            g2.setColor(c4);
            g2.fillOval(60 + 87*i, 444, 25, 25);
            g2.setColor(c4);
            g2.drawOval(60 + 87*i, 444, 25, 25);
        }

        for (int i = 0; i < 120; i++) {
            g2.setColor(c5);
            g2.fillOval(circX + 29*i, 450, 12, 12);
            g2.setColor(c5);
            g2.drawOval(circX + 29*i, 450, 12, 12);
        }

    }   


    private static void createAndShowGui() { 
        JFrame frame = new JFrame("MyTaskToo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Circle());
        frame.setSize(windowW, windowH);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

这就是我解决这个问题的方法,虽然我们不得不用绿色圆圈而不是灰色圆圈来做,但它并没有那么不同。

N.B:对于有时琐碎的事情的吸引人的评论感到抱歉,但我们得到了评论标记,最好是安全而不是抱歉。也许他们会让你对思考过程有所了解。

这是启动程序并设置窗口信息的主要方法。

public class Q2Main {

public static void main(String[] args) {
    // here we are just setting out the window end putting the circles drawin in Q2Circles into this window.
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(1000, 500);
    window.getContentPane().add(new Q2Circles(5));
    window.setVisible(true);
}}

这就是魔术发生的地方:

public class Q2Circles extends JPanel {

    // this allows the user to specify how many loops of recursion they want the programme to complete before finishing
    int recursionsToDo;

    public Q2Circles(int recursionMax){
        super();
        recursionsToDo = recursionMax;
    }

    /*
        this method is automatically called when we run the constructor as it inherits from the JFram superclass. here
        we are setting out the size of the circle by getting the size of the window to make it proportional to the rest
        of the screen and circles.
        we then pass these values into the drawCircle method to draw the circle
     */
    public void paintComponent(Graphics g){
        Rectangle rectangle = this.getBounds();
        int diameter = rectangle.width/3;
        int centerPoint = rectangle.width/2;

        drawCircle(g, 1, centerPoint, diameter);

    }

    /*
        This method is where the magic of the programme really takes place. first of all we make sure we haven't completed
        the necessary recursions. we the set the color by dividing it by the amount of times we have recursed, this will
        have the affect of getting darker the more times the method recurses. we then sset the color. finaly we fill the
        oval (draw the circle). because we want to move depending on the times it has recursed and size of the previous
        we do it based on the size of the elements from the previous call to this method. Getting the right numbers
        though was just alot of trial and error.

        we then increment the recursion counter so that we know how many times we have recursed and that can then be
        used at different points where needed. e.g for setting the color.

        each recursive call used the dimension of the other recursive calls to make the whole picture. Although the
        first recursive call creates the circles on the right of the screen. the second call draws the circle on the
        left of the screen and the last one does the circles in the middle, they all use eachothers values to make it
        complete. without one recursive step, more is missing than just what is created by that recursive call on its own.

        in all honesty though, there is alot of duplication, like the large middlecircle.


     */
    public void drawCircle(Graphics g, int amountOfRecursions, int center, int diameter){
        if (amountOfRecursions <= recursionsToDo){
            int recursionsCount = amountOfRecursions;

            int greenColor = Math.round(225 / (amountOfRecursions));
            g.setColor(new Color(0,  greenColor, 0));
            g.fillOval(center - (diameter/2), 200 - (diameter/2), diameter, diameter);

            recursionsCount++;

            drawCircle(g, recursionsCount, Math.round(center + diameter), diameter/3);
            drawCircle(g, recursionsCount, Math.round(center - diameter), diameter/3);
            drawCircle(g, recursionsCount, Math.round(center), diameter/3);

        }
    }}
相关问题