最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)時(shí)鐘表盤

 更新時(shí)間:2022年09月11日 08:33:59   作者:Jiafu_Liu  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)時(shí)鐘表盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)時(shí)鐘表盤的具體代碼,供大家參考,具體內(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; ? ?//所畫點(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){ ?// 寫時(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); ? ?// 畫時(shí)針
? ? ? ? g2d.drawLine(X, Y, xHour1,yHour1); ?
? ? ? ? g2d.setColor(Color.BLUE);
? ? ? ? g2d.setStroke(bstroke2);
? ? ? ? g2d.drawLine(X, Y, xMinute,yMinute); ? ?// 畫分針
? ? ? ? g2d.drawLine(X, Y, xMinute1,yMinute1);
? ? ? ? g2d.setColor(Color.RED);
? ? ? ? g2d.setStroke(bstroke);
? ? ? ? g2d.drawLine(X, Y, xSecond,ySecond); ? ?// 畫秒針
? ? ? ? g2d.drawLine(X, Y, xSecond1,ySecond1);
? ? ? ? //表盤中心點(diǎn)1
? ? ? ? g2d.drawOval(317,237,6,6);?
? ? ? ? g2d.fillOval(317,237,6,6);
? ? ? ? //表盤中心點(diǎn)2
? ? ? ? g2d.setColor(Color.BLACK);
? ? ? ? g2d.drawOval(319,238,4,4);?
? ? ? ? g2d.fillOval(319,238,4,4);
? ? ? ? //表盤中心圓環(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&&currentState>=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); ? ? ? ??
? ? }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot中6種API版本控制策略小結(jié)

    SpringBoot中6種API版本控制策略小結(jié)

    API版本控制是確保系統(tǒng)平穩(wěn)演進(jìn)的關(guān)鍵策略,這篇文章主要為大家詳細(xì)介紹了SpringBoot中6種API版本控制策略,大家可以根據(jù)需要自行選擇
    2025-04-04
  • java 文件目錄讀寫刪除操作詳細(xì)實(shí)現(xiàn)代碼

    java 文件目錄讀寫刪除操作詳細(xì)實(shí)現(xiàn)代碼

    這篇文章主要介紹了java 文件讀寫刪操作詳細(xì)實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2017-09-09
  • Java中BigDecimal類與int、Integer使用總結(jié)

    Java中BigDecimal類與int、Integer使用總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java中BigDecimal類與int、Integer使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • mybatis模糊查詢like語句該如何寫

    mybatis模糊查詢like語句該如何寫

    MyBatis模糊查詢通常使用LIKE關(guān)鍵字,結(jié)合concat函數(shù)拼接通配符%實(shí)現(xiàn),在MyBatis配置文件中,通過#{keyword}傳遞參數(shù),生成帶有通配符的查詢語句,MyBatis-Plus中,通過LambdaQueryWrapper類和like方法構(gòu)建模糊查詢條件,簡化查詢操作
    2024-09-09
  • SpringBoot淺析安全管理之OAuth2框架

    SpringBoot淺析安全管理之OAuth2框架

    安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會(huì)發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會(huì)出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理OAuth2框架的使用
    2022-08-08
  • spring boot--從controller到DAO操作

    spring boot--從controller到DAO操作

    這篇文章主要介紹了spring boot--從controller到DAO操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • SpringBoot如何在線程中獲取@Service Bean類

    SpringBoot如何在線程中獲取@Service Bean類

    這篇文章主要介紹了SpringBoot如何在線程中獲取@Service Bean類,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java訪問者模式實(shí)現(xiàn)優(yōu)雅的對(duì)象結(jié)構(gòu)處理

    Java訪問者模式實(shí)現(xiàn)優(yōu)雅的對(duì)象結(jié)構(gòu)處理

    Java訪問者模式是一種行為型設(shè)計(jì)模式,它通過將數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)操作分離,實(shí)現(xiàn)對(duì)復(fù)雜對(duì)象結(jié)構(gòu)的處理。它將數(shù)據(jù)結(jié)構(gòu)中的每個(gè)元素都轉(zhuǎn)換為訪問者能夠識(shí)別的形式,從而使得數(shù)據(jù)操作可以在不影響數(shù)據(jù)結(jié)構(gòu)的前提下進(jìn)行擴(kuò)展和變化
    2023-04-04
  • 詳解Java中的迭代迭代器Iterator與枚舉器Enumeration

    詳解Java中的迭代迭代器Iterator與枚舉器Enumeration

    Iterator與Enumeration分別是實(shí)現(xiàn)迭代器和枚舉器類的接口,下面就帶大家來詳解Java中的迭代迭代器Iterator與枚舉器Enumeration,以及它們之間的區(qū)別.
    2016-05-05
  • nacos客戶端一致性hash負(fù)載需求實(shí)現(xiàn)

    nacos客戶端一致性hash負(fù)載需求實(shí)現(xiàn)

    這篇文章主要介紹了nacos客戶端一致性hash負(fù)載的需求實(shí)現(xiàn)過程及步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02

最新評(píng)論

托克逊县| 苍溪县| 丰县| 华安县| 衢州市| 西丰县| 株洲县| 宁晋县| 永和县| 布尔津县| 普定县| 方城县| 芦山县| 招远市| 江口县| 乐东| 浦东新区| 高密市| 夏邑县| 武功县| 大石桥市| 岱山县| 龙胜| 弥勒县| 清远市| 定结县| 和静县| 钟祥市| 图们市| 延长县| 永平县| 池州市| 武清区| 潮安县| 历史| 普宁市| 安徽省| 都江堰市| 禹州市| 丹阳市| 巧家县|