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

Python019

Java如何让多个图片都按照一定轨迹下落,第1张

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

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

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

JavaFX参考代码如下

import javafx.animation.Animation

import javafx.animation.Interpolator

import javafx.animation.PathTransition

import javafx.animation.RotateTransition

import javafx.application.Application

import javafx.geometry.Insets

import javafx.scene.Group

import javafx.scene.Scene

import javafx.scene.control.Button

import javafx.scene.image.ImageView

import javafx.scene.layout.HBox

import javafx.scene.shape.MoveTo

import javafx.scene.shape.Path

import javafx.scene.shape.QuadCurveTo

import javafx.stage.Stage

import javafx.util.Duration

public class PathAnimateDemo extends Application {

public static void main(String[] args) {

launch(args)

}

@Override

public void start(Stage primaryStage) throws Exception {

ImageView imv=new ImageView(getClass().getResource("ball.png").toExternalForm())

Path path = new Path()// 路径运动轨迹

MoveTo mt = new MoveTo(20, 50)

QuadCurveTo quadTo2 = new QuadCurveTo(175, 190, 350, 30)

path.getElements().addAll(mt, quadTo2)

HBox hbox = new HBox(10)

Button btnStart = new Button("开始")

Button btnPause = new Button("暂停")

Button btnResume = new Button("继续")

Button btnStop = new Button("结束")

hbox.getChildren().addAll(btnStart, btnPause, btnResume, btnStop)

hbox.setPadding(new Insets(20))

hbox.setLayoutX(80)

hbox.setLayoutY(230)

Group root = new Group()

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

Scene scene = new Scene(root, 430, 300)

primaryStage.setTitle("JavaFX")

primaryStage.setScene(scene)

primaryStage.show()

//旋转动画设置

RotateTransition rt=new RotateTransition(Duration.millis(1000),imv)

rt.setInterpolator(Interpolator.LINEAR)

rt.setFromAngle(0)

rt.setToAngle(360)

rt.setCycleCount(Animation.INDEFINITE)

rt.play()

//路径动画设置

PathTransition pt = new PathTransition(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(new Duration(0))// 跳到第0秒处

pt.stop()

})

}

}

Java不支持有标识符、类、接口、枚举、方法、变量名称中使用中文,修改为:

import javax.swing.*

import java.awt.Color

import java.awt.Graphics

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.Timer

public class Tetris extends JFrame{

public static void main(String[] args) {

Tetris frame = new Tetris()

JMenuBar menu = new JMenuBar()

frame.setJMenuBar(menu)

JMenu game = new JMenu("游戏")

JMenuItem newgame = game.add("新游戏")

JMenuItem goon = game.add("继续")

JMenuItem exit = game.add("退出")

JMenu help = new JMenu("帮助")

JMenuItem about = help.add("关于")

menu.add(game)

menu.add(help)

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

frame.setSize(400, 500)

frame.setTitle("Tetris内测版")

frame.setVisible(true)

frame.setResizable(true)//即可以用鼠标拖大,小窗口

}

public Tetris(){

add(new a())

}

static class a extends JPanel {

int[][] luos=new int[20][10]

public a() {

Timer timer = new Timer(100, new TimerListener())

timer.start()

myDown()

}

void myDown() {

for(int h=18h>=0h--) {

for(int l=0l<10l++) {

if(luos[h][l]==1) {

luos[h][l]=0

luos[h+1][l]=1

}

}

}

}

public void paintComponent(Graphics g) {

super.paintComponent(g)

createDown()

listDown(g)

//画框框

g.setColor(Color.gray)

g.drawRect(20,30,300,400)

//画分数格子

g.setColor(Color.gray)

g.drawRect(325,30,60,400)

}

void createDown() {

luos[8][4]=1luos[8][5]=1

luos[9][4]=1luos[9][5]=1

}

void listDown(Graphics g) {

for(int h=0h<20h++) {

for(int l=0l<10l++) {

if(luos[h][l]==1) {

g.setColor(Color.red)

g.drawRect(h*20,l*20,20,20)}

}

}

}

class TimerListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

repaint()

}

}

}

}

下面是代码,我以前写过类似的游戏,你可以直接用

Thread MONEY_THREAD//移动图片的线程

int y = 0//图片y轴坐标

public void run() {

Thread MoneyThread = Thread.currentThread()//启动线程

while(MONEY_THREAD == MoneyThread){

y += 5//每次下落5像素

try {

Thread.sleep(5)//5毫秒画一次

} catch (InterruptedException ex) {

ex.printStackTrace()

}

repaint()

if(y >= 500){//如果到达低端

moneyImgInit()//将图片重置到顶端

stopMoneyThread()//停止线程

}

}

}