此问题与 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 条线,当我在循环内添加 scale
和 translate
方法时,它只画了 1 条线。 所以我想我是对的。
一个简单的 for 循环可以完成这项工作吗?还是我应该使用一些更复杂的算法来多次绘制蝴蝶曲线,同时更改比例和平移?
最佳答案
我使用 AffineTransform 找到了解决方案缩放和翻译。
基本上,解决方案是摆脱 g2.scale
和 g2.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;
}
}
}
关于java - 如何使用单个/简单的 for 循环以不同的比例和平移多次绘制蝴蝶曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559286/