Java examples for 2D Graphics:Image
get Icon Subimage
//package com.java2s; import java.awt.*; import javax.swing.ImageIcon; import java.awt.image.BufferedImage; public class Main { private static byte tileWidth = 64; private static byte tileHeight = 64; public static BufferedImage getIconSubimage(ImageIcon source, Point origin, int width, int height) { // Convert "source" ImageIcon to a BufferedImage so it can be sliced and diced BufferedImage img = new BufferedImage(source.getIconWidth(), source.getIconHeight(), BufferedImage.TYPE_INT_ARGB); Graphics g = img.createGraphics(); g.drawImage(source.getImage(), 0, 0, null); // Use the magic of math to grab the tile the user clicked on and save it to be used elsewhere BufferedImage subimage = img.getSubimage(origin.x - (origin.x % tileWidth), origin.y - (origin.y % tileHeight), width, height); // Return the subimage retrieved from the "source" ImageIcon return subimage; }/*from ww w . ja va 2s. c o m*/ }