欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

本文實(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&&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); ? ? ? ??
? ? }
}

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

相關(guān)文章

  • Spring?@Transactional事務(wù)失效的原因分析

    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
  • Java多線程中synchronized的工作原理

    Java多線程中synchronized的工作原理

    這篇文章主要介紹了Java多線程中synchronized的工作原理,本期講解 synchronized 工作的原理以及常見(jiàn)的鎖優(yōu)化機(jī)制,相信大家在看完這篇博文后對(duì) synchronized 工作流程有一定的理解,需要的朋友可以參考下
    2023-07-07
  • Java emoji持久化mysql過(guò)程詳解

    Java emoji持久化mysql過(guò)程詳解

    這篇文章主要介紹了Java emoji持久化mysql過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 新版idea創(chuàng)建spring boot項(xiàng)目的詳細(xì)教程

    新版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-01
  • Java 垃圾回收機(jī)制詳解及實(shí)例代碼

    Java 垃圾回收機(jī)制詳解及實(shí)例代碼

    這篇文章主要介紹了 Java 垃圾回收機(jī)制詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Java 全面系統(tǒng)介紹反射的運(yùn)用

    Java 全面系統(tǒng)介紹反射的運(yùn)用

    準(zhǔn)備入手學(xué)習(xí)java的安全了,感覺(jué)這也是一個(gè)大的趨勢(shì),想著盡早進(jìn)入到j(luò)ava安全的探索中,在反序列化鏈的學(xué)習(xí)之前,需要先學(xué)習(xí)反射,不多說(shuō)了,開(kāi)干吧
    2022-03-03
  • Java并發(fā)編程數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性方案解析

    Java并發(fā)編程數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性方案解析

    這篇文章主要為大家介紹了Java并發(fā)編程中數(shù)據(jù)庫(kù)與緩存數(shù)據(jù)一致性解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • Spring?Boot將@RestController誤用于視圖跳轉(zhuǎn)問(wèn)題解決

    Spring?Boot將@RestController誤用于視圖跳轉(zhuǎn)問(wèn)題解決

    這篇文章主要為大家介紹了Spring?Boot將@RestController誤用于視圖跳轉(zhuǎn)問(wèn)題解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼

    java 壓縮和解壓縮Zip、Jar、Gzip文件實(shí)例代碼

    本文主要介紹java壓縮和解壓縮Zip、Jar、Gzip文件的知識(shí),這里整理了相關(guān)資料,并附示例代碼有興趣的小伙伴可以參考下
    2016-09-09
  • Spring Boot系列教程之日志配置

    Spring Boot系列教程之日志配置

    這篇文章主要給大家介紹了關(guān)于Spring Boot系列教程之日志配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11

最新評(píng)論