
正文
java抛物线轨迹源代码 编程抛物线
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java一个小圆球抛物线运动,请问抛物线的运动怎么实现?
1、首先描一个坐标轴
2、确定方程式
3、打点
4、连线
5、取出打点的坐标,按照顺序依次变更颜色(做出运动效果)
6、简单的一元二次方程举例【步骤5留给题主思考】
public class View extends JFrame {
public View() {
JFrame frame = new JFrame("Equation");
frame.getContentPane().setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(50, 50));
JLabel labelA = new JLabel();
labelA.setText("a");
JTextField textA = new JTextField("0",3);
JLabel labelB = new JLabel();
labelB.setText("b");
JTextField textB = new JTextField("0",3);
JLabel labelC = new JLabel();
labelC.setText("c");
JTextField textC = new JTextField("0",3);
JButton draw = new JButton();
draw.setText("Draw");
draw.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Controller.a = Double.parseDouble(textA.getText());
Controller.b = Double.parseDouble(textB.getText());
Controller.c = Double.parseDouble(textC.getText());
repaint();
frame.pack();
frame.setSize(420,490);
}
});
panel1.add(labelA);
panel1.add(textA);
panel1.add(labelB);
panel1.add(textB);
panel1.add(labelC);
panel1.add(textC);
panel1.add(draw);
JPanel panel2 = new JPanel(){
public void paint(Graphics g){
super.paint(g);
Controller.grid(g);
Controller.Graphic1(g);
}
};
panel2.setBackground(Color.WHITE);
frame.getContentPane().add(panel1, BorderLayout.PAGE_START);
frame.getContentPane().add(panel2, BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(420,490);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
View frame = new View();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
public class Controller {
static double a=2, b=1, c=0;
public static void grid (Graphics g){
g.setColor(Color.blue);
g.drawLine(200,0,200,400);
g.drawLine(0,200,400,200);
for (int x=0; x=400; x= x +40){
g.drawLine(x,195,x,205);
}
for (int y=0; y=400; y=y+40){
g.drawLine(195,y,205,y);
}
}
public static void Graphic1(Graphics g) {
g.setColor(Color.red);
for (double x=-100;x=100;x = x+0.1){
double y = a * x * x + b * x + c;
int X = (int)Math.round(200 + x*20);
int Y = (int)Math.round(200 - y*20);
g.fillOval(X-2,Y-2,4,4);
}
}
}
相关问答
Q1: 关于java中模拟抛物线轨迹的问题
看了这套题目感觉很有兴趣java抛物线轨迹源代码,就花了一个中午亲手给你写了一个类似的例子java抛物线轨迹源代码,相信可以帮助你对这个游戏有很好的理解,从右向左那个是僵尸,点一下鼠标就出现植物,我只是起到一个抛砖引玉的作用。代码如下(绝对可以用的代码):
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.event.MouseInputAdapter;
public class PlantsAndZombies extends JFrame {
private static final long serialVersionUID = 1L;
public static final int screenWidth=800;
public static final int screenHeight=600;
Printer printer;
Zombies zombies=new Zombies();
Thread T_Zombies;
Bullet bullet=new Bullet();
Thread T_Bullet;
public PlantsAndZombies(){
this.setSize(new Dimension(screenWidth,screenHeight));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.addMouseListener(new Shoot(this));
this.setVisible(true);
printer=new Printer( this.getGraphics());
printer.Add(zombies);
printer.Add(bullet);
T_Zombies=new Thread(zombies);
T_Zombies.start();
T_Bullet=new Thread(bullet);
T_Bullet.start();
}
public void Shoot(){
bullet.getTarget(zombies);
}
public static void main(String[] args){
PlantsAndZombies game=new PlantsAndZombies();
}
public void run() {
while(true){
}
}
}
interface Drawable{
void drawMe(Graphics g);
}
class Zombies implements Drawable,Runnable{
public boolean isLive=true;
public int x=PlantsAndZombies.screenWidth;
public int y=500;
public void run() {
while(true){
if(x10){
x-=20;
}else x=PlantsAndZombies.screenWidth;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void drawMe(Graphics g){
g.drawRect(x,y,20,50);
}
}
class Bullet implements Drawable,Runnable{
private int x=0;
private int y=500;
private Zombies _z;
private float a,b,c;
private float step;
public void getTarget(Zombies z){
_z=z;
// 用三点确定一个抛物线的方法,计算弹道
int x1=0;
int y1=500;
int x2=(z.x-6*20)/2;
int y2=300; // 抛物线高度200个像素
int x3=z.x-6*20; // 假设击中僵尸用3秒钟,在这3秒钟内僵尸向前移动了6*20个像素
int y3=500;
a=(float)((y2-y1)*(x3-x2)-(y3-y2)*(x2-x1))/(float)((x2*x2-x1*x1)*(x3-x2)-(x3*x3-x2*x2)*(x2-x1));
b=(float)((y2-y1)-a*(x2*x2-x1*x1))/(float)(x2-x1);
c=y1-a*x1*x1-b*x1;
step=(float)(x3-x1)/(float)(3*20);
}
public void run() {
while(true){
try {
x+=step;
y=(int)(a*x*x+b*x+c);
if(y500){
_z.isLive=false;
}
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void drawMe(Graphics g) {
g.drawRect(x,y,20,20);
}
}
class Printer extends Thread{
private VectorDrawable v=new VectorDrawable();
private Graphics _g;
public Printer(Graphics g){
_g=g;
this.start();
}
public void Add(Drawable o){
v.add(o);
}
public void run(){
while(true){
_g.clearRect(0,0,PlantsAndZombies.screenWidth,PlantsAndZombies.screenHeight);
for(Drawable o:v){
o.drawMe(_g);
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Shoot extends MouseInputAdapter{
private PlantsAndZombies _adaptee;
public Shoot(PlantsAndZombies adaptee){
_adaptee=adaptee;
}
public void mouseClicked(MouseEvent e) {
_adaptee.Shoot();
}
}
Q2: Java中怎么控制一个物体做抛物线运动?
Graphics g 相当于是个画笔功能,所以程序肯定是要用到的
不过可以设置最基本的功能后,在外部设置物体的运动轨迹,再调用repaint功能就行。
Q3: JAVA 抛物线和坐标轴
这个其实很简单 sine在java函数里面传的是幅度值java抛物线轨迹源代码,java抛物线轨迹源代码你用一for循环自增变量表示幅度的增加,用sine(x)值的函数值表示y值这样不断的画点 ,密集的点就可以形成sianwave,当然你还可以选择 画线段 相邻两个点之间的线段越密集就越容易形成sine线
Q4: 关于java中抛物线轨迹的模拟
僵尸每次移动,计算出僵尸的坐标再+上僵尸移动的速度差,重新计算抛物线。经过我自主研发,我确定没有问题。给分我吧,哥们
关于java抛物线轨迹源代码和编程抛物线的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







