Java Utililty Methods Image

List of utility methods to do Image

Description

The list of methods to do Image are organized into topic(s).

Method

voidsizeToImage(JComponent component, Image image)
Sizes a component based on the size of the provided image.
if (image != null) {
    int height = image.getHeight(null);
    int width = image.getWidth(null);
    Dimension d = new Dimension(width, height);
    component.setPreferredSize(d);
ImageswapColor(Image image, Color imageColor, Color newColor)
swap Color
final int irgb = imageColor.getRGB();
final int nrgb = newColor.getRGB();
RGBImageFilter filter = new RGBImageFilter() {
    @Override
    public int filterRGB(int x, int y, int rgb) {
        if (rgb == irgb) {
            return nrgb;
        return rgb;
};
FilteredImageSource filteredSrc = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(filteredSrc);
voidtexturePaintHorizontal(JComponent parent, Graphics g, Image image, Rectangle areaToPaint)
Paint a single texture horizontally across the screen.
int imageWidth = image.getWidth(parent);
int imageHeight = image.getHeight(parent);
int cols = areaToPaint.width / imageWidth;
for (int x = 0; x < cols; x++) {
    g.drawImage(image, x * imageWidth + areaToPaint.x, areaToPaint.y, parent);
int remainingWidth = areaToPaint.width - cols * imageWidth;
if (remainingWidth > 0) {
...
booleanusesAlpha(Image image)
This method returns true if the specified image has at least one pixel that is not fully opaque.
if (image == null) {
    return false;
BufferedImage bimage = toBufferedImage(image);
Raster alphaRaster = bimage.getAlphaRaster();
if (alphaRaster == null) {
    return false;
DataBuffer dataBuffer = alphaRaster.getDataBuffer();
for (int i = 0; i < dataBuffer.getSize(); i++) {
    int alpha = dataBuffer.getElem(i);
    if (alpha < 255) {
        return true;
return false;
BooleanverifyImageSizeBigger(byte[] imageData, int maxDim)
verify Image Size Bigger
ImageIcon imageIcon = new ImageIcon(imageData);
Image inImage = imageIcon.getImage();
int origWidth = inImage.getWidth(null);
int origHeight = inImage.getHeight(null);
return ((origWidth > maxDim) && (origHeight > maxDim));