I want to apply two linear gradients to an ellipse. There are two ways:
Way 1: I can create two ellipses in a position and apply each gradient separately to each ellipse.
Way2: I can create a pane, set its shape to ellipse and apply both gradients at once, using JavaFX styling.
The code of both the ways is as follows:
Way 1
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class Test85 extends Application {
public static void main(String[] args) {
launch();
}
public static void setCustomSize(Region region, double width, double height) {
region.setMaxSize(width, height);
region.setPrefSize(width, height);
region.setMinSize(width, height);
}
@Override
public void start(Stage stage) throws Exception {
Pane container = new Pane();
Scene scene = new Scene(container);
stage.setScene(scene);
stage.show();
Ellipse ellipse1 = new Ellipse(500, 250, 300, 150);
Ellipse ellipse2 = new Ellipse(500, 250, 300, 150);
ellipse1.setFill(new LinearGradient(0, 0, 1, 0, true,
CycleMethod.NO_CYCLE,
new Stop(0, Color.color(1, 0, 0, 1)),
new Stop(1, Color.color(0, 0, 1, 1))));
ellipse1.setStroke(Color.BLACK);
ellipse1.setStrokeWidth(5);
ellipse2.setFill(new LinearGradient(0, 0, 0, 1, true,
CycleMethod.NO_CYCLE,
new Stop(0, Color.color(0, 1, 0, 0.5)),
new Stop(1, Color.color(1, 1, 0, 0.5))));
ellipse2.setStroke(Color.BLACK);
ellipse2.setStrokeWidth(5);
container.getChildren().addAll(ellipse1, ellipse2);
}
}
Way 2
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class Test84 extends Application {
public static void main(String[] args) {
launch();
}
public static void setCustomSize(Region region, double width, double height) {
region.setMaxSize(width, height);
region.setPrefSize(width, height);
region.setMinSize(width, height);
}
@Override
public void start(Stage stage) throws Exception {
Pane container = new Pane();
Scene scene = new Scene(container);
stage.setScene(scene);
stage.show();
Pane pane = new Pane();
setCustomSize(pane, 600, 300);
container.getChildren().add(pane);
pane.setTranslateX(300);
pane.setTranslateY(100);
pane.setStyle("-fx-background-color:" +
" linear-gradient(from 0.0% 0.0% to 100.0% 0.0%, #ff0000ff 0.0%, #0000ffff 100.0%)," +
"linear-gradient(from 0.0% 0.0% to 0.0% 100.0%, #00ff0080 0.0%, #ffff0080 100.0%);" +
"-fx-border-color: black;" +
"-fx-border-width: 5;");
pane.setShape(new Ellipse(1, 1));
}
}
Both generate the following result
My question is:
Which of the ways is better and more scalable? Say, I wish to add 10 fills of different kinds to different shapes in a software of mine. Which way reduces memory usage and maintains software performance?