⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagecanvas.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sshtools.ui.awt;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

/**
 * Simple component to draw an image. The image can be either centered in the
 * container or
 * 
 * @author $author$
 * @version $revision$
 */

public class ImageCanvas extends Canvas {

    /**
     * Centered in container
     */
    public final static int CENTERED = 0;

    /**
     * Resize to take up all space allocated to component
     */
    public final static int STRETCH = 1;

    // Private instance variables
    private MediaTracker tracker;
    private Image image;
    private int scale;
    private int border;
    private Color borderColor;
    private float valign;

    /**
     * <p>
     * Construct a new image canvas
     * </p>
     * 
     */
    public ImageCanvas() {
        super();
    }

    /**
     * <p>
     * Construct a new image canvas
     * </p>
     * 
     * @param iamge image
     * 
     */
    public ImageCanvas(Image image) {
        super();
        setImage(image);
    }

    /**
     * <p>
     * Construct a new image canvas given a <code>Class</code> from which the
     * <code>Classloader</code> can be determined, and the resource name. The
     * image will be centered in the container.
     * </p>
     * 
     * @param cls name of image
     * @param scale scale
     * 
     */
    public ImageCanvas(Class cls, String image) {
        super();
        setImage(UIUtil.loadImage(cls, image));
    }

    /**
     * Set the vertical alignment
     * 
     * @param f vertical alignment
     */
    public void setValign(float f) {
        this.valign = valign;
    }

    /**
     * Set the border width around the image
     * 
     * @param border border
     */
    public void setBorder(int border) {
        this.border = border;
    }

    /**
     * Get the border width around the image
     * 
     * @return border
     */
    public int getBorder() {
        return border;
    }

    /**
     * Get the border color
     * 
     * @return border color
     */
    public Color getBorderColor() {
        return borderColor;
    }

    /**
     * Set the border color
     * 
     * @param borderColor border color
     */
    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    /**
     * <p>
     * Set the scale. Can be one of
     * </p>
     * 
     * <ul>
     * <li><code>ImageCanvas.STRETCH</code></li>
     * <li><code>ImageCanvas.CENTERED</code></li>
     * </ul>
     * 
     * @param scale scale
     */
    public void setScale(int scale) {
        this.scale = scale;
        repaint();
    }

    /**
     * <p>
     * Get the scale. Can be one of
     * </p>
     * 
     * <ul>
     * <li><code>ImageCanvas.STRETCH</code></li>
     * <li><code>ImageCanvas.CENTERED</code></li>
     * </ul>
     * 
     * @return scale
     */
    public int getScale() {
        return scale;
    }

    /**
     * Set the image to display
     * 
     * @param image
     */
    public void setImage(Image image) {
        // Prompt a layout if the image size changes
        if ((image == null && this.image != null)
                        || (image != null && this.image == null)
                        || (image != null && this.image != null && (image.getWidth(this) != this.image.getWidth(this) || image
                                        .getHeight(this) != this.image.getHeight(this)))) {
            this.image = image;
            doLayout();
        } else {
            this.image = image;
        }
        repaint();
    }

    /**
     * Set the image to display
     * 
     * @return image
     */
    public Image getImage() {
        return image;
    }

    /*
     * Prevent flicker
     * 
     * @see java.awt.Component#update(java.awt.Graphics)
     */
    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        Dimension d = getSize();
        Dimension f = new Dimension(d.width - (border * 2), d.height - (border * 2));
        if (image != null) {
            if (scale == STRETCH) {
                g.drawImage(image, border, border, f.width, f.height, this);
            } else {
                int x = Math.max((f.width - image.getWidth(this)) / 2, 0);                
                int y = Math.max((f.height - image.getHeight(this)) / 2, 0);
                if(valign == Canvas.TOP_ALIGNMENT) {
                    y = 0;
                }
                else if(valign == Canvas.BOTTOM_ALIGNMENT) {
                    y = f.height - image.getHeight(this);
                }
                g.drawImage(image, x + border, y + border, this);
            }
        }
    }

    public Dimension getPreferredSize() {
        if (image == null) {
            return new Dimension(border * 2, border * 2);
        } else {
            return new Dimension(image.getWidth(this) + (border * 2), image.getHeight(this) + (border * 2));
        }
    }

    public Dimension getMinimumSize() {
        return getPreferredSize();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -