IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Graphisme Java Discussion :

Rotation de BufferedImage


Sujet :

Graphisme Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    109
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 109
    Par défaut Rotation de BufferedImage
    Bonjour,

    Je suis à la recherche d'une méthode permettant de faire tourner une bufferedImage.

    J'ai trouvé la méthode affineTransform mais je n'arrive pas a comprendre dans quel sens il tourne l'image.

    Voici le code que j'ai actuellement :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    AffineTransform at = new AffineTransform();
    				at.rotate( angleFinal
    						 , imgUn.getImageAffiche().getWidth() / 2
    						 , imgUn.getImageAffiche().getHeight() / 2 );
    				AffineTransformOp ato = new AffineTransformOp( at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    				BufferedImage imageFinale = new BufferedImage( imgUn.getImageAffiche().getWidth()
    															 , imgUn.getImageAffiche().getHeight()
    															 , imgUn.getImageAffiche().getType() );
    				imgUn.setImageAffiche( ato.filter( imgUn.getImageAffiche(), imageFinale ) );
    On dirai que pour des angle < 180 l'image tourne en sens inverse d'une montre et pour des angles > 180 l'image tourne dans le même sens qu'une montre...
    Si quelqu'un pouvait m'éclairer sur le sujet ça serait très gentil.

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 897
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 897
    Billets dans le blog
    54
    Par défaut
    Juste au cas ou je rappelle que:
    1. AffineTransform requiert des radians pas des degrés.
    2. L'axe des ordonnées (Y) est orienté vers le bas sur l'écran, les images ou dans les Graphics.


    Donc :
    • quand on incrémente l'angle, on tourne dans le sens des aiguilles d'une montre.
    • quand on décrémente l'angle, on tourne dans le sens inverse des aiguilles d'une montre.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
     
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
     
    /**
     *
     * @author fabriceb
     */
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws Exception {
            BufferedImage loadedImage = ImageIO.read(Main.class.getClassLoader().getResource("test/resources/AvataFacebook2009.png"));
            GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
            final BufferedImage image = gc.createCompatibleImage(loadedImage.getWidth(), loadedImage.getHeight(), loadedImage.getTransparency());
            Graphics g = image.getGraphics();
            try {
                g.drawImage(loadedImage, 0, 0, null);
            } finally {
                g.dispose();
            }
            SwingUtilities.invokeLater(new Runnable() {
     
                /**
                 * {@inheritDoc}
                 */
                @Override
                public void run() {
                    final ImagePanel imagePanel = new ImagePanel(image);
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(imagePanel, BorderLayout.CENTER);
                    frame.setSize(500, 500);
                    frame.setVisible(true);
                    Timer timer = new Timer(75, new ActionListener() {
     
                        /**
                         * {@inheritDoc}
                         */
                        @Override
                        public void actionPerformed(ActionEvent event) {
                            double angle = imagePanel.getAngle();
                            angle += ImagePanel.PI2 * 1 / 360.0;
                            imagePanel.setAngle(angle);
                        }
                    });
                    timer.setRepeats(true);
                    timer.start();
                }
            });
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package test;
     
    import java.awt.AlphaComposite;
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.Stroke;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.text.MessageFormat;
    import javax.swing.JPanel;
     
    /**
     *
     * @author fabriceb
     */
    public class ImagePanel extends JPanel {
     
        public static final double PI2 = 2 * Math.PI;
        private BufferedImage image;
        private double angle = 0;
     
        public ImagePanel(BufferedImage image) {
            this.image = image;
        }
     
        public void setAngle(double value) {
            angle = value % PI2;
            repaint();
        }
     
        public double getAngle() {
            return angle;
        }
        private Stroke stroke = new BasicStroke(3.0f);
     
        /**
         * {@inheritDoc}
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int width = getWidth();
            int height = getHeight();
            if (image == null || width == 0 || height == 0) {
                return;
            }
            Graphics2D g2d = (Graphics2D) g.create();
            try {
                g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g2d.setColor(Color.BLACK);
                g2d.drawString(MessageFormat.format("Radians: {0}", angle), 10, 10);
                g2d.drawString(MessageFormat.format("Degrees: {0}", 360 * angle / PI2), 10, 20);
                g2d.translate(width / 2.0, height / 2.0);
                int dx = -image.getWidth() / 2;
                int dy = -image.getHeight() / 2;
                if (angle % PI2 != 0) {
                    AffineTransform transform = AffineTransform.getRotateInstance(angle);
                    g2d.transform(transform);
                    g2d.drawImage(image, dx, dy, null);
                    transform = AffineTransform.getRotateInstance(-angle);
                    g2d.transform(transform);
                    g2d.setComposite(AlphaComposite.SrcOver.derive(0.3f));
                }
                g2d.drawImage(image, dx, dy, null);
                g2d.setComposite(AlphaComposite.SrcOver);
                g2d.setStroke(stroke);
                g2d.setColor(Color.GREEN);
                g2d.drawLine(0, 0, 100, 0);
                g2d.drawLine(100, 0, 90, 10);
                g2d.drawLine(100, 0, 90, -10);
                g2d.setColor(Color.RED);
                g2d.drawLine(0, 0, 0, 100);
                g2d.drawLine(0, 100, 10, 90);
                g2d.drawLine(0, 100, -10, 90);
            } finally {
                g2d.dispose();
            }
     
        }
    }
    Nom : Rotate.jpg
Affichages : 764
Taille : 73,6 Ko
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    109
    Détails du profil
    Informations personnelles :
    Âge : 35
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 109
    Par défaut
    Merci j'arrive a tourner l'image comme je le souhaite mais maintenant il me faut réaliser la rotation d'un point avec la même transformée. Le problème, c'est que j'ai l'impression qu'il tourne plus "rapidement" que l'image, c'est à dire qu'il a un angle plus important.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    AffineTransform at = AffineTransform.getRotateInstance( angleFinal, flagTimbreUnRotation.x * imgUn.getCoefZoom() , flagTimbreUnRotation.y * imgUn.getCoefZoom() );
     
    				AffineTransformOp ato = new AffineTransformOp( at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    				BufferedImage imageFinale = new BufferedImage( imgUn.getImageAffiche().getWidth() , imgUn.getImageAffiche().getHeight() , imgUn.getImageAffiche().getType() );
    				imgUn.setImageAffiche( ato.filter( imgUn.getImageAffiche(), imageFinale ) );
     
    				Point2D pointTemp = null;
    // rotation du point flagTimbreDeuxRotation
    				flagTimbreDeuxRotation.setLocation( ato.getPoint2D( flagTimbreDeuxRotation, pointTemp ) );

Discussions similaires

  1. Réponses: 6
    Dernier message: 10/08/2007, 16h37
  2. Rotation de Bitmap -> ScanLine
    Par jujuesteban dans le forum Langage
    Réponses: 7
    Dernier message: 03/07/2003, 15h11
  3. Rotation d'un bouton ?
    Par ken_survivant dans le forum Composants
    Réponses: 3
    Dernier message: 01/04/2003, 18h16
  4. matrice et rotation
    Par charly dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 07/12/2002, 17h59
  5. algo : rotation d'objet 3d
    Par numeror dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 19/08/2002, 22h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo