程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java鐘表實例

java鐘表實例

編輯:關於JAVA

對於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();

}

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved