Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.AlphaComposite;
import java.awt.Color;

import java.awt.Graphics2D;
import java.awt.Rectangle;

import java.awt.image.BufferedImage;

public class Main {
    /**
     * Creates a buffered image for the given parameters. If there is not enough
     * memory to create the image then a OutOfMemoryError is thrown.
     */
    public static BufferedImage createBufferedImage(int w, int h, Color background) {
        BufferedImage result = null;

        if (w > 0 && h > 0) {
            int type = (background != null) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
            result = new BufferedImage(w, h, type);

            // Clears background
            if (background != null) {
                Graphics2D g2 = result.createGraphics();
                clearRect(g2, new Rectangle(w, h), background);
                g2.dispose();
            }
        }

        return result;
    }

    /**
     * Clears the given area of the specified graphics object with the given
     * color or makes the region transparent.
     */
    public static void clearRect(Graphics2D g, Rectangle rect, Color background) {
        if (background != null) {
            g.setColor(background);
            g.fillRect(rect.x, rect.y, rect.width, rect.height);
        } else {
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
            g.fillRect(rect.x, rect.y, rect.width, rect.height);
            g.setComposite(AlphaComposite.SrcOver);
        }
    }
}