Make vertical Menu Bar in JavaFX - menu

Since my interaction with computer, I have seen only menu bar in horizontal direction only. The menuitem of such menubar will be popping downwards. In JavaFX it is easy to create such a menu with a horizontal menubar.
Is it possible to create a vertical menubar in JavaFX ? Also I want the menuitems to be popped out either to left or right, not downwards.
Can I implement such a menu of my desire ? Someone please help.

You can leverage the MenuButton for that:
#Override
public void start(Stage primaryStage) {
MenuButton m = new MenuButton("Eats");
m.setPrefWidth(100);
m.setPopupSide(Side.RIGHT);
m.getItems().addAll(new MenuItem("Burger"), new MenuItem("Hot Dog"));
MenuButton m2 = new MenuButton("Drinks");
m2.setPrefWidth(100);
m2.setPopupSide(Side.RIGHT);
m2.getItems().addAll(new MenuItem("Juice"), new MenuItem("Milk"));
VBox root = new VBox();
root.getChildren().addAll(m, m2);
Scene scene = new Scene(root, 300, 250);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
where the style.css is
.menu-button {
-fx-skin: "com.sun.javafx.scene.control.skin.MenuButtonSkin";
-fx-background-color: red, green, green, lightgreen;
-fx-background-insets: 0 0 -1 0, 0, 1, 2;
-fx-background-radius: 0;
-fx-padding: 0.0em; /* 0 */
-fx-text-fill: -fx-text-base-color;
}
/* TODO workaround for RT-19062 */
.menu-button .label { -fx-text-fill: -fx-text-base-color; }
.menu-button:focused {
-fx-color: beige;
-fx-background-color: -fx-focus-color, -fx-outer-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: -1.4, 0, 1, 2;
-fx-background-radius: 0;
}
.menu-button:hover {
-fx-color: darkgreen;
}
.menu-button:armed {
-fx-color: greenyellow;
}
These selectors are partially taken and overriden from caspian.css. Change the color preferences as your needs and you can also remove the arrow of the buttons through css.
The drawback of this approach is the difficulty of making nested menu items.

Related

How to "update" width/height of a node after scaling a SVG?

I have a bit of an issue here..
I'm trying to have a svgpath be a background image by using it in a stackpane. I have a set-up like this:
Where that grey triangle is a svgpath (just a basic one for testing purposes). Now that SVG is located inside an hbox like so:
class mView : View() {
override val root = vbox {
label("test text") {
//label styles
}
hbox {
stackpane {
//first blue box
}
stackpane {
//second green box
svgpath("M600 0 L350 800 L900 800 Z") {
addClass(UIStyles.bgImagesStyle)
}
}
stackpane {
//third red box
}
}
}
}
And this all works well and good, but I need to scale the svgpath down a bit..
svgpath("M600 0 L350 800 L900 800 Z") {
addClass(UIStyles.bgImagesStyle)
scaleX = .4
scaleY = .4
}
and then this happens:
I need the green box to adhere to the new, scaled-down bounds of the svgpath. Is there a way to "update" the width/height of the stackpane to the new bounds?
Thank you for any help!

Color of texture skybox unity

I'm working with google cardboard in unity.
In my main scene I have a skybox with an image as texture.
How can I get the color of the pixel I'm looking?
The skybox is an element of mainCamera, that is child of "Head".
I put also GvrReticle as child of head; is it useful for my purpose?
Thanks
Basically you wait for the end of the frame so that the camera has rendered. Then you read the rendered data into a texture and get the center pixel.
edit Be aware that if you have a UI element rendered in the center it will show the UI element color not the color behind.
private Texture2D tex;
public Color center;
void Awake()
{
StartCoroutine(GetCenterPixel());
}
private void CreateTexture()
{
tex = new Texture2D(1, 1, TextureFormat.RGB24, false);
}
private IEnumerator GetCenterPixel()
{
CreateTexture();
while (true)
{
yield return new WaitForEndOfFrame();
tex.ReadPixels(new Rect(Screen.width / 2f, Screen.height / 2f, 1, 1), 0, 0);
tex.Apply();
center = tex.GetPixel(0,0);
}
}

JavaFX reset graphics Context after scaling a canvas

I'm trying to make a chart without using any 3rd party libs. I've a zoom feature which scales the Canvas correctly, but now I need to redraw everything inside the canvas once again.
But when I do scaling the GrapphicsContext also scales and blots. I want to readjust the blotting and show points in normal drawing once zoomed. How can I achieve this?
Here is simple snippet that I'm redrawing:
private void redrawImage(Canvas canvas, int scale) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.scale(scale, scale);
gc.setStroke(Color.RED);
gc.setLineWidth(2);
gc.strokeRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setStroke(Color.GREEN);
gc.strokeLine(0, 0, canvas.getWidth(), canvas.getHeight());
gc.strokeLine(0, canvas.getHeight(), canvas.getWidth(), 0);
gc.setStroke(Color.BLUE);
gc.strokeText("TEXT", 50, 50);
}
Even I remove gc.scale(X,Y) I still see the blotted points or text, I want the scale to be always 1, but I should also zoom or scale simultaneously.
What I want to achieve is like the GoogleMaps overlaying, you see the objects when zoomed in or out are recalibrated and adjusted to a viewable scale. This is exactly what I want to achieve.
I don't know if I understand you correct.
Your redrawImage method should get an double and not an int value for the scale. Even if you try to scale to 1.9 the cast from double to int will bring it down to 1.
I've made a little example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
Canvas canvas = new Canvas(100, 100);
Canvas canvas2 = new Canvas(100, 100);
FlowPane root = new FlowPane();
root.getChildren().addAll(canvas, canvas2);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
redrawImage(canvas, 0.5);
redrawImage(canvas2, 1);
}
private void redrawImage(Canvas canvas, double scale) {
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.scale(scale, scale);
gc.setStroke(Color.RED);
gc.setLineWidth(2);
gc.strokeRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setStroke(Color.GREEN);
gc.strokeLine(0, 0, canvas.getWidth(), canvas.getHeight());
gc.strokeLine(0, canvas.getHeight(), canvas.getWidth(), 0);
gc.setStroke(Color.BLUE);
gc.strokeText("TEXT", 50, 50);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
With the following result:

How to modify javafx colorpicker?

This is my current color picker:
How can I style its color preview width, border color and radius? Is it possible to get rid of "Custom color" option in the bottom and instead of default colors use my own set of colors?
I tried to use this CSS:
.color-picker {
-fx-skin: "CustomSkin";
-fx-background-color: white;
-fx-background-radius: 2;
-fx-padding: 0;
-fx-background-insets: 0, 1;
}
.color-picker .color-picker-label .text {
visibility: false;
}
.color-picker .color-picker-label{
-fx-scale-x: 1.53;
-fx-scale-y: 1.53;
-fx-spacing: 0;
-fx-padding: 3;
-fx-border-width: 0;
-fx-background-insets: 0, 1;
-fx-border-radius: 2;
}
I know this is an old question, but I stumbled upon a similar issue when trying to modify the ColorPicker preview and hopefully the below can help others trying to make similar changes
In order to target the preview with css, you need to use .picker-color as the skin contains a pane which holds the preview as a rectangle
e.g.
.picker-color {
-fx-border-color: green;
}
which targets this portion of the control, allowing you to change the width etc:

Display color in a JavaFX TableRow

I would like to change the color of the text displayed in a TableRow.
The instruction setStyle("-fx-background-color: green"); is working well,
but the instruction setTextFill doesn't work. Is it normal ?
tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
#Override
public TableRow<Person> call(TableView<Person> param) {
final TableRow<Person> row = new TableRow<Person>() {
#Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setTextFill(Color.RED);
//setStyle("-fx-background-color: green");
}
};
return row;
}
});
The easiest way is to set the default CSS file in your application with:
.cell {
-fx-background-color: #FFCCAA;
-fx-text-fill: #000000;
}
/* if you want more different colours for even and odds: */
.cell:odd {
-fx-background-color: #FFDDDD;
-fx-text-fill: green;
}
You can add this file.css to your scene:
scene.getStylesheets().add("file.css");

Resources