Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.awt.AlphaComposite;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class Main {
    /**
     * Loads an image using {@link javax.imageio.ImageIO#read(java.io.InputStream)}
     *
     * @param s - The resource name, ex: /assets/darkmetro/checkbox.png -> darkmetro/checkbox
     * @return
     */
    public static Image load(String s) {
        try {
            return ImageIO.read(System.class.getResourceAsStream("/assets/" + s + ".png"));
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * Loads and resizes an image to the given scale
     *
     * @param s - The resource name, ex: /assets/darkmetro/checkbox.png -> darkmetro/checkbox
     * @param scale - The scale, ex: 16 -> 16x16
     * @return
     */
    public static Image load(String s, int scale) {
        return resize(load(s), scale);
    }

    public static Image resize(Image i, int scale) {
        BufferedImage resizedImage = new BufferedImage(scale, scale, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(i, 0, 0, scale, scale, null);
        g.dispose();
        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        return resizedImage;
    }
}