- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于一个简单的矢量绘图应用程序,我希望实现一个“选择框”,它是节点的 layoutBounds 的图形表示。
例子:
感谢 jewelsea和他的BoundsExample ,我现在对如何获取盒子的数据有了很好的理解。我正在努力的部分实际上是在场景中绘制盒子,以正确尊重节点上的转换的方式。
在这种情况下正确意味着边界逻辑大小随节点缩放,但选择框的笔划保持不变。这意味着选择框会随其对应的节点缩放,但笔划保持未缩放。
我可以想到两种通用策略来实现这样的选择框。
package application;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.effect.DropShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.scene.shape.StrokeLineCap;
import javafx.scene.shape.StrokeType;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
/** Demo for understanding JavaFX Layout Bounds */
public class BoundsPlayground extends Application
{
final ObservableList<Shape> shapes = FXCollections.observableArrayList();
final ObservableList<ShapePair> intersections = FXCollections.observableArrayList();
ObjectProperty<BoundsType> selectedBoundsType = new SimpleObjectProperty<BoundsType>(
BoundsType.LAYOUT_BOUNDS );
public static void main( final String[] args )
{
launch( args );
}
@Override
public void start( final Stage stage )
{
stage.setTitle( "Bounds Playground" );
// define some objects to manipulate on the scene.
final Circle greenCircle = new Circle( 100, 100, 50, Color.FORESTGREEN );
greenCircle.setId( "Green Circle" );
final Circle redCircle = new Circle( 300, 200, 50, Color.FIREBRICK );
redCircle.setId( "Red Circle" );
final Line line = new Line( 25, 300, 375, 200 );
line.setId( "Line" );
line.setStrokeLineCap( StrokeLineCap.ROUND );
line.setStroke( Color.MIDNIGHTBLUE );
line.setStrokeWidth( 5 );
final Anchor anchor1 = new Anchor( "Anchor 1", line.startXProperty(), line.startYProperty() );
final Anchor anchor2 = new Anchor( "Anchor 2", line.endXProperty(), line.endYProperty() );
final Group group = new Group( greenCircle, redCircle, line, anchor1, anchor2 );
// monitor intersections of shapes in the scene.
for ( final Node node : group.getChildrenUnmodifiable() )
{
if ( node instanceof Shape )
{
shapes.add( (Shape) node );
}
}
testIntersections();
// enable dragging for the scene objects.
final Circle[] circles = { greenCircle, redCircle, anchor1, anchor2 };
for ( final Circle circle : circles )
{
enableDrag( circle );
circle.centerXProperty().addListener( new ChangeListener<Number>()
{
@Override
public void changed( final ObservableValue<? extends Number> observableValue, final Number oldValue,
final Number newValue )
{
testIntersections();
}
} );
circle.centerYProperty().addListener( new ChangeListener<Number>()
{
@Override
public void changed( final ObservableValue<? extends Number> observableValue, final Number oldValue,
final Number newValue )
{
testIntersections();
}
} );
}
// define an overlay to show the layout bounds of the scene's shapes.
final Group layoutBoundsOverlay = new Group();
layoutBoundsOverlay.setMouseTransparent( true );
for ( final Shape shape : shapes )
{
if ( !(shape instanceof Anchor) )
{
layoutBoundsOverlay.getChildren().add( new BoundsDisplay( shape ) );
}
}
// layout the scene.
final StackPane background = new StackPane();
background.setStyle( "-fx-background-color: cornsilk;" );
final Scene scene = new Scene( new Group( background, group, layoutBoundsOverlay ), 600, 500 );
group.setScaleX( 5 );
group.setScaleY( 5 );
background.prefHeightProperty().bind( scene.heightProperty() );
background.prefWidthProperty().bind( scene.widthProperty() );
stage.setScene( scene );
stage.show();
createUtilityWindow( stage, layoutBoundsOverlay, new Shape[]{ greenCircle, redCircle } );
}
// update the list of intersections.
private void testIntersections()
{
intersections.clear();
// for each shape test it's intersection with all other shapes.
for ( final Shape src : shapes )
{
for ( final Shape dest : shapes )
{
final ShapePair pair = new ShapePair( src, dest );
if ( !(pair.a instanceof Anchor) && !(pair.b instanceof Anchor) && !intersections.contains( pair )
&& pair.intersects( selectedBoundsType.get() ) )
{
intersections.add( pair );
}
}
}
}
// make a node movable by dragging it around with the mouse.
private void enableDrag( final Circle circle )
{
final Delta dragDelta = new Delta();
circle.setOnMousePressed( new EventHandler<MouseEvent>()
{
@Override
public void handle( final MouseEvent mouseEvent )
{
// record a delta distance for the drag and drop operation.
dragDelta.x = circle.getCenterX() - mouseEvent.getX();
dragDelta.y = circle.getCenterY() - mouseEvent.getY();
circle.getScene().setCursor( Cursor.MOVE );
}
} );
circle.setOnMouseReleased( new EventHandler<MouseEvent>()
{
@Override
public void handle( final MouseEvent mouseEvent )
{
circle.getScene().setCursor( Cursor.HAND );
}
} );
circle.setOnMouseDragged( new EventHandler<MouseEvent>()
{
@Override
public void handle( final MouseEvent mouseEvent )
{
circle.setCenterX( mouseEvent.getX() + dragDelta.x );
circle.setCenterY( mouseEvent.getY() + dragDelta.y );
}
} );
circle.setOnMouseEntered( new EventHandler<MouseEvent>()
{
@Override
public void handle( final MouseEvent mouseEvent )
{
if ( !mouseEvent.isPrimaryButtonDown() )
{
circle.getScene().setCursor( Cursor.HAND );
}
}
} );
circle.setOnMouseExited( new EventHandler<MouseEvent>()
{
@Override
public void handle( final MouseEvent mouseEvent )
{
if ( !mouseEvent.isPrimaryButtonDown() )
{
circle.getScene().setCursor( Cursor.DEFAULT );
}
}
} );
}
// a helper enumeration of the various types of bounds we can work with.
enum BoundsType
{
LAYOUT_BOUNDS, BOUNDS_IN_LOCAL, BOUNDS_IN_PARENT
}
// a translucent overlay display rectangle to show the bounds of a Shape.
class BoundsDisplay extends Rectangle
{
// the shape to which the bounds display has been type.
final Shape monitoredShape;
private ChangeListener<Bounds> boundsChangeListener;
BoundsDisplay( final Shape shape )
{
setFill( Color.LIGHTGRAY.deriveColor( 1, 1, 1, 0.35 ) );
setStroke( Color.LIGHTGRAY.deriveColor( 1, 1, 1, 0.5 ) );
setStrokeType( StrokeType.INSIDE );
setStrokeWidth( 3 );
monitoredShape = shape;
monitorBounds( BoundsType.LAYOUT_BOUNDS );
}
// set the type of the shape's bounds to monitor for the bounds display.
void monitorBounds( final BoundsType boundsType )
{
// remove the shape's previous boundsType.
if ( boundsChangeListener != null )
{
final ReadOnlyObjectProperty<Bounds> oldBounds;
switch ( selectedBoundsType.get() )
{
case LAYOUT_BOUNDS:
oldBounds = monitoredShape.layoutBoundsProperty();
break;
case BOUNDS_IN_LOCAL:
oldBounds = monitoredShape.boundsInLocalProperty();
break;
case BOUNDS_IN_PARENT:
oldBounds = monitoredShape.boundsInParentProperty();
break;
default :
oldBounds = null;
}
if ( oldBounds != null )
{
oldBounds.removeListener( boundsChangeListener );
}
}
// determine the shape's bounds for the given boundsType.
final ReadOnlyObjectProperty<Bounds> bounds;
switch ( boundsType )
{
case LAYOUT_BOUNDS:
bounds = monitoredShape.layoutBoundsProperty();
break;
case BOUNDS_IN_LOCAL:
bounds = monitoredShape.boundsInLocalProperty();
break;
case BOUNDS_IN_PARENT:
bounds = monitoredShape.boundsInParentProperty();
break;
default :
bounds = null;
}
// set the visual bounds display based upon the new bounds and keep it in sync.
if ( bounds != null )
{
updateBoundsDisplay( bounds.get() );
// keep the visual bounds display based upon the new bounds and keep it in sync.
boundsChangeListener = new ChangeListener<Bounds>()
{
@Override
public void changed( final ObservableValue<? extends Bounds> observableValue,
final Bounds oldBounds, final Bounds newBounds )
{
updateBoundsDisplay( newBounds );
}
};
bounds.addListener( boundsChangeListener );
}
}
// update this bounds display to match a new set of bounds.
private void updateBoundsDisplay( final Bounds newBounds )
{
setX( newBounds.getMinX() );
setY( newBounds.getMinY() );
setWidth( newBounds.getWidth() );
setHeight( newBounds.getHeight() );
}
}
// an anchor displayed around a point.
class Anchor extends Circle
{
Anchor( final String id, final DoubleProperty x, final DoubleProperty y )
{
super( x.get(), y.get(), 10 );
setId( id );
setFill( Color.GOLD.deriveColor( 1, 1, 1, 0.5 ) );
setStroke( Color.GOLD );
setStrokeWidth( 2 );
setStrokeType( StrokeType.OUTSIDE );
x.bind( centerXProperty() );
y.bind( centerYProperty() );
}
}
// records relative x and y co-ordinates.
class Delta
{
double x, y;
}
// records a pair of (possibly) intersecting shapes.
class ShapePair
{
private final Shape a, b;
public ShapePair( final Shape src, final Shape dest )
{
a = src;
b = dest;
}
public boolean intersects( final BoundsType boundsType )
{
if ( a == b )
{
return false;
}
a.intersects( b.getBoundsInLocal() );
switch ( boundsType )
{
case LAYOUT_BOUNDS:
return a.getLayoutBounds().intersects( b.getLayoutBounds() );
case BOUNDS_IN_LOCAL:
return a.getBoundsInLocal().intersects( b.getBoundsInLocal() );
case BOUNDS_IN_PARENT:
return a.getBoundsInParent().intersects( b.getBoundsInParent() );
default :
return false;
}
}
@Override
public String toString()
{
return a.getId() + " : " + b.getId();
}
@Override
public boolean equals( final Object other )
{
final ShapePair o = (ShapePair) other;
return o != null && (a == o.a && b == o.b || a == o.b && b == o.a);
}
@Override
public int hashCode()
{
int result = a != null ? a.hashCode() : 0;
result = 31 * result + (b != null ? b.hashCode() : 0);
return result;
}
}
// define a utility stage for reporting intersections.
private void createUtilityWindow( final Stage stage, final Group boundsOverlay,
final Shape[] transformableShapes )
{
final Stage reportingStage = new Stage();
reportingStage.setTitle( "Control Panel" );
reportingStage.initStyle( StageStyle.UTILITY );
reportingStage.setX( stage.getX() + stage.getWidth() );
reportingStage.setY( stage.getY() );
// define content for the intersections utility panel.
final ListView<ShapePair> intersectionView = new ListView<ShapePair>( intersections );
final Label instructions = new Label( "Click on any circle in the scene to the left to drag it around." );
instructions.setMinSize( Control.USE_PREF_SIZE, Control.USE_PREF_SIZE );
instructions.setStyle( "-fx-font-weight: bold; -fx-text-fill: darkgreen;" );
final Label intersectionInstructions =
new Label( "Any intersecting bounds in the scene will be reported below." );
instructions.setMinSize( Control.USE_PREF_SIZE, Control.USE_PREF_SIZE );
// add the ability to set a translate value for the circles.
final CheckBox translateNodes = new CheckBox( "Translate circles" );
translateNodes.selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( final ObservableValue<? extends Boolean> observableValue, final Boolean oldValue,
final Boolean doTranslate )
{
if ( doTranslate )
{
for ( final Shape shape : transformableShapes )
{
shape.setTranslateY( 100 );
testIntersections();
}
}
else
{
for ( final Shape shape : transformableShapes )
{
shape.setTranslateY( 0 );
testIntersections();
}
}
}
} );
translateNodes.selectedProperty().set( false );
// add the ability to add an effect to the circles.
final Label modifyInstructions = new Label( "Modify visual display aspects." );
modifyInstructions.setStyle( "-fx-font-weight: bold;" );
modifyInstructions.setMinSize( Control.USE_PREF_SIZE, Control.USE_PREF_SIZE );
final CheckBox effectNodes = new CheckBox( "Add an effect to circles" );
effectNodes.selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( final ObservableValue<? extends Boolean> observableValue, final Boolean oldValue,
final Boolean doTranslate )
{
if ( doTranslate )
{
for ( final Shape shape : transformableShapes )
{
shape.setEffect( new DropShadow() );
testIntersections();
}
}
else
{
for ( final Shape shape : transformableShapes )
{
shape.setEffect( null );
testIntersections();
}
}
}
} );
effectNodes.selectedProperty().set( true );
// add the ability to add a stroke to the circles.
final CheckBox strokeNodes = new CheckBox( "Add outside strokes to circles" );
strokeNodes.selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( final ObservableValue<? extends Boolean> observableValue, final Boolean oldValue,
final Boolean doTranslate )
{
if ( doTranslate )
{
for ( final Shape shape : transformableShapes )
{
shape.setStroke( Color.LIGHTSEAGREEN );
shape.setStrokeWidth( 10 );
testIntersections();
}
}
else
{
for ( final Shape shape : transformableShapes )
{
shape.setStrokeWidth( 0 );
testIntersections();
}
}
}
} );
strokeNodes.selectedProperty().set( true );
// add the ability to show or hide the layout bounds overlay.
final Label showBoundsInstructions = new Label( "The gray squares represent layout bounds." );
showBoundsInstructions.setStyle( "-fx-font-weight: bold;" );
showBoundsInstructions.setMinSize( Control.USE_PREF_SIZE, Control.USE_PREF_SIZE );
final CheckBox showBounds = new CheckBox( "Show Bounds" );
boundsOverlay.visibleProperty().bind( showBounds.selectedProperty() );
showBounds.selectedProperty().set( true );
// create a container for the display control checkboxes.
final VBox displayChecks = new VBox( 10 );
displayChecks.getChildren().addAll( modifyInstructions, translateNodes, effectNodes, strokeNodes,
showBoundsInstructions, showBounds );
// create a toggle group for the bounds type to use.
final ToggleGroup boundsToggleGroup = new ToggleGroup();
final RadioButton useLayoutBounds = new RadioButton( "Use Layout Bounds" );
final RadioButton useBoundsInLocal = new RadioButton( "Use Bounds in Local" );
final RadioButton useBoundsInParent = new RadioButton( "Use Bounds in Parent" );
useLayoutBounds.setToggleGroup( boundsToggleGroup );
useBoundsInLocal.setToggleGroup( boundsToggleGroup );
useBoundsInParent.setToggleGroup( boundsToggleGroup );
final VBox boundsToggles = new VBox( 10 );
boundsToggles.getChildren().addAll( useLayoutBounds, useBoundsInLocal, useBoundsInParent );
// change the layout bounds display depending on which bounds type has been selected.
useLayoutBounds.selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( final ObservableValue<? extends Boolean> observableValue, final Boolean aBoolean,
final Boolean isSelected )
{
if ( isSelected )
{
for ( final Node overlay : boundsOverlay.getChildren() )
{
((BoundsDisplay) overlay).monitorBounds( BoundsType.LAYOUT_BOUNDS );
}
selectedBoundsType.set( BoundsType.LAYOUT_BOUNDS );
testIntersections();
}
}
} );
useBoundsInLocal.selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( final ObservableValue<? extends Boolean> observableValue, final Boolean aBoolean,
final Boolean isSelected )
{
if ( isSelected )
{
for ( final Node overlay : boundsOverlay.getChildren() )
{
((BoundsDisplay) overlay).monitorBounds( BoundsType.BOUNDS_IN_LOCAL );
}
selectedBoundsType.set( BoundsType.BOUNDS_IN_LOCAL );
testIntersections();
}
}
} );
useBoundsInParent.selectedProperty().addListener( new ChangeListener<Boolean>()
{
@Override
public void changed( final ObservableValue<? extends Boolean> observableValue, final Boolean aBoolean,
final Boolean isSelected )
{
if ( isSelected )
{
for ( final Node overlay : boundsOverlay.getChildren() )
{
((BoundsDisplay) overlay).monitorBounds( BoundsType.BOUNDS_IN_PARENT );
}
selectedBoundsType.set( BoundsType.BOUNDS_IN_PARENT );
testIntersections();
}
}
} );
useLayoutBounds.selectedProperty().set( true );
final WebView boundsExplanation = new WebView();
boundsExplanation
.getEngine()
.loadContent(
"<html><body bgcolor='darkseagreen' fgcolor='lightgrey' style='font-size:12px'><dl>"
+ "<dt><b>Layout Bounds</b></dt><dd>The boundary of the shape.</dd><br/>"
+ "<dt><b>Bounds in Local</b></dt><dd>The boundary of the shape and effect.</dd><br/>"
+ "<dt><b>Bounds in Parent</b></dt><dd>The boundary of the shape, effect and transforms.<br/>The co-ordinates of what you see.</dd>"
+ "</dl></body></html>" );
boundsExplanation.setPrefWidth( 100 );
boundsExplanation.setMinHeight( 130 );
boundsExplanation.setMaxHeight( 130 );
boundsExplanation.setStyle( "-fx-background-color: transparent" );
// layout the utility pane.
final VBox utilityLayout = new VBox( 10 );
utilityLayout
.setStyle( "-fx-padding:10; -fx-background-color: linear-gradient(to bottom, lightblue, derive(lightblue, 20%));" );
utilityLayout.getChildren().addAll( instructions, intersectionInstructions, intersectionView,
displayChecks, boundsToggles, boundsExplanation );
utilityLayout.setPrefHeight( 530 );
reportingStage.setScene( new Scene( utilityLayout ) );
reportingStage.show();
// ensure the utility window closes when the main app window closes.
stage.setOnCloseRequest( new EventHandler<WindowEvent>()
{
@Override
public void handle( final WindowEvent windowEvent )
{
reportingStage.close();
}
} );
}
}
最佳答案
事实证明,SceneBuilder 本身是我所知道的最大的免费 JavaFX 项目,它已经解决了这个确切的问题。
通过研究 com.oracle.javafx.scenebuilder.kit.editor
SceneBuilder 软件包 source code我很放心,我提出的第二个战略是要走的路。
关于selection - 在 JavaFX 中绘制变换独立布局边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048114/
我学习 SDL 二维编程已有一段时间了,现在我想创建一个结合使用 SDL 和 OpenGL 的程序。我是这样设置的: SDL_Init(SDL_INIT_VIDEO); window = SDL_Cr
尝试查找可在地块中使用的不同类型项目的列表 来自不同样本的投影类型: projection = list(type = "equirectangular") projection = list(typ
我正在尝试使用 Java Graphics API 绘制 GIF,但无法使用下面的代码成功绘制 GIF。仅绘制 GIF 的第一张图像或缩略图,但不播放。 public void paintCompon
我目前正在使用 JFrame 并尝试绘制一个矩形,但我不知道如何执行代码 paint(Graphics g),如何获取 Graphics 对象? package com.raggaer.frame;
这个领域的新手,希望得到一些帮助。 我有一个"Missile.java" 类,我在那里画东西。我想绘制一个 ImageView,我正在使用以下代码: ImageView v = (ImageView)
下面列出了圆形的例子 这是我的 JavaScript 代码。 最佳答案 假设您的 randomColor 是正确的,您只需要: 从 canvas.onclick 中移除 context.clearR
我在绘制和缩放 ImageView 时遇到问题。请帮帮我.. 当我画一些东西然后拖动或缩放图像时 - 绘图保留在原处,如您在屏幕截图中所见。而且我只需要简单地在图片上绘图,并且可以缩放和拖动这张图片。
我们可以在形式之外绘制图像和文本...我的意思是在字面上... 我知道问这个问题很愚蠢但是我们能不能... 最佳答案 您可以通过创建表单并将其 TransparentColor 属性设置为背景色来“作
我在绘制/布局期间收到 3 个对象分配警告 super.onDraw(canvas); canvas.drawColor(Color.WHITE); Paint textPaint = new Pai
我有一个示例时间序列数据框: df = pd.DataFrame({'year':'1990','1991','1992','1993','1994','1995','1996',
我试图想出一种简洁的方法来绘制 R 数据框中所有列的 GridView 。问题是我的数据框中既有离散值又有数值。为简单起见,我们可以使用 R 提供的名为 iris 的示例数据集。我会使用 par(mf
我有一个由 10 列和 50 行组成的 data.frame。我使用 apply 函数逐列计算密度函数。现在我想绘制我一次计算的密度。 换句话说,而不是绘图... plot(den[[1]]) plo
我想知道我们如何才能在第一个和第二个组件之外绘制个人,如下所示: 最佳答案 这可能有效: pc.cr <- princomp(USArrests, cor = TRUE) pairs(pc.cr$lo
我是Pandas和matplotlib的新手,想绘制此DataFrame season won team matches pct_won 0 20
我正在尝试为 distplot 子图编写一个 for 循环。 我有一个包含许多不同长度列的数据框。 (不包括 NaN 值) fig = make_subplots( rows=len(asse
我想创建一个具有密度的 3d 图。 我使用函数 density 首先为特定的 x 值创建一个二维图,然后该函数创建密度并将它们放入 y 变量中。现在我有第二组 x 值并将其再次放入密度函数中,然后我得
全部, 我一直在研究全局所有 MTB 步道的索引。我是 Python 人,所以对于所有涉及的步骤,我都尝试使用 Python 模块。 我能够像这样从 OSM 立交桥 API 中获取关系: from O
我正在使用 e1071 包中的支持向量机对我的数据进行分类,并希望可视化机器实际如何进行分类。但是,在使用 plot.svm 函数时,出现无法解决的错误。 脚本: library("e1071") d
我制作了以下图表,它是使用 xts 对象创建的。 我使用的代码很简单 plot(graphTS1$CCLL, type = "l", las = 2, ylab = "(c)\nCC for I
在绘制状态图时,您如何知道哪些状态放在框中,哪些状态用于转换箭头?我注意到转换也是状态。 我正在查看 this page 上的图 1 : 最佳答案 转换不是状态。转换是将对象从一种状态移动到下一种状态
我是一名优秀的程序员,十分优秀!