J2ME中实现的绕圆算法。是我去年玩手机的时候写的个小东西,随后我会写一些关于这个问题的深入思考。
它基本实现效果为中间一个圆,外面一个圆围着中间的这个圆做等距环绕
它基本实现效果为中间一个圆,外面一个圆围着中间的这个圆做等距环绕
代码为:
=================================
package Rounds;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Rect extends MIDlet implements CommandListener{
private Command quit;
private Command pause;
private BallPoint ball;
private int[] o=new int[2];
public int rans(){
Random ran=new Random();
int re=ran.nextInt();
re=re%255;
return re;
}
public class BallPoint extends Canvas{
Random ran=new Random();
int leng=Math.max(getWidth(), getHeight())-10;
int radius=leng/2;
int ox=getWidth()/2;
int oy=getHeight()/2;
double degree=0;
/*
* 利用三角函数求确定角度后小圆心相对大圆心的坐标点
*/
public double[] TFunction(double degree){
double re[]=new double[2];
re[0]=Math.abs(Math.cos(degree)*radius);
re[1]=Math.abs(Math.sqrt(radius*radius-re[0]*re[0]));
return re;
}
public double[] GetXY(double degree){
double re[]={0,0};
if(degree==0||degree==6.28){
re[0]=radius;
}else if(degree==1.57){
re[1]=-radius;
}else if(degree==3.14){
re[0]=-radius;
re[1]=-radius;
}else if(degree==4.71){
re[1]=radius;
}else{
re=TFunction(degree);
if(degree>0&°ree<1.57){
re[1]=-re[1];
}else if(degree>1.57&°ree<3.14){
re[0]=-re[0];
re[1]=-re[1];
}else if(degree>3.14&°ree<4.71){
re[0]=-re[0];
}
}
return re;
}
protected void paint(Graphics graphics) {
graphics.setColor(255, 255, 255);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(255, 0, 0);
graphics.fillArc(ox-15, oy-15, 30, 30, 0, 360);
double re[]=GetXY(degree);
int x=(int)re[0];
int y=(int)re[1];
x=ox+x;
y=oy+y;
graphics.setColor(0,0,0);
graphics.fillArc(x-10,y-10,20,20,0,360);
}
}
private Command pause;
private BallPoint ball;
private int[] o=new int[2];
public int rans(){
Random ran=new Random();
int re=ran.nextInt();
re=re%255;
return re;
}
public class BallPoint extends Canvas{
Random ran=new Random();
int leng=Math.max(getWidth(), getHeight())-10;
int radius=leng/2;
int ox=getWidth()/2;
int oy=getHeight()/2;
double degree=0;
/*
* 利用三角函数求确定角度后小圆心相对大圆心的坐标点
*/
public double[] TFunction(double degree){
double re[]=new double[2];
re[0]=Math.abs(Math.cos(degree)*radius);
re[1]=Math.abs(Math.sqrt(radius*radius-re[0]*re[0]));
return re;
}
public double[] GetXY(double degree){
double re[]={0,0};
if(degree==0||degree==6.28){
re[0]=radius;
}else if(degree==1.57){
re[1]=-radius;
}else if(degree==3.14){
re[0]=-radius;
re[1]=-radius;
}else if(degree==4.71){
re[1]=radius;
}else{
re=TFunction(degree);
if(degree>0&°ree<1.57){
re[1]=-re[1];
}else if(degree>1.57&°ree<3.14){
re[0]=-re[0];
re[1]=-re[1];
}else if(degree>3.14&°ree<4.71){
re[0]=-re[0];
}
}
return re;
}
protected void paint(Graphics graphics) {
graphics.setColor(255, 255, 255);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(255, 0, 0);
graphics.fillArc(ox-15, oy-15, 30, 30, 0, 360);
double re[]=GetXY(degree);
int x=(int)re[0];
int y=(int)re[1];
x=ox+x;
y=oy+y;
graphics.setColor(0,0,0);
graphics.fillArc(x-10,y-10,20,20,0,360);
}
}
class Rounder extends Thread{
public void run(){}
}
public Rect() {
ball=new BallPoint();
quit=new Command("退出",Command.EXIT,1);
ball.addCommand(quit);
pause=new Command("暂停",Command.STOP,2);
ball.addCommand(pause);
ball.setCommandListener(this);
}
public void run(){}
}
public Rect() {
ball=new BallPoint();
quit=new Command("退出",Command.EXIT,1);
ball.addCommand(quit);
pause=new Command("暂停",Command.STOP,2);
ball.addCommand(pause);
ball.setCommandListener(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
Display.getDisplay(this).setCurrent(ball);
Rounder rounder=new Rounder();
for(double i=1;i<=628;i++){
ball.degree=i/100;
ball.repaint();
try{
rounder.sleep(100);
}catch(Exception e){}
}
}
public void commandAction(Command command, Displayable arg1) {
Display.getDisplay(this).setCurrent(ball);
Rounder rounder=new Rounder();
for(double i=1;i<=628;i++){
ball.degree=i/100;
ball.repaint();
try{
rounder.sleep(100);
}catch(Exception e){}
}
}
try{
if(command==quit){
destroyApp(true);
notifyDestroyed();
}
if(command==pause){
pauseApp();
}
}catch(MIDletStateChangeException me){
System.out.println(me+" caught.");
}
}
}
转载于:https://blog.51cto.com/buguaisp2/172662