Posted By

roock on 11/18/08


Tagged

image editor graphics draw


Versions (?)

Rubber Band on AWT/Swing Component


 / Published in: Java
 

  1. import java.awt.Component;
  2. import java.awt.Rectangle;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseMotionAdapter;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9.  
  10. public class RubberBand {
  11.  
  12. private Rectangle rectangle;
  13. private final List rubberBandListeners = new ArrayList();
  14. private final Component component;
  15. private int maxWidth;
  16. private int maxHeight;
  17.  
  18. public RubberBand(Component component) {
  19. this.component = component;
  20. addListeners();
  21. }
  22.  
  23. private void addListeners() {
  24. component.addMouseMotionListener(new MouseMotionAdapter() {
  25. public void mouseDragged(MouseEvent event) {
  26. reactOnMouseDragged(event);
  27. }
  28. });
  29. component.addMouseListener(new MouseAdapter() {
  30. public void mousePressed(MouseEvent event) {
  31. reactOnMousePressed(event);
  32. }
  33.  
  34. public void mouseClicked(MouseEvent event) {
  35. reactOnMouseClicked();
  36. }
  37. });
  38. }
  39.  
  40. public int getX() {
  41. return rectangle.width >= 0 ? rectangle.x : (rectangle.x + rectangle.width);
  42. }
  43.  
  44. public int getY() {
  45. return rectangle.height >= 0 ? rectangle.y : (rectangle.y + rectangle.height);
  46. }
  47.  
  48. public int getWidth() {
  49. return Math.abs(rectangle.width);
  50. }
  51.  
  52. public int getHeight() {
  53. return Math.abs(rectangle.height);
  54. }
  55.  
  56. public void addRubberBandListener(Runnable rubberBandListener) {
  57. rubberBandListeners.add(rubberBandListener);
  58. }
  59.  
  60. public void notifyRubberBandListeners() {
  61. Iterator iterator = rubberBandListeners.iterator();
  62. while (iterator.hasNext()) {
  63. ((Runnable)iterator.next()).run();
  64. }
  65. }
  66.  
  67. public void kill() {
  68. rectangle = null;
  69. notifyRubberBandListeners();
  70. }
  71.  
  72. private void reactOnMouseDragged(MouseEvent event) {
  73. if (rectangle == null) createRubberBand(0,0);
  74. int x = Math.min(event.getX(), maxWidth-1);
  75. x = Math.max(0, x);
  76. int y = Math.min(event.getY(), maxHeight-1);
  77. y = Math.max(0, y);
  78. rectangle.width = x - rectangle.x;
  79. rectangle.height = y - rectangle.y;
  80. notifyRubberBandListeners();
  81. }
  82.  
  83. private void createRubberBand(int x, int y) {
  84. rectangle = new Rectangle(x, y, 0, 0);
  85. }
  86.  
  87. private void reactOnMousePressed(MouseEvent event) {
  88. createRubberBand(event.getX(), event.getY());
  89. notifyRubberBandListeners();
  90. }
  91.  
  92. private void reactOnMouseClicked() {
  93. kill();
  94. }
  95.  
  96. public boolean isActive() {
  97. return rectangle != null;
  98. }
  99.  
  100.  
  101. public void setMaxBounds(int width, int height) {
  102. this.maxWidth = width;
  103. this.maxHeight = height;
  104. }
  105.  
  106. }

Report this snippet  

You need to login to post a comment.