Guest User

Untitled

a guest
Jun 3rd, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1.  
  2. /* Whiteboard.java */
  3.  
  4. import java.awt.*;
  5.  
  6. import java.awt.event.*;
  7. import java.io.File;
  8. import java.util.*;
  9.  
  10. import javax.swing.JButton;
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JPanel;
  13.  
  14. public class Whiteboard extends Canvas implements MouseListener,
  15. MouseMotionListener,
  16. KeyListener,
  17. ActionListener,
  18. WhiteboardContext {
  19.  
  20. private Image buffer, contentBuffer;
  21.  
  22. public static final int WIDTH=WhiteboardContext.WIDTH,
  23. HEIGHT=WhiteboardContext.HEIGHT;
  24.  
  25. private int currentColor=0, currentTool=1;
  26.  
  27. private int xOffset=0, yOffset=0;
  28.  
  29. private Tool.Shape currentShape = null;
  30.  
  31. private Transport tr;
  32. public int TextBoxUpdateFlag=0;
  33.  
  34. private TextField tf;
  35. public TextField cf;
  36. public TextField ccf;
  37.  
  38. JFileChooser fc=new JFileChooser();
  39.  
  40. String message="";
  41.  
  42. private boolean readOnly;
  43.  
  44. private boolean repaint=true;
  45.  
  46. private Vector myShapes = new Vector();
  47.  
  48. private static final Cursor
  49. SELECT_CURSOR= new Cursor(Cursor.DEFAULT_CURSOR),
  50. DRAW_CURSOR=new Cursor(Cursor.CROSSHAIR_CURSOR);
  51.  
  52. public int getXOffset() { return xOffset; }
  53. public int getYOffset() { return yOffset; }
  54.  
  55. public void setXOffset(int newValue) { xOffset = newValue; }
  56. public void setYOffset(int newValue) { yOffset = newValue; }
  57. public String host;
  58.  
  59. public String getText() {
  60. return tf.getText();
  61. }
  62.  
  63. private static final Color[] COLORS= {
  64. Color.black, Color.gray, Color.lightGray, Color.white, Color.red, Color.orange, Color.yellow,
  65. Color.green, Color.cyan, Color.blue, Color.magenta,
  66. };
  67.  
  68. public Whiteboard(Transport t, boolean readOnly) {
  69. tr=t;
  70.  
  71. this.readOnly=readOnly;
  72. tr.setWhiteboard(this);
  73. addMouseListener(this);
  74. addMouseMotionListener(this);
  75. addKeyListener(this);
  76. setCursor(DRAW_CURSOR);
  77. }
  78. public void chatOnClinetStart(chatClient t1,Whiteboard w1)
  79. {
  80.  
  81. }
  82. public void buildWhiteboard(Container p) {
  83. Panel p2 = new Panel(new BorderLayout()),
  84. p3=new Panel(new BorderLayout()),
  85. p4=new Panel(new BorderLayout()),
  86. p5=new Panel(new BorderLayout());
  87. p.setBackground(Color.white);
  88. p2.setBackground(Color.lightGray);
  89. p.setLayout(new BorderLayout());
  90. p.add("North", this);
  91. p.add("Center", p2);
  92. p2.add("North", p3);
  93. p2.add("Center",p4);
  94. p4.add("North",p5);
  95. p3.add("West", new Label(" Text:"));
  96. p5.add("West",new Label(" TextBox"));
  97. p4.add("West",new Label(" ChatHere:"));
  98. p5.add("Center",ccf=new TextField(""));
  99. p4.add("Center",cf = new TextField(""));
  100. // String sample="Java-Version: "+System.getProperty("java.version");
  101. // String sample="MaxMemory: "+Runtime.getRuntime().maxMemory();
  102. p3.add("Center", tf = new TextField(""));
  103. Button b;
  104. Button c;
  105. Button d;
  106. Whiteboard s=Whiteboard.this;
  107. p3.add("East", b = new Button("Undo!"));
  108. p4.add("East", c=new Button("Send!"));
  109. p5.add("East", d=new Button("Upload"));
  110. b.addActionListener(AC);
  111. d.addActionListener(new MyUploadButton(this, host));
  112. c.addActionListener(new MyButton(this));
  113. if (readOnly) {
  114. tf.setText("Nur Lesezugriff gestattet");
  115. tf.setEditable(false);
  116. }
  117. }
  118.  
  119. public Dimension getPreferredSize() {
  120. return new Dimension(WIDTH+20, HEIGHT);
  121. }
  122.  
  123. /** @deprecated */
  124. public boolean isFocusTraversable() {
  125. return true;
  126. }
  127.  
  128. public boolean isFocusable() {
  129. return true;
  130. }
  131.  
  132. public void update(Graphics g) {
  133. if (buffer == null)
  134. buffer = createImage(getSize().width, getSize().height);
  135. Graphics bufg = buffer.getGraphics();
  136. paint(bufg);
  137. g.drawImage(buffer, 0, 0, this);
  138. }
  139.  
  140. public void paint(Graphics g) {
  141. if (g==null) return;
  142. paintContents(g);
  143. g.setColor(Color.black);
  144. g.fillRect(20,0,1,HEIGHT);
  145. g.setColor(Color.white);
  146. g.fillRect(0,0,20,HEIGHT);
  147. if(readOnly) return;
  148. for(int i=0;i<Tool.TOOLS.length;i++) {
  149. Tool.TOOLS[i].paint(g, i, currentTool == i);
  150. }
  151. int cnt=-1;
  152. for(int i=HEIGHT-20*COLORS.length; i<HEIGHT; i+=20){
  153. cnt++;
  154. Color c=COLORS[cnt];
  155. g.setColor(c);
  156. g.fillRect(0, i, 20, 20);
  157. if (cnt==currentColor) {
  158. c=(c==Color.black?Color.white:Color.black);
  159. g.setColor(c);
  160. g.drawRect(2, i+2, 16,16);
  161. }
  162. }
  163. }
  164.  
  165. public void paintContents(Graphics g) {
  166. if (contentBuffer == null)
  167. contentBuffer = createImage(getSize().width, getSize().height);
  168. if (currentShape!=null && currentShape.needsRepaint())
  169. repaint=true;
  170. if (repaint) {
  171. paintContents2(contentBuffer.getGraphics());
  172. repaint=false;
  173. }
  174. g.drawImage(contentBuffer, 0, 0, this);
  175. if (currentShape != null)
  176. currentShape.paint(this, g);
  177. }
  178.  
  179. public void paintContents2(Graphics g) {
  180. g.clearRect(0,0,WIDTH+20,HEIGHT);
  181. xOffset=yOffset=0;
  182. Vector shapes=tr.getAllShapes();
  183. Enumeration enum1 = shapes.elements();
  184. while (enum1.hasMoreElements()) {
  185. Tool.Shape s = (Tool.Shape)enum1.nextElement();
  186. s.updateState(this);
  187. }
  188. if (currentShape != null)
  189. currentShape.updateState(this);
  190. enum1 = shapes.elements();
  191. while (enum1.hasMoreElements()) {
  192. Tool.Shape s = (Tool.Shape)enum1.nextElement();
  193. s.paint(this, g);
  194. }
  195. }
  196.  
  197. public void addShape(Tool.Shape s) {
  198. myShapes.addElement(s);
  199. tr.addShape(s);
  200. }
  201.  
  202. public void newShapesAvailable() {
  203. repaint=true;
  204. repaint();
  205. }
  206.  
  207. // Mouse events
  208.  
  209. public void mousePressed(MouseEvent evt) {
  210. if (readOnly) return;
  211. int x = evt.getX();
  212. int y = evt.getY();
  213. if (x<=20) { // Icon bar clicked
  214. if (y >= HEIGHT - 20*COLORS.length) {// Color selector
  215. currentColor = (y - HEIGHT + 20*COLORS.length) / 20;
  216. repaint=true;
  217. } else if (y < Tool.TOOLS.length * 20) {
  218. currentTool = y / 20;
  219. repaint=true;
  220. } else {
  221. System.out.println("Nothing here");
  222. }
  223. } else {
  224. if (currentShape != null) {
  225. // Multi click --> cancel
  226. currentShape = null;
  227. } else {
  228. currentShape = Tool.TOOLS[currentTool].createShape
  229. (this, x-xOffset, y-yOffset, COLORS[currentColor]);
  230. }
  231. }
  232. repaint();
  233. }
  234.  
  235.  
  236. public void mouseDragged(MouseEvent evt) {
  237. if (readOnly) return;
  238. int x = evt.getX();
  239. int y = evt.getY();
  240. if (currentShape != null) {
  241. currentShape.updatePoint(this, x-xOffset, y-yOffset);
  242. repaint();
  243. }
  244. }
  245.  
  246. public void mouseReleased(MouseEvent evt) {
  247. if (readOnly) return;
  248. int x = evt.getX();
  249. int y = evt.getY();
  250. if (currentShape != null) {
  251. currentShape.endPoint(this, x-xOffset, y-yOffset);
  252. addShape(currentShape);
  253. currentShape = null;
  254. repaint=true;
  255. repaint();
  256. }
  257. }
  258.  
  259. public void mouseMoved(MouseEvent evt) {
  260. int x = evt.getX();
  261. if (x<=20) { // Icon bar
  262. setCursor(SELECT_CURSOR);
  263. } else {
  264. setCursor(DRAW_CURSOR);
  265. }
  266. }
  267.  
  268. public void mouseEntered(MouseEvent evt) {}
  269. public void mouseExited(MouseEvent evt) {}
  270. public void mouseClicked(MouseEvent evt) {}
  271.  
  272. // keyboard events
  273.  
  274. public void keyPressed(KeyEvent evt) {
  275. if (readOnly) return;
  276. if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
  277. currentShape = null;
  278. repaint();
  279. }
  280. }
  281.  
  282. public void keyReleased(KeyEvent evt) {}
  283. public void keyTyped(KeyEvent evt) {
  284. if (readOnly) return;
  285. char c = evt.getKeyChar();
  286. if (c>='1' && c < '1'+COLORS.length) { // color
  287. currentColor=c-'1';
  288. repaint();
  289. } else {
  290. for(int i=0;i<Tool.TOOLS.length;i++) {
  291. if (Tool.TOOLS[i].getToolID() == c) {
  292. currentTool = i;
  293. repaint();
  294. }
  295. }
  296. }
  297. }
  298.  
  299. // Action listener
  300. public void actionPerformed(ActionEvent evt) {
  301. int answer = fc.showOpenDialog(this);
  302. if (answer == JFileChooser.APPROVE_OPTION)
  303. {
  304.  
  305.  
  306. }
  307.  
  308. }
  309.  
  310. ActionListener AC = new ActionListener(){
  311. public void actionPerformed(ActionEvent ae){
  312.  
  313. if (myShapes.size() == 0) return;
  314. Tool.Shape lastShape =
  315. (Tool.Shape) myShapes.elementAt(myShapes.size()-1);
  316. tr.addShape(new Tool.DeleteShape(lastShape));
  317. myShapes.removeElementAt(myShapes.size()-1);
  318.  
  319. }
  320. };
  321. }
Advertisement
Add Comment
Please, Sign In to add comment