Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1561597
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:16:09

一起学习
对于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钟表实例java钟表实例java钟表实例java钟表实例java钟表实例java钟表实例java钟表实例java钟表实例java钟表实例java钟表实例java钟表实例java钟表实例
阅读(213) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~