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
Related
I am new to overflow so I apologize in advance if I am not precise enough.
During a class project I have to make a tanquin game in javafx. I must be able to cut an image into x part in order to insert them then in an Arraylist with an id and the part of the image. But I have the error attached here.
I guess the reader is null and that the error is related to that. But I don't know how to fix it.
I never used those library so i dont know how to use them.
Thank you in advance for your reply.
Code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Image image = new Image("https://scontent-yyz1-1.cdninstagram.com/v/t51.2885-15/sh0.08/e35/c180.0.720.720a/s640x640/75272157_461406551233157_7963091763107249286_n.jpg?_nc_ht=scontent-yyz1-1.cdninstagram.com&_nc_cat=105&_nc_ohc=eu-9K8wyx6oAX9H695F&oh=429cc1e6dcec03badc9aac439b6b8ac0&oe=5EAD8EA3", true);
PixelReader reader = image.getPixelReader();
WritableImage newImage = new WritableImage(reader,100,100,100,100);
ImageView imageView = new ImageView(newImage);
HBox hbox = new HBox(imageView);
Scene scene = new Scene(hbox, 640, 640);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
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);
}
}
So I'm trying to simply change between two scenes in javafx, but I've come into this re-occurring problem that I can't seem to fix. It is demonstrated in the following code:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
public class TestApplication extends Application
{
private Stage stage;
private Scene scene, scene2;
public void start(Stage s)
{
scene=new Scene(new Group());
scene2=new Scene(new Group());
scene.setFill(Color.GREEN);
scene2.setFill(Color.ORANGE);
scene.setOnMouseClicked(e-> changeScene(scene2));
scene2.setOnMouseClicked(e-> changeScene(scene));
stage=s;
s.setScene(scene);
s.show();
}
public void changeScene(Scene nex)
{
stage.setScene(nex);
System.out.println("here");
}
public static void main(String[] args)
{
launch(args);
}
}
Am I doing something wrong? How can I fix this?
What's going wrong
You are not placing anything in the scenes (just an empty group). By default scenes are (usually) going to size to the preferred size of their contained content. As your scenes have no content of any size, then the scenes shouldn't really have any size. I think the fact that the first scene even shows up is a bit of a quirk of the JavaFX system where it seems to set some default size to the initial scene when it can't work out any preferred size for the scene (just so that the initial window shows up).
How to fix it
To fix it, put some content in the enclosed scenes (and/or set the initial scene size in your scene constructors).
import javafx.application.Application;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.paint.Color;
public class TestApplication extends Application {
private Stage stage;
private Scene scene, scene2;
public void start(Stage s) {
scene = new Scene(new Group(new Label("1")), 200, 150);
scene2 = new Scene(new Group(new Label("2")), 200, 150);
scene.setFill(Color.GREEN);
scene2.setFill(Color.ORANGE);
scene.setOnMouseClicked(e -> changeScene(scene2));
scene2.setOnMouseClicked(e -> changeScene(scene));
stage = s;
s.setScene(scene);
s.show();
}
private void changeScene(Scene nex) {
stage.setScene(nex);
}
public static void main(String[] args) {
launch(args);
}
}
I need to spawn multiple shapes with different properties in javafx but when I run the program none of the shapes are being added. Any guidance in the right direction is appreciated. I would create individual objects but I need to continuously remove and recreate the objects with random properties once they go out of bounds.
package game
import java.util.ArrayList;
import javafx.animation.AnimationTimer;
import static javafx.application.Application.launch;
import javafx.scene.shape.Circle;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Game extends Application {
static ArrayList <Circle> circles = new ArrayList();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Scene scene = new Scene(root,800, 600);
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
spawnCircles();
}
};
timer.start();
root.getChildren().addAll(circles);
primaryStage.setScene(scene);
primaryStage.show();
}
void spawnCircles() {
if (circles.size() < 20) {
createCircle();
}
}
void createCircle() {
Circle circle = new Circle(Math.random() * 100, Color.RED);
circle.setCenterX(Math.random()* 800);
circle.setCenterY(Math.random()* 600);
circles.add(circle);
}
}
In your start method you add an empty ArrayList to your root pane and later your AnimationTimer adds circles to your ArrayList. How do you expect them to magically show up in your root pane. Just add them to the root pane instead of the ArrayList or make this a Group instead of an ArrayList.
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);
}
}