嘿我试图在javafx和scenebuilder中构建一个简单的绘图应用程序。 我有一个ColorPicker有很多种颜色,但我希望用户能够从Slider(f.ex RGB或HEX)自定义颜色。
“公共颜色(双红色, 双绿色, 双蓝色, 双重不透明度) 创建一个新的颜色实例 参数: 红色 - 红色成分从0到1不等 绿色 - 绿色成分从0到1不等 蓝色 - 蓝色成分从0到1不等 不透明度 - 不透明度范围从0到1“
所以我构建了这个程序(参见“setSliderColor”方法):
package sample;
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.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
try {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Paint app");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}catch (Exception e){
e.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
控制器类:
package sample;
import javafx.fxml.FXML;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.scene.shape.StrokeLineCap;
public class Controller {
GraphicsContext gc;
private Color totalSlideColor;
private static double buttonSizeValue = 0;
private static double sliderValue = 0;
@FXML
Canvas canvas;
@FXML
ColorPicker colorPicker = new ColorPicker();
@FXML
Slider slider, rslider, gslider, bslider;
@FXML
Button slet, tegn, op, ned;
@FXML
public void drawCanvas() {
gc = canvas.getGraphicsContext2D();
System.out.println("Tegner!");
try {
canvas.setOnMousePressed(event -> {
setColor();
setSize();
gc.beginPath();
gc.setLineCap(StrokeLineCap.ROUND);
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
canvas.setOnMouseDragged(event -> {
setColor();
setSize();
gc.lineTo(event.getSceneX(), event.getSceneY());
gc.stroke();
});
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
@FXML
private void setColor() {
gc = canvas.getGraphicsContext2D();
gc.setStroke(colorPicker.getValue());
System.out.println(colorPicker.getValue());
}
@FXML
private void setSliderColor() {
try {
rslider.setMin(0);
rslider.setMax(1);
rslider.setBlockIncrement(0.1);
rslider.setShowTickLabels(true);
double redValue = rslider.getValue();
gslider.setMin(0);
gslider.setMax(1);
gslider.setBlockIncrement(0.1);
gslider.setShowTickLabels(true);
double greenValue = rslider.getValue();
bslider.setMin(0);
bslider.setMax(1);
bslider.setBlockIncrement(0.1);
bslider.setShowTickLabels(true);
double blueValue = bslider.getValue();
totalSlideColor = new Color(redValue, greenValue, blueValue, 1);
System.out.println(totalSlideColor.toString());
gc.setStroke(totalSlideColor);
} catch (Exception e) {
e.printStackTrace();
}
}
@FXML
private void setSize() {
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
buttonSizeValue = sliderValue;
slider.setMin(1);
slider.setMax(100);
double sliderDrawValue = slider.getValue();
gc.setLineWidth(sliderDrawValue);
}
@FXML
private void setButtonSize() {
op.setOnMousePressed(event -> {
buttonSizeValue += 10;
gc.setLineWidth(buttonSizeValue);
System.out.println("op 10 størrelse");
});
ned.setOnMousePressed(event -> {
buttonSizeValue -= 10;
gc.setLineWidth(buttonSizeValue);
System.out.println("ned 10 størrelse");
});
sliderValue = buttonSizeValue;
slider.setValue(buttonSizeValue); //Vi sætter værdien af slideren til at være det, som knappens værdi er blevet til.
setSize();
}
@FXML
private void eraseDraw() {
System.out.println("Sletter!");
slider.onDragDetectedProperty();
gc.setLineWidth(slider.getValue());
colorPicker.setValue(new Color(0.95, 0.95, 0.95, 1));
}
}
我认为你不需要FXML
这是在控制器类中找到的方法,我尝试使幻灯片给出0到1之间的数字(增量为0,1)。 它不起作用 - 我缺少什么? 有没有更好的方法来定义3个滑块?
@FXML
private void setSliderColor() {
try {
rslider.setMin(0);
rslider.setMax(1);
rslider.setBlockIncrement(0.1);
rslider.setShowTickLabels(true);
double redValue = rslider.getValue();
gslider.setMin(0);
gslider.setMax(1);
gslider.setBlockIncrement(0.1);
gslider.setShowTickLabels(true);
double greenValue = rslider.getValue();
bslider.setMin(0);
bslider.setMax(1);
bslider.setBlockIncrement(0.1);
bslider.setShowTickLabels(true);
double blueValue = bslider.getValue();
totalSlideColor = new Color(redValue, greenValue, blueValue, 1);
System.out.println(totalSlideColor.toString());
gc.setStroke(totalSlideColor);
} catch (Exception e) {
e.printStackTrace();
}
}