您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)使用Java實(shí)現(xiàn)動態(tài)模擬時鐘的案例的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內(nèi)容如下
應(yīng)用名稱:java動態(tài)模擬時鐘
用到的知識:javaGUI,java 繪圖
開發(fā)環(huán)境:win10+eclipse+jdk1.8
功能說明:通過java繪圖畫出一個虛擬的動態(tài)時鐘
效果圖:
源代碼:
import javax.swing.*; import java.awt.*; import java.util.*; import java.lang.Thread; import java.text.DecimalFormat; public class StillClock extends JPanel { /** * @param args */ private int hour; private int minute; private int second; //構(gòu)造函數(shù) public StillClock() { setCurrentTime(); } //返回小時 public int getHour() { return hour; } public int getMinute() { return minute; } public int getSecond() { return second; } //繪制時鐘 protected void paintComponent(Graphics g) { super.paintComponent(g); //初始化 int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; //畫圓 g.setColor(Color.black); g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius); g.drawString("12", xCenter - 5, yCenter - clockRadius + 15); g.drawString("9", xCenter - clockRadius + 3, yCenter + 5); g.drawString("3", xCenter + clockRadius - 10, yCenter + 3); g.drawString("6", xCenter - 3, yCenter + clockRadius - 3); //畫秒針 int sLength = (int)(clockRadius * 0.8); int xSecond = (int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60))); int ySecond = (int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60))); g.setColor(Color.red); g.drawLine(xCenter, yCenter, xSecond, ySecond); //畫分針 int mLenth = (int)(clockRadius * 0.65); int xMinute = (int)(xCenter + mLenth * Math.sin(minute * (2 * Math.PI / 60))); int yMinute = (int)(xCenter - mLenth * Math.cos(minute * (2 * Math.PI / 60))); g.setColor(Color.blue); g.drawLine(xCenter, yCenter, xMinute, yMinute); //畫時針 int hLength = (int)(clockRadius * 0.5); int xHour = (int)(xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); int yHour = (int)(yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); g.setColor(Color.green); g.drawLine(xCenter, yCenter, xHour, yHour); //畫數(shù)字時鐘 g.setColor(Color.black); DecimalFormat s=new DecimalFormat("00"); g.drawString(s.format(getHour()) + ":" + s.format(getMinute()) + ":" + s.format(getSecond()), xCenter - 22, yCenter - clockRadius - 15); } public void setCurrentTime() { Calendar calendar = new GregorianCalendar(); this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame("DiaplayClock"); frame.setResizable(false); frame.setTitle("DiaplayClock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,350); frame.setVisible(true); while(true) { StillClock clock = new StillClock(); frame.getContentPane().add(clock); clock.setVisible(true); frame.validate(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } clock.setVisible(false); frame.remove(clock); clock = null; frame.validate(); } } }
感謝各位的閱讀!關(guān)于“使用Java實(shí)現(xiàn)動態(tài)模擬時鐘的案例”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。