Search code examples
javaeclipseimagejlabelimage-rotation

Rotation Of An Image In JLabel - Java


I am currently making a game with Java. I added an image to the screen using a JLabel and I want to know how to rotate it. I have done a lot of research but have not found anything useful. Can I have some help? Here is my code:

(Code from first class)

public static ImageIcon image = new ImageIcon("Images/unnamed.png");
public static JLabel label = new JLabel("", image, JLabel.CENTER);

(Code from second class)

Main.label.setBounds(Game.birdX, Game.birdY, Game.birdWidth, Game.birdWidth);
add(Main.label);

Sorry if I am doing something wrong on this website, it's one of my first times using it. Also, I have only been coding Java for 2 weeks now.


Solution

  • One way is to use the Rotated Icon.

    You just specify the degrees of rotation and a new Icon will be created for you.

    Also, don't use static variable. This suggests improper design. The icon and label should just be instance variables in the class where you use them.