Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Cursor;
import java.awt.Dimension;

import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;

import java.awt.Toolkit;
import java.awt.Transparency;

public class Main {
    /**
     * Create a custom cursor out of the specified image, putting the hotspot in the exact center
     * of the created cursor.
     */
    public static Cursor createImageCursor(Image img) {
        return createImageCursor(img, null);
    }

    /**
     * Create a custom cursor out of the specified image, with the specified hotspot.
     */
    public static Cursor createImageCursor(Image img, Point hotspot) {
        Toolkit tk = Toolkit.getDefaultToolkit();

        // for now, just report the cursor restrictions, then blindly create
        int w = img.getWidth(null);
        int h = img.getHeight(null);
        Dimension d = tk.getBestCursorSize(w, h);
        //         int colors = tk.getMaximumCursorColors();
        //         Log.debug("Creating custom cursor [desiredSize=" + w + "x" + h +
        //                   ", bestSize=" + d.width + "x" + d.height +
        //                   ", maxcolors=" + colors + "].");

        // if the passed-in image is smaller, pad it with transparent pixels and use it anyway.
        if (((w < d.width) && (h <= d.height)) || ((w <= d.width) && (h < d.height))) {
            Image padder = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .getDefaultConfiguration().createCompatibleImage(d.width, d.height, Transparency.BITMASK);
            Graphics g = padder.getGraphics();
            g.drawImage(img, 0, 0, null);
            g.dispose();

            // and reassign the image to the padded image
            img = padder;

            // and adjust the 'best' to cheat the hotspot checking code
            d.width = w;
            d.height = h;
        }

        // make sure the hotspot is valid
        if (hotspot == null) {
            hotspot = new Point(d.width / 2, d.height / 2);
        } else {
            hotspot.x = Math.min(d.width - 1, Math.max(0, hotspot.x));
            hotspot.y = Math.min(d.height - 1, Math.max(0, hotspot.y));
        }

        // and create the cursor
        return tk.createCustomCursor(img, hotspot, "samskivertDnDCursor");
    }
}