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.
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