Tuesday, October 23, 2012

Translate image using Graphics2D.translate()

Example to draw bufferedImage on translated Graphics2D:

draw bufferedImage on translated Graphics2D


package javaswing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaTestSwing {
    
    static JFrameWin jFrameWindow;
    
    public static class MyComponent extends JComponent{
        
        BufferedImage bufferedImage = null;
        Dimension myDimension = new Dimension(50, 50);

        public MyComponent() {
            try {
                bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
                int imageWidth = bufferedImage.getWidth();
                int imageHeight = bufferedImage.getHeight();
                myDimension = new Dimension(imageWidth * 2, imageHeight * 2);
            } catch (IOException ex) {
                Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        

        @Override
        public Dimension getPreferredSize() {
            return myDimension;
        }

        @Override
        public Dimension getMaximumSize() {
            return myDimension;
        }

        @Override
        public Dimension getMinimumSize() {
            return myDimension;
        }

        @Override
        protected void paintComponent(Graphics g) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D;
            
            //2X
            graphics2D = (Graphics2D)g.create();
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //1X
            graphics2D = (Graphics2D)g.create();
            graphics2D.translate(30, 0);
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //0.5X
            graphics2D = (Graphics2D)g.create();
            graphics2D.translate(60, 0);
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //dispose Graphics2D object
            graphics2D.dispose();
        }
    }
    
    public static class JFrameWin extends JFrame{
        public JFrameWin(){
            this.setTitle("java-buddy.blogspot.com");
            this.setSize(300, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
            MyComponent myComponent = new MyComponent();
             
            Box horizontalBox = Box.createHorizontalBox();

            horizontalBox.add(myComponent);
            this.add(horizontalBox);
        }
    }
    

    public static void main(String[] args){
        Runnable doSwingLater = new Runnable(){
            public void run() {
                jFrameWindow = new JFrameWin();
                jFrameWindow.setVisible(true);
            }
        };
        
        SwingUtilities.invokeLater(doSwingLater);
        
    }

}


No comments:

Post a Comment