I am using anchorpane in my javafx application. I want to draw a rectangle and fill it with gradient color like this: the left side of the rectangle is blue and the right side is red and I want it to seem that from left to right, the blue color decreases and red color increases.
I know how to put a rectangle ( how to use the Rectangle class in javafx ) but I don't know how to fill it this way. Any ideas?
See docs on linear gradients.
public class Gradient extends Application {
public static final double S = 100;
#Override
public void start(Stage stage) {
Stop[] stops = new Stop[] {
new Stop(0, Color.BLUE),
new Stop(1, Color.RED)
};
LinearGradient gradient = new LinearGradient(
0, 0,
1, 0,
true,
CycleMethod.NO_CYCLE,
stops
);
Rectangle rectangle = new Rectangle(S, S, gradient);
stage.setScene(new Scene(new Group(rectangle)));
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Related
I need a function.
import javafx.scene.image.Image;
private Image cropImage(Image image) {
// Cut Image to max Circle or square
return Image;
}
The important thing is that I don't want to change the scale of the image but cut the maximum circle or square in the middle of the image.
I have been trying to do this for two days now.
Can you help me with this?
Thank you very much. Rene
Something like this:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Image img = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Osias_Beert_the_Elder_-_Dishes_with_Oysters%2C_Fruit%2C_and_Wine_-_Google_Art_Project.jpg/350px-Osias_Beert_the_Elder_-_Dishes_with_Oysters%2C_Fruit%2C_and_Wine_-_Google_Art_Project.jpg");
Image img2 = crop(img, false);
Image img3 = crop(img, true);
HBox box = new HBox(8, new ImageView(img),new ImageView(img2),new ImageView(img3));
Scene scene = new Scene(box);
stage.setScene(scene);
stage.show();
}
private Image crop(Image img, boolean toCircle) {
double d = Math.min(img.getWidth(),img.getHeight());
double x = (d-img.getWidth())/2;
double y = (d-img.getHeight())/2;
Canvas canvas = new Canvas(d, d);
GraphicsContext g = canvas.getGraphicsContext2D();
if (toCircle) {
g.fillOval(0, 0, d, d);
g.setGlobalBlendMode(BlendMode.SRC_ATOP);
}
g.drawImage(img, x, y);
return canvas.snapshot(null, null);
}
}
You can do that via the setClip method of the ImageView.
so I am writing a program about the monte carlous math problem and I am doing the visual side of it. I mostly have everything set, the only issue is that I can't put the pixels in a loop for some reason. Any help? I've already tried a for loop though.
public class Main extends Application {
#Override
public void start(Stage primaryStage)
{
int width = 1;
int random = ((int)Math.random()*400)+1;
// Create the Colors
Color white = Color.web("rgb(255,255,255)");
Color black = Color.web("rgb(0,0,0)");
//making the square
Rectangle square = new Rectangle(115,50,400,400);
square.setFill(black);
square.setStroke(white);
square.setStrokeWidth(width);
//Drawing the Circle and pixels
Circle circle = new Circle();
Circle pixel = new Circle();
//Setting the properties of the circle
circle.setCenterX(315.0f); //moves the cirlce left or right
circle.setCenterY(250.0f); //moves the circle up and down
circle.setRadius(200.0f);
circle.setFill(black);
circle.setStroke(white);
circle.setStrokeWidth(width);
//setting properties of the pixels
for(int i = 0; i<1000; i++)
{
pixel.setCenterX((int)(Math.random()*400)+1);
pixel.setCenterY((int)(Math.random()*400)+1);
pixel.setRadius(1);
pixel.setFill(white);
}
//Creating a Group object
Group Root = new Group(circle);
Pane pane = new Pane();
pane.getChildren().addAll(square,circle,pixel);
Scene scene = new Scene(pane,630,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Code
public class PixelLoop extends Application {
#Override
public void start(Stage primaryStage) {
int width = 1;
int random = ((int)Math.random()*400)+1;
// Create the Colors
Color white = Color.web("rgb(255,255,255)");
Color black = Color.web("rgb(0,0,0)");
//making the square
Rectangle square = new Rectangle(115,50,400,400);
square.setFill(black);
square.setStroke(white);
square.setStrokeWidth(width);
//Drawing the Circle and pixels
Circle circle = new Circle();
//Setting the properties of the circle
circle.setCenterX(315.0f); //moves the cirlce left or right
circle.setCenterY(250.0f); //moves the circle up and down
circle.setRadius(200.0f);
circle.setFill(black);
circle.setStroke(white);
circle.setStrokeWidth(width);
//Creating a Group object
Group Root = new Group(circle);
Pane pane = new Pane();
pane.getChildren().addAll(square,circle);
//setting properties of the pixels
for(int i = 0; i<1000; i++) {
Circle pixel = new Circle();
pixel.setCenterX((int)(Math.random()*400)+1);
pixel.setCenterY((int)(Math.random()*400)+1);
pixel.setRadius(1);
pixel.setFill(white);
pane.getChildren().add(pixel);
}
Scene scene = new Scene(pane,630,500);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) { Application.launch(args); }
}
In the beginning there was the problem that you overwrote your object over and over again. That is the reason why there was just one pixel.
As #Tobias Brösamle said, you should create a new object every time you pass through the loop and then add it to the pane.
If you want negativ Coordinates try this
I'm trying to learn how JavaFX animations work, so I've tried to create an animation with Earth moving in a circle, based on this tutorial: https://gamedevelopment.tutsplus.com/tutorials/introduction-to-javafx-for-game-development--cms-23835.
For some reason, the scene I want to show is set in the stage, but the graphics or animations aren't. I'm pretty sure this one is trivial, I just can't seem to find the reason. Here's my Main class code:
public class Main extends Application {
private static Stage stage;
private static Window window;
private static Scene scWindow;
#Override
public void start(Stage primaryStage) {
stage = primaryStage;
stage.setTitle("Animation");
showWindow();
stage.show();
}
public static void showWindow() {
window = new Window();
scWindow = new Scene(window, 400, 400);
stage.setScene(scWindow);
}
public static void main(String[] args) {
launch(args);
}
}
and this is the Window class code:
public class Window extends BorderPane {
final long startNanoTime = System.nanoTime();
Image earth = new Image("http://icdn.pro/images/en/g/o/google-earth-icone-8927-128.png");
Image space = new Image("https://space-facts.com/wp-content/uploads/magellanic-clouds.png");
GraphicsContext gc;
public Window() {
Group root = new Group();
Canvas canvas = new Canvas(512, 512);
gc = canvas.getGraphicsContext2D();
root.getChildren().add(canvas);
gc.setFill(Color.BLACK);
Anim a = new Anim();
a.start();
}
private class Anim extends AnimationTimer {
public void handle(long currentNanoTime)
{
double t = (currentNanoTime - startNanoTime) / 1000000000.0;
double x = 232 + 128 * Math.cos(t);
double y = 232 + 128 * Math.sin(t);
gc.drawImage( space, 0, 0 );
gc.drawImage( earth, x, y );
}
}
}
There's no exception thrown or anything, I have no idea what's wrong.
EDIT:
I know that I haven't put anything in the setCenter/Left etc. in the constructor, so actually if it is the reason, how should I put the animation e.g. on the center of the border pane?
You forgot to add root to the scene. Add something like
setCenter(root);
to the constructor of Window.
Or simply use the Canvas as center:
public Window() {
Canvas canvas = new Canvas(512, 512);
gc = canvas.getGraphicsContext2D();
setCenter(canvas);
gc.setFill(Color.BLACK);
Anim a = new Anim();
a.start();
}
I have a grid.
Now the background of the grid is lightGray.
But how can I get the background of the grid with random colors? So not one color.
How can I make this?
Can someone help me?
thanks in advance.
the code is:
public class Grid2 extends Application {
double gridSize = 20;
#Override
public void start(Stage primaryStage) {
Scene scene = new Scene(new Group(), 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
scene.setFill(createGridPattern());
}
public ImagePattern createGridPattern() {
double w = gridSize;
double h = gridSize;
Canvas canvas = new Canvas(w, h);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setStroke(Color.BLACK);
gc.setFill(Color.LIGHTGRAY.deriveColor(1, 1, 1, 0.2));
gc.fillRect(0, 0, w, h);
gc.strokeRect(0, 0, w, h);
Image image = canvas.snapshot(new SnapshotParameters(), null);
ImagePattern pattern = new ImagePattern(image, 0, 0, w, h, false);
return pattern;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
You can probably add a new Jpanel() object of each cell of your grid to get a different color. By making different component of group of some specific cells or just even one cell, you can have different background color for the same.
I'm trying to create a basic UML diagramming tool with JavaFX.
First I did this:
public class Asd extends Application {
Double WINDOW_HEIGHT = (double) 400;
Double WINDOW_WIDTH = (double) 800;
Double RECTANGLE_DEFAULT_HEIGHT = (double) 50;
Double RECTANGLE_DEFAULT_WIDTH = (double) 100;
#Override
public void start(Stage stage) throws Exception {
final Pane root = new Pane();
Rectangle background = new Rectangle(WINDOW_WIDTH, WINDOW_HEIGHT,
Color.WHITE);
root.getChildren().add(background);
Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
// On window click
root.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent mouseEvent) {
Rectangle rect = new Rectangle();
rect.setX(mouseEvent.getX());
rect.setY(mouseEvent.getY());
rect.setWidth(RECTANGLE_DEFAULT_WIDTH);
rect.setHeight(RECTANGLE_DEFAULT_HEIGHT);
rect.setFill(null);
rect.setStroke(Color.BLACK);
root.getChildren().add(rect);
}
});
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
With that, I can draw a rectangle (that would represent a UML Class) in the exact point I click.
Now I want to add text. If instead of
root.getChildren().add(rect);
I do
Text text = new Text("mockText");
StackPane stackPane = new StackPane();
stackPane.getChildren().addAll(rect, text);
root.getChildren().add(stackPane);
the text is placed in the center of the rectangle, but all rectangles are positioned at coordinates (0,0)
So, any way to change the position of each StackPane? Or any alternative to add text to the rectangles directly without using StackPane?
OK, got it. Layout elements have methods setLayoutX & setLayoutY that can be used in cases like this one, so I don't need anymore Rectangle.setX/setY & finally the handle method should be:
public void handle(MouseEvent mouseEvent) {
Rectangle rect = new Rectangle();
rect.setWidth(RECTANGLE_DEFAULT_WIDTH);
rect.setHeight(RECTANGLE_DEFAULT_HEIGHT);
rect.setFill(null);
rect.setStroke(Color.BLACK);
Text text = new Text("mockText");
StackPane stackPane = new StackPane();
stackPane.getChildren().addAll(rect, text);
stackPane.setLayoutX(mouseEvent.getX());
stackPane.setLayoutY(mouseEvent.getY());
root.getChildren().add(stackPane);
}