Why SVG file does not change scale in Java? - java

I have a SVG file and I'm trying to integrate it within a button using javafx, I succeed in that but it keeps a big size, at the begging the file was like this
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M8 256c0 137 111 248 248 248s248-111 248-248S393 8 256 8 8 119 8 256zM256 40c118.7 0 216 96.1 216 216 0 118.7-96.1 216-216 216-118.7 0-216-96.1-216-216 0-118.7 96.1-216 216-216zm-32 88v64H120c-13.2 0-24 10.8-24 24v80c0 13.2 10.8 24 24 24h104v64c0 28.4 34.5 42.8 54.6 22.6l128-128c12.5-12.5 12.5-32.8 0-45.3l-128-128c-20.1-20-54.6-5.8-54.6 22.7zm160 128L256 384v-96H128v-64h128v-96l128 128z"/></svg>
so I thought I've to change its scale, so I add a specific width and height
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="150px" height="150px">...
I've used SVGLoader from git, and I wrote this code to see the results
package tutoFX;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import net.javainthebox.caraibe.svg.SVGContent;
import net.javainthebox.caraibe.svg.SVGLoader;
public class SVGLoaderSample extends Application {
#Override
public void start(Stage stage) {
SVGContent content = SVGLoader.load(getClass().getResource("arrow-alt-circle-right10.svg").toString());
// create a button and set the graphics node
Button button = new Button();
button.setGraphic(content);
// add the button to the scene and show the scene
HBox layout = new HBox(button);
HBox.setMargin(button, new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.setTitle("SVGLoader Sample");
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
enter image description here
but not as expected, some of you might say use this
// Scale the image and wrap it in a Group to make the button
// properly scale to the size of the image
content.setScaleX(0.1);
content.setScaleY(0.1);
Yes it changes the size of the icon, but it keeps the node bigger
enter image description here
thanks for help

I advise you to use import javafx.scene.Group;
package tutoFX;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import net.javainthebox.caraibe.svg.SVGContent;
import net.javainthebox.caraibe.svg.SVGLoader;
public class SVGLoaderSample extends Application {
#Override
public void start(Stage stage) {
SVGContent content = SVGLoader.load(getClass().getResource("arrow-alt-circle-right10.svg").toString());
// Scale the image and wrap it in a Group to make the button
// properly scale to the size of the image
content.setScaleX(0.1);
content.setScaleY(0.1);
Group graphic = new Group(content);
// create a button and set the graphics node
Button button = new Button();
button.setGraphic(graphic);
// add the button to the scene and show the scene
HBox layout = new HBox(button);
HBox.setMargin(button, new Insets(10));
Scene scene = new Scene(layout);
stage.setScene(scene);
stage.setTitle("SVGLoader Sample");
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}

Related

Bind TextArea's font size to current Scene's width using JavaFX

I want a TextArea's font-size to increase or decrease depending on the width property of the Scene it's in. But I don't want the font-size to grow beyond 16px or shrink beyond 10px.
I found this older post that has the code for making TextArea's font-size grow and shrink from being binded to the Scene's width property but I'm not sure how to add the conditional Bindings for the functionality I want.
This is the code from that post which suits my needs:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FontBind extends Application {
private DoubleProperty fontSize = new SimpleDoubleProperty(10);
private IntegerProperty blues = new SimpleIntegerProperty(50);
#Override
public void start(Stage primaryStage) {
Button btn = new Button("click me, I change color");
btn.setOnAction((evt)->{blues.set(blues.get()+20);});//max?
Label lbl = new Label("I'm a label");
TextArea ta = new TextArea("Lots of text can be typed\nand even number 1234567890");
HBox hbox = new HBox(new Label("I never change"));
VBox child = new VBox(btn, lbl, ta);
VBox root = new VBox(child, hbox);
Scene scene = new Scene(root, 300, 250);
fontSize.bind(scene.widthProperty().add(scene.heightProperty()).divide(50));
child.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize.asString(), ";"
,"-fx-base: rgb(100,100,",blues.asString(),");"));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Any help would be very much appreciated. I really want to learn more about using Bindings but I'm having trouble understanding how to implement the Bindings methods

Why is the canvas not clearing after applying effect by ColorAdjust?

After applying the color adjust effect and redrawing the canvas , the canvas is not being cleared so that i can update the canvas with some other image, the images are overlapping.
I have a Border Pane containing Scroll Pane to adjust large images
The scroll Pane contains the canvas
A function that clears the canvas then adjusts the brightness and applies the effect on canvas.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage primaryStage;
// load the image
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_europeo4.jpg/1024px-Gatto_europeo4.jpg");
// the container for the image as a javafx node
Canvas canvas = new Canvas(600,700);
GraphicsContext gc = canvas.getGraphicsContext2D();
#Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
primaryStage.setTitle("Test");
BorderPane root = new BorderPane();
// container for image layers
ScrollPane scrollPane = new ScrollPane();
// use scrollpane for image view in case the image is large
scrollPane.setContent(canvas);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
changeBrightness(canvas,image);// function to add effect on canvas
// put scrollpane in scene
root.setCenter(scrollPane);
primaryStage.setScene(new Scene(root, 780.0, 620.0));
primaryStage.show();
}
private void changeBrightness(Canvas canvas, Image image) {
//clearing canvas before changing brightness working
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.3);
gc.setEffect(colorAdjust);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
//trying to clear canvas after changing brightness
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
gc.drawImage(image,0,0,canvas.getWidth()/2,canvas.getHeight()/2);
}
public static void main(String[] args) {
launch(args);
}
}
You have to remove the effect on your Graphics object before calling gc.clearRect(...)
Here the code:
package com.simple.test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage primaryStage;
// load the image
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_europeo4.jpg/1024px-Gatto_europeo4.jpg");
// the container for the image as a javafx node
Canvas canvas = new Canvas(600,700);
GraphicsContext gc = canvas.getGraphicsContext2D();
#Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
primaryStage.setTitle("Test");
BorderPane root = new BorderPane();
// container for image layers
ScrollPane scrollPane = new ScrollPane();
// use scrollpane for image view in case the image is large
scrollPane.setContent(canvas);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
changeBrightness(canvas,image);// function to add effect on canvas
// put scrollpane in scene
root.setCenter(scrollPane);
primaryStage.setScene(new Scene(root, 780.0, 620.0));
primaryStage.show();
}
private void changeBrightness(Canvas canvas, Image image) {
//clearing canvas before changing brightness working
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.3);
gc.setEffect(colorAdjust);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
// Remove the effect
gc.setEffect(null);
//trying to clear canvas after changing brightness
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
// Now apply again
gc.setEffect(colorAdjust);
// Redraw
gc.drawImage(image,0,0,canvas.getWidth()/2,canvas.getHeight()/2);
// Remove the effect again
gc.setEffect(null);
}
public static void main(String[] args) {
launch(args);
}
}
You may look at this for further information:
JavaFX GraphicsContext clearRect doesn't work with clip mask
Why won't gc.clearRect clear the canvas?
Btw, cat is cute.

Set up size of button (javafx) using css

I create a button and I want to set up its width and height through css. I tried this but it didn't work.
Other properties( such as font-size and text fill) are fine and work correctly. But the width of Button seems to be fixed and doesn't change.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
VBox root = new VBox();
Button button = new Button("Button");
root.getChildren().add(button);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
button.setId("button");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And in "Style.css":
#button{
-fx-width: 300;
-fx-font-size:20;
-fx-text-fill: yellow;
}

JavaFX Slider ticks not showing correctly

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

JavaFX setPrefWrapLength() method

It doesn't matter in the following code whether i pass the value 50 or 300 to the function. all my buttons are always in a row unless i resize the window. According to theory after the row of my nodes reaches 50 pixels (suppose i pass that value to the setPrefWrapLength() method) my next node should be in the next row. But that is not happening. Can someone explain me why?
package test2;
import com.sun.java.swing.plaf.gtk.GTKConstants;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test2 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn1 = new Button("one");
Button btn2 = new Button("two");
Button btn3 = new Button("three");
Button btn4 = new Button("four");
FlowPane fpane = new FlowPane(Orientation.HORIZONTAL, 10, 10, btn1, btn2, btn3, btn4);
fpane.setPrefWrapLength(50);
Scene scene = new Scene(fpane, 300, 250);
primaryStage.setTitle("Flow Pane");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
update :
I found out the following line in the javaFX documentation and i think this is in context with this question
Note that prefWrapLength is used only for calculating the preferred size and may not reflect the actual wrapping dimension, which tracks the actual size of the flowpane.

Categories

Resources