java實(shí)現(xiàn)時(shí)鐘表盤(pán)
本文實(shí)例為大家分享了java實(shí)現(xiàn)時(shí)鐘表盤(pán)的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)計(jì)并實(shí)現(xiàn)一個(gè)模擬時(shí)鐘功能的應(yīng)用程序。程序中應(yīng)顯示時(shí)針、分針和秒針,并同時(shí)以數(shù)字形式顯示當(dāng)前時(shí)間。
實(shí)現(xiàn)結(jié)果:
源代碼如下:
//ClockPanel.java import javax.swing.*; import java.util.Calendar; ? import java.util.GregorianCalendar; ? import java.util.Timer; ? import java.util.TimerTask; ? import java.text.SimpleDateFormat; import java.util.Locale; ? import java.awt.*; import java.awt.event.*; public class ClockPanel extends JPanel{ ? ? private GregorianCalendar calendar; ? ? private JButton btn; ? ? private JButton btn2; ? ? private int currentState=8;? ? ? private String zone; ? ? private int hourTemp; ? ? final int X=320, Y=240, R=120; ? // 圓心坐標(biāo),半徑 ? ? private int xPos,yPos; ? ? private int hour,minute,second;? ? ? private int xHour,yHour,xMinute,yMinute,xSecond,ySecond;//表針位置(大端) ? ? private int xHour1,yHour1,xMinute1,yMinute1,xSecond1,ySecond1;//表針位置(小端) ? ? private double a_sec,a_min ,a_hour;//角度 ? ? ClockPanel() { ? // 創(chuàng)建定時(shí)器對(duì)象 ? ? ? ? Timer t = new Timer(); ? ? ? ? ? Task task = new Task(); ? ? ? ? ? t.schedule(task, 0, 1000);? ? ? ? ? setLayout(new BorderLayout(10,20));? ? ? ? ? btn=new JButton("時(shí)區(qū) ?上"); ? ? ? ? btn2=new JButton("時(shí)區(qū) 下"); ? ? ? ? btn.setBorder(BorderFactory.createRaisedBevelBorder()); ? ? ? ? btn2.setBorder(BorderFactory.createRaisedBevelBorder()); ? ? ? ? btn.setBackground(Color.green); ? ? ? ? btn2.setBackground(Color.green); ? ? ? ? btn.addActionListener(new ButtonListener());? ? ? ? ? btn2.addActionListener(new ButtonListener());? ? ? ? ? add(btn,BorderLayout.WEST);? ? ? ? ? add(btn2,BorderLayout.EAST);? ? ? } ? ? //相關(guān)事件處理 ? ? private class ButtonListener implements ActionListener { ? ? ? ? public void actionPerformed(ActionEvent event) { ? ? ? ? ? ? ? ? if (event.getSource()==btn) ? ? ? ? ? ? ? ? currentState++; ? ? ? ? ? ? if (event.getSource()==btn2) ? ? ? ? ? ? ? ? currentState--; ? ? ? ? } ? ? } ? ? public void paintComponent(Graphics g){? ? ? ? ? super.paintComponent(g);? ? ? ? ? double alfa; ? ?//所畫(huà)點(diǎn)對(duì)應(yīng)的角度 ? ? ? ? Graphics2D g2d=(Graphics2D)g; ? ? ? ? BasicStroke bstroke=new BasicStroke(1.0f); ? ? ? ? BasicStroke bstroke2=new BasicStroke(2.0f); ? ? ? ? BasicStroke bstroke3=new BasicStroke(3.0f); ? ? ? ? g2d.setStroke(bstroke2); ? ? ? ? for(int i=0;i<=360;i+=6) ?{ ? ? ? ? ? ? alfa=Math.toRadians(i); ?//角度用弧度表示 ? ? ? ? ? ? xPos=X+(int)(R*Math.cos(alfa)); ? // x坐標(biāo) ? ? ? ? ? ? yPos=Y-(int)(R*Math.sin(alfa)); ? // y坐標(biāo) ? ? ? ? ? ? int xBegin=320+(int)(144*Math.sin(alfa)); ? ? ? ? ? ? int yBegin=240-(int)(144*Math.cos(alfa)); ? ? ? ? ? ? int xEnd=320+(int)(159*Math.sin(alfa)); ? ? ? ? ? ? int yEnd=240-(int)(159*Math.cos(alfa)); ? ? ? ? ? ? g2d.setColor(Color.BLACK); ? ? ? ? ? ? g2d.drawLine(xBegin,yBegin,xEnd,yEnd); ? ? ? ? ? ? g2d.setColor(Color.RED); ? ? ? ? ? ? switch(i){ ?// 寫(xiě)時(shí)鐘數(shù)字刻度 ? ? ? ? ? ? ? ? case 0: g2d.drawString("3", xPos,yPos); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 90: g2d.drawString("12", xPos,yPos); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 180: g2d.drawString("9", xPos,yPos); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? case 270: g2d.drawString("6",xPos,yPos); ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? if(i%30==0){ ? ? ? ? ? ? ? ? g2d.drawLine(xBegin,yBegin,xEnd,yEnd); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? g2d.setColor(Color.BLACK); ? ? ? ? g2d.setStroke(bstroke3); ? ? ? ? g2d.drawLine(X, Y, xHour,yHour); ? ?// 畫(huà)時(shí)針 ? ? ? ? g2d.drawLine(X, Y, xHour1,yHour1); ? ? ? ? ? g2d.setColor(Color.BLUE); ? ? ? ? g2d.setStroke(bstroke2); ? ? ? ? g2d.drawLine(X, Y, xMinute,yMinute); ? ?// 畫(huà)分針 ? ? ? ? g2d.drawLine(X, Y, xMinute1,yMinute1); ? ? ? ? g2d.setColor(Color.RED); ? ? ? ? g2d.setStroke(bstroke); ? ? ? ? g2d.drawLine(X, Y, xSecond,ySecond); ? ?// 畫(huà)秒針 ? ? ? ? g2d.drawLine(X, Y, xSecond1,ySecond1); ? ? ? ? //表盤(pán)中心點(diǎn)1 ? ? ? ? g2d.drawOval(317,237,6,6);? ? ? ? ? g2d.fillOval(317,237,6,6); ? ? ? ? //表盤(pán)中心點(diǎn)2 ? ? ? ? g2d.setColor(Color.BLACK); ? ? ? ? g2d.drawOval(319,238,4,4);? ? ? ? ? g2d.fillOval(319,238,4,4); ? ? ? ? //表盤(pán)中心圓環(huán) ? ? ? ? g2d.setColor(Color.ORANGE); ? ? ? ? g2d.drawOval(300,220,40,40);? ? ? ? ? g2d.setColor(Color.black); ? ? ? ? g2d.drawString("15010140079",290,376);? ? ? ? ? GregorianCalendar gre=new GregorianCalendar();? ? ? ? ? SimpleDateFormat dateforamt1=new SimpleDateFormat("yyyy年MM月dd日E"); ? ? ? ? //SimpleDateFormat dateforamt2=new SimpleDateFormat("H時(shí)m分s秒"); ? ? ? ? g2d.setColor(Color.black); ? ? ? ? g2d.setFont(new Font("SAN_SERIF",Font.BOLD,20));? ? ? ? ? g2d.drawString(dateforamt1.format(gre.getTime()),250,50); ? ? ? ? g2d.drawString(hour+"時(shí)"+minute+"分"+second+"秒",260,430); ? ? ? ? //時(shí)區(qū)判斷 ? ? ? ? if(currentState>12){ ? ? ? ? ? ? currentState=-11; ? ? ? ? } ? ? ? ? else if(currentState<-11){ ? ? ? ? ? ? currentState=12; ? ? ? ? } ? ? ? ? if(currentState<=12&¤tState>=1) ? ? ? ? ? ? zone="東"+currentState+"區(qū)"; ? ? ? ? else ? ? ? ? ? ? zone="西"+(1-currentState)+"區(qū)"; ? ? ? ? g2d.drawString(zone,170,50);? ? ? } ? ? class Task extends TimerTask { ? ? ? ? ? public void run() { ? ? ? ? ? ? ? calendar = new GregorianCalendar(); ? ? ? ? ? ? ? hourTemp=currentState>0?(currentState-8):(currentState-1); ? ? ? ? ? ? hour = calendar.get(Calendar.HOUR)+hourTemp; ? ? ? ? ? ? ? minute = calendar.get(Calendar.MINUTE); ? ? ? ? ? ? ? second = calendar.get(Calendar.SECOND);? ? ? ? ? ? ? a_sec = second * 2 * Math.PI / 60; ? ? ? ? ? ? a_min = minute * 2 * Math.PI / 60 + a_sec / 60; ? ? ? ? ? ? a_hour = hour * 2 * Math.PI / 12 + a_min / 12; ? ? ? ? ? ? // 計(jì)算時(shí)、分、秒針的末端位置 ? ? ? ? ? ? xSecond=320+(int)(110*Math.sin(a_sec)); ? ? ? ? ? ? ySecond=240-(int)(110*Math.cos(a_sec)); ? ? ? ? ? ? xMinute=320+(int)(90 *Math.sin(a_min)); ? ? ? ? ? ? yMinute=240-(int)(90 *Math.cos(a_min)); ? ? ? ? ? ? xHour= ?320+(int)(70 *Math.sin(a_hour)); ? ? ? ? ? ? yHour= ?240-(int)(70 *Math.cos(a_hour)); ? ? ? ? ? ? xSecond1=320-(int)(22*Math.sin(a_sec)); ? ? ? ? ? ? ySecond1=240+(int)(22*Math.cos(a_sec)); ? ? ? ? ? ? xMinute1=320-(int)(15*Math.sin(a_min)); ? ? ? ? ? ? yMinute1=240+(int)(15*Math.cos(a_min)); ? ? ? ? ? ? xHour1 ?=320-(int)(5 *Math.sin(a_hour)); ? ? ? ? ? ? yHour1 ?=240+(int)(5 *Math.cos(a_hour)); ? ? ? ? ? ? repaint(); ? ? ? ? ? } ? ? ? } ? } // //Clock.java import javax.swing.*; public class Clock{? ? ? public static void main(String[] args) { ? ? ? ? ?JFrame frame=new JFrame("Clock"); ? ?//創(chuàng)建圖文框 ? ? ? ? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ? ? ? ? ?frame.getContentPane().add(new ClockPanel()); //添加面板 ? ? ? ? ?frame.setVisible(true); ? ? ? ? ?frame.setSize(640,480); ? ? ? ?? ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- java實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘并設(shè)置鬧鐘功能
- Java實(shí)現(xiàn)的簡(jiǎn)單數(shù)字時(shí)鐘功能示例
- java多線程編程制作電子時(shí)鐘
- java實(shí)現(xiàn)的小時(shí)鐘示例分享
- Java編程小實(shí)例—數(shù)字時(shí)鐘的實(shí)現(xiàn)代碼示例
- Java實(shí)現(xiàn)的動(dòng)態(tài)數(shù)字時(shí)鐘功能示例【顯示世界時(shí)間】
- java實(shí)現(xiàn)時(shí)鐘效果
- Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- JavaFX實(shí)現(xiàn)簡(jiǎn)易時(shí)鐘效果(一)
相關(guān)文章
Spring?@Transactional事務(wù)失效的原因分析
一個(gè)程序中不可能沒(méi)有事務(wù),Spring中,事務(wù)的實(shí)現(xiàn)方式分為兩種:編程式事務(wù)和聲明式事務(wù)。日常項(xiàng)目中,我們都會(huì)使用聲明式事務(wù)?@Transactional來(lái)實(shí)現(xiàn)事務(wù),本文來(lái)和大家聊聊什么情況會(huì)導(dǎo)致@Transactional事務(wù)失效2022-09-09新版idea創(chuàng)建spring boot項(xiàng)目的詳細(xì)教程
這篇文章給大家介紹了新版idea創(chuàng)建spring boot項(xiàng)目的詳細(xì)教程,本教程對(duì)新手小白友好,若根據(jù)教程創(chuàng)建出現(xiàn)問(wèn)題導(dǎo)致失敗可下載我提供的源碼,在文章最后,本教程較新,文中通過(guò)圖文給大家介紹的非常詳細(xì),感興趣的朋友可以參考下2024-01-01Java并發(fā)編程數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性方案解析
這篇文章主要為大家介紹了Java并發(fā)編程中數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04Spring?Boot將@RestController誤用于視圖跳轉(zhuǎn)問(wèn)題解決
這篇文章主要為大家介紹了Spring?Boot將@RestController誤用于視圖跳轉(zhuǎn)問(wèn)題解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼
本文主要介紹java壓縮和解壓縮Zip、Jar、Gzip文件的知識(shí),這里整理了相關(guān)資料,并附示例代碼有興趣的小伙伴可以參考下2016-09-09