📄 j2me_thread.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
public class J2me_Thread extends MIDlet
{
private Display display;
ShowCanvas canvas;
public J2me_Thread()
{
display = Display.getDisplay(this);
canvas = new ShowCanvas(display);
}
public void startApp(){
System.out.println("startApp is called.");
canvas.start();
}
public void pauseApp(){
System.out.println("pauseApp is called.");
}
public void destroyApp(boolean unconditional){
System.out.println("destroyApp is called.");
}
}
class ShowCanvas extends Canvas
{
Thread firstThread,secondThread;
Display display;
Ceremony ceremony;
DrawQueen queen;
int threadNum;
public ShowCanvas(Display d)
{
display = d; // save the display
threadNum=3;
// initialize the array of balls
ceremony = new Ceremony(this);
queen = new DrawQueen(this);
}
void start() {
display.setCurrent(this);
Thread t1 = new Thread(ceremony);
Thread t2 = new Thread(queen);
t1.start();
t2.start();
repaint();
}
public void paint(Graphics g)
{
int height=getHeight();
int width=getWidth();
int x=15;
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
ceremony.paint(g);
queen.paint(g);
}
}
class Ceremony implements Runnable
{
Graphics g;
Canvas canvas;
int lastxs=0;
int lastys=0;
int lastxm=0;
int lastym=0;
int lastxh=0;
int lastyh=0;
int hour, minute, second;
Ceremony(Canvas c) {
super();
canvas = c;
}
public void run() {
while (true) {
canvas.repaint();
// use the delay to control the speed of the ball
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}
/**
* Paint the ball.
*/
void paint(Graphics g) {
int xh,yh,xm,ym,xs,ys,s,m,h,xcenter,ycenter;
Calendar dat=Calendar.getInstance();
s=dat.get(Calendar.SECOND);
m=dat.get(Calendar.MINUTE);
h=dat.get(Calendar.HOUR);
hour=h%12; minute=m; second=s;
show(g, 120,200,50);
}
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(255,0,0);
g.drawArc(center_x-radius,center_y-radius,radius*2,radius*2,0,360);
theta=(double)(hour*60*60+minute*60+second)/43200.0*2.0*Math.PI;
g.setColor(255,0,0);
drawNiddle(g,center_x,center_y,hrs_nid_len,theta);
theta=(double)(minute*60+second)/3600.0*2.0*Math.PI;
g.setColor(0,255,0);
drawNiddle(g,center_x,center_y,min_nid_len,theta);
theta=(double)second/60.0*2.0*Math.PI;
g.setColor(0,0,255);
drawNiddle(g,center_x,center_y,sec_nid_len,theta);
}
private void drawNiddle(Graphics g,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)));
}
}
class DrawQueen implements Runnable
{
Thread myThread;
int N=0;
Graphics g;
Canvas canvas;
int x,y;
public DrawQueen(Canvas c)
{
super();
canvas = c;
this.x=x;
this.y=y;
}
public void run() {
while (true) {
canvas.repaint();
// use the delay to control the speed of the ball
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
}
}
public void paint(Graphics g)
{
g.setColor(0,0,0);
int n=8;
boolean col[]=new boolean[8];
boolean bias[]=new boolean[16];
boolean isget=false;
int k=0,m=0,odd=0,oddnum=0,b;
int leftBias[]=new int[16];
int a[]=new int[8];
for(m=0;m<9;m++){g.drawLine(18+m*15, 18, 18+m*15, 140);}
for(m=0;m<9;m++){g.drawLine(18, 18+m*15,140, 18+m*15); }
for(k=0;k<n;k++)
{
col[k]=true;
bias[k]=true;
bias[k+n]=true;
leftBias[k+n]=0;
leftBias[k]=0;
}
if(n%2!=0)
{
odd=n/2;
}
else
{
odd=-1;
}
k=0;
m=0;
int i=0,j=0;
for(i=0;i<n+1;i++)
{
if(isget)
break;
if(j==n) //退到的行已经在最后一列
{
b=i-2;
col[a[b]]=true; //把退到行的列置为有效
bias[a[b]-b+n]=true;
leftBias[a[b]+b]--;
j=a[b]+1;
i=b;
}
else
{
j=0;
}
for(;j<n;j++)
{
if(i==0) //如果是零行就顺次摆放
{
if(k>=n/2)
{
if(odd==-1) //如果皇后数是奇数
{
return ;
}
else if(k==n/2+1) //如果皇后数是偶数
{
return ;
}
}
col[k]=false; //被占用的列被标记
bias[k-i+n]=false; //右斜线被标记
leftBias[i+j]++;
a[i]=k++;break;
}
else
{
if(!bias[j-i+n]||!col[j]||leftBias[i+j]!=0) //j列是否已使用和,此斜线是否被用
{
continue;
}
a[i]=j;
if(i==n-1) //找到最后一个点,成功一个
{
m++;
//记录成功数
if(m==(N%45+1))
{
for(int w=0;w<8;w++)
{
g.setColor(23,45,67);
g.fillArc(23+w*15, 23+a[w]*15, 8, 8, 0, 360);
}
isget=false;
N++;
return;
}
}
else{
col[j]=false;
bias[j-i+n]=false;
leftBias[i+j]++;
break;
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -