📄 simmeter.java
字号:
/*
JaNetSim --- Java Network Simulator
-------------------------------------
This software was developed at the Network Research Lab, Faculty of
Computer Science and Information Technology (FCSIT), University of Malaya.
This software may be used and distributed freely. FCSIT assumes no responsibility
whatsoever for its use by other parties, and makes no guarantees, expressed or
implied, about its quality, reliability, or any other characteristic.
We would appreciate acknowledgement if the software is used.
FCSIT ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION AND
DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
FROM THE USE OF THIS SOFTWARE.
*/
package janetsim;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimMeter implements java.io.Serializable {
private class DataPoint implements java.io.Serializable {
long tick;
double value;
DataPoint(long t,double v) { tick=t; value=v; }
}
private class MeterPanel extends JPanel implements ActionListener,MouseListener {
ActionListener hideListener=null;
MeterPanel() {
super();
addMouseListener(this);
}
void registerListener(ActionListener listener) {
hideListener=listener;
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)) {
JPopupMenu popup=new JPopupMenu();
JMenuItem menuItem;
menuItem=new JMenuItem("Cancel");
popup.add(menuItem);
popup.addSeparator();
menuItem=new JMenuItem("Reset");
menuItem.addActionListener(this);
popup.add(menuItem);
if(hideListener!=null) {
popup.addSeparator();
menuItem=new JMenuItem("Hide");
menuItem.addActionListener(hideListener);
popup.add(menuItem);
}
popup.show(this,e.getX(),e.getY());
}
}
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
if(cmd.equals("Reset")) {
reset();
repaint();
}
}
public Dimension getMinimumSize() {
return new Dimension(Sim.METER_MINWIDTH,Sim.METER_MINHEIGHT);
}
public Dimension getPreferredSize() {
return new Dimension(Sim.METER_MINWIDTH,Sim.METER_MINHEIGHT);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension panelsize=getSize();
if(datapoints.size()>1) {
DataPoint point=(DataPoint)datapoints.get(datapoints.size()-1);
DataPoint firstpoint=(DataPoint)datapoints.get(0);
if(point.tick!=firstpoint.tick) xScale=(double)panelsize.width/(point.tick-firstpoint.tick);
if(maxy!=miny) yScale=(double)panelsize.height/(maxy-miny);
xOffset=firstpoint.tick;
yOffset=miny;
}
g.setColor(Color.black);
for(int i=1;i<datapoints.size();i++) {
DataPoint lastpoint=(DataPoint)datapoints.get(i-1);
DataPoint thispoint=(DataPoint)datapoints.get(i);
g.drawLine((int)((lastpoint.tick-xOffset)*xScale),
(int)(panelsize.height-(lastpoint.value-yOffset)*yScale),
(int)((thispoint.tick-xOffset)*xScale),
(int)(panelsize.height-(thispoint.value-yOffset)*yScale));
}
if(datapoints.size()>1) {
g.setColor(Color.cyan);
g.drawLine(0,panelsize.height/2,panelsize.width,panelsize.height/2);
Font f=new Font("Helvetica",Font.PLAIN,Sim.DEFAULT_COMP_POINTSIZE);
FontMetrics fm=g.getFontMetrics(f);
g.setFont(f);
int bottomy=panelsize.height-fm.getHeight()+fm.getAscent();
int centery=panelsize.height/2-fm.getHeight()+fm.getAscent();
int uppery=fm.getAscent();
g.setColor(Color.white);
String str=String.valueOf(yOffset);
g.drawString(str,(panelsize.width-fm.stringWidth(str))/2,bottomy);
str=String.valueOf(yOffset+(maxy-miny)/2);
g.drawString(str,(panelsize.width-fm.stringWidth(str))/2,centery);
str=String.valueOf(maxy);
g.drawString(str,(panelsize.width-fm.stringWidth(str))/2,uppery);
g.setColor(new Color(30,120,30));
java.text.DecimalFormat df=new java.text.DecimalFormat("0.000");
long firsttick=((DataPoint)datapoints.get(0)).tick;
str=df.format(SimClock.Tick2MSec(firsttick))+"ms";
g.drawString(str,0,bottomy);
str=df.format(SimClock.Tick2MSec(lasttick))+"ms";
g.drawString(str,panelsize.width-fm.stringWidth(str),bottomy);
}
}
}
private double xScale;
private double yScale;
private double xOffset;
private double yOffset;
private double maxy;
private double miny;
private long lasttick;
private java.util.List datapoints;
private transient MeterPanel panel=null;
public SimMeter() {
datapoints=new java.util.ArrayList();
reset(); //must come after the above line!
}
public JPanel getMeterPanel(ActionListener listener) {
if(panel==null) {
panel=new MeterPanel();
}
panel.registerListener(listener);
return panel;
}
public void destroyMeterPanel() {
panel=null;
}
public synchronized void add(long tick,double value) {
if(tick<lasttick) reset();
lasttick=tick;
datapoints.add(new DataPoint(tick,value));
if(value>maxy) maxy=value;
if(value<miny) miny=value;
if(panel!=null) panel.repaint();
}
public synchronized void reset() {
datapoints.clear();
xScale = 1.0; yScale = 1.0;
xOffset = 0; yOffset = 0;
maxy=Double.MIN_VALUE;
miny=Double.MAX_VALUE;
lasttick=-1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -