導航:首頁 > 編程語言 > 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運動軌跡相關的資料

熱點內容
用圖片的地圖再編譯的地方 瀏覽:462
python監控系統進程 瀏覽:236
群暉怎麼取消照片共享文件夾 瀏覽:156
程序員那麼可愛第幾集陸璃懷孕 瀏覽:615
西門子st編程手冊 瀏覽:59
mt4編程書籍 瀏覽:21
單片機模擬實驗設置電壓 瀏覽:948
如何用電腦打開安卓手機內存 瀏覽:860
java數據訪問層 瀏覽:181
代碼優化是編譯程序的必要階段 瀏覽:623
程序員那麼可愛孩子還在嗎 瀏覽:513
以下哪些是資料庫編程技術 瀏覽:164
水冷壓縮冷凝機組 瀏覽:177
小米路由器app怎麼加黑名單 瀏覽:433
證券交易2012pdf 瀏覽:208
單線程和多線程編譯 瀏覽:155
游戲被加密了刪不了怎麼辦 瀏覽:475
二建6米的柱子加密多少箍筋 瀏覽:648
怎麼簡單易懂的了解伺服器 瀏覽:356
mcpe怎麼看伺服器地址 瀏覽:994