当前位置: 技术问答>java相关
Applet中怎样局部重画
来源: 互联网 发布时间:2015-09-03
本文导语: 全部重画时,闪烁的太厉害 | 1.用双重缓冲 2.局部重画 给你一个小程序 //MyClock.java import java.awt.Graphics; import java.awt.Color; import java.applet.Applet; import java.text.*; import java.util.*; import java.applet...
全部重画时,闪烁的太厉害
|
1.用双重缓冲
2.局部重画
给你一个小程序
//MyClock.java
import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;
import java.text.*;
import java.util.*;
import java.applet.AudioClip;
public class MyClock extends Applet implements Runnable {
SimpleDateFormat formatter; //用来显示时间的样式
Date currentDate; // 现在时间
Thread clockThread = null;
boolean keepRunning = true;
Clock myClock;
AudioClip di;
public void init() {
int x,y;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
currentDate = new Date();
di=getAudioClip(getCodeBase(),"audio/di.au");
}
public void start() {
if(clockThread == null) {
clockThread = new Thread(this);
clockThread.start();
}
}
public void run() {
while(keepRunning) {
repaint();
di.play();
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
}
}
//用此方法取代stop(),来要求线程停止执行
public void pleaseStop() { keepRunning = false; }
public void stop(){
if(clockThread!=null){
pleaseStop();
}
}
public void paint(Graphics g) {
int s, m, h;
String today;
setBackground(Color.white);
currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 0;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 0;
}
formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
today = formatter.format(currentDate);
myClock = new Clock(h, m, s);
myClock.show(g, 70, 70, 70);
}
public void update(Graphics g) {
myClock.clear(g, 70, 70, 70, getBackground());
g.setColor(getForeground());
paint(g);
}
}
class Clock {
Clock(int hrs, int min, int sec) {
hour = hrs % 12;
minute = min;
second = sec;
}
void show(Graphics g, int center_x, int center_y, int radius) {
int hrs_nid_len = (int)(radius * 0.5), // 定义时针的长度
min_nid_len = (int)(radius * 0.7), // 定义分针的长度
sec_nid_len = (int)(radius * 0.85); // 定义秒针的长度
double theta;
// 画出钟面
g.setColor(Color.green);
g.drawOval(center_x - radius, center_y - radius,
radius * 2, radius * 2);
g.setColor(Color.blue);
g.drawString("1",105,20);
g.drawString("2",125,45);
g.drawString("3",center_x*2-5,center_y);
g.drawString("4",125,105);
g.drawString("5",105,125);
g.drawString("6",center_x,center_y*2);
g.drawString("7",35,130);
g.drawString("8",10,105);
g.drawString("9",0,center_y);
g.drawString("10",10,45);
g.drawString("11",35,20);
g.drawString("12",center_x-5,10);
// 画出时针
theta = (double)(hour * 60 * 60 + minute * 60 + second) /
43200.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, hrs_nid_len, theta);
// 画出分针
theta = (double)(minute * 60 + second) /
3600.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, min_nid_len, theta);
// 画出秒针
theta = (double)second / 60.0 * 2.0 * Math.PI;
drawNiddle(g, Color.red, center_x, center_y, sec_nid_len, theta);
}
void clear(Graphics g, int center_x, int center_y, int radius, Color clearColor) {
int hrs_nid_len = (int)(radius * 0.5), // 定义时针的长度
min_nid_len = (int)(radius * 0.7), // 定义分针的长度
sec_nid_len = (int)(radius * 0.85); // 定义秒针的长度
double theta;
// 清除时针
theta = (double)(hour * 60 * 60 + minute * 60 + second) /
43200.0 * 2.0 * Math.PI;
drawNiddle(g, clearColor, center_x, center_y, hrs_nid_len, theta);
// 清除分针
theta = (double)(minute * 60 + second) /
3600.0 * 2.0 * Math.PI;
drawNiddle(g, clearColor, center_x, center_y, min_nid_len, theta);
// 清除秒针
theta = (double)second / 60.0 * 2.0 * Math.PI;
drawNiddle(g, clearColor, center_x, center_y, sec_nid_len, theta);
}
private void drawNiddle(Graphics g, Color c, int x, int y,
int len, double theta) {
g.setColor(c);
g.drawLine(x, y, (int)(x + len * Math.sin(theta)),
(int)(y - len * Math.cos(theta)));
}
int hour, minute, second;
}
2.局部重画
给你一个小程序
//MyClock.java
import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;
import java.text.*;
import java.util.*;
import java.applet.AudioClip;
public class MyClock extends Applet implements Runnable {
SimpleDateFormat formatter; //用来显示时间的样式
Date currentDate; // 现在时间
Thread clockThread = null;
boolean keepRunning = true;
Clock myClock;
AudioClip di;
public void init() {
int x,y;
formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
currentDate = new Date();
di=getAudioClip(getCodeBase(),"audio/di.au");
}
public void start() {
if(clockThread == null) {
clockThread = new Thread(this);
clockThread.start();
}
}
public void run() {
while(keepRunning) {
repaint();
di.play();
try { Thread.sleep(1000); }
catch(InterruptedException e) {}
}
}
//用此方法取代stop(),来要求线程停止执行
public void pleaseStop() { keepRunning = false; }
public void stop(){
if(clockThread!=null){
pleaseStop();
}
}
public void paint(Graphics g) {
int s, m, h;
String today;
setBackground(Color.white);
currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
try {
s = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
s = 0;
}
formatter.applyPattern("m");
try {
m = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
m = 0;
}
formatter.applyPattern("h");
try {
h = Integer.parseInt(formatter.format(currentDate));
} catch (NumberFormatException n) {
h = 0;
}
formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
today = formatter.format(currentDate);
myClock = new Clock(h, m, s);
myClock.show(g, 70, 70, 70);
}
public void update(Graphics g) {
myClock.clear(g, 70, 70, 70, getBackground());
g.setColor(getForeground());
paint(g);
}
}
class Clock {
Clock(int hrs, int min, int sec) {
hour = hrs % 12;
minute = min;
second = sec;
}
void show(Graphics g, int center_x, int center_y, int radius) {
int hrs_nid_len = (int)(radius * 0.5), // 定义时针的长度
min_nid_len = (int)(radius * 0.7), // 定义分针的长度
sec_nid_len = (int)(radius * 0.85); // 定义秒针的长度
double theta;
// 画出钟面
g.setColor(Color.green);
g.drawOval(center_x - radius, center_y - radius,
radius * 2, radius * 2);
g.setColor(Color.blue);
g.drawString("1",105,20);
g.drawString("2",125,45);
g.drawString("3",center_x*2-5,center_y);
g.drawString("4",125,105);
g.drawString("5",105,125);
g.drawString("6",center_x,center_y*2);
g.drawString("7",35,130);
g.drawString("8",10,105);
g.drawString("9",0,center_y);
g.drawString("10",10,45);
g.drawString("11",35,20);
g.drawString("12",center_x-5,10);
// 画出时针
theta = (double)(hour * 60 * 60 + minute * 60 + second) /
43200.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, hrs_nid_len, theta);
// 画出分针
theta = (double)(minute * 60 + second) /
3600.0 * 2.0 * Math.PI;
drawNiddle(g, Color.blue, center_x, center_y, min_nid_len, theta);
// 画出秒针
theta = (double)second / 60.0 * 2.0 * Math.PI;
drawNiddle(g, Color.red, center_x, center_y, sec_nid_len, theta);
}
void clear(Graphics g, int center_x, int center_y, int radius, Color clearColor) {
int hrs_nid_len = (int)(radius * 0.5), // 定义时针的长度
min_nid_len = (int)(radius * 0.7), // 定义分针的长度
sec_nid_len = (int)(radius * 0.85); // 定义秒针的长度
double theta;
// 清除时针
theta = (double)(hour * 60 * 60 + minute * 60 + second) /
43200.0 * 2.0 * Math.PI;
drawNiddle(g, clearColor, center_x, center_y, hrs_nid_len, theta);
// 清除分针
theta = (double)(minute * 60 + second) /
3600.0 * 2.0 * Math.PI;
drawNiddle(g, clearColor, center_x, center_y, min_nid_len, theta);
// 清除秒针
theta = (double)second / 60.0 * 2.0 * Math.PI;
drawNiddle(g, clearColor, center_x, center_y, sec_nid_len, theta);
}
private void drawNiddle(Graphics g, Color c, int x, int y,
int len, double theta) {
g.setColor(c);
g.drawLine(x, y, (int)(x + len * Math.sin(theta)),
(int)(y - len * Math.cos(theta)));
}
int hour, minute, second;
}
|
建议加一个JPanel试试。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。