【问题标题】:Why doesn't my code produce a polygon when the coordinates are correctly generated?正确生成坐标后,为什么我的代码不生成多边形?
【发布时间】:2019-04-17 17:29:03
【问题描述】:

目前,我创建了一个代码,可以生成四边形的坐标并将它们插入到一个列表中以生成四边形的角以制作多边形

我目前生成了以下代码,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);
    }

}

但它根本不生成多边形!

我想知道这是否有问题?

【问题讨论】:

  • 需要全班同学看看发生了什么,因为您引用的是 theta、vertical 等,我们不知道这些值是什么。
  • 也有助于查看输出 - 我想知道 vertical/Math.cos(this.theta) 中的除法是否正确以及更多“坐标已正确生成”(如果确实如此的话,那么错误必须drawPolygon()中,我们不知道它做了什么,但它的名字)
  • @Benson99 上传代码
  • 一个典型的错误可能是多边形是一侧的并且只能从另一侧看到(即您可能以错误的顺序通过了角落)但仅凭此代码很难说跨度>

标签: java polygon


【解决方案1】:

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

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

这是 1024x768 窗口中的结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2012-10-15
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多