1

I'm somewhat new to java graphics and am trying to learn how to use rotation. from what I have read 'affineTransform' is the key to pretty much all of it. Here I can't seem to figure out how I would create this rectangle and apply rotation to it. any ideas?

package games;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Rotation extends JPanel {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.add(new Rotation());
        f.setSize(600, 600);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    int i = 0;

    public void paint(Graphics g) {
        Rectangle ship = new Rectangle(50, 50, 200, 100); //  <------

        AffineTransform at = new AffineTransform();
        at.rotate(Math.toRadians(i++), ship.getWidth() / 2, ship.getHeight() / 2);

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(ship, at, null); //   <----------------

        repaint();
    }
}
Nick DeFazio
  • 2,412
  • 27
  • 31
Logan Hunt
  • 39
  • 7
  • 1
    Don't override `paint`, override `paintComponent` instead; do call `super.paintComponent`; Don't apply a transformation to the current `Graphics` context, use `Graphics#create` to create a snapshot of the current context and use the copy instead. Don't call `repaint` instead a `paint` method; do use `this` as the `ImageObserver` for `drawImage` – MadProgrammer Oct 27 '15 at 23:08
  • 1
    For [example](http://stackoverflow.com/questions/31958932/how-do-i-rotate-objects-images-independently-in-java/31959926#31959926), [example](http://stackoverflow.com/questions/32774263/i-am-trying-to-draw-the-line-in-the-middle-i-tried-different-coordinates/32774421#32774421) – MadProgrammer Oct 27 '15 at 23:12
  • 1
    And [example](http://stackoverflow.com/a/3420651/230513). – trashgod Oct 28 '15 at 00:47

0 Answers0