导航:首页 > 编程语言 > java运动轨迹

java运动轨迹

发布时间:2022-09-25 20:35:30

1. java双缓冲。一个小球运动的动画,但是轨迹和预期不一样,不能循环播放。求指导!!感激不尽!!

public class Ball extends Applet implements Runnable {
int x = 0;
int y = 0;
int m = 3;
int n = 4;
Thread t;
Image buffer;
Graphics bufferg;

public void init() {
this.setSize(400, 400);
t = new Thread(this);
t.start();
Dimension d = getSize();
buffer = createImage(d.width, d.height);
}

public void run() {
try {
while (true) {
repaint();
Thread.sleep(200);
}
} catch (Exception e) {
}
}

public void update(Graphics g) {
paint(g);
}

public void paint(Graphics g) {
if (bufferg == null)
bufferg = buffer.getGraphics();
Dimension d = getSize();
bufferg.setColor(Color.white);
bufferg.fillRect(0, 0, d.width, d.height);
bufferg.setColor(Color.blue);
bufferg.fillOval(x, y, 30, 30);
g.drawImage(buffer, 0, 0, this);
if (x + 30 > d.width || x < 0) {
m *= -1;
}
if (y + 30 > d.height || y < 0) {
n *= -1;
}
x += m;
y += n;
}
}

这是根据你的程序改的, 遇到边就弹, 但是你说的要弹回原来的轨迹, 这个应该和你的起始位置有关系吧
比如你设置边框为450 600, 起始位置为(0, 200), 步长为(9, 12), 应该能回到起始位置
我这里步长设的3和4, 方便调试.

2. Java如何让多个图片都按照一定轨迹下落

图片的位移(下落),可以通过修改图片的x,y坐标来实现, 在Swing/Html中,我们可以使用Timer定时(比如每隔100毫秒)去修改图片的x,y坐标即可实现,

多个图片都按照一定的轨迹移动,那都按照自己的轨迹的算法,去定时修改x,y坐标即可.

JavaFX是java先进的图形界面框架, 里面有3D和各种动画, 所以按照轨迹移动,都能轻松实现

importjavafx.animation.Animation;
importjavafx.animation.Interpolator;
importjavafx.animation.PathTransition;
importjavafx.animation.RotateTransition;
importjavafx.application.Application;
importjavafx.geometry.Insets;
importjavafx.scene.Group;
importjavafx.scene.Scene;
importjavafx.scene.control.Button;
importjavafx.scene.image.ImageView;
importjavafx.scene.layout.HBox;
importjavafx.scene.shape.MoveTo;
importjavafx.scene.shape.Path;
importjavafx.scene.shape.QuadCurveTo;
importjavafx.stage.Stage;
importjavafx.util.Duration;

{
publicstaticvoidmain(String[]args){
launch(args);
}

@Override
publicvoidstart(StageprimaryStage)throwsException{
ImageViewimv=newImageView(getClass().getResource("ball.png").toExternalForm());
Pathpath=newPath();//路径;运动轨迹
MoveTomt=newMoveTo(20,50);
QuadCurveToquadTo2=newQuadCurveTo(175,190,350,30);
path.getElements().addAll(mt,quadTo2);

HBoxhbox=newHBox(10);

ButtonbtnStart=newButton("开始");
ButtonbtnPause=newButton("暂停");
ButtonbtnResume=newButton("继续");
ButtonbtnStop=newButton("结束");
hbox.getChildren().addAll(btnStart,btnPause,btnResume,btnStop);
hbox.setPadding(newInsets(20));
hbox.setLayoutX(80);
hbox.setLayoutY(230);
Grouproot=newGroup();

root.getChildren().addAll(imv,path,hbox);//不添加path.就可以不显示path了

Scenescene=newScene(root,430,300);

primaryStage.setTitle("JavaFX");
primaryStage.setScene(scene);
primaryStage.show();
//旋转动画设置
RotateTransitionrt=newRotateTransition(Duration.millis(1000),imv);
rt.setInterpolator(Interpolator.LINEAR);
rt.setFromAngle(0);
rt.setToAngle(360);
rt.setCycleCount(Animation.INDEFINITE);
rt.play();


//路径动画设置
PathTransitionpt=newPathTransition(Duration.millis(800),path,imv);//路径动画
pt.setCycleCount(Animation.INDEFINITE);
pt.setAutoReverse(true);
btnStart.setOnAction(e->{
pt.playFromStart();//从头开始播放

});

//----按钮的响应设置---
btnPause.setOnAction(e->{
pt.pause();
});
btnResume.setOnAction(e->{
pt.play();//播放
});
btnStop.setOnAction(e->{
pt.jumpTo(newDuration(0));//跳到第0秒处
pt.stop();
});
}
}

3. 请问C++或Java能控制机器人运动吗吗

能肯定是能。不过他们的控制还是依赖于更底层的单片机以及电路结构。也就是说,你可以用java实现一个类似人工智能的功能,给出一个指令。例如:利用java,从摄像头采集数据做智能分析,然后计算出一个机械手臂的运动轨迹。然而,java的功能也就到此了。这个机械手臂如何按照指定的轨迹运动呢?它需要一些马达来控制关节的旋转。这些马达都是由单片机控制的。单片机可以由C语言编程,或者汇编语言编程。
java是大脑。而底层的C语言你可以认为是小脑和肌肉神经。

4. java 画了一个圆,怎么让它上下左右移动啊

移动圆,改变它的圆心即可,可以通过给圆心设置一个运动轨迹函数实现,实例代码为;


{
//继承
privateintx=100,y=100,r=100;

//初始值
publicjoinDemo1()
{
super("小图形");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(800,600);
this.setVisible(true);
Threadthread=newThread(newGraphicss());
thread.start();

}
publicvoidpaint(Graphicsg)
{
super.paint(g);
g.fillOval(x,y,r,r);
}
publicstaticvoidmain(String[]args)
{
newjoinDemo1();
}

{
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub

for(intj=0;j<=240;j++){
revalidate();
//System.out.println(j);
try{
Thread.sleep(1000);//当前线程休眠0.01秒

}catch(InterruptedExceptione){
e.printStackTrace();
}

y=y+j;
repaint();

}

}
}
}

5. 高人帮帮忙:做一个java程序,一个红球,一个蓝色的球,运行时两球都会移动

好复杂的说,激起了我写程序的欲望,不过欲望归欲望,能力归能力

先要实现一个计算小球直线运动轨迹的函数,并把轨迹映射到像素,毕竟我们要让他显示在桌面上,然后是碰撞反射,就是改变小球运动方向,其实不管是球碰球,还是球碰墙,都是一样的道理,球碰球需要根据半径,碰撞的位置建立一道虚拟的墙,

计算路径——〉监测碰撞——〉改变方向——〉移动

奇怪,怎么回按原路返回?特殊要求啊?

6. java动画,图像运动会留下运动轨迹,如何解决

public void paintComponent(Graphics g) {
super.paintComponent(g);
// TODO

}

7. 如何用java编写一个T形四颗*的上下左右移动

移动圆,改变它的圆心即可,可以通过给圆心设置一个运动轨迹函数实现,实例代码为;
public class joinDemo1 extends JFrame{ //继承 private int x=100, y=100, r=100; //初始值 public joinDemo1() { super("小图形"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(800, 600); this.setVisible(true); Thread thread=new Thread(new Graphicss()); thread.start(); } public void paint(Graphics g) { super.paint(g); g.fillOval(x, y, r, r); } public static void main(String[] args) { new joinDemo1(); } class Graphicss implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for (int j = 0; j <= 240; j++) { revalidate(); // System.out.println(j); try { Thread.sleep(1000);// 当前线程休眠0.01秒 } catch (InterruptedException e) { e.printStackTrace(); } y=y+j; repaint(); } }}}

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

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

importjava.awt.Dimension;

importjava.awt.Graphics;

importjava.awt.event.MouseEvent;

importjava.util.Vector;

importjavax.swing.JFrame;

importjavax.swing.event.MouseInputAdapter;

{

=1L;

=800;

=600;

Printerprinter;

Zombieszombies=newZombies();

ThreadT_Zombies;

Bulletbullet=newBullet();

ThreadT_Bullet;

publicPlantsAndZombies(){

this.setSize(newDimension(screenWidth,screenHeight));

this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

this.addMouseListener(newShoot(this));

this.setVisible(true);

printer=newPrinter(this.getGraphics());

printer.Add(zombies);

printer.Add(bullet);

T_Zombies=newThread(zombies);

T_Zombies.start();

T_Bullet=newThread(bullet);

T_Bullet.start();

}

publicvoidShoot(){

bullet.getTarget(zombies);

}

publicstaticvoidmain(String[]args){

PlantsAndZombiesgame=newPlantsAndZombies();

}

publicvoidrun(){

while(true){

}

}

}

interfaceDrawable{

voiddrawMe(Graphicsg);

}

,Runnable{

publicbooleanisLive=true;

publicintx=PlantsAndZombies.screenWidth;

publicinty=500;

publicvoidrun(){

while(true){

if(x>10){

x-=20;

}elsex=PlantsAndZombies.screenWidth;

try{

Thread.sleep(500);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

publicvoiddrawMe(Graphicsg){

g.drawRect(x,y,20,50);

}

}

classBulletimplementsDrawable,Runnable{

privateintx=0;

privateinty=500;

privateZombies_z;

privatefloata,b,c;

privatefloatstep;

publicvoidgetTarget(Zombiesz){

_z=z;

//用三点确定一个抛物线的方法,计算弹道

intx1=0;

inty1=500;

intx2=(z.x-6*20)/2;

inty2=300;//抛物线高度200个像素

intx3=z.x-6*20;//假设击中僵尸用3秒钟,在这3秒钟内僵尸向前移动了6*20个像素

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

}

publicvoidrun(){

while(true){

try{

x+=step;

y=(int)(a*x*x+b*x+c);

if(y>500){

_z.isLive=false;

}

Thread.sleep(50);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

publicvoiddrawMe(Graphicsg){

g.drawRect(x,y,20,20);

}

}

classPrinterextendsThread{

privateVector<Drawable>v=newVector<Drawable>();

privateGraphics_g;

publicPrinter(Graphicsg){

_g=g;

this.start();

}

publicvoidAdd(Drawableo){

v.add(o);

}

publicvoidrun(){

while(true){

_g.clearRect(0,0,PlantsAndZombies.screenWidth,PlantsAndZombies.screenHeight);

for(Drawableo:v){

o.drawMe(_g);

}

try{

Thread.sleep(500);

}catch(InterruptedExceptione){

e.printStackTrace();

}

}

}

}

{

privatePlantsAndZombies_adaptee;

publicShoot(PlantsAndZombiesadaptee){

_adaptee=adaptee;

}

publicvoidmouseClicked(MouseEvente){

_adaptee.Shoot();

}

}

9. Java中怎么控制一个物体做抛物线运动

Graphics g 相当于是个画笔功能,所以程序肯定是要用到的
不过可以设置最基本的功能后,在外部设置物体的运动轨迹,再调用repaint功能就行。

10. java里弹球游戏怎么改变运动轨迹

通过控制线程的方式,改变弹珠在屏幕中的像素点,来做出移动的效果!

阅读全文

与java运动轨迹相关的资料

热点内容
argon2d算法的币 浏览:50
世界上最简单的解压神器 浏览:566
一人之下小说txt全文 浏览:584
.超大尺度男男电影 浏览:396
无法找到加密狗将进入演示模式 浏览:134
韩国李彩谭主演的电影 浏览:560
redisphp管理 浏览:958
被人切掉蛋蛋电影 浏览:894
美国最新女机器人电影 浏览:22
万达电影院用英语怎么说 浏览:123
伊朗人购买加密货币 浏览:373
杭州哪儿找程序员 浏览:268
警察卧底监狱韩国电影叫什么电影 浏览:607
app激活小米移动网络连接到服务器地址 浏览:84
决策树归纳算法 浏览:873
欧美以小孩为主角的电影 浏览:432
肉写得好的古言作者 浏览:187
韩国电影失踪国语在线观看 浏览:40
盗墓电影免费大全 浏览:178
内地大尺度电影 浏览:297