Reputation: 1947
I made a rectangle (a Polygon object) be drawn on a JPanel-extending class.
I then tried to create another Polygon object, that should be a 90 degree rotation of the original Polygon.
I used the rotation matrix.
For some reason it doesn't work. Running the program only displays the original rectangle.
What's wrong? Thanks
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);
}
}
Upvotes: 0
Views: 633
Reputation: 3650
I believe you are rotating it off the screen, so I won't draw as a result.
Your code will rotate it 90 around the top left corner, resulting in being to the left of the screen
Upvotes: 1