Kev*_*ans 5 java javafx jackson
我正在尝试使用Jackson的ObjectMapper将以下Player对象写为String。
package models.Game;
import models.Game.Enums.SnowballState;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import java.util.ArrayList;
import java.util.List;
public class Player {
private Circle circle;
private String name;
private Color color;
private int points = 0;
public int getLives() {
return lives;
}
private int lives = 3;
private List<Snowball> snowballs;
private Circle oldCircle;
private int stepSize = 10;
public Player(String name, Color color) {
this.name = name;
circle = new Circle();
oldCircle = new Circle();
this.color = color;
snowballs = new ArrayList<>();
snowballs.add(new Snowball(this));
snowballs.add(new Snowball(this));
snowballs.add(new Snowball(this));
}
public Player() {
}
private void removeLife() {
this.lives--;
}
public int getHit() {
removeLife();
return getLives();
}
public int shotSuccess() {
points+= 50;
return points;
}
public int getSnowballAmount() {
int balls = 0;
for (Snowball ball : snowballs) {
if (ball.getState() == SnowballState.CREATED) {
balls++;
}
}
return balls;
}
public List<Snowball> getSnowballs() {
return snowballs;
}
public Snowball getNextSnowball() {
for (Snowball ball : snowballs) {
if (ball.getState() == SnowballState.CREATED) {
return ball;
}
}
return null;
}
public void createSnowball() {
if (getSnowballAmount() < 3) {
snowballs.add(new Snowball(this));
}
}
public Color getColor() {
return this.color;
}
public Circle getCircle() {
return this.circle;
}
public void moveLeft() {
saveOld();
circle.setTranslateX(circle.getTranslateX() - stepSize);
}
public void moveRight() {
saveOld();
circle.setTranslateX(circle.getTranslateX() + stepSize);
}
public void moveUp() {
saveOld();
circle.setTranslateY(circle.getTranslateY() - stepSize);
}
public void moveDown() {
saveOld();
circle.setTranslateY(circle.getTranslateY() + stepSize);
}
public void undo() {
circle.setTranslateX(oldCircle.getTranslateX());
circle.setTranslateY(oldCircle.getTranslateY());
}
private void saveOld() {
oldCircle.setTranslateX(circle.getTranslateX());
oldCircle.setTranslateY(circle.getTranslateY());
}
public Snowball shootSnowball(Snowball ball, double mouseX, double mouseY) {
double polarDirection = Math.atan2(mouseY - circle.getTranslateY(), mouseX - circle.getTranslateX() + 50);
ball.setState(SnowballState.ALIVE);
ball.setDirection(polarDirection);
ball.getCircle().setTranslateX(circle.getTranslateX() + 50);
ball.getCircle().setTranslateY(circle.getTranslateY());
return ball;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下命令来执行此操作:
String json = null;
try {
json = objectMapper.writeValueAsString(instanceOfPlayerClass);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到以下相关错误消息:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:类型的无效类型定义
com.sun.javafx.scene.NodeEventDispatcher:无法为[简单类型,类com.sun.javafx.scene.NodeEventDispatcher]构造BeanSerializer:(java.lang.reflect.InaccessibleObjectException)使最终的最终com.sun.javafx.event.BasicEventDispatcher公开。com.sun.javafx.event.BasicEventDispatcher.getPreviousDispatcher()可访问:模块javafx.base不会将com.sun.javafx.event导出到模块com.fasterxml.jackson .databind(通过参考链:models.communication.Websockets.ConnectionSubmitModel [“ player”]-> models.Game.Player [“ circle”]-> javafx.scene.shape.Circle [“ parent”]-> javafx.scene .layout.GridPane [“ parent”]-> javafx.scene.layout.AnchorPane [“ eventDispatcher”])
就像错误所说的那样,这与JavaFx没有导出特定的依赖关系有关,但是由于我不受JavaFx的控制,因此我不确定如何解决此问题。
您试图存储该类Circle,它是一个 JavaFX 类,实际上并不是一个数据类(它是一个 UI 元素),具有许多属性(如半径、厚度、颜色、填充、边框等)。因此,它以各种方式与 JavaFX 系统捆绑在一起,并且不能很好地存储。
相反,只需将所需的信息存储在您自己的一个简单的类中,该类包含您Circle读回时再次创建对象所需的信息。
| 归档时间: |
|
| 查看次数: |
520 次 |
| 最近记录: |