为什么我的代码在正确生成坐标时不生成多边形?
Why doesn't my code produce a polygon when the coordinates are correctly generated?
目前,我已经创建了一个代码,可以生成四边形的坐标并将它们插入到列表中以生成四边形的角以形成多边形
我目前生成了以下代码,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);
}
}
但它根本不生成多边形!
我想知道这是否有问题?
您的代码从不调用 drawArrow
。如果是,它将绘制多边形。这是输出:
0000 bottomrightx = 99
toprightx= 91
topleftx= 42
bottomleftx = 50
bottomrighty = 700
toprighty = 692
toplefty = 735
bottomlefty = 750
这是 1024x768 的结果 window:
目前,我已经创建了一个代码,可以生成四边形的坐标并将它们插入到列表中以生成四边形的角以形成多边形
我目前生成了以下代码,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);
}
}
但它根本不生成多边形!
我想知道这是否有问题?
您的代码从不调用 drawArrow
。如果是,它将绘制多边形。这是输出:
0000 bottomrightx = 99
toprightx= 91
topleftx= 42
bottomleftx = 50
bottomrighty = 700
toprighty = 692
toplefty = 735
bottomlefty = 750
这是 1024x768 的结果 window: