嗨,每一個,所以我想寫一個代碼,當我運行它會畫一個SierpinskiTriangle。該程序必須有一個for循環,我已經有,但當我試圖讓程序complile時,我有一個問題。它說雙不能轉換爲Point2D任何人都可以幫助我嗎?嘗試代碼一個小程序,畫一個帶有for循環的SierpinskiTriangle
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Line2D;
public class ST extends JFrame {
int N = 50000;
double sqrt3 = Math.sqrt(3);
double x = 0.0, y = 0.0;
public static void main(String args[]) {
ST t = new ST();
t.add(new JComponent() {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
for (int i = 0; i < N; i++) {
double r = Math.random();
double x0, y0;
if (r < 1./3.) {
x0 = 0.0; y0 = 0.0;
} else if (r < 2./3.) {
x0 = 0.0; y0 = 2.0 ;
} else {
x0 = sqrt3; y0 = 1.0;
}
double x = (x0 + x)/2;
double y = (y0 + y)/2;
Shape l = new Line2D.Double(x,y);
g2d.draw(l);
}
}
});
t.setDefaultCloseOperation(EXIT_ON_CLOSE);
t.setSize(220, 300);
t.setVisible(true);
}
}
請詳細說明。哪條線發生錯誤? – fjf2002 2015-02-24 21:22:12
好的我在20號線,31號線和36號線有錯誤 – user3718833 2015-02-24 21:46:37