/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: DiagramEditor.java ** PURPOSE: an instance of the graph editor framework ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ import java.applet.*; import java.util.*; import java.awt.Graphics; import practicaloo.*; class CircleNode extends Node { public void plot(Graphics gc) { gc.drawOval(center().get_x() - RADIUS, center().get_y() - RADIUS, 2 * RADIUS, 2 * RADIUS); } public Rectangle enclosing_rect() { Point c = (Point)center().clone(); Point d = (Point)center().clone(); c.move(-RADIUS - OFFSET, -RADIUS - OFFSET); d.move(RADIUS + OFFSET, RADIUS + OFFSET); return new Rectangle(c, d); } public boolean is_inside(Point p) { Ellipse c = new Ellipse(center(), RADIUS, RADIUS); return c.is_inside(p); } public Point boundary_point(Point exterior) { Point r = (Point)center().clone(); double a = r.angle(exterior); r.move((int)(RADIUS * Math.cos(a)), (int)(RADIUS * Math.sin(a))); return r; } private static int RADIUS = 25; private static int OFFSET = 1; } class DotNode extends Node { public void plot(Graphics gc) { gc.fillOval(center().get_x() - RADIUS, center().get_y() - RADIUS, 2 * RADIUS, 2 * RADIUS); } public Rectangle enclosing_rect() { Point c = (Point)center().clone(); Point d = (Point)center().clone(); c.move(-RADIUS - OFFSET, -RADIUS - OFFSET); d.move(RADIUS + OFFSET, RADIUS + OFFSET); Rectangle r = new Rectangle(c, d); return r; } public boolean is_inside(Point p) { Ellipse c = new Ellipse(center(), RADIUS, RADIUS); return c.is_inside(p); } public Point boundary_point(Point exterior) { Point r = (Point)center().clone(); double a = r.angle(exterior); r.move((int)(RADIUS * Math.cos(a)), (int)(RADIUS * Math.sin(a))); return r; } private static int RADIUS = 25; private static int OFFSET = 1; } class Line extends Edge { public void plot(Graphics g, Point a, Point b) { Segment s = new Segment(a, b); s.plot(g); } public boolean is_on(Point p, Point a, Point b) { Segment s = new Segment(a, b); return s.distance(p) < 3; } } class Arrow extends Edge { public void plot(Graphics gc, Point a, Point b) { Segment s = new Segment(a, b); s.plot(gc); int dx = b.get_x() - a.get_x(); int dy = b.get_y() - a.get_y(); double angle = Math.atan2(dy, dx); int x1 = b.get_x() - (int)(ARROW_LEN * Math.cos(angle - Math.PI / 6)); int y1 = b.get_y() - (int)(ARROW_LEN * Math.sin(angle - Math.PI / 6)); int x2 = b.get_x() - (int)(ARROW_LEN * Math.cos(angle + Math.PI / 6)); int y2 = b.get_y() - (int)(ARROW_LEN * Math.sin(angle + Math.PI / 6)); Segment r1 = new Segment(b, new Point(x1, y1)); r1.plot(gc); Segment r2 = new Segment(b, new Point(x2, y2)); r2.plot(gc); } public boolean is_on(Point p, Point a, Point b) { Segment s = new Segment(a, b); return s.distance(p) < 3; } private static int ARROW_LEN = 8; } public class DiagramEditor extends GraphEditor { public String[] get_types() { String[] types = { "CircleNode", "DotNode", "Line", "Arrow" }; return types; } }