this.add(south, null);
this.add(southWest, null);
this.add(west, null);
}
//单击后可以进行缩放处理
public void mouseClicked(MouseEvent e) {
isSelected = true;
this.setBorder(new LineBorder(Color.BLACK, 4));
northWest.setVisible(true);
north.setVisible(true);
northEast.setVisible(true);
east.setVisible(true);
southEast.setVisible(true);
south.setVisible(true);
southWest.setVisible(true);
west.setVisible(true);
this.repaint();
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
//防止鼠标在缩放时移出了方向点区域,图片的缩放不能进行
public void mouseReleased(MouseEvent e) {
if(isSelected){
if(getCursor().getType()!=Cursor.DEFAULT_CURSOR){
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
}
//鼠标进入哪个缩放点区域
public void mouseEntered(MouseEvent e) {
if (isSelected) {
if (e.getComponent() == northWest) {
setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
} else if (e.getComponent() == north) {
setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
} else if (e.getComponent() == northEast) {
setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
} else if (e.getComponent() == east) {
setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
} else if (e.getComponent() == southEast) {
setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
} else if (e.getComponent() == south) {
setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
} else if (e.getComponent() == southWest) {
setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
} else if (e.getComponent() == west) {
setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
} /*else if(e.getComponent() == this){
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}*/
}
}
public void mouseExited(MouseEvent e) {
}
//缩放及拖动的操作
public void mouseDragged(MouseEvent e) {
if (isSelected) {
int type = getCursor().getType();
int x, y, w, h;
x = this.getX();
y = this.getY();
w = this.getWidth();
h = this.getHeight();
switch (type) {
case Cursor.NW_RESIZE_CURSOR://西北方向缩放
if(w - e.getX() >20&& h - e.getY()>20){
w = w - e.getX();
h = h - e.getY();
this.setBounds(x e.getX(), y e.getY(), w, h);
}
break;
case Cursor.N_RESIZE_CURSOR://北
if(h-e.getY()>20){
h = h - e.getY();
this.setBounds(x, y e.getY(),w, h);
}
break;
case Cursor.NE_RESIZE_CURSOR://东北
if(w e.getX()>20 && h - e.getY()>20){
w = w e.getX();
h = h - e.getY();
this.setBounds(x, y e.getY(),w, h);
}
break;
case Cursor.E_RESIZE_CURSOR://东
if(w e.getX()>20){
w = w e.getX();
this.setBounds(x, y, w, h);
}
break;
case Cursor.SE_RESIZE_CURSOR://东南
if(w e.getX()>20&& h e.getY()>20){
w = w e.getX();
h = h e.getY();
this.setBounds(x, y, w, h);
}
break;
case Cursor.S_RESIZE_CURSOR://南
if(h e.getY()>20){
h = h e.getY();
this.setBounds(x, y, w, h);
}
break;
case Cursor.SW_RESIZE_CURSOR://西南
if(w-e.getX()>20&& h e.getY()>20){
w = w - e.getX();
h = h e.getY();
this.setBounds(x e.getX(), y,w, h);
}
break;
case Cursor.W_RESIZE_CURSOR://西
if(w-e.getX()>20){
w = w - e.getX();
this.setBounds(x e.getX(), y,w, h);
}
break;
case Cursor.DEFAULT_CURSOR://拖动操作
if(e.getComponent()==this){
this.setBounds(x e.getX(),y e.getY(),w,h);
}
default:
break;
}
//重定位各缩放点坐标位置
northWest.setBounds(0, 0, 5, 5);
north.setBounds((w - 3) / 2, 0, 5, 5);
northEast.setBounds(w - 5, 0, 5, 5);
east.setBounds(w - 5, (h - 3) / 2,5, 5);
southEast.setBounds(w - 5, h - 5, 5, 5);
south.setBounds((w - 3) / 2, h - 5,5, 5);
southWest.setBounds(0, h - 5, 5, 5);
west.setBounds(0, (h - 3) / 2, 5, 5);
//重绘
this.repaint();
}
}
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
public void paint(Graphics g) {
super.paint(g);
//绘制图片在中央
g.drawImage(this.imageIcon.getImage(), 5, 5, this.getWidth() - 10,
this.getHeight() - 10, this);
}
}
}
上一篇: Bugzilla sendmail配置笔记
下一篇: JSP中SQL数据库编程技术
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




