| |
| java钟表实例 |
| |
发布者: 发布时间:2007-11-26 |
|
|
|
对于java中钟表的开发,许多初学者很感兴趣,先分析如下:应用技术:java多线程,java图像加载,java双缓冲技术多媒体:用于指针走动,字幕显示。双缓冲:用于消除图像的闪烁。参考代码:package picture;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.applet.*;import java.util.Date;import java.io.*;import java.awt.geom.*;import java.awt.Color;import java.awt.image.*;public class Clock extends Applet implements MouseListener, Runnable,ActionListener {Image image, image1;Toolkit tool;JPanel panel;Graphics gg;int width, height, width1, height1;Thread thread, thread2;MediaTracker m;double angel1 = 0, angel2 = 0;int xsec, ysec, xsec2, ysec2;int c = 0;int xmin, ymin, xmin2, ymin2;int xhou, yhou, xhou2, yhou2;int xstr = 235, ystr = 253;int y = ystr;JButton button1 = new JButton();JButton button2 = new JButton();JButton button3 = new JButton();JButton button4 = new JButton();AudioClip music;Color colorh = Color.GREEN, colorm = Color.BLACK, colors = Color.YELLOW;String[] string = new String[5];int kk = 0;public void init() {this.setLayout(new BorderLayout());this.setBackground(Color.BLACK);tool = getToolkit();image = tool.getImage("image.PNG");m = new MediaTracker(this);m.addImage(image, 0);try {m.waitForID(0);} catch (Exception e) {System.out.println(e.getMessage());}width1 = image.getWidth(this);height1 = image.getHeight(this);width = this.WIDTH;height = this.HEIGHT;// music =Applet.newAudioClip(new URL("D:\\JAVA\\Picture\\钟声.wav")) ;// music.loop();this.addMouseListener(this);button1.setText("时针颜色");button1.addActionListener(this);button2.setText("分针颜色");button2.addActionListener(this);button3.setText("秒针颜色");button3.addActionListener(this);button4.setText("选择皮肤");button4.addActionListener(this);button1.setBackground(Color.BLACK);button1.setForeground(Color.WHITE);button2.setBackground(Color.BLACK);button2.setForeground(Color.WHITE);button3.setBackground(Color.BLACK);button3.setForeground(Color.WHITE);button4.setBackground(Color.BLACK);button4.setForeground(Color.WHITE);JPanel panel = new JPanel();panel.setBackground(Color.BLACK);panel.setLayout(new FlowLayout(FlowLayout.CENTER));panel.add(button1);panel.add(button2);panel.add(button3);panel.add(button4);this.setLayout(new BorderLayout());this.add(panel, BorderLayout.SOUTH);string[0] = "时间就像海绵里水";string[1] = "只要挤总会有的";string[2] = "珍惜身边每一个人";string[3] = "因为随着岁月流逝,";string[4] = "他们将......";image1 = createImage(getWidth(), getHeight() - 35);gg = image1.getGraphics();}public void start() {if (thread == null) {thread = new Thread(this);thread.start();}if (thread2 == null) {thread2 = new Thread(new thread2());thread2.start();}}public void paint(Graphics g) {super.paint(g);g.drawImage(image1, 0, 0, this);Date date = new Date();int hour = date.getHours();int min = date.getMinutes();int sec = date.getSeconds();String m = Integer.toString(hour) + ":" + Integer.toString(min) + ":" +Integer.toString(sec);double ans = (Math.PI / 30) * sec;double anm = (3.1415926535897931D / 30) * min;double anh = ((hour + min / 60.0) * (2 * Math.PI / 12));xsec2 = 172 + (int) (45 * Math.sin(ans));ysec2 = 165 - (int) (45 * Math.cos(ans));xsec = 172 - (int) (13 * Math.sin(ans));ysec = 165 + (int) (13 * Math.cos(ans));xmin2 = 172 + (int) (40 * Math.sin(anm));ymin2 = 165 - (int) (40 * Math.cos(anm));xmin = 172 - (int) (10 * Math.sin(anm));ymin = 165 + (int) (10 * Math.cos(anm));xhou = 172 + (int) (30 * Math.sin(anh));yhou = 165 - (int) (30 * Math.cos(anh));xhou2 = 172 - (int) (10 * Math.sin(anh));yhou2 = 165 + (int) (10 * Math.cos(anh));g.setColor(colors); //秒针g.drawLine(xsec, ysec, xsec2, ysec2);g.setColor(colorm); //分针g.drawLine(xmin, ymin, xmin2, ymin2);g.setColor(colorh); //时针g.drawLine(xhou2, yhou2, xhou, yhou);g.setColor(Color.RED);g.fillOval(169, 162, 9, 9);g.drawString(m, 150, 100);g.setColor(Color.WHITE);g.drawString(string[0], xstr, y);g.drawString(string[1], xstr, y + 20);g.drawString(string[2], xstr, y + 40);g.drawString(string[3], xstr, y + 60);g.drawString(string[4], xstr, y + 80);}public void update(Graphics g) {paint(g);}public void run() {while (true) {try {thread.sleep(1000);gg.setColor(Color.WHITE);gg.fillRect(0, 0, width, height);gg.drawImage(image, 110, 100, width1, height1, this);repaint();} catch (Exception e) {System.out.print(e.getMessage());}}}private class thread2 implements Runnable {public void run() {while (true) {try {thread2.sleep(100);} catch (Exception e) {}y--;if (y <= 5) {y = ystr;}repaint();}}}public void mouseClicked(MouseEvent e) {System.out.print(e.getX());System.out.print(e.getY());}public void mouseExited(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void fileSelect() {JFileChooser choose = new JFileChooser();choose.setFileSelectionMode(JFileChooser.FILES_ONLY);choose.setCurrentDirectory(new File("."));choose.setFileFilter(new javax.swing.filechooser.FileFilter() {public boolean accept(File file) {String name = file.getName().toLowerCase();return name.endsWith(".gif")|| name.endsWith(".jpg")|| name.endsWith(".jpeg")|| file.isDirectory();}public String getDescription() {return "图片文件";}});int result = choose.showOpenDialog(this);String name = null;if (result == JFileChooser.APPROVE_OPTION) {name = choose.getSelectedFile().getAbsolutePath();}image = tool.getImage(name);m.addImage(image, 0);try {m.waitForAll();} catch (Exception e) {System.out.print(e.getMessage());}}public void actionPerformed(ActionEvent e) {if (e.getSource() == button1 || e.getSource() == button2 ||e.getSource() == button3) {JColorChooser choose = new JColorChooser();Color c = choose.showDialog(this, "颜色选取", Color.BLACK);if (c == null) {c = Color.BLACK;}if (e.getSource() == button1) {colorh = c;}if (e.getSource() == button2) {colorm = c;}if (e.getSource() == button3) {colors = c;}}if (e.getSource() == button4) {fileSelect();}}}建立工程后,可以更改钟表皮肤,图片只有自己找了。有意见请指教。qq号:564541093
|
| (转载文章请保留出处:北天JAVA技术网(www.java114.com)) |
| |
| 更多精彩文章: |
| 编写你自己的单点登录(SSO)服务 |
| 用java读取各种计算机文件系统的文件列表 |
| 关于applet写入文件的处理 |
| Java语言实用技巧:常用VI高级命令集锦 |
| Java程序的加密和反加密 |
| WebSphere Studio 5.0发布 |
| |
| 最近评论: |
|
|
| 鍥炲 |
|
|
|
| 那个雨天的想法! |
| wow gold,wow power leveling.wow power leveling,wow power leveling,
max(5065) |
|
|
| 如果真的有来生! |
| 四川旅游,九寨沟旅游,稻城亚丁旅游,四姑娘山旅游,海螺沟旅游,西藏旅游,
max(2491) |
|
|
| 如果真的有来生! |
| 四川旅游,九寨沟旅游,稻城亚丁旅游,四姑娘山旅游,海螺沟旅游,西藏旅游,
max(6336) |
|
|
| 那天的情景! |
| Maple Story mesos,MapleStory mesos,ms mesos,mesos,SilkRoad Gold,
max(2867) |
|
|
| 如果真的有来生! |
| 四川旅游,九寨沟旅游,稻城亚丁旅游,四姑娘山旅游,海螺沟旅游,西藏旅游,
max(8700) |
|
|
| 左边的风景! |
| wow gold,wow power leveling.wow power leveling,wow power leveling,
max(9969) |
|
|
| 轻轻走过你的窗前! |
| world of warcraft gold,cheap world of warcraft gold,warcraft gold,world of warcraft gold,cheap world of warcraft gold,warcraft gold, max(8668) |
|
|
| 轻轻走过你的窗前! |
| world of warcraft gold,cheap world of warcraft gold,warcraft gold,world of warcraft gold,cheap world of warcraft gold,warcraft gold max(6061) |
|
|
| 不在的哪天! |
| final fantasy xi gil,final fantasy xi gil,final fantasy xi gil,final fantasy xi gil,
max(3422) |
|
|
| |
| 免责声明:该文章由网友发表,如果对您造成侵权,请联系站长。 |
|