我一直在尋找一種方法來製作Java的靜態可視化表示,但我不確定我發現什麼是最正確的方式來完成即時通訊尋找。JAVA可視化表示
我正在尋找一種方法來簡單地生成一個圖像來表示Java中電路板的狀態。例如,假設我有一個Java中的Checkers的實現,我想要的是在每個單獨的移動之後代表遊戲板的方式。
我看了看JavaFX和Swing,但從我發現它們更適合實際的動態GUI,而不是圖像處理(我可能是錯的)來判斷。
真的有什麼選擇嗎?或者我應該使用任何外部應用程序/軟件?
我一直在尋找一種方法來製作Java的靜態可視化表示,但我不確定我發現什麼是最正確的方式來完成即時通訊尋找。JAVA可視化表示
我正在尋找一種方法來簡單地生成一個圖像來表示Java中電路板的狀態。例如,假設我有一個Java中的Checkers的實現,我想要的是在每個單獨的移動之後代表遊戲板的方式。
我看了看JavaFX和Swing,但從我發現它們更適合實際的動態GUI,而不是圖像處理(我可能是錯的)來判斷。
真的有什麼選擇嗎?或者我應該使用任何外部應用程序/軟件?
你可以嘗試這樣的事情(在JavaFX中),其將呈現一個棋盤的圖像,PNG文件。不知道這是你尋找什麼,因爲你的問題對我來說有點不清楚,但也許對你有幫助。
import javafx.application.*;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.*;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
public class Checkout extends Application {
private static final double SQUARE_SIZE = 20;
private static final double PIECE_RADIUS = SQUARE_SIZE/2.5;
private static final Color WHITE = Color.BEIGE;
private static final Color BLACK = Color.ROSYBROWN;
// setupindex, player, position (1-32 index into top to bottom black squares on the board).
private static final int[][][] setups =
{
{
{31, 27, 19},
{17, 12, 5}
},
{
{30, 26, 18},
{19, 11, 10},
},
{
{31, 24, 19},
{18, 11, 10}
}
};
private static final String SNAPSHOT_FILE_PREFIX = "snapshot-";
private static final String SNAPSHOT_FILE_SUFFIX = ".png";
private WritableImage snapshotFxImage =
new WritableImage((int) (SQUARE_SIZE * 8), (int) (SQUARE_SIZE * 8));
private BufferedImage snapshotBufferedImage =
new BufferedImage((int) (SQUARE_SIZE * 8), (int) (SQUARE_SIZE * 8), BufferedImage.TYPE_INT_ARGB);
private static final String USER_HOME =
System.getProperties().getProperty("user.home");
@Override
public void start(Stage stage) throws IOException {
Group layout = new Group();
renderBoard(layout);
Scene scene = new Scene(layout);
int i = 0;
for (int[][] setup : setups) {
List<Node> pieces = renderSetup(setup);
layout.getChildren().addAll(pieces);
snapshot(scene, i);
layout.getChildren().removeAll(pieces);
i++;
}
Platform.setImplicitExit(false);
Platform.exit();
}
private void renderBoard(Group layout) {
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
Rectangle square = new Rectangle(
c * SQUARE_SIZE,
r * SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE
);
Color fill =
(((c + r) % 2) == 0)
? Color.WHITE
: Color.BLACK;
square.setFill(fill);
layout.getChildren().add(square);
}
}
}
private List<Node> renderSetup(int[][] setup) {
List<Node> pieces = new ArrayList<>();
for (int player = 0; player < 2; player++) {
for (int pieceIndex = 0; pieceIndex < setup[player].length; pieceIndex++) {
Color fill =
player == 0
? WHITE
: BLACK;
Circle piece = new Circle(PIECE_RADIUS, fill);
pieces.add(piece);
movePieceTo(piece, setup[player][pieceIndex]);
}
}
return pieces;
}
private void movePieceTo(Circle circle, int p) {
int r = (p - 1)/4;
int c = ((p - 1) % 4) * 2 + (((r % 2) == 0) ? 1 : 0);
circle.setCenterX(c * SQUARE_SIZE + SQUARE_SIZE/2);
circle.setCenterY(r * SQUARE_SIZE + SQUARE_SIZE/2);
}
private void snapshot(Scene scene, int snapshotIdx) throws IOException {
Path path = Paths.get(
USER_HOME,
SNAPSHOT_FILE_PREFIX + snapshotIdx + SNAPSHOT_FILE_SUFFIX
);
scene.snapshot(snapshotFxImage);
SwingFXUtils.fromFXImage(snapshotFxImage, snapshotBufferedImage);
ImageIO.write(snapshotBufferedImage, "png", path.toFile());
System.out.println("Saved: " + path.toString());
}
public static void main(String[] args) {
launch(args);
}
}
您可以使用Graphics
類繪製到內存中的圖像。然後,您可以將其保存到文件或以某種方式顯示。繼承人如何你可能會開始做一個例子:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SO45990731 {
public static void main(String [] args) throws IOException {
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
// TODO draw your checker board
g.drawOval(100, 100, 300, 300);
// cleanup and write
g.dispose();
ImageIO.write(image, "png", new File("board.png"));
}
}
不是最簡單的解決方案,但以簡單的方式解決了問題,非常感謝你很多 – eXistanCe
@eXistanCe這種方法是用Java繪製圖像的一種非常標準的方式。你也可以考慮閱讀一下你的電路板的圖像,然後繪製peices(或者使用'g.drawImage()'將它們從其他圖像放置到結果上)。這將使得創建一個漂亮的輸出而不用java中的所有內容變得更容易。 –
@eXistanCe幾乎忘了,'Graphics'對象可以轉換爲'Graphics2D'類,爲繪圖提供更多的功能。 –
也許你應該看看[演出風俗繪畫(https://docs.oracle.com/ javase/tutorial/uiswing/painting /)和[圖形入門](https://docs.oracl e.com/javase/tutorial/2d/basic2d/index.html) – MadProgrammer
'即時通訊尋找的是一種方式,在每一個動作之後,代表遊戲板。「 - 如果你的意思是你只是想創建一個圖像,那麼你可以使用[Screen Image](https://tips4java.wordpress.com/2008/10/13/screen-image/)類。它允許您創建任何Swing組件的圖像。 – camickr
「即時通訊尋找的是一種方式,在每一個動作之後,代表遊戲板」 - >這不是一個「實際的動態GUI」嗎?你能更好地解釋你想做什麼嗎?什麼是「Java的靜態視覺表示」? – jewelsea