`

自定义Decoration

    博客分类:
  • GEF
阅读更多
大家都知道new PolygonDecoration就表示带箭头,但有的时候不想是箭头,想搞个圆圈,或者方块,那怎么做呢?那就只能自定义PolygonDecoration咯,我昨天自定义了一个圆圈,给大家看看吧
package com.netunit.workbench.parts;

import org.eclipse.draw2d.Ellipse; 
import org.eclipse.draw2d.RotatableDecoration; 
import org.eclipse.draw2d.geometry.Point; 
import org.eclipse.draw2d.geometry.Rectangle; 

public class CircleDecoration extends Ellipse implements RotatableDecoration { 
        private int myRadius = 5; 
        private Point myCenter = new Point(); 
        
        public void setRadius(int radius){ 
                erase(); 
                 myRadius = Math.abs(radius); 
                 bounds = null; 
                 repaint(); 
         } 
        
       public void setLineWidth(int width){ 
                 super.setLineWidth(width); 
         } 
        
         public Rectangle getBounds() { 
                if (bounds == null){ 
                        int diameter = myRadius * 2; 
                       bounds = new Rectangle(myCenter.x - myRadius, myCenter.y - myRadius, diameter, diameter); 
                      bounds.expand(lineWidth / 2, lineWidth / 2); 
              } 
                return bounds; 
       } 
         
         public void setLocation(Point p) { 
                 if (myCenter.equals(p)){ 
                         return; 
                 }        
                 myCenter.setLocation(p); 
                bounds = null; 
        } 
         
         public void setReferencePoint(Point p) { 
                //ignore, does not make sense to rotate circle 
         } 
 
} 

下面是效果图:
  • 大小: 22.5 KB
2
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics