Hello,
I'm not new to Java but I am new to JavaFX.
I plan to have a container/Canvas with multiple shapes (Lines, Text, Rectangle etc) in it. This Container can be X times in the Szene with different Text Shapes. I need to Zoom and Pan (maybe rotation) the whole Szene and the Containers/Canvas.
So I was playing around with that but I have two issues.
1) all Canvas classes that I found (like Pane for example) do resize with the main window resize event. The content of the canvas isn't centered any more.
2) I added a couple of Rectangles to the canvas and both the rectangles and the canvas have a mouse listener which will rotate the item/canvas. Problem is, that even if I click the rectangle also the underlaying canvas is rotated...I think I need some kind of Z-Info to find out what was clicked.
Here is the little example program, it makes no produktiv sense but it demonstrates my problem.
Does anybody has a tip what canvas class would fit and does not resize with the main window and how to figure out what was clicked?
public class Test extends Application
{
Scene mainScene;
Group root;
public static void main(String[] args)
{
launch(args);
}
@Override
public void init()
{
root = new Group();
int x = 0;
int y = -100;
for(int i = 0; i < 5; i++)
{
x = 0;
y = y + 100;
for (int j = 0; j < 5; j++)
{
final Rectangle rect = new Rectangle(x, y, 30 , 30);
final RotateTransition rotateTransition = RotateTransitionBuilder.create()
.node(rect)
.duration(Duration.seconds(4))
.fromAngle(0)
.toAngle(720)
.cycleCount(Timeline.INDEFINITE)
.autoReverse(true)
.build();
rect.setOnMouseClicked(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent me)
{
if(rotateTransition.getStatus().equals(Animation.Status.RUNNING))
{
rotateTransition.setToAngle(0);
rotateTransition.stop();
rect.setFill(Color.BLACK);
rect.setScaleX(1.0);
rect.setScaleY(1.0);
}
else
{
rect.setFill(Color.AQUAMARINE);
rect.setScaleX(2.0);
rect.setScaleY(2.0);
rotateTransition.play();
}
}
});
root.getChildren().add(rect);
x = x + 100;
}
}
}
public void start(Stage primaryStage)
{
final Pane pane = new Pane();
pane.setStyle("-fx-background-color: #CCFF99");
pane.setOnScroll(new EventHandler<ScrollEvent>()
{
@Override
public void handle(ScrollEvent se)
{
if(se.getDeltaY() > 0)
{
pane.setScaleX(pane.getScaleX() + 0.01);
pane.setScaleY(pane.getScaleY() + 0.01);
}
else
{
pane.setScaleX(pane.getScaleX() - 0.01);
pane.setScaleY(pane.getScaleY() - 0.01);
}
}
});
pane.getChildren().addAll(root);
pane.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event)
{
System.out.println(event.getButton());
if(event.getButton().equals(MouseButton.PRIMARY))
{
System.out.println("primary button");
final RotateTransition rotateTransition2 = RotateTransitionBuilder.create()
.node(pane)
.duration(Duration.seconds(10))
.fromAngle(0)
.toAngle(360)
.cycleCount(Timeline.INDEFINITE)
.autoReverse(false)
.build();
rotateTransition2.play();
}
}
});
mainScene = new Scene(pane, 400, 400);
primaryStage.setScene(mainScene);
primaryStage.show();
}
}
Edited by: 953596 on 19.08.2012 12:03