正确生成坐标后,为什么我的代码没有生成多边形?

时间:2019-04-17 17:29:04

标签: java polygon

当前,我已经创建了一个代码,该代码将生成四边形的坐标并将其插入到列表中以生成四边形的角以创建多边形

我目前已生成以下代码,而system.out.prints让我知道生成了哪些坐标,并将其绘制在图形上,并对生成的图形感到满意

这是代码:

   package endOfYearGame;

import java.awt.Color;
import java.awt.Graphics2D;

public class arrow {
    double theta;
    int x;
    int y;
    int horizontal = 70;
    int vertical = 10;
    int originalX = 50;
    int originalY = 800-50;
    public arrow() {
        this.theta = Math.PI/4.0;
        this.x = originalX;
        this.y = originalY;
    }

    public void rotateRight() {
        this.theta = this.theta - 0.1;
    }
    public void rotateLeft() {
        this.theta = this.theta + 0.1;
    }

    public void drawArrow(Graphics2D win) {
        win.setColor(Color.BLACK);
        //x's of the rectangular arrows
        int bottomRightX = (int)(this.x+horizontal*Math.cos(theta));
        int topRightX = (int)(bottomRightX-Math.sin(this.theta)*vertical);
        int topLeftX = (int)(this.x-Math.sin(this.theta)*vertical);

        //y's of the rectangular arrows
        int bottomRightY = (int)(this.y-Math.sin(this.theta)*horizontal);
        int topRightY =  (int)(bottomRightY-vertical*Math.cos(this.theta)) ;
        int topLeftY = (int)(this.y-vertical/Math.cos(this.theta));
        int Xs[] = {this.x, bottomRightX, topRightX, topLeftX};
        int Ys[] = {this.y, bottomRightY, topRightY, topLeftY};
        int Xss[] = {this.x, bottomRightX, topRightX, topLeftX,this.x};
        int Yss[] = {this.y, bottomRightY, topRightY, topLeftY,this.y};
        win.setColor(Color.RED);
        win.drawPolygon(Xs,Ys,4);
        win.fillPolygon(Xss, Yss, 4);
        System.out.println("0000 bottomrightx = "+bottomRightX);
        System.out.println("toprightx= "+topRightX);
        System.out.println("topleftx= " + topLeftX);
        System.out.println("bottomleftx = "+this.x);
        System.out.println("bottomrighty = "+ bottomRightY );
        System.out.println("toprighty = "+topRightY);
        System.out.println("toplefty = "+topLeftY);
        System.out.println("bottomlefty = "+this.y);
    }

}

但是它根本不产生多边形!

我想知道这是否有问题吗?

1 个答案:

答案 0 :(得分:1)

您的代码从不调用drawArrow。如果这样做,它将绘制多边形。输出如下:

0000 bottomrightx = 99
toprightx= 91
topleftx= 42
bottomleftx = 50
bottomrighty = 700
toprighty = 692
toplefty = 735
bottomlefty = 750

这是1024x768窗口中的结果:

Large gray image with spec of red in the bottom right