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

org.mazarineblue.runner.swing.ImagePanel Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2012-2014 Alex de Kruijff
 * Copyright (c) 2014-2015 Specialisterren
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.mazarineblue.runner.swing;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author Alex de Kruijff {@literal }
 */
public class ImagePanel
        extends JPanel {

    private BufferedImage image;

    public ImagePanel(String path) {
        this(new File(path));
    }

    public ImagePanel(File file) {
        try {
            image = ImageIO.read(file);
        } catch (IOException ex) {
            defaultImage(ex);
        }
    }

    public ImagePanel(URL url) {
        try {
            image = ImageIO.read(url);
        } catch (IOException ex) {
            defaultImage(ex);
        }
    }

    private void defaultImage(IOException ex) {
        String txt = "Image IO error";
        image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.LIGHT_GRAY);
        graphics.fillRect(0, 0, 200, 50);
        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("Arial Black", Font.BOLD, 20));
        graphics.drawString(txt, 10, 25);
        Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy