我正在為一個大學專案撰寫一個 java 軟體 - javafx 但我在以圖形方式實作投票功能時遇到了一些問題。
我有一個這樣的政黨圖示:

我想在java中在這個圖示上方放一個X字母,以獲得這樣的東西:

我的解決方案可能是在影像上方畫一條線和另一條線,但我不知道該怎么做。
package unimi.sysvotes.elettore;
import java.io.File;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
public class VotoController {
@FXML Pane listaUnoCandidatoUninominale;
@FXML ImageView listaUnoSimboloUno;
@FXML
private void initialize() {
File fileUno = new File("C:\\Users\\Administrator\\Desktop\\progettazione\\simboli\\popoloDellaLiberta.jpg");
Image imageUno = new Image("file:///" fileUno.getAbsolutePath());
listaUnoSimboloUno.setImage(imageUno);
}
@FXML
private void listaUnoSimboloUnoAction(MouseEvent me) throws IOException {
System.out.println("Votato");
/*
How put Two line above ImageView popolo?
*/
Line lineOne = new Line(10, 10, 80, 80);
lineOne.setFill(null);
lineOne.setStroke(Color.BLACK);
lineOne.setStrokeWidth(2);
Line lineTwo = new Line(80, 10, 10, 80);
lineTwo.setFill(null);
lineTwo.setStroke(Color.BLACK);
lineTwo.setStrokeWidth(2);
}
}
uj5u.com熱心網友回復:
這是基于窗格的替代解決方案。
使用 Pane 而不是 Group,以便可以使用 CSS 設定 Pane 樣式。如果不需要,則可以使用 Group 代替。
分層是由JavaFX 用于渲染的
使用以下代碼將 X 添加到影像頂部的窗格子項中。
public SelectableImageView(Image image) {
imageView = new ImageView(image);
getChildren().add(imageView);
selectionMark = createSelectionMark();
// ... additional listener and initialization code here.
}
private Node[] createSelectionMark() {
double w = imageView.getImage().getWidth();
double h = imageView.getImage().getHeight();
Line l1 = new Line(INSET, INSET, w - INSET, h - INSET);
l1.setStrokeWidth(INSET * .75);
l1.setStrokeLineCap(StrokeLineCap.ROUND);
Line l2 = new Line(INSET, h - INSET, w - INSET, INSET);
l2.setStrokeWidth(INSET * .75);
l2.setStrokeLineCap(StrokeLineCap.ROUND);
return new Node[] { l1, l2 };
}
private void addSelection() {
getChildren().addAll(selectionMark);
}
可執行示例
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import java.util.*;
import java.util.stream.Collectors;
public class SelectableImagesApp extends Application {
private static final double PADDING = 20;
@Override
public void start(Stage stage) throws Exception {
TilePane imageTiles = new TilePane(PADDING, PADDING);
imageTiles.setPadding(
new Insets(PADDING)
);
imageTiles.getChildren().addAll(
createSelectableImages()
);
imageTiles.setPrefColumns(ImageNames.values().length);
stage.setScene(new Scene(imageTiles));
stage.show();
}
public enum ImageNames {
Medusa,
Dragon,
Treant,
Unicorn
}
private List<SelectableImageView> createSelectableImages() {
return Arrays.stream(ImageNames.values())
.map(m ->
new SelectableImageView(
new Image(
Objects.requireNonNull(
SelectableImagesApp.class.getResource(
m "-icon.png"
)
).toExternalForm()
)
)
).collect(Collectors.toList());
}
public static void main(String[] args) {
launch(args);
}
}
class SelectableImageView extends Pane {
private final ImageView imageView;
private final Node[] selectionMark;
private BooleanProperty selected = new SimpleBooleanProperty(false);
private final double INSET = 10;
public SelectableImageView(Image image) {
imageView = new ImageView(image);
getChildren().add(imageView);
selectionMark = createSelectionMark();
selected.addListener((observable, wasSelected, isSelected) -> {
if (isSelected) {
addSelection();
} else {
removeSelection();
}
});
this.setStyle("-fx-background-color: lightblue;");
this.setPickOnBounds(true);
this.setOnMouseClicked(e -> {
selected.set(!selected.get());
});
}
private Node[] createSelectionMark() {
double w = imageView.getImage().getWidth();
double h = imageView.getImage().getHeight();
Line l1 = new Line(INSET, INSET, w - INSET, h - INSET);
l1.setStrokeWidth(INSET * .75);
l1.setStrokeLineCap(StrokeLineCap.ROUND);
Line l2 = new Line(INSET, h - INSET, w - INSET, INSET);
l2.setStrokeWidth(INSET * .75);
l2.setStrokeLineCap(StrokeLineCap.ROUND);
return new Node[] { l1, l2 };
}
private void addSelection() {
getChildren().addAll(selectionMark);
}
private void removeSelection() {
getChildren().removeAll(selectionMark);
}
public boolean isSelected() {
return selected.get();
}
public BooleanProperty selectedProperty() {
return selected;
}
public void setSelected(boolean selected) {
this.selected.set(selected);
}
}
uj5u.com熱心網友回復:
我用過StackPane。
我將畫布和 ImageView 放在堆疊中,然后在畫布中畫線。我還更改了堆疊窗格中節點的順序。
public class VotoController {
@FXML Pane listaUnoCandidatoUninominale;
@FXML StackPane listaUnoSimboloUnoStack;
@FXML ImageView listaUnoSimboloUno;
@FXML Canvas listaUnoSimboloUnoX;
@FXML
private void initialize() {
File fileUno = new File("C:\\Users\\Administrator\\Desktop\\progettazione\\simboli\\popoloDellaLiberta.jpg");
Image imageUno = new Image("file:///" fileUno.getAbsolutePath());
listaUnoSimboloUno.setImage(imageUno);
}
@FXML
private void votoCandidatoUninominalAction(MouseEvent me) throws IOException {
System.out.println("Votato");
listaUnoCandidatoUninominale.setStyle("-fx-background-image: url('file:C:/Users/Administrator/Desktop/progettazione/voto/backgroundcandidatouninominale.jpg');");
}
@FXML
private void listaUnoSimboloUnoAction(MouseEvent me) throws IOException {
System.out.println("Votato");
GraphicsContext gc = listaUnoSimboloUnoX.getGraphicsContext2D();
Line lineOne = new Line(10, 10, 40, 40);
lineOne.setFill(null);
lineOne.setStroke(Color.BLACK);
lineOne.setStrokeWidth(2);
gc.beginPath();
gc.setLineWidth(5);
gc.moveTo(0, 0);
gc.lineTo(0, 0);
gc.lineTo(50, 50);
gc.stroke();
gc.beginPath();
gc.setLineWidth(5);
gc.moveTo(50, 0);
gc.lineTo(50, 0);
gc.lineTo(0, 50);
gc.stroke();
ObservableList<Node> childs = listaUnoSimboloUnoStack.getChildren();
if (childs.size() > 1) {
//
Node topNode = childs.get(childs.size()-1);
topNode.toBack();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/476551.html
