简体   繁体   English

着色JavaFX形状

[英]Colouring JavaFX Shape

I have a Scene graph which contains two buttons, one for creating rectangles and one for creating circles, there is also a slider which, when moved will change the colour of the shape objects. 我有一个场景图,其中包含两个按钮,一个用于创建矩形,一个用于创建圆,还有一个滑块,当移动时该滑块将更改形状对象的颜色。 The shapes are also draggable. 形状也是可拖动的。 Unfortunately, the colour slider doesn't quite work the way i want it to. 不幸的是,颜色滑块并不完全符合我的期望。 The idea is that once the shape is created via the button, and the shape is clicked on with the mouse, the colour of the shape will change depending on where the slider is dragged (the only colour is red). 这个想法是,一旦通过按钮创建了形状,并用鼠标单击了该形状,形状的颜色将根据拖动滑块的位置而改变(唯一的颜色是红色)。

The issue is that i can't independently colour and recolour the shapes as i want - if the rectangle is created first followed by the circle, I can colour the rectangle but not the circle. 问题是我无法根据需要独立地对形状进行着色和重新着色-如果先创建矩形,然后创建圆形,则可以为矩形着色,但不能为圆形着色。 if i create the circle fist followed by the rectangle, i can colour the circle and then the rectangle, but upon selecting the circle again, cannot colour it 如果我先创建圆形拳头,然后再创建矩形,则可以先为圆形着色,然后再为矩形着色,但是再次选择圆形后,就无法为其着色

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.stage.Stage;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;


public class SquareRectangle extends Application
{
    private Rectangle selectedRectangle;
    private Circle selectedCircle;
    private int red = 255;
    private int green = 0;
    private int blue = 0;

    public static void main(String [] args)
    {
        launch(args);
    }
    public void start(Stage primaryStage)
    {
        Pane root = new Pane();
        Scene scene = new Scene(root,800,800);

        Button Rectangles = new Button("Rectangles");
        Button Circles = new Button("Circles");
        Slider ColorSlider = new Slider(0,255,0);
        Circles.setLayoutX(90);
        ColorSlider.setLayoutY(70);

        Rectangles.setOnAction(e ->{
            Rectangle rect = new Rectangle();
            rect.setLayoutX(1080/2);
            rect.setLayoutY(400);
            rect.setOnMouseDragged(f ->{
                rect.setX(f.getX());
                rect.setY(f.getY());
            });
            rect.setOnMousePressed(g ->{
                selectedRectangle = (Rectangle) g.getTarget();
            });
            root.getChildren().add(rect);
            rect.setHeight(100);
            rect.setWidth(200);
        });

        Circles.setOnAction( e ->{
            Circle circle = new Circle(300,300,100);
            circle.setLayoutX(200);
            circle.setLayoutY(200);
            circle.setOnMouseDragged(f ->{
                circle.setCenterX(f.getX());
                circle.setCenterY(f.getY());
            });
            circle.setOnMousePressed(g ->{
                selectedCircle = (Circle) g.getTarget();
            });
            root.getChildren().add(circle);
        });

        ColorSlider.valueProperty().addListener(
            (ObservableValue<? extends Number> ov,Number curVal, Number newVal)->{
                    if(selectedRectangle != null){
                        red = (int) ColorSlider.getValue();
                        selectedRectangle.setFill(Color.rgb(red,green,blue));
                }
                    else if(selectedCircle != null && selectedRectangle == null){
                        red = (int) ColorSlider.getValue();
                        selectedCircle.setFill(Color.rgb(red,green,blue));
                }
            });


        root.getChildren().addAll(Rectangles,Circles,ColorSlider);

        primaryStage.setTitle("Shapes");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

I'm not quite sure what I'm missing or have done wrong? 我不太确定自己缺少什么或做错了什么?

Whenever you choose a shape, that shape is then set selectedRectangle = (Rectangle) g.getTarget(); 无论何时选择形状,都将设置该形状selectedRectangle = (Rectangle) g.getTarget(); , or selectedCircle = (Circle) g.getTarget(); selectedCircle = (Circle) g.getTarget(); .

In your code for changing the colour, it will always check if the rectangle is not null: 在更改颜色的代码中,它将始终检查矩形是否不为空:

if (selectedRectangle != null) {
    red = (int) ColorSlider.getValue();
    selectedRectangle.setFill(Color.rgb(red, green, blue));
} else if (selectedCircle != null && selectedRectangle == null) {
    red = (int) ColorSlider.getValue();
    selectedCircle.setFill(Color.rgb(red, green, blue));
}

Using your code above, make sure that selectedCircle is null in the first statement ( if (selectedRectangle != null && selectedCircle == null) { ) 使用上面的代码,确保第一条语句中的selectedCircle为空( if (selectedRectangle != null && selectedCircle == null) {

Also, you need to ensure that the other shape is set to null when a new shape is selected: 此外,您还需要确保在选择新形状时将另一个形状设置为null:

selectedRectangle = (Rectangle) g.getTarget();
selectedCircle = null;

and vice versa for the circle. 反之亦然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM