001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Feb 14, 2003 005 * Time: 9:35:11 PM 006 */ 007 008 package EVolve.util.painters.shapes; 009 010 import EVolve.visualization.AxesPanel; 011 import java.awt.Graphics2D; 012 import java.awt.*; 013 014 public class Box extends Shape{ 015 private int width, height; 016 private boolean wrapped; 017 public static final int maxLength = 600; 018 private AxesPanel panel; 019 020 public Box(long entity_type,long entity_id, int width, int height) { 021 super(entity_type,entity_id); 022 this.entity_type = entity_type; 023 this.width = width; 024 this.height = height; 025 this.wrapped = false; 026 } 027 028 public Box(long entity_type,long entity_id, int width, int height, boolean wrapped) { 029 this(entity_type,entity_id,width,height); 030 this.wrapped = wrapped; 031 } 032 033 public Box(long entity_type,long entity_id, int width, int height, boolean wrapped, AxesPanel panel) { 034 this(entity_type, entity_id, width, height, wrapped); 035 this.panel = panel; 036 } 037 038 public String getName() { 039 return "Box"; 040 } 041 042 public void draw(Graphics2D g) { 043 boolean broken = false; 044 045 if ((panel != null)&&((width > maxLength) || (height > maxLength))) { 046 broken = true; 047 } 048 049 drawBox(g,x,y,width,height,broken); 050 051 if (consumerMap == null) return; 052 053 drawArrows(g); 054 } 055 056 public void fill(Graphics2D g) { 057 if (color != null) 058 g.fillRect(x,y,width,height); 059 wrap(x,y,width,height,g); 060 } 061 062 public boolean insideShape(int x, int y) { 063 if ((x<=this.x+width) && (x>=this.x) && 064 (y<=this.y+height) && (y>=this.y)) 065 return true; 066 return false; 067 } 068 069 public int getWidth() { 070 return (panel!=null)&&(width>maxLength) ? maxLength : width; 071 } 072 073 public int getRealWidth() { 074 return width; 075 } 076 077 public int getHeight() { 078 return (panel!=null)&&(height>maxLength) ? maxLength : height; 079 } 080 081 public int getRealHeight() { 082 return height; 083 } 084 085 public void setSize(int width, int height) { 086 this.width = width <=0 ? 1 : width; 087 this.height = height; 088 } 089 090 private void wrap(int x, int y, int width, int height, Graphics2D g) { 091 if (wrapped) { 092 g.setColor(Color.black); 093 g.drawRect(x+1,y+1,width-2,height-2); 094 } 095 } 096 097 private void drawBox(Graphics2D g, int x, int y, int width, int height, boolean broken) { 098 int[][][] points = new int[2][2][6]; 099 100 if (color != null) 101 g.setColor(color); 102 else 103 g.setColor(defaultColor); 104 105 if (broken) { 106 if (width > maxLength) { 107 points[0][0][0] = x; points[0][0][1] = x+maxLength/2-5; points[0][0][2] = x+maxLength/2-10; 108 points[0][0][3] = x+maxLength/2-5; points[0][0][4] = x; points[0][0][5] = x; 109 110 points[0][1][0] = y; points[0][1][1] = y; points[0][1][2] = y+height/2; 111 points[0][1][3] = y+height; points[0][1][4] = y+height; points[0][1][5] = y; 112 113 points[1][0][0] = x+maxLength/2; points[1][0][1] = x+maxLength; points[1][0][2] = x+maxLength; 114 points[1][0][3] = x+maxLength/2; points[1][0][4] = x+maxLength/2-5; points[1][0][5] = x+maxLength/2; 115 116 points[1][1][0] = y; points[1][1][1] = y; points[1][1][2] = y+height; 117 points[1][1][3] = y+height; points[1][1][4] = y+height/2; points[1][1][5] = y; 118 } else { 119 points[0][0][0] = x; points[0][0][1] = x+width; points[0][0][2] = x+width; 120 points[0][0][3] = x+width/2; points[0][0][4] = x; points[0][0][5] = x; 121 122 points[0][1][0] = y; points[0][1][1] = y; points[0][1][2] = y+maxLength/2-5; 123 points[0][1][3] = y+maxLength/2-10; points[0][1][4] = y+maxLength/2-5; points[0][1][5] = y; 124 125 points[1][0][0] = x; points[1][0][1] = x+width/2; points[1][0][2] = x+width; 126 points[1][0][3] = x+width; points[1][0][4] = x; points[1][0][5] = x; 127 128 points[1][1][0] = y+maxLength/2; points[1][1][1] = y+maxLength/2-5; points[1][1][2] = y+maxLength/2; 129 points[1][1][3] = y+maxLength; points[1][1][4] = y+maxLength; points[1][1][5] = y; 130 } 131 g.drawPolygon(points[0][0],points[0][1],points[0][0].length); 132 g.drawPolygon(points[1][0],points[1][1],points[1][0].length); 133 } 134 135 if (color != null) { 136 if (!broken) { 137 g.fillRect(x,y,width,height); 138 wrap(x, y, width, height, g); 139 } else { 140 g.fillPolygon(points[0][0],points[0][1],points[0][0].length); 141 g.fillPolygon(points[1][0],points[1][1],points[1][0].length); 142 143 wrap(x, y, maxLength, height, g); 144 } 145 } else { 146 g.drawRect(x,y,width,height); 147 } 148 149 wrap(x, y, width, height, g); 150 } 151 152 public void move(int x, int y) { 153 super.move(x,y); 154 155 if (panel == null) return; 156 157 if (width > maxLength) { 158 panel.addString(x+maxLength/2-10,y+height/2-2,String.valueOf(width)); 159 } else if (height > maxLength){ 160 panel.addString(x+maxLength/2-10,y+height/2,String.valueOf(width)); 161 } 162 } 163 }