Java SwingFXUtils.toFXImage不是';不要在罐子里工作
我有一个JavaFX画布,用于使用drawUnit()方法绘制GameUnit。 当我使用Maven从IntelliJ运行应用程序时,不会发生错误。但是当我编译Jar并运行它时,SwingFXUtils.toFXImage()方法破坏了我的应用程序。我100%确定问题出在这个方法中,因为我检查了它。SpriteAnimation.nextSprite()方法返回一个buffereImage 这是主要课程:Java SwingFXUtils.toFXImage不是';不要在罐子里工作,java,javafx,Java,Javafx,我有一个JavaFX画布,用于使用drawUnit()方法绘制GameUnit。 当我使用Maven从IntelliJ运行应用程序时,不会发生错误。但是当我编译Jar并运行它时,SwingFXUtils.toFXImage()方法破坏了我的应用程序。我100%确定问题出在这个方法中,因为我检查了它。SpriteAnimation.nextSprite()方法返回一个buffereImage 这是主要课程: public class Game extends Application { p
public class Game extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
stage.setTitle("The Game");
stage.setFullScreenExitKeyCombination(new KeyCharacterCombination(""));
stage.setFullScreenExitHint("");
stage.setFullScreen(true);
Group root = new Group();
Scene scene = new Scene(root);
GameCanvas canvas = new GameCanvas();
canvas.widthProperty().bind(scene.widthProperty());
canvas.heightProperty().bind(scene.heightProperty());
root.getChildren().add(canvas);
new AnimationTimer() {
@Override
public void handle(long now) {
canvas.clear();
canvas.drawUnits();
}
}.start();
stage.setScene(scene);
stage.show();
}
}
这是GameCanvas类:
public class GameCanvas extends Canvas {
public void drawUnits(){
GameData.getAllUnits().forEach(this::drawUnit);
}
public void drawUnit(GameUnit unit){
GraphicsContext context = getGraphicsContext2D();
SpriteAnimation animation = unit.getAnimation();
context.drawImage(
SwingFXUtils.toFXImage(animation.nextSprite(), null),
unit.getPosition().getX(), unit.getPosition().getY(),
unit.getWidth(), unit.getHeight()
);
}
public void clear() {
GraphicsContext context = getGraphicsContext2D();
context.clearRect(0, 0, getWidth(), getHeight());
}
}
这是SpriteAnimation类:
public class SpriteAnimation {
private BufferedImage[] sprites;
private int counter;
private int speed;
private int delay;
public SpriteAnimation(BufferedImage[] sprites, int speed) {
this.sprites = sprites;
this.speed = speed;
counter = 0;
delay = 0;
}
public BufferedImage nextSprite() {
if (delay == speed - 1) {
delay = 0;
counter++;
}
else {
delay++;
}
return sprites[counter % sprites.length];
}
}
这是ImageLoader类:
public class ImageLoader implements Loader<BufferedImage> {
private static final Logger LOGGER = Logger.getLogger(ImageLoader.class.getName());
@Override
public BufferedImage upload(String pathToResource) {
try (InputStream inputStream = ImageLoader.class.getClassLoader().getResourceAsStream(pathToResource))
{
if (inputStream != null) {
return ImageIO.read(inputStream);
}
} catch (IOException e) {
LOGGER.log(Level.INFO, "Can not upload image!");
}
return null;
}
}
private static final ImageLoader LOADER = new ImageLoader();
BufferedImage[] unitsImage = {
LOADER.upload("image/character/TileA1.png"),
LOADER.upload("image/character/TileA2.png")
};
这是pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.some.game</groupId>
<artifactId>The_Game</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.some.game</groupId>
<artifactId>GameData</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.some.game</groupId>
<artifactId>ResourceLoaders</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.some.game.Game</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.some.game.GameLauncher</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
类启动器FXAppLauncher:
public class FXAppLauncher {
public static void main(String[] args) {
FXApp.main(args);
}
}
项目结构:查看pom文件后,需要添加:
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11</version>
</dependency>
我可以在新项目中重现我的问题。现在,我将展示一个完整的示例,其中包含所有代码和pomfile@AntonSorokin太好了,你绝对应该这么做。我怀疑,如果您对pom进行建议的更改,它将解决问题。完成!文章末尾有一个新项目。@AntonSorokin很好,你尝试过建议的更改吗?是的!!!非常感谢你!在pom中添加新模块解决了问题!:)评论不用于扩展讨论;这段对话已经结束。
public class FXAppLauncher {
public static void main(String[] args) {
FXApp.main(args);
}
}
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11</version>
</dependency>
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Color;
import javafx.scene.text.Text;
public class BuffToFx extends Application{
@Override
public void start(Stage primaryStage) throws Exception{
FlowPane root = new FlowPane(5, 5);
primaryStage.setTitle("BufferImage to Fx");
BufferedImage img = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(Color.RED);
g.drawOval(256, 256, 128, 128);
try{
ImageView view = new ImageView( javafx.embed.swing.SwingFXUtils.toFXImage(img, null) );
root.getChildren().add(view);
} catch(Error e){
Text error = new Text(e.toString());
root.getChildren().add(error);
}
Scene scene = new Scene(root, 790, 675);
primaryStage.setScene(scene);
primaryStage.show();
}
}