Matlab及Java實(shí)現(xiàn)小時(shí)鐘效果
本文實(shí)例為大家分享了Matlab及Java實(shí)現(xiàn)小時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
一年前曾經(jīng)用matlab的gui做了一個(gè)時(shí)鐘,由于是直接用GUIDE和ActiveX控件寫(xiě)的,程序雖說(shuō)有許多行,大多數(shù)都是自動(dòng)生成的,自己寫(xiě)的只有十幾行而已。閑著沒(méi)事,就耗費(fèi)了下午的時(shí)間用matlab和Java分別又寫(xiě)了寫(xiě)。具體代碼如下:
1.matlab腳本文件:
%%%%%%%%%%%%%%%設(shè)置圖像屬性并獲取圖像句柄%%%%%%%%%%%%%%%%%%%%%%%%%%%h=figure(’name’,’我的時(shí)鐘’,’NumberTitle’,’off’,’color’,[1 1 0]);set(h,’menubar’,’none’,’position’,[200,200,400,450]);%%%%%%%%%%%%畫(huà)出時(shí)鐘的外輪廓%%%%%%%%%%%%%%s1=[0:pi/1000:2*pi];hl=plot(2*cos(s1),2*sin(s1),’black’,’linewidth’,1.5);axis equaltitle(’我的時(shí)鐘’);hold on%%%%%%%%%%%繪制表盤(pán)刻度%%%%%%%%%%%%%%%%%%for n=pi*2:-pi/30:pi/30 %繪制表盤(pán),繪制分鐘的刻度 a1=0.95*cos(n):0.000005*cos(n)/2:cos(n);b1=0.95*sin(n):0.000005*sin(n)/2:sin(n); plot(2*a1,2*b1,’r-’);endfor n=pi*2:-pi/6:pi/30%繪制表盤(pán),繪制小時(shí)的刻度 a1=0.9*cos(n):0.1*cos(n)/2:cos(n);b1=0.9*sin(n):0.1*sin(n)/2:sin(n); plot(2*a1,2*b1,’r-’);endtext(1.5,0,’3’,’FontSize’,12)text(-0.05,-1.7,’6’,’FontSize’,12)text(-1.7,0,’9’,’FontSize’,12)text(-0.1,1.7,’12’,’FontSize’,12)%%%%%%%%%%%%%%%%獲取當(dāng)前時(shí)間并進(jìn)行角度與弧度轉(zhuǎn)換%%%%%%%%%%%%%%%%%%%%%%%%%%%% axis([-2.1 2.1 -2.1 2.1]) time=datestr(now); sec=pi/2-str2num(time(19:20))*pi/30; min=pi/2-(str2num(time(16:17))+sec/60)*pi/30; hour=pi/2-(str2num(time(13:14))+min/60)*pi/6; w1=-pi/30; w2=-pi/1800; w3=-pi/108000; pausetime=1; %%%%%%%%%%%%%%%%開(kāi)始繪圖并不斷刷新%%%%%%%%%%%%while 1 axis off x1=0:0.75*cos(sec)/2:0.75*cos(sec);y1=0:0.75*sin(sec)/2:0.75*sin(sec); %根據(jù)秒針的位置繪制分針 x2=0:0.6*cos(min)/2:0.6*cos(min);y2=0:0.6*sin(min)/2:0.6*sin(min); %根據(jù)分針的位置繪制分針 x3=0:0.45*cos(hour)/2:0.45*cos(hour);y3=0:0.45*sin(hour)/2:0.45*sin(hour); %根據(jù)時(shí)針的位置繪制分針 hp1=plot(2*x1,2*y1,’r-’,’linewidth’,1.5); hp2=plot(2*x2,2*y2,’b-’,’linewidth’,2); hp3=plot(2*x3,2*y3,’g-’,’linewidth’,3.5); sec=sec+w1*pausetime; %計(jì)算一秒以后秒針的角度位置 min=min+w2*pausetime; %計(jì)算一秒以后分針的角度位置 hour=hour+w3*pausetime; pause(1); delete(hp1); delete(hp2); delete(hp3);end
2.Java應(yīng)用文件(文件名為MyClock.java)
import java.awt.*;import java.util.GregorianCalendar;import javax.swing.*;import javax.swing.Timer;import java.util.*;import java.awt.event.*;public class MyClock extends JPanel { final double RAD=Math.PI/180;//角度與弧度的轉(zhuǎn)化 public void paint(Graphics g) { super.paint(g);//調(diào)用父類(lèi)方法 Graphics2D g2=(Graphics2D)g;//主要用于改變線條粗細(xì) int h=getSize().height;//獲取窗口的長(zhǎng)和寬,主要用于當(dāng)用鼠標(biāo)改變窗口時(shí),時(shí)鐘也跟著變化 int w=getSize().width; int hour,min,sec,hh,mm,ss; double x,y; setBackground(Color.yellow);//設(shè)置背景值 g.setColor(Color.black);//畫(huà)筆顏色 int r=(Math.min(h, w)/2-50); float x0=w/2;//時(shí)鐘中心位置 float y0=h/2; g2.setFont(new Font('楷體',Font.PLAIN,20)); g2.drawString('我的時(shí)鐘',165, 50); g2.setFont(new Font('Times New Roman',Font.PLAIN,20)); g2.drawString('Designed by TW',235, 420);g.drawString('12',(int)(w/2)-5,(int)(h/2)-r+30); g.drawString('3',(int)(w/2)-25+r,Math.round(h/2)+10); g.drawString('6',(int)(w/2),(int)(h/2)+r-20); g.drawString('9',(int)(w/2)-r+20,Math.round(h/2)+10); //設(shè)置分鐘刻度 之所以沒(méi)有用線條,主要是因?yàn)閐rawline的參數(shù)要求是整數(shù),因此刻度會(huì)不準(zhǔn)確 for(int i=1;i<=12;i++) { double buffer=Math.PI*(0.5-i/6.0); int posX = (int)Math.round(x0+r*Math.cos(buffer)); int posY = (int)Math.round(y0-r*Math.sin(buffer)); g.setColor(Color.red); g.fill3DRect(posX, posY, 8, 8, true); } //設(shè)置秒鐘刻度f(wàn)or(int i=1;i<60;i++) { if(i%5!=0) { double buffer= Math.PI*i/30.0; int posX = (int)Math.round(x0+r*Math.cos(buffer)); int posY = (int)Math.round(y0-r*Math.sin(buffer)); g.setColor(Color.black); g.fill3DRect(posX, posY, 6, 6, false); } } //獲取當(dāng)前系統(tǒng)時(shí)間 GregorianCalendar date=new GregorianCalendar(); hour=(int)date.get(Calendar.HOUR); min=(int)date.get(Calendar.MINUTE); sec=(int)date.get(Calendar.SECOND); // System.out.println(hour); // System.out.println(min); // System.out.println(sec); //進(jìn)行角度換算 ss=90-sec*6; mm=90-min*6; hh=90-hour*30-min/2; //畫(huà)出時(shí)分秒的指針 g2.setStroke(new BasicStroke(1.0f)); x=(int)(r*0.9*Math.cos(RAD*ss)+x0); y=(int)(r*0.9*Math.sin(RAD*ss)+y0); g.setColor(Color.red); g.drawLine((int)(x0),(int)(y0),(int)x,(int)(h-y)); g2.setStroke(new BasicStroke(2.2f)); x=(int)(r*0.7*Math.cos(RAD*mm)+x0); y=(int)(r*0.7*Math.sin(RAD*mm)+y0); g.setColor(Color.blue); g.drawLine((int)x0,(int)y0,(int)x,(int)(h-y)); g2.setStroke(new BasicStroke(3.4f)); x=(int)(r*0.5*Math.cos(RAD*hh))+x0; y=(int)(r*0.5*Math.sin(RAD*hh))+y0; g.setColor(Color.green); g.drawLine((int)x0,(int)y0,(int)x,(int)(h-y)); } //設(shè)置窗口大小 public Dimension getPreferredSize() { return new Dimension(400,450); }// public static void main(String[] args) { Graphics g=null; JFrame frame=new JFrame('我的時(shí)鐘'); Container contentPane=frame.getContentPane(); final MyClock tw=new MyClock(); contentPane.add(tw,BorderLayout.CENTER); frame.pack(); frame.setVisible(true); //用于畫(huà)板的刷新 int delay=1000; //創(chuàng)建一個(gè)監(jiān)聽(tīng)事件 ActionListener drawClock=new ActionListener() { public void actionPerformed(ActionEvent evt) { tw.repaint(); } }; //創(chuàng)建一個(gè)時(shí)間計(jì)數(shù)器,每一秒觸發(fā)一次 new Timer(delay,drawClock).start(); }}
運(yùn)行結(jié)果如下圖:
1.matlab時(shí)鐘界面:
2.Java時(shí)鐘界面
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python TestSuite生成測(cè)試報(bào)告過(guò)程解析2. python讓函數(shù)不返回結(jié)果的方法3. python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計(jì)處理操作4. JSP之表單提交get和post的區(qū)別詳解及實(shí)例5. python實(shí)現(xiàn)讀取類(lèi)別頻數(shù)數(shù)據(jù)畫(huà)水平條形圖案例6. PHP循環(huán)與分支知識(shí)點(diǎn)梳理7. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題8. chat.asp聊天程序的編寫(xiě)方法9. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案10. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器
