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

}


No comments:

Post a Comment