⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queuedrawer.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		g2d.setPaint(border);
		g2d.draw(moreStatiA);	
		g2d.draw(moreStatiRect);
		drawCenteredText("" + occ, readColor(filledc), null, x + ELEM_WIDTH, y + ELEM_HEIGHT / 2.0, g2d, false, true);
		drawCenteredText("X" + mult, filledc, null, x + ELEM_WIDTH, y - ELEM_HEIGHT / 2.0, g2d, false, true);
	}
	
	private void drawLostJobs(Color filledc, Color emptyc, Color border, boolean gradientFill, Graphics2D g2d){
		double x = START_GAP,
			   y = getElementXY(0).y;
		moreStatiRect = new Rectangle2D.Double(x, y, ELEM_WIDTH * 2.0, ELEM_HEIGHT);
		Area moreStatiA = new Area(moreStatiRect);
		int occ = lostJobs;
		int mult = 1;
		while(occ > LOST_JOBS_MAX){
			 occ = (int) (occ / 10);
			 mult = mult * 10;
		}
		moreStatiA.subtract(
			new Area(
				new Rectangle2D.Double(
					x,
					y,
					ELEM_WIDTH * 2 - ELEM_WIDTH * occ * 2/ LOST_JOBS_MAX,
					ELEM_HEIGHT)));
		if (gradientFill){
			GradientPaint gp;
			gp = new GradientPaint(
				(float)x, 
				(float)y, 
				emptyc.brighter(), 
				(float)x, 
				(float)(y + ELEM_HEIGHT), 
				emptyc.darker(),
				false);
			g2d.setPaint(gp);
			g2d.fill(moreStatiRect);
			gp = new GradientPaint(
				(float)x, 
				(float)y, 
				filledc.brighter(), 
				(float)x, 
				(float)(y + ELEM_HEIGHT), 
				filledc.darker(),
				false);
			g2d.setPaint(gp);
			g2d.fill(moreStatiA);

		}
		else{
			g2d.setPaint(filledc);
			g2d.fill(moreStatiA);
			g2d.setPaint(emptyc);
			g2d.fill(moreStatiRect);
		}

		g2d.setPaint(border);
		g2d.draw(moreStatiA);	
		g2d.draw(moreStatiRect);
		drawCenteredText("" + occ, readColor(filledc), null, x + ELEM_WIDTH, y + ELEM_HEIGHT / 2.0, g2d, false, true);
		drawCenteredText("X" + mult, filledc, null, x + ELEM_WIDTH, y - ELEM_HEIGHT / 2.0, g2d, false, true);
	}
	
	private void drawProcessor(Color startC, Color border, boolean gradientFill, Graphics2D g2d){
		double x = getProcessorXY().x,
			   y = getProcessorXY().y;
		processor.setFrame(x, y, 2 * PROC_RAD, 2 * PROC_RAD);
		if (gradientFill){
			GradientPaint gp = new GradientPaint((float)x, (float)y, startC.brighter(), (float)x, (float)(y + PROC_RAD * 2), startC.darker(),false);
			g2d.setPaint(gp);
		}
		else{
			g2d.setPaint(startC);
		}
		g2d.fill(processor);
		g2d.setPaint(border);
		g2d.draw(processor);
	}
	
	private void drawUtilization(double U, Color startC, Color border, boolean gradientFill, Graphics2D g2d) {

			double x = getProcessorXY().x,
				   y = getProcessorXY().y;
			try{
				occupiedRect = new Rectangle2D.Double(
						x,
						y,
						2 * PROC_RAD,
						2 * PROC_RAD * (1 - U));
			}
			catch (Exception e) {
				occupiedRect = new Rectangle2D.Double(
						x,
						y,
						2 * PROC_RAD,
						0.0);			
			}
			occupiedEll = new Ellipse2D.Double(x, y, 2 * PROC_RAD, 2 * PROC_RAD);
			if (gradientFill) {
				GradientPaint gp = new GradientPaint((float) x, (float) y, startC.brighter(), 
					(float) x, (float) (y + 2 * PROC_RAD), startC.darker(), false);
				g2d.setPaint(gp);
			} else {
				g2d.setPaint(startC);
			}
			occupiedArea = new Area(occupiedEll);
			occupiedArea.subtract(new Area(occupiedRect));
			g2d.fill(occupiedArea);
			g2d.setPaint(Color.BLACK);
			g2d.draw(occupiedArea);
			
//			//draw informations about processes
//			txtBounds = drawCenteredText("job n.:" + donejobs, Color.BLACK, x + PROC_RAD,y + PROC_RAD * 2 + 4 * ELEMS_GAP,g2d, false);
//			//draw orizontal line parallel to occupation
//			
//			//draw box around text
//			txtBounds.setFrame(
//					x + PROC_RAD - txtBounds.getWidth() / 2,
//					y + 2 * PROC_RAD + 4 * ELEMS_GAP - txtBounds.getHeight() / 2,
//					txtBounds.getWidth(),
//					txtBounds.getHeight());
//			
//			g2d.draw(txtBounds);
	}
	
	private void drawOccupiedPercentage( Color startC, Color border, boolean gradientFill, Graphics2D g2d) {

		if (remainingTime != 0) {
			double x = getProcessorXY().x,
				   y = getProcessorXY().y;
			occupiedRect =
				new Rectangle2D.Double(
					x,
					y,
					2 * PROC_RAD,
					2 * PROC_RAD * (1 - (double) remainingTime / (double) totTime));
			occupiedEll = new Ellipse2D.Double(x, y, 2 * PROC_RAD, 2 * PROC_RAD);
			if (gradientFill) {
				GradientPaint gp =
					new GradientPaint(
						(float) x,
						(float) y,
						startC.brighter(),
						(float) x,
						(float) (y + 2 * PROC_RAD),
						startC.darker(),
						false);
				g2d.setPaint(gp);
			} else {
				g2d.setPaint(startC);
			}
			occupiedArea = new Area(occupiedEll);
			occupiedArea.subtract(new Area(occupiedRect));
			g2d.fill(occupiedArea);
			g2d.setPaint(Color.BLACK);
			g2d.draw(occupiedArea);
			
			//draw orizontal line parallel to occupation
			Line2D.Double l =
				new Line2D.Double(
					x + PROC_RAD * 2 + ELEMS_GAP,
					y + PROC_RAD * 2 * (1 - (double) remainingTime / (double) totTime),
					x + PROC_RAD * 2 + 2 * ELEMS_GAP,
					y + PROC_RAD * 2 * (1 - (double) remainingTime / (double) totTime));
			g2d.draw(l);
			
			//draw vertical line
			l =	new Line2D.Double(
					x + PROC_RAD * 2 + 2 * ELEMS_GAP,
					y + PROC_RAD * 2 * (1 - (double) remainingTime / (double) totTime),
					x + PROC_RAD * 2 + 2 * ELEMS_GAP,
					y + 2 * PROC_RAD + 4 * ELEMS_GAP - txtBounds.getHeight()/2);
			g2d.draw(l);
			
			//draw horizontal line under text
			txtBounds.setFrame(
					x + PROC_RAD - txtBounds.getWidth() / 2,
					y + 2 * PROC_RAD + 4 * ELEMS_GAP - txtBounds.getHeight() / 2,
					txtBounds.getWidth(),
					txtBounds.getHeight());
			
			g2d.draw(txtBounds);
		}										
	}

	/**
	 * 
	 * @param s
	 * @param c
	 * @param centerX
	 * @param centerY
	 */
	private Rectangle2D drawCenteredText(String s, Color fg, Color bg, double centerX, double centerY, Graphics2D g2d, boolean drawBorder, boolean draw){
		double x, y;
		Color ctmp = g2d.getColor();
		Rectangle2D bordersR = new Rectangle2D.Float();
		g2d.setFont(f);
		txtBounds = f.getStringBounds(s, g2d.getFontRenderContext());
		x = centerX - txtBounds.getWidth()/2.0;
		y = centerY - txtBounds.getY() - txtBounds.getHeight()/2; 
		txtBounds.setRect(
			x - ELEMS_GAP,
			y - txtBounds.getHeight() / 2.0 - ELEMS_GAP,
			txtBounds.getWidth() + 2 * ELEMS_GAP,
			txtBounds.getHeight() + 2 * ELEMS_GAP);
		
		if(draw){
			if (drawBorder) {
				bordersR.setFrame(
					centerX - txtBounds.getWidth()/2.0,
					centerY - txtBounds.getHeight()/2.0,
					txtBounds.getWidth(),
					txtBounds.getHeight());
				g2d.setColor(bg);
				g2d.fill(bordersR);
				g2d.setColor(fg);
				g2d.draw(bordersR);
			}
			g2d.setColor(fg);
			g2d.drawString(s, (float)x,(float)y);

		}
		g2d.setColor(ctmp);
		return txtBounds;
	}

	private Color invertedColor(Color c){
		int invertColor = 255;		
		return new Color(invertColor - c.getRed(), invertColor - c.getGreen(), invertColor - c.getBlue());
		
	}
	
	private Color readColor(Color c){
		Color rc;
		int r = 0,
			g = 0,
			b = 0;
		if(c.getBlue() < 128){
			b = 255;
		}
		if(c.getGreen() < 128){
			g = 255;
		}
		if(c.getRed() < 128){
			r = 255;
		}
		rc = new Color(r,g,b);
		return rc; 		
	}
	
	private int queueLenght(){
		int a = (int) ((panelW - (PROC_RAD * 2 + ELEMS_GAP) - 4 * ELEM_WIDTH
						- 4 * ELEMS_GAP - START_GAP - END_GAP)/(ELEM_WIDTH + ELEMS_GAP));
		if ((queueMax > 0) && (queueMax < a))
			return (int) queueMax;
		return a;

	}
	
	/**
	 * Restituisce l'ascissa dell'angolo superiore sinistro dell'elemento 'elem'
	 * @param elem
	 * @return
	 */
	private Point2D.Double getElementXY(int elem){
		return new Point2D.Double(START_GAP + ELEMS_GAP + (ELEMS_GAP + ELEM_WIDTH * 2) * 2 + (ELEMS_GAP + ELEM_WIDTH) * elem,
									panelH * 0.4 - ELEM_HEIGHT / 2.0);
									//(panelH - ELEM_HEIGHT) / 2.0);
	}
	
	private Point2D.Double getProcessorXY(){
		return new Point2D.Double(getElementXY(queueLenght()).x, 
						panelH * 0.4 - PROC_RAD);
	}
	
	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 java.lang.Runnable#run()
	 */
	public void run() {
		long currentTime = System.currentTimeMillis();
		this.setVisible(true);
		while(true){
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
				currentTime = System.currentTimeMillis();
				if ((currentTime < runTime)) {
					remainingTime = runTime - currentTime;
				} else {
					remainingTime = 0;
					runTime = 0;
				}
				this.repaint();
		}
		
	}
	
	public void setColors(Color emptyC, Color queueC, Color animC, boolean gradient){
		this.emptyC = emptyC;
		this.busyC = queueC.darker().darker();
		this.queueC = queueC;
		this.animC = animC;
		this.gradientF = gradient;
		this.repaint();
	}
	
	
	private void drawRemainingJobs(int donejobs, int totjobs, float x1, float y, Graphics2D g2d){
		if(totjobs != 0){
			float w = (float) ((ELEM_WIDTH  + ELEMS_GAP) * 8.0f);
			float x = x1 - w / 2.0f;
			float h = (float) ELEM_HEIGHT;
			float percent = (float)donejobs / (float)totjobs;
			Color col1 = Color.RED;
			Color col2 = Color.GREEN;//( 0.9f, 0.0f, 0.0f);
			Line2D jline, tjline;
			Rectangle2D jR = new Rectangle2D.Float((x + percent * w), y, w, h);
			GeneralPath tjpath =  new GeneralPath();
			
			//disegno triangolo jobs totali
			tjpath.moveTo(x, y);
			tjpath.lineTo(x + w, y + h);
			tjpath.lineTo(x, y + h);
			tjpath.closePath();
			GradientPaint gp = new GradientPaint(x, y, Color.GRAY, x + w, y, Color.LIGHT_GRAY,false);
			g2d.setPaint(gp);
			g2d.fill(tjpath);		
	
			//area dei job rimanenti
			Area a = new Area(tjpath);		
			a.intersect(new Area(jR));
			gp = new GradientPaint(x, y, col1, x + w, y, col2,false);
			g2d.setPaint(gp);		
			g2d.fill(a);
			g2d.setPaint(Color.BLACK);
			g2d.draw(tjpath);	
			
			//legenda:
			for (int i = 0; i < 3; i++){
				tjline = new Line2D.Float(x + w * i / 2, y + h, x + w * i / 2, y + h + h / 5);
				//g2d.draw(tjline);
				drawCenteredText("" + (int)(totjobs * ((double)i / 2.0)), Color.BLACK, null, x + w * i / 2, y + 1.2 * h, g2d, false, true);
			
			}
		}		
	}
	
	public void setTotalJobs(int totjobs){
		 this.totjobs = totjobs;
	}
	
	/**
	 * Disegna una legenda
	 * @param ca Colori della legenda
	 * @param sa Rispettivi commenti
	 * @param f font
	 * @param x ascissa iniziale
	 * @param y oridnata iniziale
	 * @param g2d
	 * @return larghezza pannello
	 */
	private double drawLegend(Color[] ca, String[] sa, Font f, double x, double y, Graphics2D g2d, boolean draw){
		Rectangle2D[] ra = new Rectangle2D[ca.length];
		Rectangle2D[] tba = new Rectangle2D[ca.length];
		double maxw = 0.0,
			   gap = 5.0;
		String ts = "Legenda";
		//backup
		Color ctmp = g2d.getColor();
		Font ftmp = g2d.getFont();
		
		
		g2d.setFont(f);
		Rectangle2D tr = f.getStringBounds(ts, g2d.getFontRenderContext());
		maxw = tr.getWidth();
		for(int i = 0; i < ca.length; i++){
			tba[i]= f.getStringBounds(sa[i], g2d.getFontRenderContext());
			ra[i] = new Rectangle2D.Double(
				x + gap, 
				y + gap + (tr.getHeight() + gap) * (i + 1), 
				tr.getHeight(), 
				tr.getHeight());
			if(draw){
				g2d.setColor(ca[i]);
				g2d.fill(ra[i]);
				g2d.setColor(Color.BLACK);
				g2d.draw(ra[i]);
				g2d.drawString(
					sa[i], 
					(float) (x + gap * 2 + tr.getHeight()),	
					(float) (y + gap + (tr.getHeight() + gap) * (i + 1) + tr.getHeight()/2.0 - tr.getY()/2.0));
			}
			if(maxw < tba[i].getWidth()) maxw = tba[i].getWidth();
		}
		if(draw){
			g2d.drawRect(
				(int) x, 
				(int) y, 
				(int) (maxw + 3.0 * gap + tr.getHeight()), 
				(int) (y + (tr.getHeight() + gap) * (ca.length + 1) + gap));
			g2d.drawRect(
				(int) x,
				(int) y,
				(int) (maxw + 3.0 * gap + tr.getHeight()),
				(int) (tr.getHeight() + gap));
			g2d.fillRect(
				(int) x,
				(int) y,
				(int) (maxw + 3.0 * gap + tr.getHeight()),
				(int) (tr.getHeight() + gap));
			g2d.setColor(Color.WHITE);
			g2d.drawString(
				ts, 
				(float) (x + gap + (maxw - tr.getWidth() + tr.getHeight())/2.0), 
				(float) (y + tr.getY()/2.0 + tr.getHeight()));
		}


	//restore
	g2d.setFont(ftmp);	
	g2d.setColor(ctmp);
	return (maxw + 3.0 * gap + tr.getHeight());

	}
	
	private void changeView(){
		view = ++view % VIEWS;
	}
	
	private RoundRectangle2D drawViewButton(double x, double y, Graphics2D g2d, boolean draw, boolean clicked){
		Rectangle2D dvR;
		RoundRectangle2D dvRR;
		Color tmp;
		Color fg = Color.LIGHT_GRAY;
		Color bg = Color.BLACK;
		
		if (!clicked){
			tmp = fg;
			fg = bg;
			bg = tmp;
		}
		dvR = drawCenteredText("change view", fg, bg, x, y, g2d, false, false);
		dvRR = new RoundRectangle2D.Double(
						x, 
						y,
						dvR.getWidth(),
						dvR.getHeight(), 
						5.0, 5.0);
		if(draw){
			tmp = g2d.getColor();
			g2d.setColor(bg);
			g2d.fill(dvRR);
			g2d.setColor(fg);
			g2d.draw(dvRR);
			drawCenteredText(
				"change view", 
				fg, bg, 
				x + dvR.getWidth() / 2.0, 
				y + dvR.getHeight() / 2.0, 
				g2d, false, true);
			g2d.setColor(tmp);
		}
		return dvRR;
	}
}

⌨️ 快捷键说明

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