📄 plotpanel.java
字号:
// $Id: plotpanel.java,v 1.7 2003/10/07 21:46:02 idgay Exp $/** * Copyright(C) 2002 by Tom Pycke <Tom.Pycke@advalvas.be> * */package net.tinyos.plot;import net.tinyos.sim.TinyViz;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import java.lang.*;import java.util.*;public class plotpanel extends JPanel { /* BORDER_TY * --------------- * | | * BORDER_LX | | BORDER_RX * --------------- * BORDER_BY */ private int BORDER_LX = 40;// private int BORDER_RX = 23; private int BORDER_RX = 120; private int BORDER_TY = 19; private int BORDER_BY = 15; private final static RenderingHints AALIAS = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); private boolean antiAliasing; private boolean fitToScreen; private ArrayList functionList; private ArrayList colorList; private ArrayList descriptionList; private ArrayList completeFunctionList; private ArrayList completeColorList; private ArrayList completeDescriptionList; private double maxX, maxY, minX, minY; private int screenWidth, screenHeight; private double maxWidth, maxHeight; private double scale = 1.0; private int cursorX, cursorY; private Label cursorPosition; private Color bgColor = Color.white; private boolean printDescription = false; private int fontHeight; private TinyViz tv; public void setTv(TinyViz tv) { this.tv = tv; } private JComboBox cb; //the combo box in the control panel public void setCb(JComboBox cb) { this.cb = cb; } public void init(){ functionList = new ArrayList(); colorList = new ArrayList(); descriptionList = new ArrayList(); completeFunctionList = new ArrayList(); completeColorList = new ArrayList(); completeDescriptionList = new ArrayList(); if(cb!=null) cb.removeAllItems(); maxX = maxY = 5.0; minX = minY = -5.0; this.setDoubleBuffered(true); } /** * Constructor */ public plotpanel () { init(); updateData(); setLayout(null); cursorPosition = new Label(""); add(cursorPosition); cursorPosition.setLocation(BORDER_LX, 0); cursorPosition.setVisible(false); cursorPosition.setSize (200,18); cursorPosition.setAlignment (Label.LEFT); addMouseListener ( new MouseAdapter() { public void mouseClicked(MouseEvent e) { double widthX = maxX - minX; double widthY = maxY - minY; if (cursorX > BORDER_LX && cursorX < screenWidth + BORDER_LX && cursorY > BORDER_TY && cursorY < screenHeight + BORDER_TY) { maxX += (Math.round((e.getX() - BORDER_LX)*100.0)/100.0 - screenWidth/2) * (widthX) / screenWidth; maxY += (Math.round((-e.getY() + BORDER_TY)*100.0)/100.0 + screenHeight/2) * (widthY) / screenHeight; minX += (Math.round((e.getX() - BORDER_LX)*100.0)/100.0 - screenWidth/2) * (widthX) / screenWidth; minY += (Math.round((-e.getY() + BORDER_TY)*100.0)/100.0 + screenHeight/2) * (widthY) / screenHeight; } repaint(); } }); addMouseMotionListener ( new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { cursorX = e.getX(); cursorY = e.getY(); if (cursorX > BORDER_LX && cursorX < screenWidth + BORDER_LX && cursorY > BORDER_TY && cursorY < screenHeight + BORDER_TY) { setCursor (new Cursor(Cursor.CROSSHAIR_CURSOR)); cursorPosition.setText("(" + double2String(screenToWorldX(cursorX)) + ", " + double2String(screenToWorldY(cursorY)) + ")"); cursorPosition.setVisible(true); } else { cursorPosition.setVisible(false); cursorPosition.setText(""); setCursor (new Cursor(Cursor.DEFAULT_CURSOR)); } } }); } /** * Adds a function to the functionlist. * * This appends Function <code>f</code> with Color <code>c</code> to * the to-plot list. * * @param f An implementation of <code>Function</code> * @param c A <code>Color</code> */ public void addFunction (Function f, Color c) { addFunction(f, c, new String("")); } /** * Adds a function to the functionlist. * * This appends Function <code>f</code> with Color <code>c</code> to * the to-plot list. * * @param f An implementation of <code>Function</code> * @param c A <code>Color</code> * @param s The function's description */ public void addFunction (Function f, Color c, String s) { if(!completeDescriptionList.contains(s)){ functionList.add (f); colorList.add (c); descriptionList.add (s); completeFunctionList.add (f); completeColorList.add (c); completeDescriptionList.add (s); if(s.length()>0) cb.addItem(s); } } /** * Removes a function from the functionlist. * * This removes a Function <code>s</code> from * the to-plot list. * * @param s The function's description */ public void removeFunction (String s) { int index = descriptionList.indexOf(s); if(index!=-1){ functionList.remove(index); colorList.remove(index); descriptionList.remove(index); } repaint(); } /** * Gets a function from the completefunctionlist. * * @param s The function's description */ public Function getFunction (String s) { int index = completeDescriptionList.indexOf(s); if(index!=-1){ return (Function)completeFunctionList.get(index); } else return null; } /** * Replaces a function on the functionlist. * * This copies a Function <code>s</code> from * the in-waiting list to the to-plot list. * * @param s The function's description */ public void replaceFunction (String s) { int index = completeDescriptionList.indexOf(s); if(index!=-1 && !descriptionList.contains(s)){ functionList.add(completeFunctionList.get(index)); colorList.add(completeColorList.get(index)); descriptionList.add(completeDescriptionList.get(index)); } repaint(); } /** * Removes the last function added from the function, color and description list */ public void removeLast() { if (descriptionList.size() > 0) { descriptionList.remove (functionList.size() - 1); colorList.remove (functionList.size() - 1); functionList.remove (functionList.size() - 1); } repaint(); } /** * Returns the minimum x-value (world) of the plot's view. * * @return The lowest x-value shown on the plot (in world's coordinates) */ public double getMinX () { return minX; } /** * Returns the maximum x-value (world) of the plot's view. * * @return The highest x-value shown on the plot (in world's coordinates) */ public double getMaxX () { return maxX; } /** * Returns the minimum y-value (world) of the plot's view. * * @return The lowest y-value shown on the plot (in world's coordinates) */ public double getMinY () { return minY; } /** * Returns the maximum y-value (world) of the plot's view. * * @return The highest y-value shown on the plot (in world's coordinates) */ public double getMaxY () { return maxY; } /** * Sets the maximum x-value of the plot's view. * * @param maxX The new maximum x-value of the plot's view. */ public void setMaxX (double maxX) { this.maxX = maxX; } /** * Sets the minimum x-value of the plot's view. * * @param minX The new minimum x-value of the plot's view. */ public void setMinX (double minX) { this.minX = minX; } /** * Sets the maximum y-value of the plot's view. * * @param maxY The new maximum y-value of the plot's view. */ public void setMaxY (double maxY) { this.maxY = maxY; } /** * Sets the minimum y-value of the plot's view. * * @param minY The new minimum y-value of the plot's view. */ public void setMinY (double minY) { this.minY = minY; } /** * Fits the graph to the screen using data from the EmpiricalFunction objects, if any. * */ public void FitToScreen() { boolean initialized=false; Iterator func = functionList.iterator(); while(func.hasNext()){ Function f=(Function)func.next(); if(f instanceof EmpiricalFunction){ EmpiricalFunction ef = (EmpiricalFunction)f; Enumeration e = ef.points.elements(); while(e.hasMoreElements()){ if(!initialized){ maxX=Double.MIN_VALUE; maxY=Double.MIN_VALUE; minX=Double.MAX_VALUE; minY=Double.MAX_VALUE; initialized=true; } EmpiricalFunction.PlotPoint p = (EmpiricalFunction.PlotPoint)e.nextElement(); if(p.x>maxX) maxX=p.x; if(p.x<minX) minX=p.x; if(p.y>maxY) maxY=p.y; if(p.y<minY) minY=p.y; } } } if(initialized){ if(maxX==minX) maxX++; if(maxY==minY) maxY++; minX=minX-Math.ceil(0.02*(maxX-minX)); maxX=maxX+Math.ceil(0.02*(maxX-minX)); minY=minY-Math.ceil(0.02*(maxY-minY));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -