1、 编写一个Java GUI应用程序,采用Java多线程技术,模拟自由落体和平抛运动:一个球自由落下,一个球水平抛出。(本题30分)
(自由落体物理公式:h= g *t2/2 ;平抛运动物理公式:h= g *t2/2 ,x=26*t ;
h代表高度,t代表时间,g代表重力加速度=9.8 m/s2 )
2、 编写一个Java GUI应用程序,采用Java多线程技术,有两个线程,模拟垂直上抛运动和水平抛体运动:一个球垂直上抛,一个球水平抛出。(本题30分)
(垂直上抛物理公式:h=v0*t-g*t2/2 ;平抛运动物理公式:h=g*t2/2 ,x=v*t ;
h代表高度,v0代表初速度=30 m/s ,t代表时间,g代表重力加速度=9.8 m/s2 ,v代表平抛速度=30 m/s )
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class Ball extends JFrame{
Ball(){
this.setSize(500,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.show();
JPanel panel=new JPanel();
panel.setSize(500,500);
this.add(panel);
Thread t1=new Thread(new b1(panel.getGraphics()));
Thread t2=new Thread(new b2(panel.getGraphics()));
t1.start();
t2.start();
}
public void paint(Graphics g){
}
public static void main(String arg[]){
new Ball();
}
}
class b1 implements Runnable{
int r;
int x,y;
int p,q;
Graphics g;
public b1(Graphics g){
this.g=g;
r=10;
p=1;
q=10;
}
public void start(){
g.setColor(Color.black);
g.fillOval(x, y, r, r);
try {
Thread.sleep(180);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
}
public void run() {
float t;
t=0;
while(t<10f){
y=(int)(q*t*t/(2));
x=250;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
t+=0.2f;
start();
}
}
}
class b2 implements Runnable{
int r;
int x,y;
int p,q;
Graphics g;
public b2(Graphics g){
this.g=g;
r=10;
p=1;
q=10;
}
public void start(){
g.setColor(Color.black);
g.fillOval(x, y, r, r);
try {
Thread.sleep(180);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
}
public void run() {
float t;
t=0;
while(t<10f){
y=(int)(q*t*t/(2));
x=(int)(26*t);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
t+=0.2f;
start();
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class Ball0 extends JFrame{
Ball0(){
this.setSize(500,500);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.show();
JPanel panel=new JPanel();
panel.setSize(500,500);
this.add(panel);
Thread t1=new Thread(new b3(panel.getGraphics()));
Thread t2=new Thread(new b4(panel.getGraphics()));
t1.start();
t2.start();
}
public void paint(Graphics g){
}
public static void main(String arg[]){
new Ball0();
}
}
class b3 implements Runnable{
int r;
int x,y;
int v,q;
Graphics g;
public b3(Graphics g){
this.g=g;
r=10;
v=30;
q=10;
}
public void start(){
g.setColor(Color.black);
g.fillOval(x, y, r, r);
try {
Thread.sleep(180);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
}
public void run() {
float t;
t=0;
while(t<10f){
y=(int)(q*t*t/(2)-v*t)+250;
x=250;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
t+=0.2f;
start();
}
}
}
class b4 implements Runnable{
int r;
int x,y;
int v,q;
Graphics g;
public b4(Graphics g){
this.g=g;
r=10;
v=30;
q=10;
}
public void start(){
g.setColor(Color.black);
g.fillOval(x, y, r, r);
try {
Thread.sleep(180);
} catch (InterruptedException e) {
e.printStackTrace();
}
g.setColor(Color.white);
g.fillRect(0, 0, 500, 500);
}
public void run() {
float t;
t=0;
while(t<10f){
y=(int)(q*t*t/(2));
x=(int)(v*t);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
t+=0.2f;
start();
}
}
}
怎么越看越像高中物理?居然弄成了编程题了!占个位,有空来回答。