Thursday, November 29, 2012

Draw partial scaled image using Graphics.drawImage()

Last post demonstrate a simple way to draw scaled image using Graphics.drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) method. We can also use drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) method to draw part of the image.

drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)

Where:
img - the specified image to be drawn. This method does nothing if img is null.
dx1 - the x coordinate of the first corner of the destination rectangle.
dy1 - the y coordinate of the first corner of the destination rectangle.
dx2 - the x coordinate of the second corner of the destination rectangle.
dy2 - the y coordinate of the second corner of the destination rectangle.
sx1 - the x coordinate of the first corner of the source rectangle.
sy1 - the y coordinate of the first corner of the source rectangle.
sx2 - the x coordinate of the second corner of the source rectangle.
sy2 - the y coordinate of the second corner of the source rectangle.
observer - object to be notified as more of the image is scaled and converted.

Draw partial scaled image using Graphics.drawImage()
Draw partial scaled image using Graphics.drawImage()


package javaswing;

import java.awt.Graphics;
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);

                g.drawImage(image,
                        0,          //dx1
                        h,          //dy1
                        w,          //dx2
                        h + h,      //dy2
                        0,          //sx1
                        0,          //sy1
                        w/2,        //sx2
                        h/2,        //sy2
                        null);      //ImageObserver
                
            } 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 scaled image using Graphics.drawImage()

Draw scaled image using Graphics.drawImage()


package javaswing;

import java.awt.Graphics;
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);
                
                int scale = 2;
                g.drawImage(image, 0, h, w*scale, h*scale, 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);
        
    }

}



We can also call drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) method to draw part of the image.

Wednesday, November 28, 2012

Example of using createCompatibleImage()

Example of using createCompatibleImage() method to create Compatible BufferedImage and Translucent Compatible BufferedImage.

Example of using createCompatibleImage()
Example of using createCompatibleImage()


package javaswing;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
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.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"));

                //Get current GraphicsConfiguration
                GraphicsConfiguration graphicsConfiguration 
                        = GraphicsEnvironment
                        .getLocalGraphicsEnvironment()
                        .getDefaultScreenDevice()
                        .getDefaultConfiguration();
                
                //Create a Compatible BufferedImage
                BufferedImage bufferedImage 
                        = graphicsConfiguration.createCompatibleImage(
                        image.getWidth(null), 
                        image.getHeight(null));
                //Copy from original Image to new Compatible BufferedImage
                Graphics tempGraphics = bufferedImage.getGraphics();
                tempGraphics.drawImage(image, 0, 0, null);
                tempGraphics.dispose();
                
                //Create a Compatible BufferedImage for translucent image
                BufferedImage bufferedImage_translucent
                        = graphicsConfiguration.createCompatibleImage(
                        image.getWidth(null), 
                        image.getHeight(null),
                        Transparency.TRANSLUCENT);
                //Copy from original Image to new Compatible BufferedImage
                Graphics tempGraphics_translucent 
                        = bufferedImage_translucent.getGraphics();
                tempGraphics_translucent.drawImage(image, 0, 0, null);
                tempGraphics_translucent.dispose();
                
                g.drawImage(bufferedImage, 0, 0, null);
                g.drawImage(bufferedImage_translucent, 0, bufferedImage.getHeight(), 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);
        
    }

}


Tuesday, November 27, 2012

Example of using Graphics.copyArea()

The method copyArea(int x, int y, int width, int height, int dx, int dy) copies an area of the component by a distance specified by dx and dy. From the point specified by x and y, this method copies downwards and to the right. To copy an area of the component to the left or upwards, specify a negative value for dx or dy. If a portion of the source rectangle lies outside the bounds of the component, or is obscured by another window or component, copyArea will be unable to copy the associated pixels. The area that is omitted can be refreshed by calling the component's paint method.

Parameters:
x - the x coordinate of the source rectangle.
y - the y coordinate of the source rectangle.
width - the width of the source rectangle.
height - the height of the source rectangle.
dx - the horizontal distance to copy the pixels.
dy - the vertical distance to copy the pixels.

Graphics.copyArea() example


package javaswing;

import java.awt.Graphics;
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.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 {
                BufferedImage bufferedImage = ImageIO.read(this.getClass().getResource("duke.png"));
                g.drawImage(bufferedImage, 0, 0, null);
                
                int source_x = 0;
                int source_y = 0;
                int source_width = bufferedImage.getWidth();
                int source_height = bufferedImage.getHeight();
                int dx = bufferedImage.getWidth();
                int dy = 0;
                
                //copy horizontally
                for(int i = 0; i < 3; i++){
                    g.copyArea(
                        source_x,
                        source_y,
                        source_width,
                        source_height,
                        dx,
                        dy);
                    dx += source_width;
                }
                
                //copy vertically
                source_x = 0;
                source_y = 0;
                source_width = bufferedImage.getWidth() * 4;
                source_height = bufferedImage.getHeight();
                dx = 0;
                dy = bufferedImage.getHeight();
                g.copyArea(
                        source_x,
                        source_y,
                        source_width,
                        source_height,
                        dx,
                        dy);
                
            } 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);
        
    }

}


Friday, November 23, 2012

How to Get Started (FAST!) with JavaFX 2 and Scene Builder


Scene Builder

“How to Get Started (FAST!) with JavaFX 2 and Scene Builder.” Heckler, who has development experience in numerous environments, shows developers how to develop a JavaFX application using Scene Builder “in less time than it takes to drink a cup of coffee, while learning your way around in the process”.

Read it HERE.

Wednesday, November 14, 2012

Java 7 Concurrency Cookbook



Java remains the global standard for developing various applications and enterprise software, and the launch of Java 7 brings with it exciting new capabilities for concurrent programming by way of the concurrency utilities enhancement. This allows developers to make the most of their applications with parallel task performance. "Java 7 Concurrency Cookbook" covers all elements of the Java concurrency API, providing essential recipes for taking advantage of the exciting new capabilities.

On your computer, you can listen to music while you edit a Word document and read your emails, all at once! This is because your operating system allows the concurrency of tasks, much like the Java platform which offers various classes to execute concurrent tasks inside a Java program. "Java 7 Concurrency Cookbook" covers the most important features of the Java concurrency API, with special emphasis on the new capabilities of version 7.

With each version, Java increases the available functionality to facilitate development of concurrent programs. This book covers the most important and useful mechanisms included in version 7 of the Java concurrency API, so you will be able to use them directly in your applications.

"Java 7 Concurrency Cookbook" includes recipes to enable you to achieve everything from the basic management of threads and tasks, to the new Fork /Join framework, through synchronization mechanisms between tasks, different types of concurrent tasks that Java can execute, data structures that must be used in concurrent applications and the classes of the library that can be customized.

With the step-by-step examples in this book you’ll be able to apply the most important and useful features of the Java 7 concurrency API.

Approach
"Java 7 Concurrency Cookbook" is a practical Cookbook packed with real-world solutions. Intermediate–advanced level Java developers will learn from task-based recipes to use Java’s concurrent API to program thread safe solutions.

Who this book is for
If you are a Java developer who wants to take your knowledge of concurrent programming and multithreading further, as well as discover the new concurrency features of Java 7, then "Java 7 Concurrency Cookbook" is for you.

You should already be comfortable with general Java development practices and a basic grasp of threads would be an advantage.

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

}