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