-2

I have a problem with the rotation of an image. This is what I have tried, but it is not working.

Any help would be appreciated.

@Override
public void paintComponent(Graphics p) {

    BufferedImage arrow = LoadImage("C:\\Users\\Pawel Celuch\\Desktop\\arrow.png");

    super.paintComponent(p);
    Graphics2D g2d = (Graphics2D) p;
    g2d.drawImage(arrow, (int)x, (int)y+550, this);

}
grizzthedj
  • 7,131
  • 16
  • 42
  • 62
  • 2
    So .. what's your question? Be warned, if it is something vague & broad like *"Can someone help me?"* it will likely be closed soon. A tip though: don't do potentially 'long running' tasks like loading resources in a paint method. Load the image when the component is being constructed & store the image as an attribute of the class. – Andrew Thompson May 21 '18 at 12:12
  • Change your question like to make it more understandable, like "How do I display rotating image?" – mentallurg May 21 '18 at 12:17
  • Don't read the image in the painting method. You can't control when the component will be repainted so you don't want to keep reading the image. Read the image in the constructor of your class. – camickr May 21 '18 at 14:39

1 Answers1

0

You can use a rotate method with an angle of the AffineTransform class in java.

private double currentAngle = 60.7D;

@Override
protected void paintComponent(final Graphics g) {
     BufferedImage arrow = LoadImage("C:\\Users\\Pawel Celuch\\Desktop\\arrow.png");
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    AffineTransform orgX = g2d.getTransform();
    AffineTransform newX = (AffineTransform) (orgX.clone());
    // adjust center of image view of the panel
    int centX = this.getWidth() / 2;
    int centY = this.getHeight() / 2;
    newX.rotate(Math.toRadians(currentAngle), centX, centY);
    g2d.setTransform(newX);
    g2d.drawImage(arrow, x, y+550, this);
    g2d.setTransform(orgX);

}

The range of a currentAngle must be between 0 to 360 degree.

enter image description here

tommybee
  • 2,409
  • 1
  • 20
  • 23