Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Copyright 2011-2012 Paulo Augusto Peccin. See licence.txt distributed with this file.

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage asCompatibleImage(Image img) {
        BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
                img.getHeight(null));
        Graphics2D gc = ret.createGraphics();
        gc.drawImage(img, 0, 0, null);
        gc.dispose();
        return ret;
    }

    public static BufferedImage asCompatibleImage(Image img, int transparency) {
        BufferedImage ret = defaultScreenDeviceConfiguration().createCompatibleImage(img.getWidth(null),
                img.getHeight(null), transparency);
        Graphics2D gc = ret.createGraphics();
        gc.setComposite(AlphaComposite.Src);
        gc.drawImage(img, 0, 0, null);
        gc.dispose();
        return ret;
    }

    public static GraphicsConfiguration defaultScreenDeviceConfiguration() {
        return defaultScreenDevice().getDefaultConfiguration();
    }

    public static GraphicsDevice defaultScreenDevice() {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        if (env == null)
            throw new UnsupportedOperationException("Could not get Local Graphics Environment");
        GraphicsDevice dev = env.getDefaultScreenDevice();
        if (dev == null)
            throw new UnsupportedOperationException("Could not get Default Graphics Device");
        return dev;
    }
}