2017-11-26 35 views
1

私はフラグを描く予定のプログラムのために星を作ろうとしています。私はかなりJava Appletクラスに限られています。プロセスは異なる中心の星ごとに同じでなければならないので、XとYの値を持つメソッドに中心を渡し、中心からの角度に基づいてPolygonを使って描画します。問題は、出てくる形が決して星ではないことです。正弦波と余弦を使って星を作りようとしています

public void paint(Graphics g) 
{ 
    super.paint(g); 
    Polygon star= new Polygon(); 
    int radius=20; 
    int roundX=(int)Math.round(radius*Math.cos(Math.toRadians(54))); 
    int roundY=(int)Math.round(radius*Math.sin(Math.toRadians(54))); 
    int originX=100,originY=100; 
    //int radius=20; 
    int topY=originY+radius; 
    int bottomLeftX=originX+roundX; 
    int bottomY=originY-roundY; 
    int middleY=originY+(int)Math.round(radius*Math.sin(Math.toRadians(18)));; 
    int midRightX=originX+(int)Math.round(radius*Math.cos(Math.toRadians(18)));; 
    int midLeftX=originX-(int)Math.round(radius*Math.cos(Math.toRadians(18))); 
    int bottomRightX=originX+roundX; 
    int bottomRightY=middleY-roundY; 
    star.addPoint(originX, originY); 
    star.addPoint(originX, topY); 
    star.addPoint(bottomLeftX, bottomY); 
    star.addPoint(midRightX, middleY); 
    //star.addPoint(midLeftX, middleY); 
    //star.addPoint(bottomRightX, bottomY); 
    //star.addPoint(originX, topY); 
    g.drawPolygon(star); 
} 

ので左下の点はx軸から中心から234度または54度であるべきです。これらのいずれかをコード化して操作(+またはマイナス)を変更すると、それは意図したものとは反対の方向に発射するようです。中点は中心から18度上になるはずですが、外見上はx軸にほぼ沿って見えます。私は星に近づくためにそれを微調整することができるかどうかを確認するためにポイントを1つずつ無効にしていましたが、これで私は目隠しして酔っ払いながら彫刻に相当することを認識しました。

あなたが何が間違っているのか理解してもらえれば、大変感謝しています。

+1

あなたが期待している/希望しているイメージの種類の小さなスケッチが役立つかもしれませんが、いくつか追加してもよろしいですか? –

+0

* "私はJava Appletクラスにかなり制限されています。" * 1)なぜアプレットをコーディングするのですか?教師がそれを指定している場合は、[CS教師が** Javaアプレットを教えるのを止めるべき理由](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should -stop-teaching-java-applets /)を使用します。 2)[Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/)および[Plugin-Free Webへの移動]も参照してください。 (https://blogs.oracle.com/java-platform-group/moving-to-a-plugin-free-web)。 –

答えて

3

多分あなたは5つ星の星が欲しいです。

private double sin(int degree) { 
    return Math.sin(Math.toRadians(degree)); 
} 

private double cos(int degree) { 
    return Math.cos(Math.toRadians(degree)); 
} 

@Override 
public void paint(Graphics g) { 
    super.paint(g); 
    Polygon star = new Polygon(); 

    int radius = 20; 
    int originX = 100; 
    int originY = 100; 
    int innerRadius = (int) Math.round(radius * sin(18)/sin(126)); 

    Point[] points = new Point[11]; 
    // top center 
    points[0] = new Point(); 
    points[0].x = 0; 
    points[0].y = -radius; 
    // inner top left 
    points[1] = new Point(); 
    points[1].x = -(int) Math.round(innerRadius * cos(54)); 
    points[1].y = -(int) Math.round(innerRadius * sin(54)); 
    // top left 
    points[2] = new Point(); 
    points[2].x = -(int) Math.round(radius * cos(18)); 
    points[2].y = -(int) Math.round(radius * sin(18)); 
    // inner bottom left 
    points[3] = new Point(); 
    points[3].x = -(int) Math.round(innerRadius * cos(18)); 
    points[3].y = (int) Math.round(innerRadius * sin(18)); 
    // bottom left 
    points[4] = new Point(); 
    points[4].x = -(int) Math.round(radius * cos(54)); 
    points[4].y = (int) Math.round(radius * sin(54)); 
    // inner bottom center 
    points[5] = new Point(); 
    points[5].x = 0; 
    points[5].y = innerRadius; 
    // bottom right 
    points[6] = new Point(); 
    points[6].x = (int) Math.round(radius * cos(54)); 
    points[6].y = (int) Math.round(radius * sin(54)); 
    // inner bottom right 
    points[7] = new Point(); 
    points[7].x = (int) Math.round(innerRadius * cos(18)); 
    points[7].y = (int) Math.round(innerRadius * sin(18)); 
    // top right 
    points[8] = new Point(); 
    points[8].x = (int) Math.round(radius * cos(18)); 
    points[8].y = -(int) Math.round(radius * sin(18)); 
    // inner top right 
    points[9] = new Point(); 
    points[9].x = (int) Math.round(innerRadius * cos(54)); 
    points[9].y = -(int) Math.round(innerRadius * sin(54)); 
    // top center 
    points[10] = new Point(); 
    points[10].x = 0; 
    points[10].y = -radius; 

    for (int i = 0; i < points.length; i++) { 
     star.addPoint(originX + points[i].x, originY + points[i].y); 
    } 
    g.drawPolygon(star); 

} 
+0

ありがとう、私は、ポイントクラスが存在することを知らなかった。今私はそれを50回呼び出さなければならないので、これをメソッドにカプセル化することに取り掛かります。 –

+0

あなたはすでに十分に助けてくれましたが、カプセル化しようとすると、メソッド内から呼び出すのに問題があり、Graphics gをメソッドに渡すことができず、スターオブジェクトを返すことができませんそれを描くためにgを得る?あなたはコードで書く必要はありませんが、私はこれを私が呼び出すことのできる方法の中に入れていかなければなりませんか?私は主に正しい方向にポイントが必要です。適切なドキュメントへのリンクが必要です。 –

+0

私はあなたの問題を理解していません。作成したポリゴンスターオブジェクトを簡単に返すように、コード内のメソッドpaint()を簡単に変更できます。変数gですべてを削除し、戻り値の型をPolygonに変更し、最後に作成されたスターオブジェクトを返します。名前を変更してください。私はドキュメントを手助けすることはできませんが、私はあなたのためにそれをgoogleできると思います。 – mayamar

関連する問題