statuspanel.java

来自「操作系统课程设计之内存分配模拟,采用首次适应算法实现(JAVA版)」· Java 代码 · 共 55 行

JAVA
55
字号
/**
 * StatusPanel类
 * 构建内存空间分配图
 * @author linpeitian
 */

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

public class StatusPanel extends JPanel{

	private int[] block = new int[128];
	MAllocator mallocator = null;
	
	public StatusPanel(){	}
	
	public StatusPanel(MAllocator mallocator){
		this.mallocator = mallocator;
	}
	
	/**
	 * 内存分配显示条,绘图函数
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		block = mallocator.getBlock();
		g.drawRect(0, 0, 440, 20);
		g.setColor(new Color(0, 0, 255));
		g.fillRect(0, 0, 440, 20);
		int count = 0;
		for(int i = 0 ; i < 128; i++){
			if(block[i] == 1){
				count++;
			}else{
				if(count == 0) continue;
				int x1 = (int)(440*(i - count)/128);
				int x2 = (int)(count * 440 / 128);
				g.drawRect(x1, 0, x2, 20);
				g.setColor(new Color(0, 255, 0));
				g.fillRect(x1, 0, x2, 20);
				count = 0;
			}
		}
		if(count != 0){
			int x1 = (int)(440*(128 - count)/128);
			int x2 = (int)(count * 440 / 128);
			g.drawRect(x1, 0, x2, 20);
			g.setColor(new Color(0, 255, 0));
			g.fillRect(x1, 0, x2, 20);
			count = 0;
		}
	}
	
}

⌨️ 快捷键说明

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