import
java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
/**
* 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。
* 未设置背景图片的情况下,同JPanel。
*
* @author 003
*/
public class JImagePane extends JPanel
{
private static final long serialVersionUID = -8251916094895167058L;
/**
* 居中
*/
public static final String CENTRE = "Centre";
/**
* 平铺
*/
public static final String TILED = "Tiled";
/**
* 拉伸
*/
public static final String SCALED = "Scaled";
/**
* 背景图片
*/
private Image backgroundImage;
/**
* 背景图片显示模式
*/
private String imageDisplayMode;
/**
* 背景图片显示模式索引(引入此属性有助于必要时扩展)
*/
private int modeIndex;
/**
* 构造一个没有背景图片的JImagePane
*/
public JImagePane()
{
this(null, CENTRE);
}
/**
* 构造一个具有指定背景图片和指定显示模式的JImagePane
* @param image 背景图片
* @param modeName 背景图片显示模式
*/
public JImagePane(Image image, String modeName)
{
super();
setBackgroundImage(image);
setImageDisplayMode(modeName);
}
/**
* 设置背景图片
* @param image 背景图片
*/
public void setBackgroundImage(Image image)
{
this.backgroundImage = image;
this.repaint();
}
/**
* 获取背景图片
* @return 背景图片
*/
public Image getBackgroundImage()
{
return backgroundImage;
}
/**
* 设置背景图片显示模式
* @param modeName 模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE
*/
public void setImageDisplayMode(String modeName)
{
if(modeName != null)
{
modeName = modeName.trim();
//居中
if(modeName.equalsIgnoreCase(CENTRE))
{
this.imageDisplayMode = CENTRE;
modeIndex = 0;
}
//平铺
else if(modeName.equalsIgnoreCase(TILED))
{
this.imageDisplayMode = TILED;
modeIndex = 1;
}
//拉伸
else if(modeName.equalsIgnoreCase(SCALED))
{
this.imageDisplayMode = SCALED;
modeIndex = 2;
}
this.repaint();
}
}
/**
* 获取背景图片显示模式
* @return 显示模式
*/
public String getImageDisplayMode()
{
return imageDisplayMode;
}
/**
* 绘制组件
* @see javax.swing.JComponent#paintComponent(Graphics)
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//如果设置了背景图片则显示
if(backgroundImage != null)
{
int width = this.getWidth();
int height = this.getHeight();
int imageWidth = backgroundImage.getWidth(this);
int imageHeight = backgroundImage.getHeight(this);
switch(modeIndex)
{
//居中
case 0:
{
int x = (width - imageWidth) / 2;
int y = (height - imageHeight) / 2;
g.drawImage(backgroundImage, x, y, this);
break;
}
//平铺
case 1:
{
for(int ix = 0; ix < width; ix += imageWidth)
{
for(int iy = 0; iy < height; iy += imageHeight)
{
g.drawImage(backgroundImage, ix, iy, this);
}
}
break;
}
//拉伸
case 2:
{
g.drawImage(backgroundImage, 0, 0, width, height, this);
break;
}
}
}
}
}
import java.awt.Image;
import javax.swing.JPanel;
/**
* 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。
* 未设置背景图片的情况下,同JPanel。
*
* @author 003
*/
public class JImagePane extends JPanel
{
private static final long serialVersionUID = -8251916094895167058L;
/**
* 居中
*/
public static final String CENTRE = "Centre";
/**
* 平铺
*/
public static final String TILED = "Tiled";
/**
* 拉伸
*/
public static final String SCALED = "Scaled";
/**
* 背景图片
*/
private Image backgroundImage;
/**
* 背景图片显示模式
*/
private String imageDisplayMode;
/**
* 背景图片显示模式索引(引入此属性有助于必要时扩展)
*/
private int modeIndex;
/**
* 构造一个没有背景图片的JImagePane
*/
public JImagePane()
{
this(null, CENTRE);
}
/**
* 构造一个具有指定背景图片和指定显示模式的JImagePane
* @param image 背景图片
* @param modeName 背景图片显示模式
*/
public JImagePane(Image image, String modeName)
{
super();
setBackgroundImage(image);
setImageDisplayMode(modeName);
}
/**
* 设置背景图片
* @param image 背景图片
*/
public void setBackgroundImage(Image image)
{
this.backgroundImage = image;
this.repaint();
}
/**
* 获取背景图片
* @return 背景图片
*/
public Image getBackgroundImage()
{
return backgroundImage;
}
/**
* 设置背景图片显示模式
* @param modeName 模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE
*/
public void setImageDisplayMode(String modeName)
{
if(modeName != null)
{
modeName = modeName.trim();
//居中
if(modeName.equalsIgnoreCase(CENTRE))
{
this.imageDisplayMode = CENTRE;
modeIndex = 0;
}
//平铺
else if(modeName.equalsIgnoreCase(TILED))
{
this.imageDisplayMode = TILED;
modeIndex = 1;
}
//拉伸
else if(modeName.equalsIgnoreCase(SCALED))
{
this.imageDisplayMode = SCALED;
modeIndex = 2;
}
this.repaint();
}
}
/**
* 获取背景图片显示模式
* @return 显示模式
*/
public String getImageDisplayMode()
{
return imageDisplayMode;
}
/**
* 绘制组件
* @see javax.swing.JComponent#paintComponent(Graphics)
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
//如果设置了背景图片则显示
if(backgroundImage != null)
{
int width = this.getWidth();
int height = this.getHeight();
int imageWidth = backgroundImage.getWidth(this);
int imageHeight = backgroundImage.getHeight(this);
switch(modeIndex)
{
//居中
case 0:
{
int x = (width - imageWidth) / 2;
int y = (height - imageHeight) / 2;
g.drawImage(backgroundImage, x, y, this);
break;
}
//平铺
case 1:
{
for(int ix = 0; ix < width; ix += imageWidth)
{
for(int iy = 0; iy < height; iy += imageHeight)
{
g.drawImage(backgroundImage, ix, iy, this);
}
}
break;
}
//拉伸
case 2:
{
g.drawImage(backgroundImage, 0, 0, width, height, this);
break;
}
}
}
}
}
使用方法
JImagePane panel
=
new
JImagePane(
new
ImageIcon(
"
003.png
"
).getImage(), JImagePane.SCALED);
// 或者
JImagePane panel = new JImagePane();
panel.setBackgroundImage( new ImageIcon( " 003.png " ).getImage());
panel.setImageDisplayMode(JImagePane.SCALED);
// 或者
JImagePane panel = new JImagePane();
panel.setBackgroundImage( new ImageIcon( " 003.png " ).getImage());
panel.setImageDisplayMode(JImagePane.SCALED);
方法2
import
java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JPanel;
import mmlab.image.Rectangle;
/**
* Custom Jpanel
*
* @author Andy Yang
*/
public class FaceLabel extends JPanel {
private static final long serialVersionUID = 39082560987930759L;
private boolean state = false;
private Rectangle rectangle = null;
private Image image = null;
/** 记录鼠标是否进入控件 */
private boolean hover;
private String personName = null;
private double photoScaled = 1.0;
/**
* 无参构造方法
*/
public FaceLabel() {
}
/**
* 带有Color的构造方法
*
* @param color
* 背景颜色
*/
public FaceLabel(Image image) {
this.image = image;
// 定义图像尺寸
Dimension size = new Dimension(this.image.getWidth(null),
this.image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setBackground(new Color(112, 112, 112));
// 定义布局方式为空
setLayout(null);
}
/**
* 重写基类的无参构造方法
*/
@Override
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(this.getImage(), 0, 0, null);
if (isState() && (getRectangle() != null)) {
// JOptionPane.showMessageDialog(null,getPhotoScaled());
Rectangle rect = getRectangle();
int x = rect.x;
int y = rect.y;
int width = rect.width;
int height = rect.height;
if (getPhotoScaled() == 1.0) {
g2d.setColor(new Color(255, 0, 0));
g2d.drawRect(x, y, width, height);
// g2d.draw3DRect(x,y,width,height,true);
g2d.setColor(new Color(255, 255, 0));
g2d.drawString(this.getPersonName(), x, y - 5);
} else {
String sX = String.valueOf(Math.ceil(
(double) rect.x / photoScaled));
String sY = String.valueOf(Math.ceil(
(double) rect.y / photoScaled));
String sW = String.valueOf(Math.ceil(
(double) rect.width / photoScaled));
String sH = String.valueOf(Math.ceil(
(double) rect.height / photoScaled));
int indexX = sX.indexOf(".");
int indexY = sY.indexOf(".");
int indexW = sW.indexOf(".");
int indexH = sH.indexOf(".");
x = Integer.parseInt(sX.substring(0, indexX));
y = Integer.parseInt(sY.substring(0, indexY));
width = Integer.parseInt(sW.substring(0, indexW));
height = Integer.parseInt(sH.substring(0, indexH));
g2d.setColor(new Color(255, 0, 0));
g2d.drawRect(x, y, width, height);
// g2d.draw3DRect(x,y,width,height,true);
g2d.setColor(new Color(0, 0, 255));
g2d.drawString(this.getPersonName(), x, y - 5);
}
}
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public String getPersonName() {
return (personName == null) ? "null" : personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public double getPhotoScaled() {
return photoScaled;
}
public void setPhotoScaled(double photoScaled) {
this.photoScaled = photoScaled;
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JPanel;
import mmlab.image.Rectangle;
/**
* Custom Jpanel
*
* @author Andy Yang
*/
public class FaceLabel extends JPanel {
private static final long serialVersionUID = 39082560987930759L;
private boolean state = false;
private Rectangle rectangle = null;
private Image image = null;
/** 记录鼠标是否进入控件 */
private boolean hover;
private String personName = null;
private double photoScaled = 1.0;
/**
* 无参构造方法
*/
public FaceLabel() {
}
/**
* 带有Color的构造方法
*
* @param color
* 背景颜色
*/
public FaceLabel(Image image) {
this.image = image;
// 定义图像尺寸
Dimension size = new Dimension(this.image.getWidth(null),
this.image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setBackground(new Color(112, 112, 112));
// 定义布局方式为空
setLayout(null);
}
/**
* 重写基类的无参构造方法
*/
@Override
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(this.getImage(), 0, 0, null);
if (isState() && (getRectangle() != null)) {
// JOptionPane.showMessageDialog(null,getPhotoScaled());
Rectangle rect = getRectangle();
int x = rect.x;
int y = rect.y;
int width = rect.width;
int height = rect.height;
if (getPhotoScaled() == 1.0) {
g2d.setColor(new Color(255, 0, 0));
g2d.drawRect(x, y, width, height);
// g2d.draw3DRect(x,y,width,height,true);
g2d.setColor(new Color(255, 255, 0));
g2d.drawString(this.getPersonName(), x, y - 5);
} else {
String sX = String.valueOf(Math.ceil(
(double) rect.x / photoScaled));
String sY = String.valueOf(Math.ceil(
(double) rect.y / photoScaled));
String sW = String.valueOf(Math.ceil(
(double) rect.width / photoScaled));
String sH = String.valueOf(Math.ceil(
(double) rect.height / photoScaled));
int indexX = sX.indexOf(".");
int indexY = sY.indexOf(".");
int indexW = sW.indexOf(".");
int indexH = sH.indexOf(".");
x = Integer.parseInt(sX.substring(0, indexX));
y = Integer.parseInt(sY.substring(0, indexY));
width = Integer.parseInt(sW.substring(0, indexW));
height = Integer.parseInt(sH.substring(0, indexH));
g2d.setColor(new Color(255, 0, 0));
g2d.drawRect(x, y, width, height);
// g2d.draw3DRect(x,y,width,height,true);
g2d.setColor(new Color(0, 0, 255));
g2d.drawString(this.getPersonName(), x, y - 5);
}
}
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public Rectangle getRectangle() {
return rectangle;
}
public void setRectangle(Rectangle rectangle) {
this.rectangle = rectangle;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public String getPersonName() {
return (personName == null) ? "null" : personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public double getPhotoScaled() {
return photoScaled;
}
public void setPhotoScaled(double photoScaled) {
this.photoScaled = photoScaled;
}
}
关键代码:
/**
* 重写基类的无参构造方法
*/
@Override
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(this.getImage(), 0, 0, null);
}
* 重写基类的无参构造方法
*/
@Override
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(this.getImage(), 0, 0, null);
}