Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.33 KB | None | 0 0
  1. // package drawingPad;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.util.*;
  6. import javax.swing.*;
  7.  
  8. public class DrawingPad2 extends DrawingPad
  9. {
  10. public DrawingPad2()
  11. {
  12. this(true);
  13. }
  14.  
  15. public DrawingPad2(boolean isApplet)
  16. {
  17. super(isApplet);
  18. canvas.addKeyListener((KeyListener) listener);
  19. }
  20.  
  21. public static void main(String[] args)
  22. {
  23. JFrame frame = new JFrame();
  24. frame.setTitle("Drawing Pad 2");
  25. frame.setBackground(Color.lightGray);
  26. frame.getContentPane().setLayout(new BorderLayout());
  27. frame.getContentPane().add(new DrawingPad2(false), BorderLayout.CENTER);
  28. frame.addWindowListener(new AppCloser());
  29. frame.pack();
  30. frame.setSize(700, 500);
  31. frame.show();
  32. }
  33.  
  34. protected EventListener makeCanvasListener(ScribbleCanvas canvas)
  35. {
  36. return new KeyToolListener(this, canvas);
  37. }
  38.  
  39. protected void initActions()
  40. {
  41. actions = new Vector();
  42. actions.addElement(new ToolAction("Scribble",
  43. getImageIcon("scribble.gif"),
  44. "scribble tool", this,
  45. currentTool = new ScribbleTool()));
  46. actions.addElement(new ToolAction("Line", new ToolIcon(ToolIcon.LINE),
  47. "draw line segments", this,
  48. new TwoEndsShapeTool(new LineShape())));
  49. actions.addElement(new ToolAction("Rectangle", new ToolIcon(ToolIcon.RECT),
  50. "draw rectangles", this,
  51. new TwoEndsShapeTool(new RectShape())));
  52. actions.addElement(new ToolAction("Oval", new ToolIcon(ToolIcon.OVAL),
  53. "draw ovals", this,
  54. new TwoEndsShapeTool(new OvalShape())));
  55. actions.addElement(new ToolAction("Filled Rectangle",
  56. new ToolIcon(ToolIcon.FILLRECT),
  57. "draw filled rectangles", this,
  58. new TwoEndsShapeTool(new FilledRectShape())));
  59. actions.addElement(new ToolAction("Filled Oval",
  60. new ToolIcon(ToolIcon.FILLOVAL),
  61. "draw filled ovals", this,
  62. new TwoEndsShapeTool(new FilledOvalShape())));
  63. actions.addElement(new ToolAction("Text", new ToolIcon(ToolIcon.TEXT),
  64. "type text", this,
  65. new TextTool()));
  66. actions.addElement(new ToolAction("Eraser",
  67. getImageIcon("eraser.gif"),
  68. "eraser tool", this,
  69. new EraserTool()));
  70. }
  71. }
  72.  
  73.  
  74. package drawingPad;
  75.  
  76. import java.awt.*;
  77. import java.awt.event.*;
  78. import java.net.URL;
  79. import java.net.MalformedURLException;
  80. import java.util.*;
  81. import javax.swing.*;
  82.  
  83. public class DrawingPad extends Scribble2
  84. {
  85. protected Vector actions;
  86. protected Tool currentTool;
  87.  
  88. public DrawingPad()
  89. {
  90. this(true);
  91. }
  92.  
  93. public DrawingPad(boolean isApplet)
  94. {
  95. super(isApplet);
  96. if (!isApplet)
  97. {
  98. init();
  99. }
  100. }
  101.  
  102. public void init()
  103. {
  104. initActions();
  105. getContentPane().add(makeToolBar(actions), BorderLayout.WEST);
  106. getContentPane().add(makeMenuBar(actions), BorderLayout.NORTH);
  107. }
  108.  
  109. public static void main(String[] args)
  110. {
  111. JFrame frame = new JFrame();
  112. frame.setTitle("Drawing Pad");
  113. frame.setBackground(Color.lightGray);
  114. frame.getContentPane().setLayout(new BorderLayout());
  115. frame.getContentPane().add(new DrawingPad(false), BorderLayout.CENTER);
  116. frame.addWindowListener(new AppCloser());
  117. frame.pack();
  118. frame.setSize(600, 400);
  119. frame.show();
  120. }
  121.  
  122. public void setCurrentTool(Tool tool)
  123. {
  124. currentTool = tool;
  125. }
  126.  
  127. public Tool getCurrentTool()
  128. {
  129. return currentTool;
  130. }
  131.  
  132. protected EventListener makeCanvasListener(ScribbleCanvas canvas)
  133. {
  134. return new ToolListener(this, canvas);
  135. }
  136.  
  137. protected ImageIcon getImageIcon(String filename)
  138. {
  139. if (isApplet)
  140. {
  141. try
  142. {
  143. URL url = new URL(getCodeBase(), filename);
  144. return new ImageIcon(url);
  145. } catch (MalformedURLException e)
  146. {
  147. return null;
  148. }
  149. }
  150. else
  151. {
  152. return new ImageIcon(filename);
  153. }
  154. }
  155.  
  156. protected void initActions()
  157. {
  158. actions = new Vector();
  159. actions.addElement(new ToolAction("Scribble",
  160. getImageIcon("scribble.gif"),
  161. "scribble tool", this,
  162. currentTool = new ScribbleTool()));
  163. actions.addElement(new ToolAction("Line", new ToolIcon(ToolIcon.LINE),
  164. "draw line segments", this,
  165. new TwoEndsTool(TwoEndsTool.LINE)));
  166. actions.addElement(new ToolAction("Rectangle", new ToolIcon(ToolIcon.RECT),
  167. "draw rectangles", this,
  168. new TwoEndsTool(TwoEndsTool.RECT)));
  169. actions.addElement(new ToolAction("Oval", new ToolIcon(ToolIcon.OVAL),
  170. "draw ovals", this,
  171. new TwoEndsTool(TwoEndsTool.OVAL)));
  172. actions.addElement(new ToolAction("Eraser",
  173. getImageIcon("eraser.gif"),
  174. "eraser tool", this,
  175. new EraserTool()));
  176. }
  177.  
  178. protected Component makeToolBar(Vector actions)
  179. {
  180. Box toolbar = new Box(BoxLayout.Y_AXIS);
  181. Enumeration enum = actions.elements();
  182. while (enum.hasMoreElements())
  183. {
  184. Action a = (Action) enum.nextElement();
  185. JButton button = new JButton((Icon) a.getValue(Action.DEFAULT));
  186. button.setToolTipText((String) a.getValue(Action.SHORT_DESCRIPTION));
  187. button.addActionListener(a);
  188. toolbar.add(button);
  189. }
  190. toolbar.add(Box.createVerticalGlue());
  191. return toolbar;
  192. }
  193.  
  194. protected Component makeMenuBar(Vector actions)
  195. {
  196. JMenuBar menubar = new JMenuBar();
  197. JMenu tools = new JMenu("Tool");
  198. Enumeration enum = actions.elements();
  199. while (enum.hasMoreElements())
  200. {
  201. Action a = (Action) enum.nextElement();
  202. tools.add(a);
  203. }
  204. menubar.add(tools);
  205. JMenu help = new JMenu("Help");
  206. help.add(new AboutAction());
  207. menubar.add(help);
  208. return menubar;
  209. }
  210.  
  211. class AboutAction extends AbstractAction
  212. {
  213. public AboutAction()
  214. {
  215. super("About");
  216. }
  217.  
  218. public void actionPerformed(ActionEvent event)
  219. {
  220. JOptionPane.showMessageDialog(null, "DrawingPad version 1.0\nCopyright (c) Xiaoping Jia 1998", "About",
  221. JOptionPane.INFORMATION_MESSAGE);
  222. }
  223. }
  224. }
  225.  
  226. package drawingPad;
  227.  
  228. import java.awt.*;
  229.  
  230. public class EraserTool implements Tool
  231. {
  232. public void mousePressed(Point p, ScribbleCanvas canvas)
  233. {
  234. canvas.mouseButtonDown = true;
  235. canvas.x = p.x;
  236. canvas.y = p.y;
  237. offscreen = canvas.getOffScreenGraphics();
  238. offscreen.setColor(Color.white);
  239. }
  240.  
  241. public void mouseDragged(Point p, ScribbleCanvas canvas)
  242. {
  243. if (canvas.mouseButtonDown)
  244. {
  245. int x0, y0, dx, dy;
  246. x0 = Math.min(canvas.x, p.x) - 2;
  247. y0 = Math.min(canvas.y, p.y) - 2;
  248. dx = Math.abs(p.x - canvas.x) + 5;
  249. dy = Math.abs(p.y - canvas.y) + 5;
  250. offscreen.fillRect(x0, y0, dx, dy);
  251. canvas.repaint(x0, y0, dx, dy);
  252. canvas.x = p.x;
  253. canvas.y = p.y;
  254. }
  255. }
  256.  
  257. public void mouseReleased(Point p, ScribbleCanvas canvas)
  258. {
  259. canvas.mouseButtonDown = false;
  260. canvas.setPenColor(canvas.penColor);
  261. }
  262.  
  263. Graphics offscreen;
  264. }
  265.  
  266. package drawingPad;
  267.  
  268. import java.awt.*;
  269.  
  270. public class FilledOvalShape implements TwoEndsShape
  271. {
  272. public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
  273. {
  274. int x = Math.min(x1, x2);
  275. int y = Math.min(y1, y2);
  276. int w = Math.abs(x1 - x2) + 1;
  277. int h = Math.abs(y1 - y2) + 1;
  278. g.fillOval(x, y, w, h);
  279. }
  280.  
  281. public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
  282. {
  283. int x = Math.min(x1, x2);
  284. int y = Math.min(y1, y2);
  285. int w = Math.abs(x1 - x2) + 1;
  286. int h = Math.abs(y1 - y2) + 1;
  287. g.drawOval(x, y, w, h);
  288. }
  289. }
  290.  
  291. package drawingPad;
  292.  
  293. import java.awt.*;
  294.  
  295. public class FilledRectShape implements TwoEndsShape
  296. {
  297. public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
  298. {
  299. int x = Math.min(x1, x2);
  300. int y = Math.min(y1, y2);
  301. int w = Math.abs(x1 - x2) + 1;
  302. int h = Math.abs(y1 - y2) + 1;
  303. g.fillRect(x, y, w, h);
  304. }
  305.  
  306. public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
  307. {
  308. int x = Math.min(x1, x2);
  309. int y = Math.min(y1, y2);
  310. int w = Math.abs(x1 - x2) + 1;
  311. int h = Math.abs(y1 - y2) + 1;
  312. g.drawRect(x, y, w, h);
  313. }
  314. }
  315.  
  316. package drawingPad;
  317.  
  318. public interface KeyEventTool extends Tool
  319. {
  320. void keyPressed(char c, ScribbleCanvas canvas);
  321. }
  322.  
  323. package drawingPad;
  324.  
  325. import java.awt.*;
  326.  
  327. public class LineShape implements TwoEndsShape
  328. {
  329. public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
  330. {
  331. g.drawLine(x1, y1, x2, y2);
  332. }
  333.  
  334. public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
  335. {
  336. g.drawLine(x1, y1, x2, y2);
  337. }
  338. }
  339.  
  340. package drawingPad;
  341.  
  342. import java.awt.*;
  343.  
  344. public class OvalShape implements TwoEndsShape
  345. {
  346. public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
  347. {
  348. int x = Math.min(x1, x2);
  349. int y = Math.min(y1, y2);
  350. int w = Math.abs(x1 - x2) + 1;
  351. int h = Math.abs(y1 - y2) + 1;
  352. g.drawOval(x, y, w, h);
  353. }
  354.  
  355. public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
  356. {
  357. int x = Math.min(x1, x2);
  358. int y = Math.min(y1, y2);
  359. int w = Math.abs(x1 - x2) + 1;
  360. int h = Math.abs(y1 - y2) + 1;
  361. g.drawOval(x, y, w, h);
  362. }
  363. }
  364.  
  365. package drawingPad;
  366.  
  367. import java.awt.*;
  368. import java.awt.event.*;
  369. import java.util.EventListener;
  370.  
  371. public class KeyToolListener extends ToolListener implements KeyListener
  372. {
  373. public KeyToolListener(DrawingPad drawingPad, ScribbleCanvas canvas)
  374. {
  375. super(drawingPad, canvas);
  376. }
  377.  
  378. public void keyPressed(KeyEvent e)
  379. {
  380. Tool tool = drawingPad.getCurrentTool();
  381. if (tool != null && tool instanceof KeyEventTool)
  382. {
  383. KeyEventTool keyTool = (KeyEventTool) tool;
  384. keyTool.keyPressed((char) e.getKeyChar(), canvas);
  385. }
  386. }
  387.  
  388. public void keyReleased(KeyEvent e) {}
  389. public void keyTyped(KeyEvent e) {}
  390.  
  391. public void mouseClicked(MouseEvent e)
  392. {
  393. canvas.requestFocus();
  394. }
  395. }
  396.  
  397. package drawingPad;
  398.  
  399. import java.awt.*;
  400.  
  401. public class RectShape implements TwoEndsShape
  402. {
  403. public void drawShape(Graphics g, int x1, int y1, int x2, int y2)
  404. {
  405. int x = Math.min(x1, x2);
  406. int y = Math.min(y1, y2);
  407. int w = Math.abs(x1 - x2) + 1;
  408. int h = Math.abs(y1 - y2) + 1;
  409. g.drawRect(x, y, w, h);
  410. }
  411.  
  412. public void drawOutline(Graphics g, int x1, int y1, int x2, int y2)
  413. {
  414. int x = Math.min(x1, x2);
  415. int y = Math.min(y1, y2);
  416. int w = Math.abs(x1 - x2) + 1;
  417. int h = Math.abs(y1 - y2) + 1;
  418. g.drawRect(x, y, w, h);
  419. }
  420. }
  421.  
  422. package drawingPad;
  423.  
  424. import java.awt.*;
  425. import java.awt.event.*;
  426. import java.util.EventListener;
  427. import javax.swing.*;
  428.  
  429. public class Scribble2 extends Scribble
  430. {
  431. public Scribble2()
  432. {
  433. this(true);
  434. }
  435.  
  436. public Scribble2(boolean isApplet)
  437. {
  438. super(isApplet);
  439. getContentPane().add(makeControlPanel(), BorderLayout.SOUTH);
  440. }
  441.  
  442. public static void main(String[] args)
  443. {
  444. JFrame frame = new JFrame();
  445. frame.setTitle("Scribble Pad 2");
  446. frame.setBackground(Color.lightGray);
  447. frame.getContentPane().setLayout(new BorderLayout());
  448. frame.getContentPane().add(new Scribble2(false), BorderLayout.CENTER);
  449. frame.addWindowListener(new AppCloser());
  450. frame.pack();
  451. frame.setSize(600, 400);
  452. frame.show();
  453. }
  454.  
  455. protected Component makeControlPanel()
  456. {
  457. JPanel controlPanel = new JPanel();
  458. JButton button = new JButton("Clear");
  459. controlPanel.add(button);
  460. button.addActionListener(new ClearAction());
  461.  
  462. controlPanel.add(Box.createHorizontalStrut(20));
  463.  
  464. controlPanel.add(new JLabel("Pen color"));
  465. JComboBox choice = new JComboBox();
  466. choice.addItem("black");
  467. choice.addItem("blue");
  468. choice.addItem("green");
  469. choice.addItem("red");
  470. choice.addItemListener(new ColorChoiceListener());
  471. Dimension d = choice.getPreferredSize();
  472. d.width += 10;
  473. choice.setPreferredSize(d);
  474. controlPanel.add(choice);
  475. return controlPanel;
  476. }
  477.  
  478. class ClearAction extends AbstractAction
  479. {
  480. public ClearAction()
  481. {
  482. super("clear");
  483. }
  484. public void actionPerformed(ActionEvent event)
  485. {
  486. clearCanvas();
  487. }
  488. }
  489.  
  490. class ColorChoiceListener implements ItemListener
  491. {
  492. public void itemStateChanged(ItemEvent event)
  493. {
  494. if ("black".equals(event.getItem()))
  495. canvas.setPenColor(Color.black);
  496. else if ("blue".equals(event.getItem()))
  497. canvas.setPenColor(Color.blue);
  498. else if ("green".equals(event.getItem()))
  499. canvas.setPenColor(Color.green);
  500. else if ("red".equals(event.getItem()))
  501. canvas.setPenColor(Color.red);
  502. }
  503. }
  504.  
  505.  
  506. public void clearCanvas()
  507. {
  508. canvas.clearCanvas();
  509. canvas.repaint();
  510. }
  511. }
  512.  
  513. package drawingPad;
  514.  
  515. import java.awt.*;
  516. import java.awt.event.*;
  517. import java.util.EventListener;
  518. import javax.swing.*;
  519.  
  520. public class Scribble extends JApplet
  521. {
  522. protected ScribbleCanvas canvas;
  523. protected EventListener listener;
  524. protected boolean isApplet = false;
  525.  
  526. public Scribble()
  527. {
  528. this(true);
  529. }
  530.  
  531. public Scribble(boolean isApplet)
  532. {
  533. this.isApplet = isApplet;
  534. getContentPane().setLayout(new BorderLayout());
  535. getContentPane().add(makeCanvas(), BorderLayout.CENTER);
  536. listener = makeCanvasListener(canvas);
  537. canvas.addMouseListener((MouseListener) listener);
  538. canvas.addMouseMotionListener((MouseMotionListener) listener);
  539. }
  540.  
  541. public static void main(String[] args)
  542. {
  543. JFrame frame = new JFrame();
  544. frame.setTitle("Scribble Pad");
  545. frame.setBackground(Color.lightGray);
  546. frame.getContentPane().setLayout(new BorderLayout());
  547. frame.getContentPane().add(new Scribble(false), BorderLayout.CENTER);
  548. frame.addWindowListener(new AppCloser());
  549. frame.pack();
  550. frame.setSize(600, 400);
  551. frame.show();
  552. }
  553.  
  554. protected Component makeCanvas()
  555. {
  556. canvas = new ScribbleCanvas();
  557. canvas.setBackground(Color.red);
  558. return canvas;
  559. }
  560.  
  561. protected EventListener makeCanvasListener(ScribbleCanvas canvas)
  562. {
  563. return new ScribbleCanvasListener(canvas);
  564. }
  565.  
  566. static class AppCloser extends WindowAdapter
  567. {
  568. public void windowClosing(WindowEvent e)
  569. {
  570. System.exit(0);
  571. }
  572. }
  573. }
  574. package drawingPad;
  575.  
  576. import java.awt.*;
  577. import java.awt.event.*;
  578. import javax.swing.*;
  579.  
  580. public class ScribbleCanvas extends JComponent
  581. {
  582. protected Image im;
  583. protected int width, height;
  584. protected Graphics offscreen;
  585. protected Color penColor = Color.black;
  586. protected boolean mouseButtonDown = false;
  587. protected int x, y;
  588.  
  589. public void update(Graphics g)
  590. {
  591. if (im != null)
  592. {
  593. g.drawImage(im, 0, 0, this);
  594. }
  595. }
  596.  
  597. public void paint(Graphics g)
  598. {
  599. update(g);
  600. }
  601.  
  602. public Graphics getOffScreenGraphics()
  603. {
  604. return offscreen;
  605. }
  606.  
  607. public void setBounds(int x, int y,int width, int height)
  608. {
  609. this.width = width;
  610. this.height = height;
  611. if (width > 0 && height > 0)
  612. {
  613. Image newim = createImage(width, height);
  614. offscreen = newim.getGraphics();
  615. clearCanvas();
  616. if (im != null)
  617. {
  618. offscreen.drawImage(im, 0, 0, this);
  619. }
  620. im = newim;
  621. }
  622. super.setBounds(x, y, width, height);
  623. repaint();
  624. }
  625.  
  626. public void setPenColor(Color c)
  627. {
  628. penColor = c;
  629. offscreen.setColor(penColor);
  630. }
  631.  
  632. public void clearCanvas()
  633. {
  634. offscreen.setColor(Color.white);
  635. offscreen.fillRect(0, 0, width, height);
  636. offscreen.setColor(penColor);
  637. }
  638. }
  639.  
  640.  
  641. package drawingPad;
  642.  
  643. import java.awt.*;
  644. import java.awt.event.*;
  645.  
  646. public class ScribbleCanvasListener implements MouseListener, MouseMotionListener
  647. {
  648. public ScribbleCanvasListener(ScribbleCanvas canvas)
  649. {
  650. this.canvas = canvas;
  651. }
  652.  
  653. public void mousePressed(MouseEvent e)
  654. {
  655. Point p = e.getPoint();
  656. canvas.mouseButtonDown = true;
  657. canvas.x = p.x;
  658. canvas.y = p.y;
  659. }
  660.  
  661. public void mouseReleased(MouseEvent e)
  662. {
  663. canvas.mouseButtonDown = false;
  664. }
  665.  
  666. public void mouseDragged(MouseEvent e)
  667. {
  668. Point p = e.getPoint();
  669. if (canvas.mouseButtonDown)
  670. {
  671. canvas.getOffScreenGraphics().drawLine(canvas.x, canvas.y, p.x, p.y);
  672. int x0 = Math.min(canvas.x, p.x);
  673. int y0 = Math.min(canvas.y, p.y);
  674. int dx = Math.abs(p.x - canvas.x) + 1;
  675. int dy = Math.abs(p.y - canvas.y) + 1;
  676. canvas.repaint(x0, y0, dx, dy);
  677. canvas.x = p.x;
  678. canvas.y = p.y;
  679. }
  680. }
  681.  
  682. public void mouseClicked(MouseEvent e) {}
  683. public void mouseEntered(MouseEvent e) {}
  684. public void mouseExited(MouseEvent e) {}
  685. public void mouseMoved(MouseEvent e) {}
  686.  
  687. protected ScribbleCanvas canvas;
  688. }
  689.  
  690. package drawingPad;
  691.  
  692. import java.awt.*;
  693.  
  694. public class ScribbleTool implements Tool
  695. {
  696. public void mousePressed(Point p, ScribbleCanvas canvas)
  697. {
  698. canvas.mouseButtonDown = true;
  699. canvas.x = p.x;
  700. canvas.y = p.y;
  701. }
  702.  
  703. public void mouseDragged(Point p, ScribbleCanvas canvas)
  704. {
  705. if (canvas.mouseButtonDown)
  706. {
  707. int x0, y0, dx, dy;
  708. x0 = Math.min(canvas.x, p.x);
  709. y0 = Math.min(canvas.y, p.y);
  710. dx = Math.abs(p.x - canvas.x) + 1;
  711. dy = Math.abs(p.y - canvas.y) + 1;
  712. canvas.getOffScreenGraphics().drawLine(canvas.x, canvas.y, p.x, p.y);
  713. canvas.repaint(x0, y0, dx, dy);
  714. canvas.x = p.x;
  715. canvas.y = p.y;
  716. }
  717. }
  718.  
  719. public void mouseReleased(Point p, ScribbleCanvas canvas)
  720. {
  721. canvas.mouseButtonDown = false;
  722. }
  723. }
  724.  
  725. package drawingPad;
  726.  
  727. import java.awt.*;
  728. import java.awt.event.*;
  729.  
  730. class TextTool implements KeyEventTool
  731. {
  732. public void mousePressed(Point p, ScribbleCanvas canvas)
  733. {
  734. canvas.mouseButtonDown = true;
  735. canvas.x = p.x;
  736. canvas.y = p.y;
  737. offscreen = canvas.getOffScreenGraphics();
  738. offscreen.setFont(font);
  739. text = new StringBuffer();
  740. }
  741.  
  742. public void mouseReleased(Point p, ScribbleCanvas canvas)
  743. {
  744. canvas.mouseButtonDown = false;
  745. }
  746.  
  747. public void mouseDragged(Point p, ScribbleCanvas canvas) {}
  748.  
  749. public void keyPressed(char c, ScribbleCanvas canvas)
  750. {
  751. text.append(c);
  752. offscreen.drawString(text.toString(), canvas.x, canvas.y);
  753. canvas.repaint(canvas.x, canvas.y - ascent - 1,
  754. fm.stringWidth(text.toString()) + 1,
  755. ascent + descent + 2);
  756. }
  757.  
  758. protected StringBuffer text;
  759. protected Font font = new Font("Helvetica", Font.BOLD, 24);
  760. protected FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
  761. protected int ascent = fm.getAscent();
  762. protected int descent = fm.getDescent();
  763. protected Graphics offscreen;
  764. }
  765.  
  766. package drawingPad;
  767.  
  768. import java.awt.*;
  769.  
  770. public interface Tool
  771. {
  772. void mousePressed(Point p, ScribbleCanvas canvas);
  773. void mouseReleased(Point p, ScribbleCanvas canvas);
  774. void mouseDragged(Point p, ScribbleCanvas canvas);
  775. }
  776.  
  777. package drawingPad;
  778.  
  779. import java.awt.*;
  780. import java.awt.event.*;
  781. import java.util.EventListener;
  782. import javax.swing.*;
  783.  
  784. public class ToolAction extends AbstractAction
  785. {
  786. protected DrawingPad drawingPad;
  787. protected Tool tool;
  788.  
  789. public ToolAction(String name, Icon icon, String tip, DrawingPad drawingPad, Tool tool)
  790. {
  791. super(name);
  792. putValue(Action.DEFAULT, icon);
  793. putValue(Action.SHORT_DESCRIPTION, tip);
  794. setEnabled(tool != null);
  795. this.drawingPad = drawingPad;
  796. this.tool = tool;
  797. }
  798.  
  799. public void actionPerformed(ActionEvent event)
  800. {
  801. drawingPad.setCurrentTool(tool);
  802. }
  803. }
  804.  
  805. package drawingPad;
  806.  
  807. import java.awt.*;
  808. import java.awt.event.*;
  809. import javax.swing.*;
  810.  
  811. public class ToolIcon implements Icon
  812. {
  813. public static final int SCRIBBLE = 0;
  814. public static final int LINE = 1;
  815. public static final int RECT = 2;
  816. public static final int OVAL = 3;
  817. public static final int FILLRECT = 4;
  818. public static final int FILLOVAL = 5;
  819. public static final int TEXT = 6;
  820. public static final int ERASER = 7;
  821.  
  822. protected Dimension dim = new Dimension(36, 36);
  823. protected int id;
  824.  
  825. public ToolIcon(int id)
  826. {
  827. this.id = id;
  828. }
  829.  
  830. public void paintIcon(Component c, Graphics g, int x, int y)
  831. {
  832. Color oldColor = g.getColor();
  833. g.setColor(c.getBackground());
  834. g.fillRect(x, y, dim.width, dim.height);
  835. g.setColor(Color.black);
  836. int x0 = x + 2;
  837. int y0 = y + 2;
  838. int w0 = dim.width - 4;
  839. int h0 = dim.height - 4;
  840. switch (id)
  841. {
  842. case SCRIBBLE:
  843. break;
  844. case LINE:
  845. g.drawLine(x0, y0, x0 + w0, y0 + h0);
  846. break;
  847. case RECT:
  848. g.drawRect(x0, y0, w0, h0);
  849. break;
  850. case OVAL:
  851. g.drawOval(x0, y0, w0, h0);
  852. break;
  853. case FILLRECT:
  854. g.fillRect(x0, y0, w0, h0);
  855. break;
  856. case FILLOVAL:
  857. g.fillOval(x0, y0, w0, h0);
  858. break;
  859. case TEXT:
  860. g.setFont(new Font("Serif", Font.BOLD, h0));
  861. FontMetrics fm = g.getFontMetrics();
  862. g.drawString("T", x0, y0 + fm.getAscent());
  863. break;
  864. case ERASER:
  865. break;
  866. }
  867. g.setColor(oldColor);
  868. }
  869.  
  870. public int getIconWidth() { return dim.width; }
  871. public int getIconHeight() { return dim.height; }
  872. }
  873.  
  874.  
  875. package drawingPad;
  876.  
  877. import java.awt.*;
  878. import java.awt.event.*;
  879. import java.util.EventListener;
  880.  
  881. public class ToolListener extends ScribbleCanvasListener
  882. {
  883. protected DrawingPad drawingPad;
  884.  
  885. public ToolListener(DrawingPad drawingPad, ScribbleCanvas canvas)
  886. {
  887. super(canvas);
  888. this.drawingPad = drawingPad;
  889. }
  890.  
  891. public void mousePressed(MouseEvent e)
  892. {
  893. Tool tool = drawingPad.getCurrentTool();
  894. if (tool != null)
  895. {
  896. tool.mousePressed(e.getPoint(), canvas);
  897. }
  898. }
  899.  
  900. public void mouseReleased(MouseEvent e)
  901. {
  902. Tool tool = drawingPad.getCurrentTool();
  903. if (tool != null)
  904. {
  905. tool.mouseReleased(e.getPoint(), canvas);
  906. }
  907. }
  908.  
  909. public void mouseDragged(MouseEvent e)
  910. {
  911. Tool tool = drawingPad.getCurrentTool();
  912. if (tool != null)
  913. {
  914. tool.mouseDragged(e.getPoint(), canvas);
  915. }
  916. }
  917. }
  918.  
  919. package drawingPad;
  920.  
  921. import java.awt.*;
  922.  
  923. public interface TwoEndsShape
  924. {
  925. void drawShape(Graphics g, int x1, int y1, int x2, int y2);
  926. void drawOutline(Graphics g, int x1, int y1, int x2, int y2);
  927. }
  928.  
  929. package drawingPad;
  930.  
  931. import java.awt.*;
  932.  
  933. public class TwoEndsShapeTool implements Tool
  934. {
  935. protected int xStart, yStart;
  936. protected TwoEndsShape shape;
  937. protected Graphics onscreen;
  938.  
  939. public TwoEndsShapeTool(TwoEndsShape shape)
  940. {
  941. this.shape = shape;
  942. }
  943.  
  944. public void mousePressed(Point p, ScribbleCanvas canvas)
  945. {
  946. canvas.mouseButtonDown = true;
  947. xStart = canvas.x = p.x;
  948. yStart = canvas.y = p.y;
  949. onscreen = canvas.getGraphics();
  950. onscreen.setXORMode(Color.darkGray);
  951. onscreen.setColor(Color.lightGray);
  952. if (shape != null)
  953. {
  954. shape.drawOutline(onscreen, xStart, yStart, xStart, yStart);
  955. }
  956. }
  957.  
  958. public void mouseDragged(Point p, ScribbleCanvas canvas)
  959. {
  960. if (canvas.mouseButtonDown && shape != null)
  961. {
  962. shape.drawOutline(onscreen, xStart, yStart, canvas.x, canvas.y);
  963. shape.drawOutline(onscreen, xStart, yStart, p.x, p.y);
  964. canvas.x = p.x;
  965. canvas.y = p.y;
  966. }
  967. }
  968.  
  969. public void mouseReleased(Point p, ScribbleCanvas canvas)
  970. {
  971. canvas.mouseButtonDown = false;
  972. onscreen.setPaintMode();
  973. if (shape != null)
  974. {
  975. Graphics offscreen = canvas.getOffScreenGraphics();
  976. shape.drawShape(offscreen, xStart, yStart, p.x, p.y);
  977. canvas.repaint();
  978. }
  979. }
  980. }
  981.  
  982. package drawingPad;
  983.  
  984. import java.awt.*;
  985.  
  986. public class TwoEndsTool implements Tool
  987. {
  988. public static final int LINE = 0;
  989. public static final int OVAL = 1;
  990. public static final int RECT = 2;
  991. protected int shape = LINE;
  992. protected int xStart, yStart;
  993. protected Graphics onscreen;
  994.  
  995. public TwoEndsTool(int shape)
  996. {
  997. this.shape = shape;
  998. }
  999.  
  1000. public void mousePressed(Point p, ScribbleCanvas canvas)
  1001. {
  1002. canvas.mouseButtonDown = true;
  1003. xStart = canvas.x = p.x;
  1004. yStart = canvas.y = p.y;
  1005. onscreen = canvas.getGraphics();
  1006. onscreen.setXORMode(Color.darkGray);
  1007. onscreen.setColor(Color.lightGray);
  1008. switch (shape)
  1009. {
  1010. case LINE:
  1011. drawLine(onscreen, xStart, yStart, xStart, yStart);
  1012. break;
  1013. case OVAL:
  1014. drawOval(onscreen, xStart, yStart, 1, 1);
  1015. break;
  1016. case RECT:
  1017. drawRect(onscreen, xStart, yStart, 1, 1);
  1018. break;
  1019. }
  1020. }
  1021.  
  1022. public void mouseDragged(Point p, ScribbleCanvas canvas)
  1023. {
  1024. if (canvas.mouseButtonDown)
  1025. {
  1026. switch (shape)
  1027. {
  1028. case LINE:
  1029. drawLine(onscreen, xStart, yStart, canvas.x, canvas.y);
  1030. drawLine(onscreen, xStart, yStart, p.x, p.y);
  1031. break;
  1032. case OVAL:
  1033. drawOval(onscreen, xStart, yStart, canvas.x - xStart + 1, canvas.y - yStart + 1);
  1034. drawOval(onscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
  1035. break;
  1036. case RECT:
  1037. drawRect(onscreen, xStart, yStart, canvas.x - xStart + 1, canvas.y - yStart + 1);
  1038. drawRect(onscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
  1039. break;
  1040. }
  1041. canvas.x = p.x;
  1042. canvas.y = p.y;
  1043. }
  1044. }
  1045.  
  1046. public void mouseReleased(Point p, ScribbleCanvas canvas)
  1047. {
  1048. canvas.mouseButtonDown = false;
  1049. onscreen.setPaintMode();
  1050. Graphics offscreen = canvas.getOffScreenGraphics();
  1051. switch (shape)
  1052. {
  1053. case LINE:
  1054. drawLine(offscreen, xStart, yStart, p.x, p.y);
  1055. break;
  1056. case OVAL:
  1057. drawOval(offscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
  1058. break;
  1059. case RECT:
  1060. drawRect(offscreen, xStart, yStart, p.x - xStart + 1, p.y - yStart + 1);
  1061. break;
  1062. }
  1063. canvas.repaint();
  1064. }
  1065.  
  1066. public static void drawLine(Graphics g, int x1, int y1, int x2, int y2)
  1067. {
  1068. g.drawLine(x1, y1, x2, y2);
  1069. }
  1070.  
  1071. public static void drawRect(Graphics g, int x, int y, int w, int h)
  1072. {
  1073. if (w < 0)
  1074. {
  1075. x = x + w;
  1076. w = -w;
  1077. }
  1078. if (h < 0)
  1079. {
  1080. y = y + h;
  1081. h = -h;
  1082. }
  1083. g.drawRect(x, y, w, h);
  1084. }
  1085.  
  1086. public static void drawOval(Graphics g, int x, int y, int w, int h)
  1087. {
  1088. if (w < 0)
  1089. {
  1090. x = x + w;
  1091. w = -w;
  1092. }
  1093. if (h < 0)
  1094. {
  1095. y = y + h;
  1096. h = -h;
  1097. }
  1098. g.drawOval(x, y, w, h);
  1099. }
  1100. }
Add Comment
Please, Sign In to add comment