我在JPanel扩展类上绘制了一个矩形(一个Polygon对象)。
然后我尝试创建另一个Polygon对象,该对象应该是原始Polygon的90度旋转。
我使用了旋转矩阵。
由于某种原因,它不起作用。运行程序只显示原始矩形。
怎么了?感谢
public class DrawingPanel extends JPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Point p1,p2,p3,p4;
p1 = new Point(50,50);
p2 = new Point(200,50);
p3 = new Point(200,100);
p4 = new Point(50,100);
int[] x = {(int) p1.getX(), (int) p2.getX(), (int)p3.getX(), (int) p4.getX()};
int[] y = {(int) p1.getY(), (int) p2.getY(), (int)p3.getY(), (int) p4.getY()};
Polygon poly = new Polygon(x, y, x.length);
g2d.draw(poly);
p1.setLocation(p1.getX() * Math.cos(Math.toRadians(90)) - p1.getY() * Math.sin(Math.toRadians(90)),
p1.getX() * Math.sin(Math.toRadians(90)) + p1.getY() * Math.cos(Math.toRadians(90)));
p2.setLocation(p2.getX() * Math.cos(Math.toRadians(90)) - p2.getY() * Math.sin(Math.toRadians(90)),
p2.getX() * Math.sin(Math.toRadians(90)) + p2.getY() * Math.cos(Math.toRadians(90)));
p3.setLocation(p3.getX() * Math.cos(Math.toRadians(90)) - p3.getY() * Math.sin(Math.toRadians(90)),
p3.getX() * Math.sin(Math.toRadians(90)) + p3.getY() * Math.cos(Math.toRadians(90)));
p4.setLocation(p4.getX() * Math.cos(Math.toRadians(90)) - p4.getY() * Math.sin(Math.toRadians(90)),
p4.getX() * Math.sin(Math.toRadians(90)) + p4.getY() * Math.cos(Math.toRadians(90)));
int[] x2 = {(int) p1.getX(), (int) p2.getX(), (int)p3.getX(), (int) p4.getX()};
int[] y2 = {(int) p1.getY(), (int) p2.getY(), (int)p3.getY(), (int) p4.getY()};
Polygon poly2 = new Polygon(x2, y2, x2.length);
g2d.draw(poly2);
}
}
答案 0 :(得分:1)
我相信你正在将它从屏幕上旋转出来,所以我不会因此画出来。
您的代码将围绕左上角旋转90,导致位于屏幕左侧