All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bitstrings.maven.plugins.splasher.GraphicsContext Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
/**
 *  Copyright (c) 2013 bitstrings.org - Pino Silvaggio
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.bitstrings.maven.plugins.splasher;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

public class GraphicsContext
{
    protected final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();

    protected final Map fontAliasMap = new HashMap();

    protected final Rectangle canvasBounds;

    public GraphicsContext( int canvasWidth, int canvasHeight )
    {
        this.canvasBounds = new Rectangle( canvasWidth, canvasHeight );
    }

    public Rectangle getCanvasBounds()
    {
        return canvasBounds;
    }

    public GraphicsEnvironment getGraphicsEnvironment()
    {
        return graphicsEnvironment;
    }

    public void loadFont( String alias, File fontFile )
        throws IOException, FontFormatException
    {
        Font font = Font.createFont( Font.TRUETYPE_FONT, fontFile );

        graphicsEnvironment.registerFont( font );

        if ( alias != null )
        {
            fontAliasMap.put( alias, font.getName() );
        }
    }

    public Font getFont( String name, int style, int size )
    {
        final String fontName = fontAliasMap.get( name );

        return new Font( fontName == null ? name : fontName, style, size );
    }

    public BufferedImage loadImage( File file )
        throws IOException
    {
        return ImageIO.read( file );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy