import java.awt.*; import java.awt.event.*; /** * A class which shows a piece of a real line for the graphical display * and entry of axis distances. * * @author Jonathan A. Poritz * @version 1.7, 17 Sep 1998 */ public class DistLinePanel extends Plane { /** * A reference to the containing inputpanel. * * @see FDBApplet#inputpanel */ public InputPanel inputpanel; /** * The location of a mousedown in this plane, to compute the extent of * a drag that may occur; in pixel coordinates. */ protected int[] previ = new int[2]; /** * The abstract x-coordinate of the left side of the axis distance display. */ public static final double CX = -1D; /** * The abstract y-coordinate of the upper edge of the axis distance display. */ public static final double CY = 5D; /** * The width, in abstract units, of the axis distance display. */ public static final double SX = 2D; /** * The height, in abstract units, of the axis distance display. */ public static final double SY = 5D; /** * The width, in pixels, of the axis distance display. Good defaults are: * */ public static final int X = 50; /** * The height, in pixels, of the axis distance display. Good defaults are: * */ public static final int Y = 300; /** * Constructor for DistLinePanels. * * @param nptpnl the ambient inputpanel */ public DistLinePanel(InputPanel nptpnl) { // make the basic plane with axes but no xticks super(CX, CY, SX, SY, X, Y); draw_axes = true; show_xticks = false; // save a reference to the inputpanel inputpanel = nptpnl; // deal with mouse presses this.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { // get the pixel coordinates of the press int x = e.getX(); int y = e.getY(); // convert to abstract coordinates double xx = (pixelsToAbstract(x, y))[0]; double yy = (pixelsToAbstract(x, y))[1]; // axis distances are non-negative, so if (yy < 0D) yy = 0D; if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) { // right button: zoom, so save mouse down location previ[0] = x; previ[1] = y; } else if ((yy != 0D) || (inputpanel.trace.y != 0D) || (Math.abs(inputpanel.trace.x) >= 2D)) { // left or middle button AD selection: only make the change if // allowed, i.e., no change of AD if the trace is elliptic or // parabolic and the new selected AD is 0 inputpanel.traceplanepanel.decomposed = false; inputpanel.traceplanepanel.draw_axes= true; inputpanel.traceplanepanel.repaint(); inputpanel.dist = yy; inputpanel.update(); repaint(); } } }); // deal with mouse drags this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { // get the current pixel coordinates int x = e.getX(); int y = e.getY(); // convert to abstract coordinates double xx = (pixelsToAbstract(x, y))[0]; double yy = (pixelsToAbstract(x, y))[1]; // never go negative if (yy < 0D) yy = 0D; if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0) { // right button zoom by amount proportional to the distance from // previous location zoom(1D - ((double)(previ[1] - y)) / 50D); } else if ((yy != 0D) || (inputpanel.trace.y != 0D) || (Math.abs(inputpanel.trace.x) >= 2D)) { // left or middle button AD selection: only make the change if // allowed, i.e., no change of AD if the trace is elliptic or // parabolic and the new selected AD is 0 inputpanel.traceplanepanel.decomposed = false; inputpanel.traceplanepanel.draw_axes = true; inputpanel.traceplanepanel.repaint(); inputpanel.dist = yy; inputpanel.update(); repaint(); } previ[0] = x; previ[1] = y; } }); } /** * Paint the DistLinePanel, by painting the parent Plane, putting a mark * at the currently selected axis distance and making tickmarks. * * @param g the Graphics object upon which to paint the DistLinePanel */ public void paint(Graphics g) { // paint the parent Plane super.paint(g); // draw a small disk on the site of the axis distance g.setColor(Color.black); int[] tp = abstractToPixels(0, inputpanel.dist); g.fillOval(tp[0] - 2, tp[1] - 2, 4, 4); // draw the tickmarks on the y-axis drawTickmarks(g); } /** * Scales the size of the visible window onto the line of axis distances * * @param factor the scaling factor, can be any positive number */ public void zoom(double factor) { corner[0] = corner[0] + .5D * size[0] - .5D * size[0] * factor; size[0] *= factor; corner[1] = corner[1] - size[1] + size[1] * factor; size[1] *= factor; repaint(); } }