statidrawer.java

来自「一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!」· Java 代码 · 共 279 行

JAVA
279
字号
/**    
  * Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
  
/*
 * Created on 18-mar-2004 by Ernesto
 *
 */
package jmt.jmarkov.Graphics;

import jmt.jmarkov.Graphics.constants.DrawConstrains;
import jmt.jmarkov.Graphics.constants.DrawNormal;
import jmt.jmarkov.Queues.Exceptions.NonErgodicException;
import jmt.jmarkov.Queues.QueueLogic;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

/**
 * MMQueues
 * --------------------------------------
 * 18-mar-2004 - Graphics/StatiDrawer.java
 * 
 * @author Ernesto
 */
public class StatiDrawer extends JPanel implements Notifier, Runnable {

	private int times, tmp;
	private boolean animate;
	//queue settings
	private QueueLogic ql;
	private double pb[];
	private int queueMax,
				currentJob,
				lastJob,
				jobTmp;


	//panel settings
	private double panelH = 250,
				   panelW = 400,
				   minH = 100,
				   minW = 400; 

	//draw settings
	private DrawConstrains dCst;
	private Rectangle2D txtBounds;
	private double ELEMS_GAP;
	private double STATUS_RAD;
	private double START_GAP;
	private double END_GAP;
	private Stroke stroke,
				   strokeB;
	private Font f;
	private Arc2D[] statusP;
	private Arc2D lastStatusP;
	private Ellipse2D[] statusE;
	private Ellipse2D lastStatusE, transitionE;
	private QuadCurve2D[][] arc;
	private GeneralPath arrow;
	private float arroww = 5.0f,
		  		  arrowh = 5.0f;
	private int frame, 
				maxframe;
	
	//colors
	private Color probC,
				  queueProbC,
				  emptyC,
				  queueC,
				  animQueuesC,
				  animProbC,
				  animBorderC,
				  borderC;

	/**
	 * 
	 */
	public StatiDrawer(QueueLogic ql) {
		super();
		this.ql = ql;
		init();
	}

	/**
	 * @param isDoubleBuffered
	 */
	public StatiDrawer(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
		init();
	}

	/**
	 * @param layout
	 */
	public StatiDrawer(LayoutManager layout) {
		super(layout);
		init();
	}

	/**
	 * @param layout
	 * @param isDoubleBuffered
	 */
	public StatiDrawer(LayoutManager layout, boolean isDoubleBuffered) {
		super(layout, isDoubleBuffered);
		init();
	}

	/**
	 * Inizializza i parametri della classe
	 *
	 */
	private void init(){
		probC = Color.green;
 		queueProbC = Color.green.brighter();
		queueC = Color.green.darker();
 		emptyC = Color.WHITE;
 		borderC = Color.BLACK;
 		animBorderC = Color.BLACK;
 		animQueuesC = Color.RED.darker();
 		animProbC = Color.RED.brighter();
 		maxframe = 5;
 		lastJob = 0;
 		currentJob = 0;
 		frame = 5;
		changeDrawSettings(new DrawNormal());
		updateQueueSettings();
		animate = false;
		this.reset();
	}
	
	public void changeDrawSettings(DrawConstrains dCst){
		this.dCst = dCst;
		
		resize();
		
		//assegno le costanti di disegno
		f = dCst.getFont();
		stroke = dCst.getDrawStroke();
		strokeB = dCst.getBoldStroke();
		START_GAP = dCst.getStartingGap();
		END_GAP = dCst.getStartingGap();
		STATUS_RAD = dCst.getStatusRadius();
		ELEMS_GAP = dCst.getElementsGap();		
	}
	
	private void updateQueueSettings(){
		//inizializzo la coda
		statusE = new Ellipse2D.Double[queueLenght() + 1];
		statusP = new Arc2D.Double[queueLenght() + 1];
		arc = new QuadCurve2D.Double[queueLenght() + 3][queueLenght() + 3];	 
	}
	
	private void resize(){
		int x = this.getWidth(),
			y = this.getHeight();
		if (y < minH)
			panelH = minH;
		else panelH = y;
		if (x < minW)
			panelW = minW;
		else panelW = x;
	}
	
	/* (non-Javadoc)
	 * @see Graphics.Notifier#runningIn(double)
	 */
	public void runningIn(double t) {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see Graphics.Notifier#addingToQ()
	 */
	public void addingToQ(double t) {
		currentJob++;
		frame = 0;
		animate = true;
		repaint();

	}

	/* (non-Javadoc)
	 * @see Graphics.Notifier#removingFromQ()
	 */
	public void removingFromQ() {
		frame = 0;
		currentJob--;
		animate = true;
		repaint();

	}

	/* (non-Javadoc)
	 * @see Graphics.Notifier#reset()
	 */
	public void reset() {
		currentJob = 0;
		lastJob = 0;
		animate = false;
		queueMax = 0;
		frame = 0;

	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		while(true){
			try {
				while((frame < maxframe) && (animate)){
					jobTmp = currentJob;
					if((jobTmp > lastJob+1) ||(jobTmp < lastJob-1)){
						animate = false;
						break;
					}
					repaint();
					frame++;
					Thread.sleep(1000/30);
				}
				lastJob = currentJob;
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		repaint();
		}

	}
	
	public void paint(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setStroke(stroke);
		g2d.setBackground(Color.white);
		g2d.clearRect(0,0,this.getWidth(),this.getHeight());
		changeDrawSettings(dCst);
		START_GAP = START_GAP * 2 + 
			drawLegend(
				new Color[]{probC, queueC, animQueuesC},
				new String[] {"probability", "queue", "current state"},
				dCst.getFont(),
				START_GAP,
				START_GAP,
				g2d);
		updateQueueSettings();
		pb = new double[queueLenght() + 1];
		for(int i = 0; i < queueLenght() + 1; i++){
			try{
				pb[i] = ql.getStatusProbability(i);
			}catch (NonErgodicException e) {
				pb[i] = 0.0;
			}
			if((queueLenght() < queueMax)||(queueMax == 0)){ //coda infinita o comunque maggiore dell'area visualizzabile
				//disegno il penultimo stato
				if(i == queueLenght() - 2){
					drawMoreStatus(g2d);
					drawArc(i, (i+1), false, "", g2d, borderC);
					drawArc((i+1), i, false, "", g2d, borderC);
				//disegno l'ultimo stato
				//lo stato 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?