「java模拟运动」运动学模拟
今天给各位分享java模拟运动的知识,其中也会对运动学模拟进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java GUI模拟平抛运动和自由落体运动
简单做了一个
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轴比例尺
ListPoint list1 = new ArrayListPoint();
ListPoint list2 = new ArrayListPoint();
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 ArrayListPoint();
list2 = new ArrayListPoint();
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;ilist1.size();i++){
Point pp1 = list1.get(i);
Point pp2 = list2.get(i);
if (ppp1!=nullppp2!=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!=nullppp2!=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();
}
}
java控制红绿灯及模拟车辆运动
写两个程序分别模拟红绿灯和汽车:
1)红绿灯程序以报文形式通知汽车程序;
2)汽车程序需要用多线程来实现。
3)红绿灯程序用个循环,每隔n分钟红绿灯转换,同时通知汽车程序;
JAVA 模拟小球自由落体和平抛运动
这个绝对可以的,我都试过了。请采纳,谢谢。
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class BallThread extends Applet implements Runnable{
Thread red, blue;
Graphics redPen, bluePen;
int t=0;
public void init(){
red = new Thread(this);
blue = new Thread(this);
redPen = getGraphics();
bluePen = getGraphics();
redPen.setColor(Color.red);
bluePen.setColor(Color.blue);
}
public void start(){
red.start();
blue.start();
}
public void run(){
while(true){
t=t+1;
if(Thread.currentThread()==red){
if(t100)t=0;
redPen.clearRect(0,0,110,400);
redPen.fillOval(50,(int)(1.0/2*t*9.8),15,15);
try{
red.sleep(40);
}catch(InterruptedException e){}
}else if(Thread.currentThread()==blue){
bluePen.clearRect(120,0,900,500);
bluePen.fillOval(120+7*t,(int)(1.0/2*t*9.8),15,15);
try{
blue.sleep(40);
}catch(InterruptedException e){}
}
}
}
}
关于java模拟运动和运动学模拟的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-02,除非注明,否则均为
原创文章,转载请注明出处。