?? reclabel.java
字號:
package bin;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class RecLabel extends JLabel{
/**
* Data fields
*/
private int sflag,pflag,dflag,aflag,rflag;/*flag bits*/
private Rectangle activeRec;/*the only active rectangle*/
private LinkedList recList;/*the list holds all the rectangles*/
private int oldX,oldY;/*used for move action*/
boolean canchange=true;
private final int minlen=75;
private final int thewid=50;//minlen>thewid
int cou=0;
Color []color={Color.red,Color.green,Color.blue,Color.cyan,Color.yellow,Color.pink,Color.white,Color.black};
/**
* Constructors
*/
public RecLabel(){
addMouseActions();/*registering the mouse events listener*/
recList=new LinkedList();/*new the rectangle list*/
activeRec=new Rectangle(new Point(100,100),new Dimension(1,1));
/*new the actvie rectangle*/
}
public void setChange(boolean canchange){
this.canchange=canchange;
}
public Color getColor(int i){
return color[i%8];
}
/**
* Mouse action listeners
*/
private void addMouseActions(){
addMouseListener(new MouseAdapter(){
/*mouse pressed event listener*/
public void mousePressed(MouseEvent e){
if(!canchange)
return;
int ex=e.getX(),ey=e.getY();
/*aflag==1:add a new rectangle action*/
if(aflag==1){
aflag=0;sflag=3;pflag=4;
activeRec.setLocation(ex-minlen,ey-thewid);
activeRec.setSize(minlen,thewid);
//must not intersects with any rectangle existed
if(isIntersect(activeRec)){
sflag=0;pflag=0;
return;
}
repaint();
return;
}
if(sflag==0){
//the current point is inside one rectangle existed
if(isInside(ex,ey)){
/*rflag==1:remove a rectangle*/
if(rflag==1){
rflag=0;
sflag=0;
}
repaint();
return;
}
//the selected point is a vertext of one rectangle
if(isOnPoint(ex,ey)){
repaint();
return;
}
//select a line of one rectangle
if(isOnLine(ex,ey)){
repaint();
return;
}
return;
}
}
/*mouse relessed event listener*/
public void mouseReleased(MouseEvent e){
if(!canchange)
return;
int ex=e.getX(),ey=e.getY();
if(sflag==0)return;
sflag=0;
//put the active rectangle back into the list
recList.addFirst(new Rectangle(activeRec));
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter(){
/*mouse dragged event listener*/
public void mouseDragged(MouseEvent e){
if(!canchange)
return;
int ex=e.getX(),ey=e.getY();
if(sflag==0)return;
Rectangle tempRec=new Rectangle(activeRec);
int rx=(int)activeRec.getX(),ry=(int)activeRec.getY(),
rw=(int)activeRec.getWidth(),rh=(int)activeRec.getHeight();
//move the whole rectangle
if(sflag==2){
tempRec.setLocation(rx+ex-oldX,ry+ey-oldY);
//stopped when intersects with others
if(isIntersect(tempRec)){
sflag=0;
recList.addFirst(new Rectangle(activeRec));
repaint();
return;
}
activeRec.setBounds(tempRec);
repaint();
oldX=ex;oldY=ey;
return;
}
//move one point
if(sflag==3){
switch(pflag){
case 1:
tempRec.setLocation(ex,ey);
tempRec.setSize(rw+rx-ex,rh+ry-ey);
break;
case 2:
tempRec.setLocation(rx,ey);
tempRec.setSize(ex-rx,rh+ry-ey);
break;
case 3:
tempRec.setLocation(ex,ry);
tempRec.setSize(rw+rx-ex,ey-ry);
break;
case 4:
tempRec.setLocation(rx,ry);
tempRec.setSize(ex-rx,ey-ry);
break;
default:break;
}
if(isIntersect(tempRec)){
sflag=0;
recList.addFirst(new Rectangle(activeRec));
repaint();
return;
}
activeRec.setBounds(tempRec);
repaint();
return;
}
//move a line
if(sflag==4){
switch(dflag){
case 1:
if(rw+rx-ex>minlen&&rw!=thewid){
tempRec.setLocation(ex,ry);
tempRec.setSize(rw+rx-ex,rh);
}
break;
case 2:
if(ex-rx>minlen&&rw!=thewid){
tempRec.setLocation(rx,ry);
tempRec.setSize(ex-rx,rh);
}
break;
case 3:
if(rh+ry-ey>minlen&&rh!=thewid){
tempRec.setLocation(rx,ey);
tempRec.setSize(rw,rh+ry-ey);
}
break;
case 4:
if(ey-ry>minlen&&rh!=thewid){
tempRec.setLocation(rx,ry);
tempRec.setSize(rw,ey-ry);
}
break;
default:break;
}
if(isIntersect(tempRec)){
sflag=0;
recList.addFirst(new Rectangle(activeRec));
repaint();
return;
}
activeRec.setBounds(tempRec);
repaint();
return;
}
}
});
}
/**
* Action process
*/
public void addAction(){
aflag=1;
sflag=0;
}
public void removeAction(){
rflag=1;
sflag=0;
}
public void changeAction(){
Rectangle tempRec;
Iterator IT=recList.iterator();
if(IT.hasNext()){
tempRec=(Rectangle)IT.next();
int rh=(int)tempRec.getWidth();
int rw=(int)tempRec.getHeight();
tempRec.setSize(rw,rh);
IT.remove();
recList.addFirst(new Rectangle(tempRec));
repaint();
}
return;
}
/**
* Judge the mouse point
*/
public boolean isInside(int x,int y){
Rectangle tempRec;
Rectangle tempRec2;
Iterator IT=recList.iterator();
while(IT.hasNext()){
tempRec=(Rectangle)IT.next();
tempRec2=new Rectangle((int)tempRec.getX()+2,(int)tempRec.getY()+2,
(int)tempRec.getWidth()-4,(int)tempRec.getHeight()-4);
if(tempRec2.contains(x,y)){
sflag=2;
activeRec.setBounds(tempRec);
IT.remove();
oldX=x;
oldY=y;
return true;
}
}
return false;
}
public boolean isOnPoint(int x,int y){
Rectangle tempRec;
Rectangle tempRec2;
int rx,ry,rw,rh;
Iterator IT=recList.iterator();
while(IT.hasNext()){
tempRec=(Rectangle)IT.next();
rx=(int)tempRec.getX();
ry=(int)tempRec.getY();
rw=(int)tempRec.getWidth();
rh=(int)tempRec.getHeight();
tempRec2=new Rectangle(rx-2,ry-2,4,4);
if(tempRec2.contains(x,y)){
sflag=3;
pflag=1;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
tempRec2=new Rectangle(rx+rw-2,ry-2,4,4);
if(tempRec2.contains(x,y)){
sflag=3;
pflag=2;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
tempRec2=new Rectangle(rx-2,ry-2+rh,4,4);
if(tempRec2.contains(x,y)){
sflag=3;
pflag=3;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
tempRec2=new Rectangle(rx-2+rw,ry-2+rh,4,4);
if(tempRec2.contains(x,y)){
sflag=3;
pflag=4;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
}
return false;
}
public boolean isOnLine(int x,int y){
Rectangle tempRec;
Rectangle tempRec2;
int rx,ry,rw,rh;
Iterator IT=recList.iterator();
while(IT.hasNext()){
tempRec=(Rectangle)IT.next();
rx=(int)tempRec.getX();
ry=(int)tempRec.getY();
rw=(int)tempRec.getWidth();
rh=(int)tempRec.getHeight();
tempRec2=new Rectangle(rx-2,ry-2,4,rh+4);
if(tempRec2.contains(x,y)){
sflag=4;
dflag=1;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
tempRec2=new Rectangle(rx-2+rw,ry-2,4,rh+4);
if(tempRec2.contains(x,y)){
sflag=4;
dflag=2;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
tempRec2=new Rectangle(rx-2,ry-2,rw+4,4);
if(tempRec2.contains(x,y)){
sflag=4;
dflag=3;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
tempRec2=new Rectangle(rx-2,ry-2+rh,rw+4,4);
if(tempRec2.contains(x,y)){
sflag=4;
dflag=4;
activeRec.setBounds(tempRec);
IT.remove();
return true;
}
}
return false;
}
public boolean isIntersect(Rectangle r){
Iterator IT=recList.iterator();
while(IT.hasNext()){
if(r.intersects((Rectangle)IT.next())){
return true;
}
}
return false;
}
/**
* Paint component method
*/
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Iterator IT=recList.iterator();
g2.setStroke(new BasicStroke(2));
cou=0;
g2.setPaint(Color.red);
if(sflag!=0){
g2.draw(activeRec);
}
while(IT.hasNext()){
g2.setPaint(color[cou%8]);
cou++;
g2.draw((Rectangle)IT.next());
}
}
/**
* Get one rectangle
*/
public Rectangle getRecAt(int i){
return (Rectangle)recList.get(i);
}
/**
* Get the size of rectangle list
*/
public int getListSize(){
return recList.size();
}
/**
* Get all of the rectangles
*/
public LinkedList getList(){
return recList;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -