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;
}
}
//这是我项目中用到的一个控件.你自己试着去改一下吧.
//关键代码是:
/**
* 重写基类的无参构造方法
*/
@Override
protected void paintComponent(Graphics g) {
setOpaque(true);
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(this.getImage(), 0, 0, null);
}