Java 为什么';当正确生成坐标时,我的代码是否生成多边形?
目前,我已经创建了一个生成四边形坐标的代码,并将它们插入到列表中,以生成四边形的角点,从而生成多边形 我目前已经生成了以下代码,system.out.prints让我知道生成了什么坐标。我已经将它们绘制在图形上,并且我对它生成的绘图感到满意 然而,这是代码:Java 为什么';当正确生成坐标时,我的代码是否生成多边形?,java,polygon,Java,Polygon,目前,我已经创建了一个生成四边形坐标的代码,并将它们插入到列表中,以生成四边形的角点,从而生成多边形 我目前已经生成了以下代码,system.out.prints让我知道生成了什么坐标。我已经将它们绘制在图形上,并且我对它生成的绘图感到满意 然而,这是代码: package endOfYearGame; import java.awt.Color; import java.awt.Graphics2D; public class arrow { double theta;
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);
}
}
但它根本不生成多边形
我想知道这是不是出了什么问题 您的代码从不调用
绘图箭头
。如果它这样做了,它将绘制多边形。以下是输出:
0000 bottomrightx = 99
toprightx= 91
topleftx= 42
bottomleftx = 50
bottomrighty = 700
toprighty = 692
toplefty = 735
bottomlefty = 750
以下是1024x768窗口中的结果:
需要全班同学来看看发生了什么,因为你引用的是θ、垂直等,我们不知道这些值是什么。查看输出也很有帮助-我想知道在
vertical/Math.cos(this.theta)
中除法是否正确,以及“坐标是否正确生成”(如果真的是这样,那么错误一定在drawPolygon()
中,我们不知道它是做什么的,但它的名称)@Benson99上传了代码一个典型的错误可能是多边形是一面的,只能从另一面看到(即,你可能以错误的顺序通过了各个角落),但仅凭这段代码很难说