You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FigurePanel.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package figures.gui;
  13. import figures.Point;
  14. import figures.Line;
  15. import figures.FigureElement;
  16. import figures.Group;
  17. import java.awt.*;
  18. import java.awt.geom.*;
  19. import java.awt.event.*;
  20. import java.io.*;
  21. import java.util.*;
  22. import javax.swing.*;
  23. import javax.swing.text.*;
  24. import javax.swing.border.*;
  25. public class FigurePanel extends JComponent {
  26. ButtonsPanel bp = new ButtonsPanel();
  27. FigureSurface fs = new FigureSurface();
  28. ConsolePanel cp = new ConsolePanel();
  29. public FigurePanel() {
  30. setLayout(new BorderLayout());
  31. JPanel panel = new JPanel();
  32. panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
  33. panel.add(fs);
  34. panel.add(bp);
  35. JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, cp);
  36. sp.setPreferredSize(new Dimension(500, 400));
  37. sp.setDividerLocation(250);
  38. add(BorderLayout.CENTER, sp);
  39. }
  40. class ButtonsPanel extends JPanel {
  41. JLabel msgs = new JLabel("click to add a point or line");
  42. public ButtonsPanel() {
  43. setLayout(new FlowLayout(FlowLayout.LEFT));
  44. add(new JButton(new AbstractAction("Main") {
  45. public void actionPerformed(ActionEvent e) {
  46. Main.main(new String[]{});
  47. fs.repaint();
  48. }
  49. }));
  50. add(msgs);
  51. }
  52. public void log(String msg) {
  53. msgs.setText(msg);
  54. }
  55. }
  56. static class ConsolePanel extends JPanel {
  57. JTextArea text = new JTextArea();
  58. public ConsolePanel() {
  59. super(new BorderLayout());
  60. text.setFont(StyleContext.getDefaultStyleContext().getFont("SansSerif", Font.PLAIN, 10));
  61. JScrollPane scroller = new JScrollPane(text);
  62. scroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  63. add(BorderLayout.CENTER, scroller);
  64. }
  65. public void println(String msg) {
  66. text.append(msg + '\n');
  67. }
  68. }
  69. final static Color BACKGROUND = Color.white;
  70. static class FigureSurface extends JPanel implements MouseListener, MouseMotionListener {
  71. Group canvas;
  72. public FigureSurface() {
  73. canvas = new Group(new Point(250, 250));
  74. addMouseMotionListener(this);
  75. addMouseListener(this);
  76. setPreferredSize(new Dimension(500,500));
  77. }
  78. private Point addPoint(int x, int y) {
  79. Point p = new Point(x, y);
  80. canvas.add(p);
  81. repaint();
  82. return p;
  83. }
  84. private Line addLine(Point p1, Point p2) {
  85. if (Math.abs(p1.getX()-p2.getX()) < 5 ||
  86. Math.abs(p1.getY()-p2.getY()) < 5) {
  87. return null;
  88. }
  89. Line line = null;
  90. if (p1 != null && p2 != null) {
  91. line = new Line(p1, p2);
  92. canvas.add(line);
  93. }
  94. repaint();
  95. return line;
  96. }
  97. public void paint(Graphics g) {
  98. Graphics2D g2 = (Graphics2D) g;
  99. g2.setPaint(BACKGROUND);
  100. g2.fill(new Rectangle2D.Float(0f, 0f, (float)g2.getClipBounds().width, (float)g2.getClipBounds().height));
  101. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  102. canvas.paint(g2);
  103. }
  104. int lastX, lastY;
  105. int pressX, pressY;
  106. FigureElement first = null;
  107. Point point1 = null;
  108. public void mousePressed(MouseEvent e){
  109. int x = e.getX(), y = e.getY();
  110. pressX = lastX = x; pressY = lastY = y;
  111. first = findFigureElement(x, y);
  112. if (first == null) {
  113. point1 = addPoint(x, y);
  114. }
  115. }
  116. public void mouseDragged(MouseEvent e) {
  117. int x = e.getX(), y = e.getY(), dx = lastX-x, dy = lastY-y;
  118. lastX = x;
  119. lastY = y;
  120. if (first == null) {
  121. Line line = addLine(point1, new Point(x, y));
  122. if (line != null) {
  123. canvas.add(line.getP2());
  124. first = line.getP2();
  125. canvas.add(line);
  126. }
  127. } else {
  128. first.move(-dx, -dy);
  129. }
  130. repaint();
  131. }
  132. public void mouseReleased(MouseEvent e){
  133. mouseDragged(e);
  134. first = null;
  135. point1 = null;
  136. }
  137. public void mouseMoved(MouseEvent e){}
  138. public void mouseClicked(MouseEvent e){}
  139. public void mouseExited(MouseEvent e){}
  140. public void mouseEntered(MouseEvent e){}
  141. private FigureElement findFigureElement(int x, int y) {
  142. Point2D p = new Point2D.Float((float)x, (float)y);
  143. for (Iterator i = canvas.members(); i.hasNext(); ) {
  144. FigureElement fe = (FigureElement)i.next();
  145. if (fe.contains(p)) return fe;
  146. }
  147. return null;
  148. }
  149. }
  150. }