I'm beginner of javafx:
I am developing a graphical user interface using javafx and Scene builder that has a coordinate plane with x axis and y axis. It should be like this: https://postimg.cc/image/98k9mvnb3/
when someone do a mouse click anywhere on this coordinate plane it will show the coordinate point(x,y) of the pixel on the console and there will a mark(like point or some text will be written) on the same place where mouse clicked.
For implementing these things I have to use canvas & i'm able to get the coordinate point but i'm not getting how to draw the coordinate plane and how to write something on the pixel where mouse clicked.
Here is my code:
Controller Class
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class AxisController implements Initializable{
#FXML
private AnchorPane anchr;
#FXML
private Canvas canvas;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert canvas != null : "fx:id=\"canvas\" was not injected: check your FXML file 'AxisFxml.fxml'.";
}
#FXML
private void handleMouse(MouseEvent event){
System.out.println(event.getX());
System.out.println(event.getY());
}
}
Main Class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class AxisMain extends Application {
#Override
public void start(Stage primaryStage) {
try {
AnchorPane root = FXMLLoader.load(getClass().getResource("/application/AxisFxml.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Have you tried something as simple as putting some code like this into your handleMouse method?
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLUE);
gc.fillRect(event.getX()-5,event.getY()-5,10,10);
This assumes of course that you have also attached this method to the canvas so that you actually get the events in the right coordinate system.
Related
What I want to do:
Have a slider, from 0 - 10, have it move only by a full tick(1), and show the value of the tick above the tick.
What happens:
It shows all values from 0-10 except for 9 which is mysteriously missing..
I wanted to upload an image here but I lack the reputation it seems ;)
https://pasteboard.co/HdPtzVp.png
Code:
Main Class:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class StartGui extends Application
{
#Override
public void start(Stage stage)
{
HBox box = new HBox();
PionnenSlider slider = new PionnenSlider();
box.getChildren().add(slider);
Scene scene = new Scene(box, 500, 300);
stage.setScene(scene);
stage.setTitle("SliderMinimalCode");
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Slider class:
import javafx.scene.control.Slider;
public class PionnenSlider extends Slider
{
public PionnenSlider()
{
setMinorTickCount(0);
setBlockIncrement(1);
setMajorTickUnit(1);
setValue(0);
setMax(10);
setShowTickMarks(true);
setShowTickLabels(true);
setSnapToTicks(true);
}
}
apparently it is due to lack of space for the slider, found a workaround to automaticly size it with HBox.setHgrow(sliderPionnen, Priority.ALWAYS);, although that just grows it to fill the entire HBOX.. so I guess I'l have to find a workaround for that
I would like to implement a text input using javafx. The cursor looks like a black rectangle (for people with low vision). It seems impossible with a text area or a text field. The mouse shape can be an arrow for example. Can you give ideas to implement this function ?
Here is full example
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class CaretColorizer extends Application {
#Override
public void start(Stage stage) throws Exception {
TextField redCaretTextField = new TextField("Big black caret");
redCaretTextField.setSkin(
new TextFieldCaretControlSkin(
redCaretTextField,
Color.RED
)
);
VBox layout = new VBox(10, redCaretTextField);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public class TextFieldCaretControlSkin extends TextFieldSkin {
public TextFieldCaretControlSkin(TextField textField, Color caretColor) {
super(textField);
caretPath.strokeProperty().unbind();
caretPath.fillProperty().unbind();
caretPath.setStrokeWidth(10);
caretPath.setStroke(Color.BLACK);
caretPath.setFill(Color.BLACK);
}
}
public static void main(String[] args) {
launch(args);
}
}
Basically, I want to rotate an ImageView by 90 degrees when the user hits R on the keyboard.
#FXML
private Image arrow;
#FXML
private ImageView arrowDirection;
#FXML
public void arrowRotation(KeyEvent keyEvent)
{
if(keyEvent.getCode() == KeyCode.R)
arrowDirection.setRotate(90);
}
Your question is a bit unspecific, so I assumed that you want an EventHandler that is added to the pane in which the image view rests, and that you want to set it in the Controller. This is my solution:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
public class FXMLDocumentController implements Initializable {
#FXML
Pane pane;
#FXML
ImageView imgView;
public void arrowRotation(KeyEvent event){
if(event.getCode().equals(KeyCode.R))
imgView.setRotate(90);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
Image img = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Javafx-layout-classes.svg/1000px-Javafx-layout-classes.svg.png");
imgView.setImage(img);
pane.sceneProperty().addListener(new ChangeListener<Scene>() {
#Override
public void changed(ObservableValue<? extends Scene> observable, Scene oldValue, Scene newValue) {
if(newValue != null){
pane.requestFocus();
}
}
});
pane.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
arrowRotation(event);
}
});
}
}
Of course, this needs an FXML that holds an ImageView inside of a Pane, but that can easily be adjusted to fit your needs.
The function that performs the rotation works pretty much like you thought, but I would compare using equals whenever possible. I added a listener to the sceneProperty of the pane to let the pane request the focus once the whole thing is loaded and the scene is set. This means that pressing a key will actually trigger KeyEventHandlers that are registered on the pane. Registering the EventHandler is straight forward again. Hope that helps :)
I feel like I have searched half the Web and found no solution...
I have a java application displaying a Map(different countries etc.).
currently you are able to scroll down and up using your mouse wheel...
I want it so, that you are able to scroll sideways (horizontally).
All I need is a Listener (in Swing or Javafx does not matter) triggering whenever the mousewheel gets tilted, without the need for a focus of the map (hovering with your mouse should be enough the windows should still be focused) and without any visible scrollbars.
Using the following code every time you scroll sideways a message gets printed out...
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
scene.setOnScroll(new EventHandler<ScrollEvent>(){
#Override
public void handle(ScrollEvent event) {
System.out.println("Scroll:" + event.getDeltaX());
}
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
One thing to consider:
Apparently when embedding a JFXPanel into a JFrame the sideway scrolling event is not getting passed.
I'm currently working on a project which relies on the representation of file sizes through rectangles. What I have been stuck on for quite a while however, was scaling of the height to a size that could fit inside of my Stage. This is a crucial part of the project as it allows the user to visually compare the sizes of the files they are viewing from any directory on their machine.
EDIT: I tried adding an image but I haven't got enough reputation yet :(.
http://tinypic.com/r/2e56pol/8
Why don't you just bind the height of the rectangle to the scene's using:
rectangle.heightProperty().bind(scene.heightProperty());
A complete example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class DemoTabPane extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Rectangle rectangle = new Rectangle(20,20,200,200);
rectangle.setStroke(Color.BLACK);
Scene scene = new Scene(new HBox(rectangle), 100, 100);
rectangle.heightProperty().bind(scene.heightProperty());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}