好文档 - 专业文书写作范文服务资料分享网站

MyEclipse 6 第十八章 图形界面开发--AWT,Swing,SWT

天下 分享 时间: 加入收藏 我要投稿 点赞

图18.9秒表Applet的运行效果

接下来就让我们看看Applet类的代码清单:

applets.StopWatchApplet package applets;

import java.applet.*; import java.awt.*;

import java.awt.event.*;

import javax.swing.ImageIcon; import javax.swing.JButton; /**

* 秒表Applet, 推荐大小200x200。

* 这个Applet展示了参数传递,多线程,图片加载,以及双缓冲技术 * @author BeanSoft * 日期:2008-05 */

public class StopWatchApplet extends Applet implements Runnable, ActionListener {

MediaTracker tracker;// 跟踪图象加载

Image pointImage, startImage, stopImage, resetImage;// 点图片, 开始图片, 停止图片,

Image[] digitals = new Image[10];// 数字0~9图片 Thread clockThread = null;// 主时钟线程

boolean stoped = false;// 秒表停止 int h, m, s, ds;// 时,分,秒,忽秒

// 下面的两个颜色

Color fg;// 背景色

21

刘长炯著

Color bg;// 前景色(文字颜色)

Panel southPanel = new Panel();//放在下部的面板 JButton resetButton;// 清零按钮

JButton controlButton;// 开始/停止 按钮

public void init() {

// 从参数中分析前景和背景色 try { } try { }

bg = Color.decode(getParameter(\)); bg = Color.black;

}catch(Exception e) {

fg = Color.decode(getParameter(\)); fg = Color.green; }catch(Exception e) {

// 设置布局管理器

setLayout(new BorderLayout());

tracker.addImage(pointImage, 0); for (int i = 0; i < 10; i++) { } try {

tracker.waitForAll();// 等待所有图片加载

digitals[i] = getImage(getDocumentBase(), \ + i + tracker.addImage(digitals[i], 0);

tracker = new MediaTracker(this);

pointImage = getImage(getDocumentBase(), \); startImage = getImage(getDocumentBase(), \); stopImage = getImage(getDocumentBase(), \); resetImage = getImage(getDocumentBase(), \); controlButton = new JButton(\停止\, new ImageIcon(stopImage)); resetButton = new JButton(\清零\, new ImageIcon(resetImage)); resetButton.setEnabled(false);

\);

jbInit();

validate();// 使按钮显示出来

clockThread = new Thread(this);

22

clockThread.start();

} catch (Exception e) {

刘长炯著

e.printStackTrace();

// 覆盖以防止闪烁

public void update(Graphics g) {

}

}

paint(g); }

// 主要画图代码,双缓冲技术

public void paint(Graphics g) {

int width = getSize().width;// 窗口宽度 int height = getSize().height;// 窗口高度 int x = 10, y = 0;// X偏移,Y偏移

int imageWidth, imageHeight;// 图片宽度和高度

Image offImage = createImage(width, height);// 缓冲图 Graphics offG = offImage.getGraphics();// 缓冲图图画对象 // 设置背景色,填充图案

offG.setColor(bg);

offG.fillRect(0, 0, width, height);

// 设置前景色

offG.setColor(fg);

// 检查是否加载了所有图片

if (!tracker.checkAll()) {

offG.drawString(\正在加载图片,请稍候...\, 0, height / 2); } else {

// 画出当前时间 imageWidth = digitals[0].getWidth(this); imageHeight = digitals[0].getHeight(this);

offG.drawImage(digitals[(int) Math.floor(h / 10)], x, y, this);

offG.drawImage(digitals[h % 10], x + imageWidth * 1, y, this); offG.drawImage(pointImage, x + imageWidth * 2, y, this); offG.drawImage(digitals[(int) Math.floor(m / 10)], x + imageWidth

* 3, y, this);

offG.drawImage(digitals[m % 10], x + imageWidth * 4, y, this); offG.drawImage(pointImage, x + imageWidth * 5, y, this); offG.drawImage(digitals[(int) Math.floor(s / 10)], x + imageWidth

* 6, y, this);

offG.drawImage(digitals[s % 10], x + imageWidth * 7, y, this);

23

刘长炯著

offG.drawImage(pointImage, x + imageWidth * 8, y, this); offG.drawImage(digitals[ds], x + imageWidth * 9, y, this); String str = \;

if (stoped)

str = \停止\; else

str = \计时\;

offG.drawString(\秒表 - \ + str, x, y + imageHeight * 2); String ss;

if (s < 10)

ss = \ + s;// 格式化为 \ else

ss = \ + s; String sm;

if (m < 10)

sm = \ + m;// 格式化为 \ else

sm = \ + m;

offG.setFont(new Font(\, Font.BOLD, imageHeight)); offG.drawString(h + \ + sm + \ + ss + \ + ds, x, y + imageHeight * 3);

}

// 显示缓冲图

g.drawImage(offImage, 0, 0, this); }

24

// 按钮事件

public void actionPerformed(ActionEvent e) {

Object source = e.getSource(); if (source == controlButton) {

stoped = !stoped; if (stoped) { }

controlButton.setText(\开始\);

controlButton.setIcon(new ImageIcon(startImage)); resetButton.setEnabled(true); clockThread = null;

controlButton.setText(\停止\);

controlButton.setIcon(new ImageIcon(stopImage)); clockThread = new Thread(this); clockThread.start();

resetButton.setEnabled(false);

} else {

} else if (source == resetButton) {

刘长炯著

}

if (stoped) { }

h = m = s = ds = 0;

resetButton.setEnabled(false);

repaint(); } 间

if (++ds == 10)// 1秒 { } try {

ds = 0;

if (++s == 60)// 1分 { }

s = 0;

if (++m == 60)// 1小时 { }

m = 0; ++h;

// 线程代码

public void run() {

while (Thread.currentThread() == clockThread) {

long startTime = System.currentTimeMillis();// 记住线程开始时

repaint();

startTime += 100;

Thread.sleep(Math.max(0, startTime - System.currentTimeMillis()));

25

}

// 确保睡眠100毫秒

} catch (InterruptedException ie) { }

}

// 构造界面

private void jbInit() throws Exception {

southPanel.setBackground(SystemColor.control); resetButton.addActionListener(this); controlButton.addActionListener(this); this.setBackground(Color.black);

this.add(southPanel, BorderLayout.SOUTH);

刘长炯著

MyEclipse 6 第十八章 图形界面开发--AWT,Swing,SWT

图18.9秒表Applet的运行效果接下来就让我们看看Applet类的代码清单:applets.StopWatchAppletpackageapplets;importjava.applet.*;importjava.awt.*;importjava.awt.even
推荐度:
点击下载文档文档为doc格式
5ac5d0bvw21wxgu8jpsa
领取福利

微信扫码领取福利

微信扫码分享