java - 如何使用单个/简单的 for 循环以不同的比例和平移多次绘制蝴蝶曲线?

标签 java 2d graphics2d java-2d

此问题与 my older question 相关.

我想做的是绘制同一条蝴蝶曲线最多 30 次。每次都使用随机比例/翻译/颜色。

我尝试了这段代码:

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

    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    for (int j=0;j<30;j++) {

        double tr = Math.random() * 300;
        g2.translate(tr,tr);
        double sc = Math.random() * 50 + 10;
        g2.scale(sc,sc);
        g2.setStroke(new BasicStroke(0.01f ));
        g2.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255),(int) (Math.random()*255)));
        double x1,y1;
        double x0 = 0;
        int nPoints = 1500;
        double y0 = Math.E-2;

        for(int i=0;i<nPoints;i++) {
            double t= 12*i*Math.PI/nPoints;
            x1=(Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
            y1 = (Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
            g2.draw(new Line2D.Double(x0,y0,x1,y1));
            x0=x1;
            y0=y1;
        }
    }
}

这段代码的问题是,最后它只会显示/绘制一条曲线。它不会显示超过一个。由于在 Swing 中绘画具有破坏性,我怀疑我面临的问题与 for 循环内的这些行有关:

double tr = Math.random() * 300;
g2.translate(tr,tr);
double sc = Math.random() * 50 + 10;
g2.scale(sc,sc);

为了进行快速测试,我尝试了以下代码:

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

    Graphics2D g2 = (Graphics2D)g;
    double tr = Math.random() * 300;
    g2.translate(tr,tr);
    double sc = Math.random() * 50 + 10;
    g2.scale(sc,sc);

    for (int j=0;j<30;j++) {
        g2.setStroke(new BasicStroke(0.01f ));
        g2.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255),(int) (Math.random()*255)));
        double x1,y1;
        double x0 = 0;
        int nPoints = 1500;
        double y0 = Math.E-2;
        g2.drawLine(5,j,100,j);
    }
}

这画了 30 条线,当我在循环内添加 scaletranslate 方法时,它只画了 1 条线。 所以我想我是对的

一个简单的 for 循环可以完成这项工作吗?还是我应该使用一些更复杂的算法来多次绘制蝴蝶曲线,同时更改比例和平移?

最佳答案

我使用 AffineTransform 找到了解决方案缩放和翻译。

基本上,解决方案是摆脱 g2.scaleg2.translate,并使用 g2.setTransform(tx);tx 是一个可缩放和平移的 AffineTransform

这是为我完成此操作的代码:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    for (int j=0;j<30;j++) {
        double sc = Math.random() * 30 + 10;
        AffineTransform tx = new AffineTransform();
        tx.scale(sc, sc);
        tx.translate(Math.random() * 50, Math.random() * 50);
        g2.setTransform(tx);
        g2.setStroke(new BasicStroke(0.01f ));
        g2.setColor(new Color((int)(Math.random()*255), (int)(Math.random()*255),(int) (Math.random()*255)));
        double x1,y1;
        double x0 = 0;
        int nPoints = 1500;
        double y0 = Math.E-2;

        for(int i=0;i<nPoints;i++) {
            double t= 12*i*Math.PI/nPoints;
            x1=(Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
            y1 = (Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
            g2.draw(new Line2D.Double(x0,y0,x1,y1));
            x0=x1;
            y0=y1;
        }
    }
}

Butterfly Images

关于java - 如何使用单个/简单的 for 循环以不同的比例和平移多次绘制蝴蝶曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559286/

相关文章:

python - (Python) 2D Array Checkers - 如何跳跃并吃掉对手的棋子

Java Graphics2D 正在绘制一个像素(舍入错误?)

c# - 没有OpenGL和其他库的粒子系统

java - 对类进行反射是否会意外调用方法(例如静态构造函数)?

java - Java SOAP 客户端中缺少 Content-Type "start="标记,服务器未找到附件

c++ - for循环的每次迭代都添加一个新语句。二维数组排队

java - 在 Java 中比较二维数组和一维数组

java - 当我在微服务后端之间发出请求时 SessionId 丢失

java - 在 JPanel 中加载图像?

Java:将 Text.Attribute 对象设置为其默认值时出现空指针异常,为什么?