Showing posts with label Graphics2D. Show all posts
Showing posts with label Graphics2D. Show all posts

Saturday, December 1, 2012

Translate and scale with AffineTransform

Translate and scale with AffineTransform
Translate and scale with AffineTransform


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
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{

        @Override
        protected void paintComponent(Graphics g) {
            try {
                //prepare a original Image source
                Image image = ImageIO.read(this.getClass().getResource("duke.png"));

                int w = image.getWidth(null);
                int h = image.getHeight(null);
                g.drawImage(image, 0, 0, w, h, null);

                //translate and scale with AffineTransform
                AffineTransform affineTransform = new AffineTransform();
                affineTransform.translate(0, h);
                Double sx = 2.0;
                Double sy = 2.0;
                affineTransform.scale(sx, sy);
                ((Graphics2D)g).drawImage(image, affineTransform, null);
                
            } catch (IOException ex) {
                Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
    
    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();
            this.add(myComponent);
        }
    }

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

}


Draw Image on translated and scaled Graphics

Draw Image on translated and scaled Graphics
Draw Image on translated and scaled Graphics


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
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{

        @Override
        protected void paintComponent(Graphics g) {
            try {
                //prepare a original Image source
                Image image = ImageIO.read(this.getClass().getResource("duke.png"));

                int w = image.getWidth(null);
                int h = image.getHeight(null);
                g.drawImage(image, 0, 0, w, h, null);

                //translate and scale
                g.translate(0, h);
                Double sx = 2.0;
                Double sy = 2.0;
                ((Graphics2D)g).scale(sx, sy);
                g.drawImage(image, 0, 0, null);
                
            } catch (IOException ex) {
                Logger.getLogger(JavaTestSwing.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
    
    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();
            this.add(myComponent);
        }
    }

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

}


Thursday, November 8, 2012

Draw Shape example: drawOval() and Ellipse2D.Double()

drawOval() and Ellipse2D.Double()


package javaswing;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
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{
        
        

        @Override
        protected void paintComponent(Graphics g) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            
            float x1 = width/4;
            float y1 = height/4;
            float x2 = width/4 + width/2;
            float y2 = height/4 + height/2;
            Color color1 = Color.RED;
            Color color2 = Color.BLUE;

            g.drawOval(0, 0, width/2, height/2);
            Ellipse2D.Double ellipse2D
                    = new Ellipse2D.Double(width/2, height/2, width/2, height/2);
            graphics2D.draw(ellipse2D);

        }
    }
    
    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();
            this.add(myComponent);
        }
    }

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

}


Sunday, November 4, 2012

Draw Shape example: GradientPaint

GradientPaint
GradientPaint

package javaswing;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
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{
        
        

        @Override
        protected void paintComponent(Graphics g) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            
            float x1 = width/4;
            float y1 = height/4;
            float x2 = width/4 + width/2;
            float y2 = height/4 + height/2;
            Color color1 = Color.RED;
            Color color2 = Color.BLUE;
            GradientPaint gradientPaint 
                    = new GradientPaint(x1, y1, color1, x2, y2, color2);
            
            graphics2D.setPaint(gradientPaint); 
            Rectangle2D.Double rectangle 
                    = new Rectangle2D.Double(width/4, height/4, width/2, height/2);
            graphics2D.fill(rectangle);
            
        }
    }
    
    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();
            this.add(myComponent);
        }
    }

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

}


Saturday, November 3, 2012

Draw Shape example: Ellipse2D and Rectangle2D

Ellipse2D and Rectangle2D
Ellipse2D and Rectangle2D


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
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{
        
        

        @Override
        protected void paintComponent(Graphics g) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            
            Ellipse2D.Double ellipse 
                    = new Ellipse2D.Double(0, 0, width, height);
            Rectangle2D.Double rectangle 
                    = new Rectangle2D.Double(width/4, height/4, width/2, height/2);
            
            graphics2D.draw(ellipse);
            graphics2D.fill(rectangle);
            
        }
    }
    
    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();
            this.add(myComponent);
        }
    }

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

}


Thursday, November 1, 2012

Draw Shape example

Draw Shape example


package javaswing;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
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{

        @Override
        protected void paintComponent(Graphics g) {

            //g.drawImage(bufferedImage, 0, 0, null);
            Graphics2D graphics2D = (Graphics2D)g;
            int width = getWidth();
            int height = getHeight();
            Shape shape = new java.awt.geom.Ellipse2D.Float(
                    0, 
                    0, 
                    width, 
                    height);
            
            graphics2D.draw(shape);
        }
    }
    
    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();
            this.add(myComponent);
        }
    }

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

}


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);
        
    }

}


Scale image using Graphics2D.scale()

Example to draw scaled bufferedImage on  Graphics2D:

scaled bufferedImage on  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.scale(2, 2);
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //1X
            graphics2D = (Graphics2D)g.create();
            graphics2D.drawImage(bufferedImage, 0, 0, null);
            
            //0.5X
            graphics2D = (Graphics2D)g.create();
            graphics2D.scale(0.5, 0.5);
            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);
        
    }

}