J2ME代码...请解释一下..谢谢..
原代码如下:很多变量的设置我都不知道是什么意思.请哪位大侠解释解释..特别是实现线程的publicvoidrun(){}方法..谢谢publicclassImageDem...
原代码如下:很多变量的设置我都不知道是什么意思.请哪位大侠解释解释..
特别是实现线程的public void run() {}方法..谢谢
public class ImageDemo extends Canvas implements Runnable, CommandListener {
Command start=new Command("开始",Command.OK,1);
Command stop=new Command("停止",Command.STOP,1);
private Image offscreen;
public ImageDemo(){
addCommand(start);
setCommandListener(this);
if(isDoubleBuffered()){
System.out.println("支持双缓冲区");
}else{
System.out.println("不支持双缓冲区,启动自制双缓冲区");
//可变的Image
offscreen=Image.createImage(getWidth(),getHeight());
}
}
protected void paint(Graphics g) {
if(isDoubleBuffered()){
System.out.println("On_Screen 绘图");
//调用clear,清屏
clear(g);
//调用paintAnimation绘制动画
paintAnimation(g,100,10,r);
//调用paintCross,画十字架
paintCross(g,x,y,length);
}else{
System.out.println("Off-Screen 绘图");
Graphics offg=offscreen.getGraphics();
clear(offg);
paintAnimation(offg,100,10,r);
paintCross(offg,x,y,length);
g.drawImage(offscreen,0,0,0);
}
}
public void clear(Graphics g){
//把屏幕清成白色
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
}
int r=0;
public void paintAnimation(Graphics g,int x,int y,int l){
g.setColor(0,0,0);
g.drawRect(x, y, l, l); //绘制矩形
}
int x=50;
int y=50;
int length=5;
public void paintCross(Graphics g ,int x,int y,int length){
g.setColor(255,0,0);
//合成十字架
g.drawLine(x-length,y,x+length,y);
g.drawLine(x,y-length,x,y+length);
}
boolean conti=false;
public void commandAction(Command c, Displayable s) {
String cmd=c.getLabel();
if(cmd.equals("停止")){
conti=false;
removeCommand(stop); //移除stop Command
addCommand(start);
}else if(cmd.equals("开始")){
removeCommand(start); //移除start
addCommand(stop);
conti=true;
Thread t=new Thread(this); //线程实现
t.start();
}
}
int rate=100; // 每1/20秒画一次
//实现线程run()
public void run() {
long s=0;
long e=0;
long diff=0;
while(conti){
s=System.currentTimeMillis();
r++;
if(r>10)
r=0;
repaint();
serviceRepaints();
e=System.currentTimeMillis();
diff=e-s;
if(diff<rate){
try{
Thread.sleep(rate-diff);
}catch(Exception exc){
}
}
}
}
protected void keyPressed(int keycode){
switch(getGameAction (keycode)){
case Canvas.UP:
y=y-2;
break;
case Canvas.DOWN:
y=y+2;
break;
case Canvas.LEFT:
x=x-2;
break;
case Canvas.RIGHT:
x=x+2;
break;
}
repaint();
}
}
还是迷糊..我可能太笨了.学起来还好..就这个难以理解
谢谢回答 展开
特别是实现线程的public void run() {}方法..谢谢
public class ImageDemo extends Canvas implements Runnable, CommandListener {
Command start=new Command("开始",Command.OK,1);
Command stop=new Command("停止",Command.STOP,1);
private Image offscreen;
public ImageDemo(){
addCommand(start);
setCommandListener(this);
if(isDoubleBuffered()){
System.out.println("支持双缓冲区");
}else{
System.out.println("不支持双缓冲区,启动自制双缓冲区");
//可变的Image
offscreen=Image.createImage(getWidth(),getHeight());
}
}
protected void paint(Graphics g) {
if(isDoubleBuffered()){
System.out.println("On_Screen 绘图");
//调用clear,清屏
clear(g);
//调用paintAnimation绘制动画
paintAnimation(g,100,10,r);
//调用paintCross,画十字架
paintCross(g,x,y,length);
}else{
System.out.println("Off-Screen 绘图");
Graphics offg=offscreen.getGraphics();
clear(offg);
paintAnimation(offg,100,10,r);
paintCross(offg,x,y,length);
g.drawImage(offscreen,0,0,0);
}
}
public void clear(Graphics g){
//把屏幕清成白色
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
}
int r=0;
public void paintAnimation(Graphics g,int x,int y,int l){
g.setColor(0,0,0);
g.drawRect(x, y, l, l); //绘制矩形
}
int x=50;
int y=50;
int length=5;
public void paintCross(Graphics g ,int x,int y,int length){
g.setColor(255,0,0);
//合成十字架
g.drawLine(x-length,y,x+length,y);
g.drawLine(x,y-length,x,y+length);
}
boolean conti=false;
public void commandAction(Command c, Displayable s) {
String cmd=c.getLabel();
if(cmd.equals("停止")){
conti=false;
removeCommand(stop); //移除stop Command
addCommand(start);
}else if(cmd.equals("开始")){
removeCommand(start); //移除start
addCommand(stop);
conti=true;
Thread t=new Thread(this); //线程实现
t.start();
}
}
int rate=100; // 每1/20秒画一次
//实现线程run()
public void run() {
long s=0;
long e=0;
long diff=0;
while(conti){
s=System.currentTimeMillis();
r++;
if(r>10)
r=0;
repaint();
serviceRepaints();
e=System.currentTimeMillis();
diff=e-s;
if(diff<rate){
try{
Thread.sleep(rate-diff);
}catch(Exception exc){
}
}
}
}
protected void keyPressed(int keycode){
switch(getGameAction (keycode)){
case Canvas.UP:
y=y-2;
break;
case Canvas.DOWN:
y=y+2;
break;
case Canvas.LEFT:
x=x-2;
break;
case Canvas.RIGHT:
x=x+2;
break;
}
repaint();
}
}
还是迷糊..我可能太笨了.学起来还好..就这个难以理解
谢谢回答 展开
2个回答
展开全部
Thread t=new Thread(this); //线程实现
t.start();
启动了线程开始运行run方法
//实现线程run()
public void run() {
long s=0;
long e=0;
long diff=0;
while(conti){ //这个一开始就是false 当被置为ture的时候 run方法结束 线程结束 应用结束
s=System.currentTimeMillis(); //得到绘制前的时间
r++;
if(r>10)
r=0;
repaint(); //调用paint方法
serviceRepaints(); //强制重绘
e=System.currentTimeMillis(); //得到绘制后的时间
diff=e-s; //得到时间差
if(diff<rate){ //如果绘制花的时间没达到rate(100ms)
try{
Thread.sleep(rate-diff); //线程暂停一段时间
}catch(Exception exc){
}
}
}
}
t.start();
启动了线程开始运行run方法
//实现线程run()
public void run() {
long s=0;
long e=0;
long diff=0;
while(conti){ //这个一开始就是false 当被置为ture的时候 run方法结束 线程结束 应用结束
s=System.currentTimeMillis(); //得到绘制前的时间
r++;
if(r>10)
r=0;
repaint(); //调用paint方法
serviceRepaints(); //强制重绘
e=System.currentTimeMillis(); //得到绘制后的时间
diff=e-s; //得到时间差
if(diff<rate){ //如果绘制花的时间没达到rate(100ms)
try{
Thread.sleep(rate-diff); //线程暂停一段时间
}catch(Exception exc){
}
}
}
}
威孚半导体技术
2024-08-19 广告
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层...
点击进入详情页
本回答由威孚半导体技术提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询