Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Image;

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;

public class Main {
    /**
     * This method returns true if the specified image has transparent pixels
     *
     * @param image
     * @return
     */
    public static boolean hasAlpha(Image image) {
        // If buffered image, the colour model is readily available
        if (image instanceof BufferedImage) {
            return ((BufferedImage) image).getColorModel().hasAlpha();
        }

        // Use a pixel grabber to retrieve the image's colour model;
        // grabbing a single pixel is usually sufficient
        PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
        try {
            pg.grabPixels();
        } catch (InterruptedException e) {
        }

        // Get the image's colour model
        ColorModel cm = pg.getColorModel();
        return cm.hasAlpha();
    }
}