- 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/
在 的 React 组件中菜单,我需要设置selected反射(reflect)应用程序状态的选项的属性。 在 render() , optionState从状态所有者传递给 SortMenu 组件
我是初级 Ruby-mysql 程序员,我想知道如何使我的(存储过程)查询结果更快.. 这是我的存储过程我正在使用 SQL_CACHE.. 但我不确定.. 缓存使我的过程更快.. : ( DROP
我一直在 Python 中进行套接字编程,以使用 select.select(rfile, wfile, xlist[, timeout]) 处理由已连接的客户端套接字列表发出的请求,并且我还想用 J
我试图通过用空格填充文本来创建下拉列表中的列效果,如下例所示: [Aux1+1] [*] [Aux1+1] [@Tn=PP] [Main] [*] [Main A
我为 MySQL 编写了以下查询: SELECT subquery.t1_column1, subquery.t2_id, MAX(subquery.val) FROM ( S
为什么我们要用 select 标签来编写.attr('selected','selected') 例如: $('#countryList option').filter(function () {
Lokalizacja: Gdańsk Rzeszów Wrocław 不知道发生了什么,但在那种情况下没有选择的选项,我必须从列表中选择一些东西。当我从选
我的表单中有两个选择字段。第一个是单选,另一个是多选。现在我想做的是根据单选中所选的选项,使用给定的数据选择多选中的选项。为此,我在单选更改时触发 ajax 请求: $.ajax({ type
我在 Firefox 5 中发现了一个奇怪的错误(我现在无法访问 4)。但是,我认为它可能在 Firefox 4 中工作,因为我刚买了一台新电脑,而且我不记得以前见过这个错误。 我有几个选择框。所选值
此 SQL 有何不同: 第一个: select * from table_1 a join table_2 b on a.id = b.acc_id 第二个: select * f
预选 的最佳做法是什么?在 ? 根据不同的网站,两者都有效。但是哪个更好呢?最兼容? Foo Bar 最佳答案 如果您正在编写 XHTML,则 selected="selected" 是必需的。 如
我使用 Angular JS 创建了一个多选选择框:下面是相同的代码: JS: $scope.foobars = [{ 'foobar_id': 'foobar01', 'name':
我在 jqGrid 中有几列 edittype="select"。如何读取特定行中当前选定值的选项值? 例如:当我提供以下选项时,如何获得 FedEx 等的“FE” editoption: { val
这是我更大问题的精简查询,但要点是我试图内部联接到一个选择,其中选择受到外部选择的限制。那可能吗?我在内部选择上收到有关多部分标识符 S.Item 和 S.SerialNum 的错误。 要点是这样的,
如果chat.chat_type IS NULL,我想选择user.*,但如果chat.chat_type = 1 我想选择组。* SELECT CASE WHEN ch
我正在编写一个小脚本来测试表单在提交之前是否已被更改。所以我可以使用普通输入(文本、文本区域等): if(element.defaultValue != element.value) { al
我正在尝试为 Prototype 编写一个插件,用户在其中单击下拉菜单并将其替换为多选元素。我快完成了。在用户选择他们想要显示的内容并将表单提交到同一页面之前,一切都很好。我正在使用 PHP 来使用
你如何在 MongoDB 中进行嵌套选择,类似于 SELECT id FROM table1 WHERE id IN (SELECT id FROM table2) 最佳答案 MongoDB 尚不具备
我有以下用于选择下拉列表的代码: {{unit.Text}} UnitOfMeasurements 数组中的每一项看起来像这样: Selected: false Text: "lb" Va
我正在尝试使用[选定]和[ngValue]来设置表单中包含对象的选择标记的默认值。但出于某种原因,它们似乎无法相提并论。。示例代码:。这段代码最终只显示空白作为缺省值。如果删除[ngValue],它就
我是一名优秀的程序员,十分优秀!