2017-11-26 29 views
1

그래서 깃발을 그릴 프로그램의 별을 만들기 위해 노력하고 있습니다. 저는 Java Applet 클래스에 꽤 제한되어 있습니다. 프로세스가 다른 중심을 가진 모든 별에 대해 동일해야하므로 X 및 Y 값이있는 메서드에 중심을 전달한 다음 다각형을 사용하여 중심에서 각도를 기반으로 그립니다. 문제는 나오는 모양이 별 모양이 아님을 말합니다.사인과 코사인을 사용하여 별을 만들려고하는데

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); 
} 

따라서 왼쪽 하단의 점은 가운데에서 234도, 또는 x 축에서 54 도가되어야합니다. 이 중 하나를 코딩하고 조작을 변경하면 (+ 또는 -) 의도 한 것과 반대 방향으로 촬영하는 것처럼 보입니다. 중심점은 중심보다 18도 높아야하지만, x 축과 거의 비슷하게 보입니다. 나는 별을 가까이에 가지기 위해 그것을 꼬집어 볼 수 있는지 알아보기 위해 점을 하나씩 사용하지 못하게했지만, 눈가리개를 마시고 취한 상태에서 조각하는 것과 같습니다.

내가 뭘 잘못했는지 알 수있게 도와 주시면 크게 감사하겠습니다.

+1

당신이 원하거나 기대하고있는 종류의 이미지 스케치가 도움이 될 수 있습니까? –

+0

* "Java Applet 클래스에 상당히 제한되어 있습니다."* 1) 애플릿을 코딩하는 이유는 무엇입니까? 교사가 지정했기 때문에 [CS 교사가 ** Java 애플릿 교육 **을 중단해야하는 이유] (http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should?hl=ko)를 참조하십시오. -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

감사합니다. 나는 Point 클래스가 존재한다는 것을 몰랐습니다. 이제는 50 번 호출해야하기 때문에이 메소드를 캡슐화하기 위해 노력할 것입니다. –

+0

당신은 이미 많은 도움을 주었지만, 캡슐화 할 때 메서드 내에서 호출하는 데 문제가 발생했습니다. 그래픽 g를 메서드에 전달할 수 없으며, 별 개체를 반환 할 수 없습니다. 그릴 그려? 코드로 작성하지 않아도되지만 호출 할 수있는 메소드 안에이 코드를 삽입하는 방법은 무엇입니까? 필자는 주로 올바른 방향으로 한 지점이 필요하며 적절한 문서에 대한 링크가 필요합니다. –

+0

나는 당신의 문제를 이해하지 못한다. 생성 된 Polygon-star-object를 반환하기 위해 내 코드에서 paint() 메서드를 쉽게 변경할 수 있습니다. 변수 g로 모든 것을 제거하고 반환 유형을 Polygon으로 변경하고 마지막에 생성 된 별 개체를 반환합니다. 이름을 바꾸세요. 나는 문서화에 도움을 줄 수는 없지만, 당신이 그것을 할 수 있다고 생각한다. – mayamar