0

Hey im trying to build a simple paint app in javafx and scenebuilder. I have a ColorPicker that has a big variety of colors, but i want the user to be able to customize their color from a Slider (f.ex RGB or HEX).

"public Color(double red, double green, double blue, double opacity) Creates a new instance of color Parameters: red - red component ranging from 0 to 1 green - green component ranging from 0 to 1 blue - blue component ranging from 0 to 1 opacity - opacity ranging from 0 to 1"

So i build this program (see the "setSliderColor" method):

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);
    }
}

And the controller class:

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));
    }
}

I don't think you need the FXML

This is the method found in the controller class, and i've tried making the slides give a number between 0 and 1 (with increment of 0,1). It is not working - what am i missing? Is there a better way to define the 3 Sliders?

@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();
    }
}
blixenkrone
  • 388
  • 2
  • 7
  • 20
  • You get what runtime error and on which line? – UnholySheep Mar 15 '17 at 13:05
  • "I get a runtime error". Perhaps it would be useful to post that error in the question? Also, stop suppressing the stack traces: those [tell you what is going wrong](http://stackoverflow.com/q/3988788/2775450). Your catch blocks should have `e.printStackTrace()` instead of `System.out.println(e);`. – James_D Mar 15 '17 at 13:07
  • I've posted the error and line, and used printStackTrace instead of println. – blixenkrone Mar 15 '17 at 13:27
  • So i used the e.printStackTrace() instead, and i found that the error was recursive use of the method. The error comes from "gc.setStroke(totalSlideColor);" – blixenkrone Mar 15 '17 at 13:38
  • How is this a duplicate? This is not a nullpointerexception? – blixenkrone Mar 15 '17 at 14:03
  • Uh, the [stack trace](http://stackoverflow.com/q/3988788/2775450) you posted says it is a null pointer exception. If that is not the correct stack trace, [edit] the question. – James_D Mar 15 '17 at 14:05
  • Ah sorry my bad, i looked at the wrong error message. I'll edit the question, as i located the error. Thanks. – blixenkrone Mar 15 '17 at 14:08
  • Edited the question – blixenkrone Mar 15 '17 at 14:30
  • 1
    I'm confused by your edit. Are you saying you are not getting the null pointer exception any more? What does "it is not working" mean? – James_D Mar 15 '17 at 14:37
  • No i get the null pointer now, as described: "It (e.g. The sliders) are not working - what am i missing? Is there a better way to define the 3 Sliders?". I can try to elaborate more if needed? – blixenkrone Mar 16 '17 at 12:18

0 Answers0