import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class XYThrowMain {
static class XYThrow extends JPanel {
double x1, y1; // 垂直上抛坐标
double x21, y21, x22, y22; // 斜抛坐标
double v01 = 6000;
boolean stop = false; //停止标志
int t = 0;
double v02 = 100; //斜抛出去的初速度
double g = 9.8; //重力加速度
double xscale = 300; //为了方便作图而做的缩小变换
double yscale = 20000;
private void setAffine(Graphics2D g) {
AffineTransform a1 = new AffineTransform(1, 0, 0, -1, 50, 100);
g.setTransform(a1);
}
@Override
public void paint(Graphics g1) {
super.paint(g1);
//System.out.println(x21 + " " + y21 + " " + x22 + " " + y22);
Graphics2D g = (Graphics2D) g1;
setAffine(g);
g.setPaint(Color.white);
coordinate(g);
paintV(g);
paintTow(g);
}
private void coordinate(Graphics2D g) {
g.drawLine(0, -900, 0, 100); // y
g.drawLine(-100, 0, 900, 0);// x
Paint tmp = g.getPaint();
g.setPaint(Color.red);
g.drawString("0", -10, -5); //坐标原点
g.drawString("x", 700, 10);
g.setPaint(Color.black); //画一条基准线,方便观察
g.drawLine(20, -900, 20, 100);
g.setPaint(tmp);
}
public void start() throws InterruptedException {
new Thread(new VerticalThrow()).start();
new Thread(new ParabolaThrow()).start();
for (int i = 0; i < 250; i++) {
t += 10; //模拟时间进度
Thread.sleep(100);
repaint();
}
}
private List<Line2D> towList = new ArrayList<Line2D>();
private void paintTow(Graphics2D g) {
g.setPaint(Color.red);
towList.add(new Line2D.Double(x21 / xscale, y21 / yscale, x22
/ xscale, y22 / yscale));
for (Line2D l : towList) {
g.draw(l);
}
}
private void paintV(Graphics2D g) {
Paint tmp = g.getPaint();
g.setPaint(Color.red);
g.fill(new Ellipse2D.Double(x1,y1/yscale,3,3));
g.setPaint(tmp);
}
@Override
public void update(Graphics g) {
paint(g);
}
class VerticalThrow implements Runnable {
@Override
public void run() {
while (!stop) {
x1 = 20;
y1 = v01 * t - 0.5 * g * Math.pow(t, 2);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class ParabolaThrow implements Runnable {
@Override
public void run() {
while (!stop) {
x21 = x22;
y21 = y22;
x22 = v02 * t *Math.cos(45);
y22 = v02 * t * Math.sin(45) - 0.5 * g * Math.pow(t, 2);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
final JFrame jframe = new JFrame();
jframe.addWindowListener(new WindowAdapter(){
@Override
public void windowClosed(WindowEvent e) {
jframe.dispose();
}
});
XYThrow jpanel = new XYThrow();
jframe.add(jpanel);
jframe.setBounds(50, 50, 900, 800);
jframe.setVisible(true);
jpanel.start();
}
}