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

📄 hh.txt

📁 基于图形界面的时钟客户端的获取服务器端的时间
💻 TXT
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class DayTimeGUIClient extends Applet {
	
	private Graphics gBuf;
	private Image imgBuf;
	static int year,month,day,hour,minute,second;
	
	public static void main(String[] args) {
		Frame app = new Frame("时钟客户端");
		app.setSize(800, 600);
		app.setLocationByPlatform(true);
		DayTimeGUIClient applet = new DayTimeGUIClient();
		app.add(applet);
		app.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				event.getWindow().dispose();
				System.exit(0);
			}
		});
		app.show();
		applet.start();
		while(true){
			try{
				Socket s=new Socket("localhost",2007);
				DataInputStream in=new DataInputStream(s.getInputStream());
				readTime(in);
				
				
			}catch(IOException e)
			{
				e.printStackTrace();
			}
			applet.repaint();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	static void readTime(DataInputStream in) throws IOException{
		year=in.readInt();
		month=(int)in.readByte()+1;
		day=(int)in.readByte();
		hour=(int)in.readByte();
		minute=(int)in.readByte();
		second=(int)in.readByte();
	}	
	
	public void init() {
		
	}
	
	public void paint(Graphics g) {
		final double PI=3.1415926;
		int x0,y0,x1,y1,x2,y2,t;
		int graphWidth = bounds().width;
		int graphHeight = bounds().height;
		imgBuf = createImage(graphWidth, graphHeight);		
		gBuf = imgBuf.getGraphics();
		gBuf.clearRect(0, 0, graphWidth, graphHeight);
		
		//用字符方式显示当前时间
		gBuf.drawString(year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分"
				+second+"秒",graphWidth/20,graphHeight/20);
		
		//获取直径,圆心坐标
		int r=(graphWidth<graphHeight)?graphWidth:graphHeight;
		r=r*6/7;			
		x0=graphWidth/2;
		y0=graphHeight/2;
		
		//绘制时钟刻度
		gBuf.drawOval((graphWidth-r)/2,(graphHeight-r)/2,r,r);
		r=r/2;
		for (t=0;t<60;t++){
			x1=x0+(int)(r*Math.cos(t*PI/30));
			y1=y0+(int)(r*Math.sin(t*PI/30));
			if(t%5==0){
				x2=x0+(int)(r*Math.cos(t*PI/30)*17/20);
				y2=y0+(int)(r*Math.sin(t*PI/30)*17/20);
			}else{
				x2=x0+(int)(r*Math.cos(t*PI/30)*19/20);
				y2=y0+(int)(r*Math.sin(t*PI/30)*19/20);
			}
			gBuf.drawLine(x1,y1,x2,y2);
		}
		
		//绘制时针
		t=60*((hour<12)?hour:hour%12)+minute;
		x1=x0+(int)(r*Math.cos(t*PI/360-PI/2)*4/10);
		y1=y0+(int)(r*Math.sin(t*PI/360-PI/2)*4/10);
		x2=x0+(int)(r*Math.cos(t*PI/360+PI/2)*1/10);
		y2=y0+(int)(r*Math.sin(t*PI/360+PI/2)*1/10);
		gBuf.drawLine(x1,y1,x2,y2);
		
		//绘制分针
		t=60*minute+second;
		x1=x0+(int)(r*Math.cos(t*PI/1800-PI/2)*6/10);
		y1=y0+(int)(r*Math.sin(t*PI/1800-PI/2)*6/10);
		x2=x0+(int)(r*Math.cos(t*PI/1800+PI/2)*1/10);
		y2=y0+(int)(r*Math.sin(t*PI/1800+PI/2)*1/10);
		gBuf.drawLine(x1,y1,x2,y2);
		
		//绘制秒针
		t=second;
		x1=x0+(int)(r*Math.cos(t*PI/30-PI/2)*8/10);
		y1=y0+(int)(r*Math.sin(t*PI/30-PI/2)*8/10);
		x2=x0+(int)(r*Math.cos(t*PI/30+PI/2)*1/10);
		y2=y0+(int)(r*Math.sin(t*PI/30+PI/2)*1/10);
		gBuf.drawLine(x1,y1,x2,y2);
		
		g.drawImage(imgBuf, 0, 0, this);	
		
	}
	
	public void update(Graphics g) {
		paint(g);
	}
	
}

⌨️ 快捷键说明

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