visualbar.java

来自「赛车程序:先对赛道和赛车建立模型」· Java 代码 · 共 65 行

JAVA
65
字号
/**
 * 
 */
package cn.tsinghua.ag5.CarModel;

import java.awt.*;
import java.awt.event.*;

import javax.swing.JPanel;

/**
 * @author wfq
 *
 */
public class VisualBar extends JPanel 
{
	private String m_log;
	double m_length;
	double m_max;
	
	public VisualBar(String p_log, double p_length, double p_max)
	{
		m_log = p_log;
		m_length = p_length;
		m_max = p_max;

		this.setBackground(Color.LIGHT_GRAY);
//		this.setSize(200, 80);
		this.setPreferredSize(new Dimension(200, 50));
	}
	
	public void paint( Graphics p_g)
	{
		super.paint(p_g);
		
		p_g.setFont(new Font("Monospaced", Font.PLAIN, 14));
		p_g.drawString(m_log, 10, 25);
		
		if(m_length <= 0)
		{
			p_g.setColor(Color.green);
			p_g.fill3DRect(60, 10, 5, 30, true);
		}else
		{
			p_g.setColor(Color.red);
			p_g.fill3DRect(60, 10, (int)Math.round(m_length / m_max * 130), 30, true);
		}
	}

	/**
	 * @return the length
	 */
	public double getLength() {
		return this.m_length;
	}

	/**
	 * @param p_length the length to set
	 */
	public void setLength(double p_length) {
		this.m_length = p_length;
	}

}

⌨️ 快捷键说明

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