下面列出了javafx.scene.text.Text#setTextAlignment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a Callback of a standard cell factory for a table cell.
*
* <p>The cell factory represents the String content as a JavaFX Text object using the
* "tableText" style class. The cell is given a click handler from newCellClickHandler().
*
* @param widthProperty the widthProperty of this cell's column
* @param contextMenu the context menu shown when right clicking
* @return a Callback of a standard cell factory for a table cell
*/
private Callback<TableColumn<Chapter, String>, TableCell<Chapter, String>> newStringCellFactory(
ReadOnlyDoubleProperty widthProperty, ContextMenu contextMenu) {
return tc -> {
TableCell<Chapter, String> cell = new TableCell<>();
cell.getStyleClass().add("tableCell");
Text text = new Text();
text.getStyleClass().add("tableText");
text.setTextAlignment(TextAlignment.LEFT);
cell.setGraphic(text);
text.wrappingWidthProperty().bind(widthProperty);
text.textProperty().bind(cell.itemProperty());
cell.setOnMouseClicked(newCellClickHandler(contextMenu));
return cell;
};
}
private void drawText() {
final double CENTER_X = 0.5 * width;
final double CENTER_Y = 0.5 * height;
final int NO_OF_SECTORS = getNoOfSectors();
Font font = Fonts.latoRegular(0.035 * size);
double radAngle = RadarChartMode.SECTOR == getMode() ? Math.toRadians(180 + angleStep * 0.5) : Math.toRadians(180);
double radAngleStep = Math.toRadians(angleStep);
textGroup.getChildren().clear();
for (int i = 0 ; i < NO_OF_SECTORS ; i++) {
double r = size * 0.48;
double x = CENTER_X - size * 0.015 + (-Math.sin(radAngle) * r);
double y = CENTER_Y + (+Math.cos(radAngle) * r);
Text text = new Text(data.get(i).getName());
text.setFont(font);
text.setFill(data.get(i).getTextColor());
text.setTextOrigin(VPos.CENTER);
text.setTextAlignment(TextAlignment.CENTER);
text.setRotate(Math.toDegrees(radAngle) - 180);
text.setX(x);
text.setY(y);
textGroup.getChildren().add(text);
radAngle += radAngleStep;
}
}
private void initGraphics() {
if (Double.compare(getPrefWidth(), 0.0) <= 0 || Double.compare(getPrefHeight(), 0.0) <= 0 || Double.compare(getWidth(), 0.0) <= 0 ||
Double.compare(getHeight(), 0.0) <= 0) {
if (getPrefWidth() > 0 && getPrefHeight() > 0) {
setPrefSize(getPrefWidth(), getPrefHeight());
} else {
setPrefSize(PREFERRED_WIDTH, PREFERRED_HEIGHT);
}
}
canvas = new Canvas(PREFERRED_WIDTH, PREFERRED_HEIGHT);
ctx = canvas.getGraphicsContext2D();
text = new Text(String.format(Locale.US, "%.0f%%", getValue() * 100.0));
text.setTextAlignment(TextAlignment.CENTER);
text.setTextOrigin(VPos.TOP);
text.setFill(getTextColor());
Helper.enableNode(text, getTextVisible());
getChildren().setAll(canvas, text);
}
private Text getLabel(String pString)
{
Text label = new Text();
if (aUnderlined)
{
label.setUnderline(true);
}
label.setFont(getFont());
label.setBoundsType(TextBoundsType.VISUAL);
label.setText(pString);
if(aAlignment == Align.LEFT)
{
label.setTextAlignment(TextAlignment.LEFT);
}
else if(aAlignment == Align.CENTER)
{
label.setTextAlignment(TextAlignment.CENTER);
}
else if(aAlignment == Align.RIGHT)
{
label.setTextAlignment(TextAlignment.RIGHT);
}
return label;
}
private Text createCenteredText(double cx, double cy, String styleClass) {
Text text = new Text();
text.getStyleClass().add(styleClass);
text.setTextOrigin(VPos.CENTER);
text.setTextAlignment(TextAlignment.CENTER);
double width = cx > ARTBOARD_SIZE * 0.5 ? ((ARTBOARD_SIZE - cx) * 2.0) : cx * 2.0;
text.setWrappingWidth(width);
text.setBoundsType(TextBoundsType.VISUAL);
text.setY(cy);
text.setX(cx - (width / 2.0));
return text;
}
@Override
public void initializeParts() {
value = new Text(0, ARTBOARD_HEIGHT * 0.5, String.format(FORMAT, getSkinnable().getValue()));
value.getStyleClass().add("value");
value.setTextOrigin(VPos.CENTER);
value.setTextAlignment(TextAlignment.CENTER);
value.setMouseTransparent(true);
value.setWrappingWidth(ARTBOARD_HEIGHT);
value.setBoundsType(TextBoundsType.VISUAL);
thumb = new Circle(ARTBOARD_HEIGHT * 0.5, ARTBOARD_HEIGHT * 0.5, ARTBOARD_HEIGHT * 0.5);
thumb.getStyleClass().add("thumb");
thumbGroup = new Group(thumb, value);
valueBar = new Line();
valueBar.getStyleClass().add("valueBar");
valueBar.setStrokeLineCap(StrokeLineCap.ROUND);
applyCss(valueBar);
strokeWidthFromCSS = valueBar.getStrokeWidth();
scale = new Line();
scale.getStyleClass().add("scale");
scale.setStrokeLineCap(StrokeLineCap.ROUND);
// always needed
drawingPane = new Pane();
}
private Text createCenteredText(double cx, double cy, String styleClass) {
Text text = new Text();
text.getStyleClass().add(styleClass);
text.setTextOrigin(VPos.CENTER);
text.setTextAlignment(TextAlignment.CENTER);
double width = cx > ARTBOARD_WIDTH * 0.5 ? ((ARTBOARD_WIDTH - cx) * 2.0) : cx * 2.0;
text.setWrappingWidth(width);
text.setBoundsType(TextBoundsType.VISUAL);
text.setY(cy);
return text;
}
News(NewsData data) {
this.getStyleClass().add("news-box");
TextFlow flow = new TextFlow();
if (data.isImportant()) {
Text megaphone = FontAwesomeIconFactory.get().createIcon(FontAwesomeIcon.BULLHORN, "1.2em");
megaphone.getStyleClass().clear();
megaphone.getStyleClass().add("news-box-title-important");
flow.getChildren().addAll(megaphone, new Text(" "));
}
Text titleText = new Text(data.getTitle() + System.lineSeparator());
titleText.setOnMouseClicked(e -> eventStudio().broadcast(new OpenUrlRequest(data.getLink())));
titleText.getStyleClass().add("news-box-title");
Text contentText = new Text(data.getContent());
contentText.setTextAlignment(TextAlignment.JUSTIFY);
contentText.getStyleClass().add("news-content");
flow.getChildren().addAll(titleText, contentText);
flow.setTextAlignment(TextAlignment.JUSTIFY);
Label labelDate = new Label(FORMATTER.format(data.getDate()),
MaterialDesignIconFactory.get().createIcon(MaterialDesignIcon.CLOCK));
labelDate.setPrefWidth(Integer.MAX_VALUE);
HBox.setHgrow(labelDate, Priority.ALWAYS);
HBox bottom = new HBox(labelDate);
bottom.setAlignment(Pos.CENTER_LEFT);
bottom.getStyleClass().add("news-box-footer");
if (isNotBlank(data.getLink())) {
Button link = UrlButton.urlButton(null, data.getLink(), FontAwesomeIcon.EXTERNAL_LINK_SQUARE,
"pdfsam-toolbar-button");
bottom.getChildren().add(link);
}
getChildren().addAll(flow, bottom);
}
private Text createNumber(String number, double layoutX, double layoutY) {
Text text = new Text(number);
text.setLayoutX(layoutX);
text.setLayoutY(layoutY);
text.setTextAlignment(TextAlignment.CENTER);
text.setFill(FILL_COLOR);
text.setFont(NUMBER_FONT);
return text;
}
private Node addSeparator(String name) {
Separator separator = new Separator();
separator.setPadding(new Insets(8, 0, 0, 0));
HBox.setHgrow(separator, Priority.ALWAYS);
Text text = new Text(name);
text.setTextAlignment(TextAlignment.CENTER);
HBox hBox = new HBox(text, separator);
HBox.setHgrow(hBox, Priority.ALWAYS);
return hBox;
}
private Text createNumber(String number, double layoutX, double layoutY) {
Text text = new Text(number);
text.setLayoutX(layoutX);
text.setLayoutY(layoutY);
text.setTextAlignment(TextAlignment.CENTER);
text.setFill(FILL_COLOR);
text.setFont(NUMBER_FONT);
return text;
}
private Text createNumber(String number, double layoutX, double layoutY) {
Text text = new Text(number);
text.setLayoutX(layoutX);
text.setLayoutY(layoutY);
text.setTextAlignment(TextAlignment.CENTER);
text.setFill(FILL_COLOR);
text.setFont(NUMBER_FONT);
return text;
}
private void populate() {
setStyle("-fx-background-color: rgb(230,230,230);");
Text text = new Text(label);
text.setTextAlignment(TextAlignment.CENTER);
text.setFill(Color.BLACK);
setCenter(text);
}
private final Text createText(String text) {
final Text newText = new Text(text);
newText.setFont(new Font(20));
newText.setTextOrigin(VPos.CENTER);
newText.setTextAlignment(TextAlignment.CENTER);
return newText;
}
public Text createLowerKey() {
Text lowerKey = new Text();
lowerKey.setFont(Font.font(FONT_FAMILY.toString(), FontWeight.MEDIUM, MAX_FONT_SIZE.toInt()));
lowerKey.setFill(Color.WHITE);
lowerKey.setWrappingWidth(getWrappingWidth());
lowerKey.setTextAlignment(getAlignment());
DropShadow ds = new DropShadow();
ds.setOffsetY(0.0);
ds.setOffsetX(0.0);
ds.setColor(Color.BLACK);
ds.setSpread(0.5);
lowerKey.setEffect(ds);
return lowerKey;
}
private void initGraphics() {
stops = new ArrayList<>(16);
for (Stop stop : getGradientStops()) {
if (Double.compare(stop.getOffset(), 0.0) == 0) stops.add(new Stop(0, stop.getColor()));
stops.add(new Stop(stop.getOffset() * 0.69924 + 0.285, stop.getColor()));
}
chartCanvas = new Canvas(PREFERRED_WIDTH, PREFERRED_HEIGHT);
chartCtx = chartCanvas.getGraphicsContext2D();
overlayCanvas = new Canvas(PREFERRED_WIDTH, PREFERRED_HEIGHT);
overlayCtx = overlayCanvas.getGraphicsContext2D();
unitText = new Text(getUnit());
unitText.setTextAlignment(TextAlignment.CENTER);
unitText.setTextOrigin(VPos.CENTER);
unitText.setFont(Fonts.latoLight(0.045 * PREFERRED_WIDTH));
legendStep = (getMaxValue() - getMinValue()) / 5d;
dropShadow = new DropShadow(BlurType.TWO_PASS_BOX, Color.BLACK, 5, 0, 0, 0);
minValueText = new Text(String.format(Locale.US, formatString, getMinValue()));
minValueText.setTextAlignment(TextAlignment.CENTER);
minValueText.setTextOrigin(VPos.CENTER);
minValueText.setVisible(isLegendVisible());
minValueText.setEffect(dropShadow);
legend1Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep));
legend1Text.setTextAlignment(TextAlignment.CENTER);
legend1Text.setTextOrigin(VPos.CENTER);
legend1Text.setVisible(isLegendVisible());
legend1Text.setEffect(dropShadow);
legend2Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 2));
legend2Text.setTextAlignment(TextAlignment.CENTER);
legend2Text.setTextOrigin(VPos.CENTER);
legend2Text.setVisible(isLegendVisible());
legend2Text.setEffect(dropShadow);
legend3Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend3Text.setTextAlignment(TextAlignment.CENTER);
legend3Text.setTextOrigin(VPos.CENTER);
legend3Text.setVisible(isLegendVisible());
legend3Text.setEffect(dropShadow);
legend4Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend4Text.setTextAlignment(TextAlignment.CENTER);
legend4Text.setTextOrigin(VPos.CENTER);
legend4Text.setVisible(isLegendVisible());
legend4Text.setEffect(dropShadow);
maxValueText = new Text(String.format(Locale.US, formatString, getMaxValue()));
maxValueText.setTextAlignment(TextAlignment.CENTER);
maxValueText.setTextOrigin(VPos.CENTER);
maxValueText.setVisible(isLegendVisible());
maxValueText.setEffect(dropShadow);
// Add all nodes
pane = new Pane(chartCanvas, overlayCanvas, unitText, minValueText, legend1Text, legend2Text, legend3Text, legend4Text, maxValueText);
pane.setBackground(new Background(new BackgroundFill(getChartBackgroundColor(), new CornerRadii(1024), Insets.EMPTY)));
getChildren().setAll(pane);
}
private Text getAppNameText() {
Text appVersionText = new Text(ApplicationConstants.APPLICATION_NAME);
appVersionText.setFont(Font.font("Helvetica", FontWeight.BOLD, BIG_FONT_SIZE));
appVersionText.setTextAlignment(TextAlignment.CENTER);
return appVersionText;
}
@Override
public void start(Stage stage) throws Exception {
loadData();
tree = new J48();
tree.buildClassifier(data);
noClassificationChart = buildChart("No Classification (click to add new data)", buildSingleSeries());
clusteredChart = buildChart("Clustered", buildClusteredSeries());
realDataChart = buildChart("Real Data (+ Decision Tree classification for new data)", buildLabeledSeries());
noClassificationChart.setOnMouseClicked(e -> {
Axis<Number> xAxis = noClassificationChart.getXAxis();
Axis<Number> yAxis = noClassificationChart.getYAxis();
Point2D mouseSceneCoords = new Point2D(e.getSceneX(), e.getSceneY());
double x = xAxis.sceneToLocal(mouseSceneCoords).getX();
double y = yAxis.sceneToLocal(mouseSceneCoords).getY();
Number xValue = xAxis.getValueForDisplay(x);
Number yValue = yAxis.getValueForDisplay(y);
reloadSeries(xValue, yValue);
});
Label lblDecisionTreeTitle = new Label("Decision Tree generated for the Iris dataset:");
Text txtTree = new Text(tree.toString());
String graph = tree.graph();
SwingNode sw = new SwingNode();
SwingUtilities.invokeLater(() -> {
TreeVisualizer treeVisualizer = new TreeVisualizer(null, graph, new PlaceNode2());
treeVisualizer.setPreferredSize(new Dimension(600, 500));
sw.setContent(treeVisualizer);
});
Button btnRestore = new Button("Restore original data");
Button btnSwapColors = new Button("Swap clustered chart colors");
StackPane spTree = new StackPane(sw);
spTree.setPrefWidth(300);
spTree.setPrefHeight(350);
VBox vbDecisionTree = new VBox(5, lblDecisionTreeTitle, new Separator(), spTree,
new HBox(10, btnRestore, btnSwapColors));
btnRestore.setOnAction(e -> {
loadData();
reloadSeries();
});
btnSwapColors.setOnAction(e -> swapClusteredChartSeriesColors());
lblDecisionTreeTitle.setTextFill(Color.DARKRED);
lblDecisionTreeTitle.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD, FontPosture.ITALIC, 16));
txtTree.setTranslateX(100);
txtTree.setFont(Font.font(Font.getDefault().getFamily(), FontWeight.BOLD, FontPosture.ITALIC, 14));
txtTree.setLineSpacing(1);
txtTree.setTextAlignment(TextAlignment.LEFT);
vbDecisionTree.setTranslateY(20);
vbDecisionTree.setTranslateX(20);
GridPane gpRoot = new GridPane();
gpRoot.add(realDataChart, 0, 0);
gpRoot.add(clusteredChart, 1, 0);
gpRoot.add(noClassificationChart, 0, 1);
gpRoot.add(vbDecisionTree, 1, 1);
stage.setScene(new Scene(gpRoot));
stage.setTitle("Íris dataset clustering and visualization");
stage.show();
}
private void initGraphics() {
stops = new ArrayList<>(16);
for (Stop stop : getGradientStops()) {
if (Double.compare(stop.getOffset(), 0.0) == 0) stops.add(new Stop(0, stop.getColor()));
stops.add(new Stop(stop.getOffset() * 0.69924 + 0.285, stop.getColor()));
}
chartPath = new Path();
overlayPath = new Path();
overlayPath.setFill(Color.TRANSPARENT);
centerCircle = new Circle();
thresholdCircle = new Circle();
thresholdCircle.setFill(Color.TRANSPARENT);
unitText = new Text(getUnit());
unitText.setTextAlignment(TextAlignment.CENTER);
unitText.setTextOrigin(VPos.CENTER);
unitText.setFont(Fonts.latoLight(0.045 * PREFERRED_WIDTH));
legendStep = (getMaxValue() - getMinValue()) / 5d;
dropShadow = new DropShadow(BlurType.TWO_PASS_BOX, Color.BLACK, 5, 0, 0, 0);
minValueText = new Text(String.format(Locale.US, formatString, getMinValue()));
minValueText.setTextAlignment(TextAlignment.CENTER);
minValueText.setTextOrigin(VPos.CENTER);
minValueText.setVisible(isLegendVisible());
minValueText.setEffect(dropShadow);
legend1Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep));
legend1Text.setTextAlignment(TextAlignment.CENTER);
legend1Text.setTextOrigin(VPos.CENTER);
legend1Text.setVisible(isLegendVisible());
legend1Text.setEffect(dropShadow);
legend2Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 2));
legend2Text.setTextAlignment(TextAlignment.CENTER);
legend2Text.setTextOrigin(VPos.CENTER);
legend2Text.setVisible(isLegendVisible());
legend2Text.setEffect(dropShadow);
legend3Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend3Text.setTextAlignment(TextAlignment.CENTER);
legend3Text.setTextOrigin(VPos.CENTER);
legend3Text.setVisible(isLegendVisible());
legend3Text.setEffect(dropShadow);
legend4Text = new Text(String.format(Locale.US, formatString, getMinValue() + legendStep * 3));
legend4Text.setTextAlignment(TextAlignment.CENTER);
legend4Text.setTextOrigin(VPos.CENTER);
legend4Text.setVisible(isLegendVisible());
legend4Text.setEffect(dropShadow);
maxValueText = new Text(String.format(Locale.US, formatString, getMaxValue()));
maxValueText.setTextAlignment(TextAlignment.CENTER);
maxValueText.setTextOrigin(VPos.CENTER);
maxValueText.setVisible(isLegendVisible());
maxValueText.setEffect(dropShadow);
textGroup = new Group();
// Add all nodes
pane = new Pane(chartPath, overlayPath, centerCircle, thresholdCircle, textGroup, unitText, minValueText, legend1Text, legend2Text, legend3Text, legend4Text, maxValueText);
getChildren().setAll(pane);
}
/** creates a text segment */
public static Text text(Object ... attrs)
{
Text n = new Text();
for(Object a: attrs)
{
if(a == null)
{
// ignore
}
else if(a instanceof CssStyle)
{
n.getStyleClass().add(((CssStyle)a).getName());
}
else if(a instanceof CssID)
{
n.setId(((CssID)a).getID());
}
else if(a instanceof FxCtl)
{
switch((FxCtl)a)
{
case BOLD:
n.getStyleClass().add(CssTools.BOLD.getName());
break;
case FOCUSABLE:
n.setFocusTraversable(true);
break;
case NON_FOCUSABLE:
n.setFocusTraversable(false);
break;
default:
throw new Error("?" + a);
}
}
else if(a instanceof String)
{
n.setText((String)a);
}
else if(a instanceof TextAlignment)
{
n.setTextAlignment((TextAlignment)a);
}
else
{
throw new Error("?" + a);
}
}
return n;
}