「用java计算抛物线」简单抛物线计算

博主:adminadmin 2022-11-27 13:35:06 40

今天给各位分享用java计算抛物线的知识,其中也会对简单抛物线计算进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

eclipse中Java在控制台做像抛物线一样输出*

……首先,这个明显不是抛物线,而是sin/cos曲线

我习惯用轻量组件

取两个数,x1和y1,x从最左边到最右边循环赋值,y1=f(x1)

再取两个数,x2和y2,x2就是下一个x1的值,y2=f(x2)

其中f(x)是一个函数,可以是sin(x),也可以是x的平方。

创建一个JPanel,但是别直接定义JPanel类,你需要这样创建:

ClassName variable = new ClassName(parameters);

其中这个ClassName,需要你继承JPanel,并覆盖里边的paintComponent(Graphics g)方法,不这样创建是画不出来的。

接下来就开始画,g.drawLine(x1, y1, x2, y2),精度可能不高,但是效果是如图的。

哎呀我靠逗比了,答完了才看见是在控制台输出的

前面也不用删,但是把

g.drawLine(x1, y1, x2, y2)

换成

g.drawRect(x1, y1, 1, 1)

比较好,x2和y2就扔了吧。

要在控制台输出,先定义一下每行长度和宽度,也就是横坐标和纵坐标。

越多越精细,但是太多了也不行,一行打不出来,会很……

然后用下面的两个句子:

BufferedImage b=new BufferedImage(刚才那个面板的长度、宽度、1是三个需要传递的参数);

某个面板.paint(b.createGraphics());

这样把面板上显示的内容输入在一个名字叫b的图像里

这时候就可以用两个循环嵌套,来挨个检查b上的每一个点的颜色,用这个句子:

int color=b.getRGB(x, y);其中x和y分别是横纵坐标。color就是一个16进制的数字

转成红绿蓝三色,就用下面这个:

int r=(color0xff0000)16, g=(color0xff00)8, b=color0xff;

(重名什么的去死吧!)

然后我们一般都是用黑笔来画函数图像对吧,就用if语句判断红绿蓝是否都为0,如果是则系统打印一个*号,如果不是则系统打印一个空格。

最后再加一行,当横坐标超出时,系统打印一个转行符。

用java画抛物线怎么弄

抛物线是怎么样的? y= a * x *x +b? 这个你比我懂。。

知道方程了,画线就是若干点之间的连线,也就是画 poly

至于用java 画还是c来画 或者是 C#来画,完全就是各种API的调用问题。

我估计你之所以要问,主要是不知道java 图形界面(窗体)如何生成? 那需要用 swing

角度问题 可能是最麻烦的。。。如果我来想,我会: 找到抛物线中最顶的点 P1和两边的等高点 P2,P3 用 p2-p1 得到向量 V1 用p3-p1得到向量 V2 用向量的点积来计算出角度。 当然思考是这样,画的时候就得跟据这种考虑来定点的位置了。。。 仅供参考。

关于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();

}

}

如何用java模拟小球的抛物线运动

Eclipse 开发程序

代码如下:

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

public class paint

{

public static void main(String[] args)

{

CenteredFrame frame=new CenteredFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class CenteredFrame extends JFrame

{

private static final int WIDTH=400;

private static final int HEIGHT=400;

public CenteredFrame()

{

Toolkit kit=Toolkit.getDefaultToolkit();

Dimension screenSize=kit.getScreenSize();

int screenHeight = screenSize.height;

int screenWidth = screenSize.width;

int X = (screenWidth - WIDTH)/2;

int Y = (screenHeight - HEIGHT)/2;

setLocation(X, Y);

setSize(WIDTH,HEIGHT);

Container con=getContentPane();

StringPanel panel=new StringPanel();

con.add(panel);

setResizable(true);

}

}

class StringPanel extends JPanel

{

public void paintComponent(Graphics g)

{

Graphics2D g2=(Graphics2D)g;

int x[]=new int[33];

int y[]=new int[33];

x[0]=0;

y[0]=0;

for(int i=1;i=16;i++)

{

x[i]=184+i;

y[i]=200-(17-i)*(17-i);

x[i+16]=i+200;

y[i+16]=200-i*i;

}

g.drawPolyline(x,y,33);

Line2D L1 = new Line2D.Double(0,200,400,200);

g2.draw(L1);

Line2D L2=new Line2D.Double(200,40,200,400);

g2.draw(L2);

Line2D L3=new Line2D.Double(380,195,395,200);

g2.draw(L3);

Line2D L4=new Line2D.Double(380,205,395,200);

g2.draw(L4);

Line2D L5=new Line2D.Double(200,40,195,55);

g2.draw(L5);

Line2D L6=new Line2D.Double(200,40,205,55);

g2.draw(L6);

g.drawString("X",370,190);

g.drawString("Y",205,40);

}

}

用java计算抛物线的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于简单抛物线计算、用java计算抛物线的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。