📄 plotbox.java
字号:
/* A labeled box for signal plots.@Author: Edward A. Lee and Christopher Hylands@Contributors: William Wu@Version: @(#)PlotBox.java 1.86 05/13/98@Copyright (c) 1997-1998 The Regents of the University of California.All rights reserved.Permission is hereby granted, without written agreement and withoutlicense or royalty fees, to use, copy, modify, and distribute thissoftware and its documentation for any purpose, provided that theabove copyright notice and the following two paragraphs appear in allcopies of this software.IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTYFOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGESARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IFTHE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OFSUCH DAMAGE.THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OFMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWAREPROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OFCALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY*/package DeApp1.ptplot;import java.awt.*;import java.io.*;import java.net.*;import java.util.*;//import java.lang.*; //already imported by ptplot////////////////////////////////////////////////////////////////////////////// PlotBox/** * Construct a labeled box within which to place a data plot. A title, * X and Y axis labels, tick marks, and a legend are all supported. * Zooming in and out is supported. To zoom in, drag the mouse * downwards to draw a box. To zoom out, drag the mouse upward. * Zooming out stops automatically at the point where the data fills * the drawing rectangle. * * The box can be configured either through a file with commands or * through direct invocation of the public methods of the class. * If a file is used, the file can be given as a URL through the * <code>setDataurl</code> method. The file contains any number * commands, one per line. Unrecognized commands and commands with * syntax errors are ignored. Comments are denoted by a line starting * with a pound sign "#". The recognized commands include: * <pre> * TitleText: <i>string</i> * XLabel: <i>string</i> * YLabel: <i>string</i> * </pre> * These commands provide a title and labels for the X (horizontal) and Y * (vertical) axes. * A <i>string</i> is simply a sequence of characters, possibly * including spaces. There is no need here to surround them with * quotation marks, and in fact, if you do, the quotation marks will * be included in the labels. * <p> * The ranges of the X and Y axes can be optionally given by commands like: * <pre> * XRange: <i>min</i>, <i>max</i> * YRange: <i>min</i>, <i>max</i> * </pre> * The arguments <i>min</i> and <i>max</i> are numbers, possibly * including a sign and a decimal point. If they are not specified, * then the ranges are computed automatically from the data and padded * slightly so that datapoints are not plotted on the axes. * <p> * The tick marks for the axes are usually computed automatically from * the ranges. Every attempt is made to choose reasonable positions * for the tick marks regardless of the data ranges (powers of * ten multiplied by 1, 2, or 5 are used). However, they can also be * specified explicitly using commands like: * <pre> * XTicks: <i>label position, label position, ...</i> * YTicks: <i>label position, label position, ...</i> * </pre> * A <i>label</i> is a string that must be surrounded by quotation * marks if it contains any spaces. A <i>position</i> is a number * giving the location of the tick mark along the axis. For example, * a horizontal axis for a frequency domain plot might have tick marks * as follows: * <pre> * XTicks: -PI -3.14159, -PI/2 -1.570795, 0 0, PI/2 1.570795, PI 3.14159 * </pre> * Tick marks could also denote years, months, days of the week, etc. * <p> * The X and Y axes can use a logarithmic scale with the following commands: * <pre> * XLog: on * YLog: on * </pre> * The grid labels represent powers of 10. Note that if a logarithmic * scale is used, then the values must be positive. Non-positive values * will be silently dropped. * <p> * By default, tick marks are connected by a light grey background grid. * This grid can be turned off with the following command: * <pre> * Grid: off * </pre> * It can be turned back on with * <pre> * Grid: on * </pre> * Also, by default, the first ten data sets are shown each in a unique color. * The use of color can be turned off with the command: * <pre> * Color: off * </pre> * It can be turned back on with * <pre> * Color: on * </pre> * All of the above commands can also be invoked directly by calling the * the corresponding public methods from some Java procedure. * * @author Edward A. Lee, Christopher Hylands * @version @(#)PlotBox.java 1.86 05/13/98 */public class PlotBox extends Panel { ////////////////////////////////////////////////////////////////////////// //// public methods //// /** * Handle button presses to fill the plot. This rescales so that * the data that is currently plotted just fits. * @deprecated As of JDK1.1 in java.awt.component * but we need to compile under 1.0.2 for netscape3.x compatibility. */
public boolean action (Event evt, Object arg) { if (evt.target == _fillButton) { _fillPlot(_graphics); return true; } else {
return super.action (evt, arg); // action() is deprecated in 1.1 // but we need to compile under // jdk1.0.2 for netscape3.x } } /** * Create the peer of the plot box. */ public void addNotify() { // This method is called after our Panel is first created // but before it is actually displayed. super.addNotify(); _measureFonts(); } /** * Add a legend (displayed at the upper right) for the specified * data set with the specified string. Short strings generally * fit better than long strings. You must call <code>init()</code> * before calling this method. */ public void addLegend(int dataset, String legend) { if (_debug > 8) System.out.println("PlotBox addLegend: " + dataset + " " + legend); _legendStrings.addElement(legend); _legendDatasets.addElement(new Integer(dataset)); } /** * Specify a tick mark for the X axis. The label given is placed * on the axis at the position given by <i>position</i>. If this * is called once or more, automatic generation of tick marks is * disabled. The tick mark will appear only if it is within the X * range. */ public void addXTick (String label, double position) { if (_xticks == null) { _xticks = new Vector(); _xticklabels = new Vector(); } _xticks.addElement(new Double(position)); _xticklabels.addElement(label); } /** * Specify a tick mark for the Y axis. The label given is placed * on the axis at the position given by <i>position</i>. If this * is called once or more, automatic generation of tick marks is * disabled. The tick mark will appear only if it is within the Y * range. */ public void addYTick (String label, double position) { if (_yticks == null) { _yticks = new Vector(); _yticklabels = new Vector(); } _yticks.addElement(new Double(position)); _yticklabels.addElement(label); } /** * Draw the axes using the current range, label, and title information. * If the argument is true, clear the display before redrawing. */ public synchronized void drawPlot(Graphics graphics, boolean clearfirst) { if (_debug > 7) System.out.println("PlotBox: drawPlot"+graphics+" "+clearfirst); if (graphics == null) { System.out.println("Attempt to draw axes without "+ "a Graphics object."); return; } super.paint(graphics); // Give other threads a chance, so that hopefully things are // up to date. Thread.yield(); // Find the width and height of the total drawing area, and clear it. Rectangle drawRect = bounds(); // FIXME: bounds() is deprecated // in JDK1.1, but we need to compile under 1.0.2 for // netscape3.x compatibility. graphics.setPaintMode(); if (clearfirst) { // Clear all the way from the top so that we erase the title. // If we don't do this, then zooming in with the pxgraph // application ends up blurring the title. graphics.clearRect(0,0,drawRect.width, drawRect.height); } if (_debug > 8) { System.out.println("PlotBox: drawPlot drawRect ="+ drawRect.width+" "+drawRect.height+" "+ drawRect.x+" "+drawRect.y); graphics.drawRect(0,0,drawRect.width, drawRect.height); } // If an error message has been set, display it and return. if (_errorMsg != null) { int fheight = _labelFontMetrics.getHeight() + 2; int msgy = fheight; graphics.setColor(Color.black); for(int i=0; i<_errorMsg.length;i++) { graphics.drawString(_errorMsg[i],10, msgy); msgy += fheight; System.err.println(_errorMsg[i]); } return; } // Make sure we have an x and y range if (!_xRangeGiven) { if (_xBottom > _xTop) { // have nothing to go on. _setXRange(0,0); } else { _setXRange(_xBottom, _xTop); } } if (!_yRangeGiven) { if (_yBottom > _yTop) { // have nothing to go on. _setYRange(0,0); } else { _setYRange(_yBottom, _yTop); } } // Vertical space for title, if appropriate. // NOTE: We assume a one-line title. int titley = 0; int titlefontheight = _titleFontMetrics.getHeight(); if (_title != null || _yExp != 0) { titley = titlefontheight + _topPadding; } // Number of vertical tick marks depends on the height of the font // for labeling ticks and the height of the window. graphics.setFont(_labelfont); int labelheight = _labelFontMetrics.getHeight(); int halflabelheight = labelheight/2; // Draw scaling annotation for x axis. // NOTE: 5 pixel padding on bottom. int ySPos = drawRect.height - 5; int xSPos = drawRect.width - _rightPadding; if (_xlog) _xExp = (int)Math.floor(_xtickMin); if (_xExp != 0 && _xticks == null) { String superscript = Integer.toString(_xExp); xSPos -= _superscriptFontMetrics.stringWidth(superscript); graphics.setFont(_superscriptfont); if (!_xlog) { graphics.drawString(superscript,xSPos,ySPos - halflabelheight); xSPos -= _labelFontMetrics.stringWidth("x10"); graphics.setFont(_labelfont); graphics.drawString("x10", xSPos, ySPos); } // NOTE: 5 pixel padding on bottom _bottomPadding = (3 * labelheight)/2 + 5; } // NOTE: 5 pixel padding on the bottom. if (_xlabel != null && _bottomPadding < labelheight + 5) { _bottomPadding = titlefontheight + 5; } // Compute the space needed around the plot, starting with vertical. // NOTE: padding of 5 pixels below title. _uly = titley + 5; // NOTE: 3 pixels above bottom labels. _lry = drawRect.height-labelheight-_bottomPadding-3; int height = _lry-_uly; _yscale = height/(_yMax - _yMin); _ytickscale = height/(_ytickMax - _ytickMin); ///////////////////// vertical axis // Number of y tick marks. // NOTE: subjective spacing factor. int ny = 2 + height/(labelheight+10); // Compute y increment. double yStep=_roundUp((_ytickMax-_ytickMin)/(double)ny);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -