📄 imagecanvas.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.sslexplorer.vpn.client;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.net.URL;
import java.awt.Toolkit;
/**
* 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;
/**
* <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(loadImage(cls, image));
}
public void setImage(URL url) {
setImage(Toolkit.getDefaultToolkit().getImage(url));
}
public static Image loadImage(Class clazz, String resource) {
String path = resource;
URL url = clazz.getResource(path);
Image image = null;
if (url != null) {
return Toolkit.getDefaultToolkit().getImage(url);
}
else {
return Toolkit.getDefaultToolkit().getImage(path);
}
}
/**
* 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);
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 + -