成都创新互联网站制作重庆分公司

java写出抛物线代码 java实现平抛

关于java中模拟抛物线轨迹的问题

看了这套题目感觉很有兴趣,就花了一个中午亲手给你写了一个类似的例子,相信可以帮助你对这个游戏有很好的理解,从右向左那个是僵尸,点一下鼠标就出现植物,我只是起到一个抛砖引玉的作用。代码如下(绝对可以用的代码):

创新互联基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业服务器主机托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。

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一个小圆球抛物线运动,请问抛物线的运动怎么实现?

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);

}

}

}

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写出抛物线代码 java实现平抛
文章出自:http://cxhlcq.com/article/dosgcsc.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部