首页 > 编程语言 >一个简单的时钟程序

一个简单的时钟程序

时间:2023-10-20 11:03:43浏览次数:35  
标签:y0 int 程序 gc 简单 new x0 Display 时钟


public class Sample2 {
	private Color black = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
	private Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
	private Color yellow = Display.getCurrent().getSystemColor(SWT.COLOR_YELLOW);
	private Color red = Display.getCurrent().getSystemColor(SWT.COLOR_RED);
	private Color blue = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
	private Color green = Display.getCurrent().getSystemColor(SWT.COLOR_GREEN);

	Timer timer;
	
	int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
	final double RAD = Math.PI/180;   //

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		new Sample2().createArea(shell);
//		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	private void createArea(Shell shell) {
		shell.setText("Sample");
		shell.setLayout(new GridLayout());
		shell.setSize(400, 400);
//		parent.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
		Composite container = new Composite(shell, SWT.BORDER);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;	gridLayout.marginLeft = 10;	gridLayout.marginRight = 10;
		gridLayout.marginHeight = 10;	gridLayout.marginWidth = 10;	gridLayout.verticalSpacing=0;
		container.setLayout(gridLayout);
		GridData gridData = new GridData(GridData.FILL_BOTH);
		container.setLayoutData(gridData);
		
		final Canvas drawingCanvas = new Canvas(container, SWT.BORDER);
		gridData = new GridData(GridData.FILL_BOTH);
		drawingCanvas.setLayoutData(gridData);
		drawingCanvas.setBackground(black);
		addPaintListener(drawingCanvas);
		
		int delay = 1000;
		ActionListener drawClock = new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				Display.getDefault().syncExec(new Runnable() {
					public void run() {
						if(drawingCanvas.isDisposed())
							return;
						drawingCanvas.redraw();
					}
				});
			}
		};
		timer = new Timer(delay,drawClock);
		timer.start() ;
	}
	
	protected void addPaintListener(Canvas drawingCanvas) {
		drawingCanvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.setForeground(white);
				Point size = ((Canvas) e.widget).getSize();
				int radius = (Math.min(size.x,size.y)-40)/2;
				Point center = new Point(size.x/2,size.y/2);
				e.gc.setLineCap(SWT.CAP_ROUND);
				e.gc.setLineWidth(3);
				//drawOval (int x, int y, int width, int height)
				e.gc.drawOval(center.x-radius, center.y-radius, 2*radius, 2*radius);
				
				e.gc.setLineWidth(1);
				e.gc.setForeground(red);
				
				e.gc.setForeground(yellow);
				
				//int radius = 
				x0=center.x; y0=center.y; r=radius; h=center.y + radius + 15;
				ang=60;
		        for (int i=1; i<=12; i++) {
		        	x=(int)((r+10)*Math.cos(RAD*ang)+x0);
		        	y=(int)((r+10)*Math.sin(RAD*ang)+y0);
		        	e.gc.drawString(""+i,x,h-y);
		        	ang-=30;
		        }
		        
		        Calendar now = new GregorianCalendar();
		        int nowh = now.get(Calendar.HOUR_OF_DAY );
		        int nowm = now.get(Calendar.MINUTE );
		        int nows = now.get(Calendar.SECOND );
		        String st;
		        if(nowh<10) st = "0"+nowh ; else st = ""+nowh;
		        if(nowm<10) st += ":0"+nowm; else st += ":"+nowm;
		        if(nows<10) st += ":0"+nows; else st += ":"+nows;
//		        System.out.println(st);
		        e.gc.setBackground(green);
		        e.gc.fillRectangle(0+4,0+4,50,14);
		        e.gc.setForeground(blue);
		        e.gc.drawString(st,0+5,0+5);
		        
		        ss = 90-nows*6;
		        mm = 90-nowm*6;
		        hh = 90-nowh*30-nowm/2;
		        x0=center.x; y0=center.y;
//		        g2D.setStroke(new BasicStroke(1.2f));
		        if(olds_x > 0) {
		        	e.gc.setForeground(black);
		        	e.gc.drawLine(x0,y0,olds_x,h-olds_y);
		        } else {
		             old_m = mm;
		             old_h = hh;
		        }
		        x = (int)(r*0.8*Math.cos(RAD*ss))+x0;
		        y = (int)(r*0.8*Math.sin(RAD*ss))+y0;
		        e.gc.setForeground(yellow);
		        e.gc.drawLine(x0,y0,x,h-y);
		        olds_x = x;
		        olds_y = y;
		        e.gc.setLineWidth(2);
		        if(old_m!=mm) {
		        	e.gc.setForeground(black);
		        	e.gc.drawLine(x0,y0,oldm_x,h-oldm_y);
		        }
		        x = (int)(r*0.6*Math.cos(RAD*mm))+x0;
		        y = (int)(r*0.6*Math.sin(RAD*mm))+y0;
		        e.gc.setForeground(green );
		        e.gc.drawLine(x0,y0,x,h-y);
		        oldm_x = x;
		        oldm_y = y;
		        old_m = mm;
		        e.gc.setLineWidth(3);
		        if(old_h!=hh) {
		        	e.gc.setForeground(black);
		        	e.gc.drawLine(x0,y0,oldh_x,h-oldh_y);
		        }
		        x = (int)(r*0.5*Math.cos(RAD*hh))+x0;
		        y = (int)(r*0.5*Math.sin(RAD*hh))+y0;
		        e.gc.setForeground(red );
		        e.gc.drawLine(x0,y0,x,h-y);
		        oldh_x = x;
		        oldh_y = y;
		        old_h = hh;
			}
		});
	}

}


感觉还行,是仿照书上的一个例子写的,书上使用swing实现的。

标签:y0,int,程序,gc,简单,new,x0,Display,时钟
From: https://blog.51cto.com/u_16298170/7947895

相关文章

  • 使用 Flutter 制作一个简单的笑话生成器应用程序
    在本教程中,我将向您展示如何使用Flutter制作一个简单的笑话生成器应用程序对于这个项目,我们将从RESTfulAPI获取数据API的链接:随机笑话对于这个项目,我不会关注应用程序的UI,我们只会关注如何从URL中获取数据,以及如何显示它们在我们开始之前,您必须将此包添加到您的pubspec.y......
  • 基于labview的数字时钟设计
      1.电子时钟模块   (1)用七个squareled根据的顺序建立一个数字单元,放进簇里,复制粘贴11个。如下图:   (2)创立一个字符串a。 (3)用ctrl+e快捷键打开后面板,画一个whileloop,接下来的操作都在whileloop里完成。 (4)找到FormatDate/TimeString函数,用%y年%m月%......
  • 让AutoMapper使用变得简单
     倘若在项目中真正要用的时候,我觉得还是应该对AutoMapper的方法进行一些整理,最好能够封装一下,这里我通过扩展方法的形式将其封装为AutoMapperHelper,这样以后使用AutoMapper就变的SOEASY了~ usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Data......
  • 微信小程序发布后部分用户不能及时更新
    在app.js里的onLaunch里加入如下代码onLaunch(){if(wx.canIUse('getUpdateManager')){constupdateManager=wx.getUpdateManager()updateManager.onCheckForUpdate(function(res){console.log('onCheckForUpdate====',res)......
  • 以下是一个简单的HTML代码示例,演示如何实现分身份登录(用户登录和管理员登录)
    <!DOCTYPEhtml><html><head><title>分身份登录</title></head><body><h2>用户登录</h2><formaction="user_login.php"method="post"><labelfor="user_username"......
  • Rockchip RK3399 - DRM eDP驱动程序
    在《RockchipRK3399-DRM驱动程序》》我们已经介绍过了,RK3399有两个VOP,均可以支持HDMI、eDP、DP、MIPIDSI0、MIPIDSI1显示接口,本节我们选择eDP作为分析的对象。一、设备树配置1.1edp设备节点设备节点vopb下的子节点vopb_out_edp通过edp_in_vopb(由remote-endpoint属性指定)......
  • 2023-2024-1 20231312 《计算机与程序设计》第四周学习总结
    作业信息这个作业属于哪个课程<班级的链接>2023-2024-1-计算机基础与程序设计|-这个作业要求在哪里<作业要求链接>2023-2024-1计算机基础与程序设计第四周作业|这个作业的目标《计算机基础概论》第4,5章《C语言程序设计》第3章|作业正文作业链接教材学......
  • 小程序技术未来发展的思考 - 更丰富的生态系统
    随着微信小程序、支付宝小程序和其他各种小程序的普及,小程序技术已经在移动应用开发领域占据了重要位置。然而,小程序的未来发展不仅仅限于便捷性和跨平台性,更丰富的生态系统将是小程序技术未来的一个重要趋势。在本文中,我们将探讨小程序技术未来的发展方向,并提供一个代码演示来展示......
  • Java资源文件获取方法详解:从 Classpath 到 Web 应用程序
    在Java开发中,访问和读取资源文件是一个常见的需求。这些资源可以是配置文件、图像、音频、视频、文本文件等。在Java中,获取资源文件有多种方式,包括直接通过类路径(Classpath)访问,或者通过Web应用程序的上下文路径(ContextPath)访问。以下我们将详细探讨这些方法。通过类路径(Classpath)......
  • java程序题
    指数计算问题1.有一对兔子,从出生后第3个月起每个月都生兴对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?程序分析;兔子的规律为数列1,1,2,3,5,8,13,21..... 2.水仙花数打印出所有的"水仙花数",所谓"水仙花数“是指一个三位数,其各位数......