1 package demo.notification.whiteboard; 2 3 4 5 import java.awt.Canvas ; 6 7 import java.awt.image.ImageProducer ; 8 9 import java.awt.event.MouseAdapter ; 10 11 import java.awt.event.MouseEvent ; 12 13 import java.awt.event.MouseMotionAdapter ; 14 15 import java.awt.Graphics ; 16 17 18 19 public class BrushSizeDrawCanvas extends Canvas { 20 21 22 23 WorkgroupController controller_; 24 25 ImageProducer imageProducer_; 26 27 28 29 31 private int lastX,lastY; 32 33 34 35 37 private int drawRed=255; 38 39 private int drawGreen=255; 40 41 private int drawBlue=255; 42 43 44 45 public BrushSizeDrawCanvas(WorkgroupController controller, int width, int height) { 46 47 super(); 48 49 setSize(width,height); 50 51 controller_ = controller; 52 53 initialize(); 54 55 } 56 57 58 59 public void initialize() { 60 61 imageProducer_ = controller_.getImage().getProducer(); 62 63 64 65 67 addMouseListener(new MouseAdapter () { 68 69 public void mousePressed(MouseEvent e) { 70 71 lastX = e.getX(); 72 73 lastY = e.getY(); 74 75 } 76 77 }); 78 79 80 81 83 addMouseMotionListener(new MouseMotionAdapter () { 84 85 public void mouseDragged(MouseEvent e) { 86 87 controller_.drawLine(lastX,lastY, 88 89 e.getX(),e.getY(), 90 91 drawRed,drawGreen,drawBlue); 92 93 lastX = e.getX(); 94 95 lastY = e.getY(); 96 97 repaint(); 98 99 } 100 101 }); 102 103 } 104 105 106 107 public void paint(Graphics g){ 108 109 g.drawImage(createImage(imageProducer_), 0, 0, null); 110 111 } 112 113 114 115 public void update(Graphics g){ 116 117 paint(g); 118 119 } 120 121 122 123 void setDrawColor(int red,int green,int blue) { 124 125 drawRed=red; 126 127 drawGreen=green; 128 129 drawBlue=blue; 130 131 } 132 133 134 135 public void clearAll() { 136 137 controller_.clearAll(); 138 139 repaint(); 140 141 } 142 143 144 145 void setBrushSize(int size) { 146 147 controller_.setBrushSize(size); 148 149 } 150 151 } 152 153 | Popular Tags |