📄 imagepanel.java
字号:
package edu.ou.kmi.buddyspace.utils;
/*
* ImagePanel.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2002
*
*
* Created on 16 August 2002, 17:54
*/
//import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
/**
* <code>Image Panel</code> is a panel displaying given image in given scale.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class ImagePanel extends JPanel {
protected Image image;
protected int width;
protected int height;
protected float scale;
//Container mainPanel;
protected JComponent mainPanel;
/** Constructor */
public ImagePanel(Image image, float scale, JComponent /*Container*/ mainPanel, int imgWidth, int imgHeight) {
this.image = image;
this.scale = scale;
this.mainPanel = mainPanel;
setScale(scale, imgWidth, imgHeight);
}
/** Sets scale */
public void setScale(float scale, int imgWidth, int imgHeight) {
//width = Math.round(image.getWidth(this) * scale);
//height = Math.round(image.getHeight(this) * scale);
width = Math.round(imgWidth * scale);
height = Math.round(imgHeight * scale);
setSize(width, height);
}
/** Paints the component */
public void paintComponent(Graphics g) {
super.paintComponent(g); //paint background
g.drawImage(image, 0, 0, width, height, this);
}
/** Called when included image updated */
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
if (image == img) {
if ((infoflags & ImageObserver.WIDTH) != 0) {
this.width = Math.round(width * scale);
//this.width = Math.round(image.getWidth(this) * scale);
//setSize(width, height);
//maybeResizeParent();
refresh();
}
if ((infoflags & ImageObserver.HEIGHT) != 0) {
this.height = Math.round(height * scale);
//this.height = Math.round(image.getHeight(this) * scale);
//setSize(this.width, this.height);
//maybeResizeParent();
refresh();
}
}
return super.imageUpdate(img, infoflags, x, y, width, height);
}
/** When refreshes, refreshes also parent */
protected void refresh() {
revalidate();
repaint();
mainPanel.revalidate();
mainPanel.repaint();
}
/** Changes size of parent if cannot fit in */
public void maybeResizeParent() {
int x = getX() + width;
int y = getY() + height;
Dimension d = mainPanel.getPreferredSize();
int px = (int) d.getWidth();
int py = (int) d.getHeight();
d = mainPanel.getMinimumSize();
int mx = (int) d.getWidth();
int my = (int) d.getHeight();
if (px < x || py < y) {
px = (x > px)? x : px;
py = (y > py)? y : py;
mainPanel.setPreferredSize(new Dimension(px, py));
}
if (mx < x || my < y) {
mx = (x > mx)? x : mx;
my = (y > my)? y : my;
mainPanel.setMinimumSize(new Dimension(mx, my));
}
refresh();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -