Getting the right image observer to rotate the image

So, I draw a BufferedImage bird, but I want to rotate it according to the angle of incidence. I have a bird object that contains a BufferedImage and render () method that rotates it.

public void render(Graphics2D g, ImageObserver io) {

    double theta = Math.tan((height - pastHeight) / .875);
    System.out.println(theta);
    Graphics2D g2 = (Graphics2D) bird.getGraphics();

    g2.drawImage(bird, 100, (int) height, null);

    g2.rotate(theta);

    g2.drawImage(bird, 100, (int) height, io);
}

I call it as such

bird.render(g2, ???);

in my paintcomponent method in my jcomponent.

The only problem is that I don’t know what to use as my ImageObserver ... I tried skipping in my JFrame and my JComponent, but the image no longer appears when I do it ... what would I pass for the image to appear in my window and / or how else can I achieve this rotation?

0
source share
1

, -, JComponent,

bird.render(g2, this);

JComponent ImageObserver

ImageObserver, , , , , / Graphics.

Graphics2D#rotate(double, int, int), ( ).

reset , , , .

, rotate.

Graphics#rotate(double)

rotate

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RotateImage {

    public static void main(String[] args) {
        new RotateImage();
    }

    public RotateImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage img;
        private double angel = 0d;

        public TestPane() {
            try {
                img = ImageIO.read(...);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    angel += 5;
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.rotate(Math.toRadians(angel));
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getHeight()) / 2;
                g2d.drawImage(img, x, y, this);
                g2d.dispose();
            }
        }
    }

}

g2d.rotate(Math.toRadians(angel)); g2d.rotate(Math.toRadians(angel), getWidth() / 2, getHeight() / 2);, ( Graphics ) , ...

Center

, , ( , )

+1

All Articles