Download: jar file
ArrayList
growbox.java
Rect.java
growbox.java
001: import java.awt.*; 002: import java.awt.event.*; 003: import java.util.*; 004: import java.awt.geom.*; 005: import javax.swing.*; 006: 007: 008: class MousePanel extends JPanel implements MouseMotionListener 009: { 010: ArrayList<Rect> boxes; 011: Point base, current; 012: Rect r; 013: boolean isDragging; 014: 015: MousePanel() 016: { 017: isDragging = false; 018: boxes = new ArrayList<Rect>(); 019: addMouseListener(new MouseHandler()); 020: addMouseMotionListener(this); 021: } 022: 023: public void paintComponent(Graphics g) 024: { 025: super.paintComponent(g); 026: if (boxes.size()==0) return; 027: 028: for (Rect r: boxes) r.draw(g); 029: if (boxes.size()>1) showIntersect(g); 030: } 031: 032: public void showIntersect(Graphics g) 033: { 034: Rect a, b, c; 035: a = boxes.get(0); 036: b = boxes.get(1); 037: 038: c = Rect.bounding(a,b); 039: c.fillcolor = Color.cyan; 040: //c.draw(g); 041: c = Rect.intersection(a,b); 042: if (c!=null) { 043: c.fillcolor = Color.red; 044: if (a.contains(b) || b.contains(a)) c.fillcolor = Color.blue; 045: c.draw(g); 046: } 047: } 048: 049: 050: private class MouseHandler extends MouseAdapter 051: { 052: 053: public void mousePressed(MouseEvent event) 054: { 055: current = event.getPoint(); 056: base = event.getPoint(); 057: } 058: 059: public void mouseReleased(MouseEvent event) 060: { 061: isDragging = false; 062: current = event.getPoint(); 063: 064: Graphics g = getGraphics(); 065: g.setXORMode(Color.WHITE); 066: r.fastdraw(g); 067: g.setPaintMode(); 068: g.setColor(Color.BLACK); 069: Rect rg = new Rect(base.x,base.y,current.x,current.y); 070: if (boxes.size()>1) boxes.remove(1); 071: if (rg.area()>1) boxes.add(rg); 072: repaint(); 073: } 074: } 075: 076: public void mouseDragged(MouseEvent event) 077: { 078: Graphics g = getGraphics(); 079: int bx = base.x; 080: int by = base.y; 081: g.setXORMode(Color.WHITE); 082: if (isDragging) { 083: //r = new Rect(bx,by,current.x,current.y); 084: r.fastdraw(g); 085: } 086: isDragging = true; 087: current = event.getPoint(); 088: r = new Rect(bx,by,current.x,current.y); 089: r.fastdraw(g); 090: g.setPaintMode(); 091: g.setColor(Color.BLACK); 092: } 093: 094: public void mouseMoved(MouseEvent event) 095: { 096: } 097: } 098: 099: public class growbox extends JApplet 100: { 101: public void init() 102: { 103: EventQueue.invokeLater(new Runnable() { 104: public void run() 105: { 106: MousePanel panel = new MousePanel(); 107: add(panel); 108: } 109: }); 110: } 111: public static void main(String[] args) 112: { 113: EventQueue.invokeLater(new Runnable() { 114: public void run() 115: { 116: MousePanel panel = new MousePanel(); 117: JFrame frame = new JFrame("Grow Box"); 118: frame.add(panel); 119: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 120: frame.setSize(400,400); 121: frame.setVisible(true); 122: } 123: }); 124: } 125: }
Rect.java
001: //Rect.java 002: //Definition of class Rect 003: import java.awt.Graphics; 004: import java.awt.Color; 005: 006: /** 007: * <b>Rect</b> describes a two-dimensional rectangle. 008: * coordinates increase down and to the right 009: * positions are integers 010: */ 011: 012: public class Rect 013: { 014: int xmin,ymin,xmax,ymax; 015: Color fillcolor = Color.white; 016: 017: // No-argument constructor 018: public Rect() { set( 0, 0, 1, 1 ); } 019: 020: 021: // Constructor 022: public Rect( int x1, int y1 , int x2, int y2 ) 023: { 024: set(x1,y1,x2,y2); 025: } 026: 027: // copy constructor 028: public Rect( Rect src) 029: { 030: set(src.xmin,src.ymin,src.xmax,src.ymax); 031: } 032: 033: public int[] size() { 034: int[] mySize = new int[2]; 035: mySize[0]=xmax-xmin+1; 036: mySize[1]=ymax-ymin+1; 037: return mySize; 038: } 039: 040: 041: 042: // Set x and y coordinates of Point 043: public Rect set( int x1, int y1, int x2, int y2 ) 044: { 045: if (x1>x2) { 046: xmin = x2; 047: xmax = x1; 048: } 049: else { 050: xmin = x1; 051: xmax = x2; 052: } 053: if (y1>y2) { 054: ymin = y2; 055: ymax = y1; 056: } 057: else { 058: ymin = y1; 059: ymax = y2; 060: } 061: return this; 062: } 063: 064: public boolean contains(int a, int b){ 065: if (a>xmax) return false; 066: if (a<xmin) return false; 067: if (b<ymin) return false; 068: if (b>ymax) return false; 069: return true; 070: } 071: 072: public boolean contains(Rect r) 073: { 074: if (r.xmin<xmin) return false; 075: if (r.xmax>xmax) return false; 076: if (r.ymin<ymin) return false; 077: if (r.ymax>ymax) return false; 078: return true; 079: } 080: 081: 082: static public Rect bounding(Rect a, Rect b) 083: { 084: int x1 = Math.min(a.xmin,b.xmin); 085: int y1 = Math.min(a.ymin,b.ymin); 086: int x2 = Math.max(a.xmax,b.xmax); 087: int y2 = Math.max(a.ymax,b.ymax); 088: return new Rect(x1,y1,x2,y2); 089: } 090: 091: static public Rect intersection(Rect a, Rect b) 092: { 093: if (a.xmin > b.xmax) return null; 094: if (a.xmax < b.xmin) return null; 095: if (a.ymin > b.ymax) return null; 096: if (a.ymax < b.ymin) return null; 097: int x1 = Math.max(a.xmin,b.xmin); 098: int y1 = Math.max(a.ymin,b.ymin); 099: int x2 = Math.min(a.xmax,b.xmax); 100: int y2 = Math.min(a.ymax,b.ymax); 101: return new Rect(x1,y1,x2,y2); 102: } 103: 104: public int area() 105: { 106: return (xmax-xmin+1)*(ymax-ymin+1); 107: } 108: 109: 110: // multiply point by a scalar 111: public Rect scale(double s) 112: { 113: xmax = xmin + (int) Math.round((xmax-xmin)*s); 114: ymax = ymin + (int) Math.round((ymax-ymin)*s); 115: return this; 116: } 117: 118: 119: // offset (translate) point by the amount (tx, ty) 120: public Rect translate(int tx, int ty) 121: { 122: xmin += tx; 123: ymin += ty; 124: xmax += tx; 125: ymax += ty; 126: return this; 127: } 128: 129: 130: 131: public void setSize(int width, int height) 132: { 133: if (width<1) width = 1; 134: if (height<1) height = 1; 135: xmax = xmin + width - 1; 136: ymax = ymin + height - 1; 137: } 138: 139: public void fastdraw(Graphics g) 140: { 141: g.drawRect(xmin,ymin,xmax-xmin+1,ymax-ymin+1); 142: } 143: 144: public void draw(Graphics g) 145: { 146: Color oldcolor = g.getColor(); 147: if (fillcolor!=null){ 148: g.setColor(fillcolor); 149: g.fillRect(xmin,ymin,xmax-xmin+1,ymax-ymin+1); 150: } 151: g.setColor(Color.black); 152: g.drawRect(xmin,ymin,xmax-xmin+1,ymax-ymin+1); 153: g.setColor(oldcolor); 154: } 155: 156: // convert the point into a String representation 157: public String toString() 158: { return String.format("[%d, %d; %d, %d]",xmin,ymin,xmax,ymax); } 159: 160: static public void main(String args[]) 161: { 162: Rect a, b; 163: a = new Rect(20,50,220,250); 164: System.out.println("a = " + a); 165: b = new Rect(1,0,10,20); 166: System.out.println("b = " + b + String.format(" area %d",b.area())); 167: 168: b.translate(2,10); 169: System.out.println("New b location = " + b ); 170: b.scale(3.0); 171: System.out.println("New b size (3x) = " + b ); 172: b.scale(1.5); 173: System.out.println("New b size (1.5x) = " + b ); 174: b.scale((1/1.5)); 175: System.out.println("New b size (x/1.5) = " + b ); 176: b.scale((1/3.0)); 177: System.out.println("New (Original?) b size (x/3) = " + b ); 178: b.setSize(10, 30); 179: System.out.println("New b size set to (10,30) = " + b ); 180: System.out.println("b contains (4,13) = " + b.contains(4,13)); 181: System.out.println("b contains (4,43) = " + b.contains(4,43)); 182: System.out.println("Intersection of a = " + a + "\n\tand b = " + b); 183: System.out.println("\t= " + intersection(a, b)); 184: System.out.println("Bounding Rect of a = " + a + "\n\tand b = " + b); 185: System.out.println("\t= " + bounding(a, b)); 186: 187: b.scale(3.0); 188: System.out.println("New b size (3x) = " + b ); 189: 190: System.out.println("Intersection of a = " + a + "\n\tand b = " + b); 191: System.out.println("\t= " + intersection(a, b)); 192: System.out.println("Bounding Rect of a = " + a + "\n\tand b = " + b); 193: System.out.println("\t= " + bounding(a, b)); 194: 195: 196: } 197: 198: }
Maintained by John Loomis, updated Thu Mar 27 23:06:35 2008