I have been trying to rotate an image using for loops. My code does work, but this method seems unnecessary, and the image loses pixels as it rotates. Is there an easier way to do this?
//rotates image around center a degrees
public void drawRotatedImage(Graphics g, BufferedImage image, double a, int x, int y, int pixelSize) {
//origin point for rotation
int rX = image.getWidth() / 2;
int rY = image.getHeight() / 2;
for(int x1 = 0; x1 < image.getWidth(); x1++) {
for(int y1 = 0; y1 < image.getHeight(); y1++) {
int c = image.getRGB(x1, y1);
//rotating point
int nX = (int) (((x1-rX) * Math.cos(a)) - ((y1-rY) * Math.sin(a)));
int nY = (int) (((x1-rX) * Math.sin(a)) + ((y1-rY) * Math.cos(a)));
g.setColor(new Color(c));
//drawing each pixel
g.fillRect((nX*pixelSize) + x, (nY*pixelSize) + y, pixelSize, pixelSize);
}
}
}