怎么用JAVA编呀,希望各位能帮帮忙。。。。
谢谢了
7 个解决方案
#1
自己先顶下~
#2
作业??
好多人来csdn做作业啊....
好多人来csdn做作业啊....
#3
顶
#4
我这不是作业啊 我那个刚学啊 什么都不会的 所以请高手们帮帮忙啊~
#5
主线程中有两个线程,这个就是错误的认识了。。。
而且你这个问题,最好不用两个线程,
一个就OK了,也就是从main直接跑就行了,不要再创建Thread了。除非你为了更好的UI。
你需要模拟一个时间轴,大致以0.1秒为间隔就OK了。
每0.1sec对两个物体的位置进行一次计算,包括它们的x向位置,和y向的位置。
然后绘制。
另外想到的,你需要熟悉一下 观察者模式,这样写出来的代码才算是美观。
而且你这个问题,最好不用两个线程,
一个就OK了,也就是从main直接跑就行了,不要再创建Thread了。除非你为了更好的UI。
你需要模拟一个时间轴,大致以0.1秒为间隔就OK了。
每0.1sec对两个物体的位置进行一次计算,包括它们的x向位置,和y向的位置。
然后绘制。
另外想到的,你需要熟悉一下 观察者模式,这样写出来的代码才算是美观。
#6
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();
}
}
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();
}
}
#7
写一个线程类啊 定义两个线程 一个处理垂直抛 一个处理45度抛 就可以了
#1
自己先顶下~
#2
作业??
好多人来csdn做作业啊....
好多人来csdn做作业啊....
#3
顶
#4
我这不是作业啊 我那个刚学啊 什么都不会的 所以请高手们帮帮忙啊~
#5
主线程中有两个线程,这个就是错误的认识了。。。
而且你这个问题,最好不用两个线程,
一个就OK了,也就是从main直接跑就行了,不要再创建Thread了。除非你为了更好的UI。
你需要模拟一个时间轴,大致以0.1秒为间隔就OK了。
每0.1sec对两个物体的位置进行一次计算,包括它们的x向位置,和y向的位置。
然后绘制。
另外想到的,你需要熟悉一下 观察者模式,这样写出来的代码才算是美观。
而且你这个问题,最好不用两个线程,
一个就OK了,也就是从main直接跑就行了,不要再创建Thread了。除非你为了更好的UI。
你需要模拟一个时间轴,大致以0.1秒为间隔就OK了。
每0.1sec对两个物体的位置进行一次计算,包括它们的x向位置,和y向的位置。
然后绘制。
另外想到的,你需要熟悉一下 观察者模式,这样写出来的代码才算是美观。
#6
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();
}
}
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();
}
}
#7
写一个线程类啊 定义两个线程 一个处理垂直抛 一个处理45度抛 就可以了