简单做了一个
import java.awt.borderlayout;
import java.awt.button;
import java.awt.color;
import java.awt.frame;
import java.awt.graphics;
import java.awt.panel;
import java.awt.point;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.windowadapter;
import java.awt.event.windowevent;
import java.awt.image.bufferedimage;
import java.util.arraylist;
import java.util.list;
public class myframe extends frame implements runnable,actionlistener{
double x1 = 100,y1 = 0;//a的起始位置
double x2 = 100,y2 = 0;//b的起始位置
double s2 = 26;//b的水平速度
double g = 9.8;//g
long time = 10000;//模拟10秒钟
double py = 1;//y轴比例尺
list list1 = new arraylist();
list list2 = new arraylist();
mpanel p1 = new mpanel();
button b1 = new button("启动");
thread t ;
public myframe(){
b1.addactionlistener(this);
this.add(b1,borderlayout.north);
this.add(p1,borderlayout.center);
this.setsize(800,600);
this.addwindowlistener(new windowadapter(){
public void windowclosing(windowevent e) {
system.exit(0);
}});
this.setvisible(true);
}
public void run(){
list1 = new arraylist();
list2 = new arraylist();
double xx1 = x1,yy1 = y1,xx2 = x2,yy2 = y2;//本时刻位置
long start = system.currenttimemillis();
long end = start;
while(end-start<=time){
end = system.currenttimemillis();
double t = (end - start)/1000.0;
yy1 = (y1 + g*t*t/2)*py;
xx2 = x1 + s2*t;
yy2 = (y2 + g*t*t/2)*py;
int sx1 = (int)xx1;
int sy1 = (int)yy1;
int sx2 = (int)xx2;
int sy2 = (int)yy2;
list1.add(new point(sx1,sy1));
list2.add(new point(sx2,sy2));
p1.repaint();
try {
thread.sleep(50);
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
public void actionperformed(actionevent e) {
if (t==null||!t.isalive()){
t = new thread(this);
t.start();
}
}
public class mpanel extends panel{
bufferedimage im = new bufferedimage(800,600,bufferedimage.type_int_rgb);
public void paint(graphics gg){
graphics g = im.getgraphics();
g.setcolor(color.white);
g.fillrect(0,0,800,600);
g.setcolor(color.blue);
point ppp1 = null;
point ppp2 = null;
for (int i=0;i
point pp1 = list1.get(i);
point pp2 = list2.get(i);
if (ppp1!=null&&ppp2!=null){
g.drawline(pp1.x,pp1.y,ppp1.x,ppp1.y);
g.drawline(pp2.x,pp2.y,ppp2.x,ppp2.y);
}
ppp1 = pp1;
ppp2 = pp2;
}
if (ppp1!=null&&ppp2!=null){
g.filloval(ppp1.x-5,ppp1.y-5,10,10);
g.filloval(ppp2.x-5,ppp2.y-5,10,10);
}
g.dispose();
gg.drawimage(im,0,0,800,600,p1);
}
}
public static void main(string[] args) {
new myframe();
}
}
◆◆
评论读取中....
请登录后再发表评论!
◆◆
修改失败,请稍后尝试