1 31 32 package org.antlr.xjlib.appkit.gview.object; 33 34 import org.antlr.xjlib.appkit.gview.base.Anchor2D; 35 import org.antlr.xjlib.appkit.gview.base.Rect; 36 import org.antlr.xjlib.appkit.gview.base.Vector2D; 37 import org.antlr.xjlib.appkit.gview.shape.SLabel; 38 import org.antlr.xjlib.foundation.XJXMLSerializable; 39 40 import java.awt.*; 41 42 public class GElementRect extends GElement implements XJXMLSerializable { 43 44 public static final int DEFAULT_WIDTH = 40; 45 public static final int DEFAULT_HEIGHT = 40; 46 47 protected double width = DEFAULT_WIDTH; 48 protected double height = DEFAULT_HEIGHT; 49 50 public GElementRect() { 51 } 52 53 public void setPositionOfUpperLeftCorner(double x, double y) { 54 setPosition(x+width*0.5, y+height*0.5); 55 } 56 57 public void setSize(double width, double height) { 58 this.width = width; 59 this.height = height; 60 elementDimensionDidChange(); 61 } 62 63 public void setWidth(double width) { 64 this.width = width; 65 elementDimensionDidChange(); 66 } 67 68 public double getWidth() { 69 return width; 70 } 71 72 public void setHeight(double height) { 73 this.height = height; 74 elementDimensionDidChange(); 75 } 76 77 public double getHeight() { 78 return height; 79 } 80 81 @Override 82 public void updateAnchors() { 83 setAnchor(ANCHOR_CENTER, position.copy(), Anchor2D.DIRECTION_FREE); 84 setAnchor(ANCHOR_TOP, position.add(new Vector2D(0, -height*0.5)), Anchor2D.DIRECTION_TOP); 85 setAnchor(ANCHOR_BOTTOM, position.add(new Vector2D(0, height*0.5)), Anchor2D.DIRECTION_BOTTOM); 86 setAnchor(ANCHOR_LEFT, position.add(new Vector2D(-width*0.5, 0)), Anchor2D.DIRECTION_LEFT); 87 setAnchor(ANCHOR_RIGHT, position.add(new Vector2D(width*0.5, 0)), Anchor2D.DIRECTION_RIGHT); 88 } 89 90 @Override 91 public Rect getFrame() { 92 double x = getPositionX()-getWidth()*0.5; 93 double y = getPositionY()-getHeight()*0.5; 94 double dx = getWidth(); 95 double dy = getHeight(); 96 return new Rect(x, y, dx, dy); 97 } 98 99 @Override 100 public boolean isInside(Point p) { 101 return getFrame().contains(p); 102 } 103 104 @Override 105 public void draw(Graphics2D g) { 106 if(isVisibleInClip(g)) { 107 if(labelVisible) { 108 g.setColor(labelColor); 109 if(label != null && label.length()>0) 110 drawLabel(g); 111 } 112 113 g.setColor(color); 114 g.setStroke(strokeSize); 115 drawShape(g); 116 g.setStroke(strokeNormal); 117 } 118 } 119 120 @Override 121 public void drawShape(Graphics2D g) { 122 super.drawShape(g); 123 124 Rectangle r = getFrame().rectangle(); 125 g.drawRect(r.x, r.y, r.width, r.height); 126 } 127 128 public void drawLabel(Graphics2D g) { 129 SLabel.drawCenteredString(label, (int)getPositionX(), (int)getPositionY(), g); 130 } 131 132 } 133 | Popular Tags |