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

📄 sjo.txt

📁 模拟随机表盘,还不错啊,
💻 TXT
字号:
package Java2DTest;

import java.awt.Panel;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.*;
import java.util.*;

public class DialGauge extends Panel implements Runnable {
	
	static DialGauge comp;
	static Thread thread;
	final static BasicStroke stroke = new BasicStroke(3.0f);
	Graphics2D g2;
	static double needleAngle = 0;
	Random random;
	
	public DialGauge(){
		setSize(200,200);
		random = new Random(System.currentTimeMillis());
	}
	
	Image offScreenBuffer = null;
	
	public void update(Graphics g)
	{
		Graphics gr;
		if(offScreenBuffer==null || 
			(!(offScreenBuffer.getWidth(this)==this.size().width
					&&offScreenBuffer.getHeight(this)==this.size().height)))
			
		{
			offScreenBuffer = this.createImage(size().width,size().height);
		}
		gr = offScreenBuffer.getGraphics();
		
		
		g.drawImage(offScreenBuffer,0,0,this);
		paint(gr);
	}
	
	
	
	public void run() {
		double start = 60.0;
		for(;;)
		{
			double angle = (0.5-random.nextDouble())*30.0;
			double end = start + angle;
			
			move(start,end);
			start = end;
		}

	}
	
	public void move(double start,double end)
	{
		needleAngle = start;
		double da = (end-start)/10;
		while((needleAngle<end && needleAngle>=start)||
				(needleAngle>end && needleAngle<=start))
		{
			needleAngle +=da;
			if(needleAngle>170 || needleAngle<10)
				return;
			repaint();
			try{
				Thread.sleep(50);
			}
			catch(Exception ex)
			{
				
			}
		}
	}
	
	
	public void paint(Graphics g)
	{
		Graphics2D g2d = (Graphics2D)g;
		g2d.setPaint(Color.GRAY);
		g2d.fill(new Rectangle2D.Float(0,0,220,120));
		g2d.setPaint(Color.WHITE);
		Arc2D face = new Arc2D.Float(10,10,200,200,0,180,Arc2D.PIE);
		g2d.fill(face);
		g2d.setStroke(stroke);
		
		
		
		double faceRadius = 110;
		
		g2d.setPaint(Color.BLACK);
		int stickLength = 10;
		int radius = 100;
		for (int i=0;i<11;i++)
		{
			double angle = Math.toRadians(180.0*i);
			int x0 = (int)(faceRadius + radius*Math.cos(angle));
			int x1 = (int)(faceRadius +(radius-stickLength)*Math.cos(angle));
			int y0 = (int)(faceRadius - radius*Math.sin(angle));
			int y1 = (int)(faceRadius - (radius-stickLength)*Math.sin(angle));
			g2d.drawLine(x0,y0,x1,y1);
		}
		
		g2d.draw(new Arc2D.Double(20,20,180,180,0,180,Arc2D.OPEN));
		GradientPaint blacktowhite = new GradientPaint(90,90,Color.BLACK,90+40,90-20,Color.WHITE);
		
		
		//draw needle
		
/*		g2d.setPaint(Color.RED);
		double angle = Math.toRadians(180.0 - needleAngle);
		int x0 = (int)(faceRadius + (80.0-60)*Math.cos(angle));
		int y0 = (int)(faceRadius - (80.0-60)*Math.sin(angle));
		int x = (int)(faceRadius + 80.0*Math.cos(angle));
		int y =(int)(faceRadius-80.0*Math.sin(angle));
		g2d.drawLine(x0,y0,x,y);
		
		g2d.setPaint(blacktowhite);
		g2d.fill(new Arc2D.Float(90,90,40,40,0,180,Arc2D.PIE));
		
		*/
	}

	public static void main(String[] args) {
		Frame frame = new Frame("Dial Guage Component");
		frame.setLayout(new BorderLayout());
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		comp = new DialGauge();
		thread = new Thread(comp);
		frame.add(comp,BorderLayout.CENTER);
		frame.setSize(230,170);
		Button b = new Button("Run");
		Button b2 = new Button("Stop");
		b.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent aE){
				bAction();
			}
		});
		b2.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent aE){
				b2Action();
			}
		});
		
		Panel p = new Panel();
		p.setLayout(new BorderLayout());
		p.add(b,BorderLayout.WEST);
		p.add(b2,BorderLayout.EAST);
		frame.add(p,BorderLayout.SOUTH);
		
		frame.setVisible(true);
		
	}
	public static void bAction()
	{
		thread = new Thread(comp);
		thread.start();
	}
	public static void b2Action()
	{
		thread.stop();
	}

}

⌨️ 快捷键说明

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