0

Trying to make a rainbow using 2 arcs and filling the lines between with the FXgraphics2D drawArc and drawLine functions. Its not working as expected. The assignment is to make a rainbow starting and ending with red displaying the huespectrum within it.

The requested result:

The requested result

public class Rainbow extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
    Canvas canvas = new Canvas(1920, 1080);
    draw(new FXGraphics2D(canvas.getGraphicsContext2D()));
    primaryStage.setScene(new Scene(new Group(canvas)));
    primaryStage.setTitle("Rainbow");
    primaryStage.show();
}


public void draw(FXGraphics2D g2d) {
    int radiusBuiten = 1000;
    int radiusBinnen = radiusBuiten/2;
    for (int i = 0; i < 180; i++) {
        g2d.drawArc(0, 0, radiusBuiten, radiusBuiten, 0, i);
        g2d.drawArc(radiusBinnen/2, radiusBinnen/2, radiusBinnen, radiusBinnen, 0, i);
        float hoek = i;
        double x1 = radiusBinnen * Math.cos(hoek);
        double y1 = radiusBinnen * Math.sin(hoek);
        double x2 = radiusBuiten * Math.cos(hoek);
        double y2 = radiusBuiten * Math.sin(hoek);
        g2d.setColor(Color.getHSBColor(i/500.0f, 1, 1));
        g2d.draw(new Line2D.Double((int)x1,(int)y1, (int)x2, (int)y2));
    }


}




public static void main(String[] args) {
    launch(Rainbow.class);
}

}

Drawing the arcs is going as pleased but filling in the lines between the arcs is giving me trouble. Im also not sure what the value should be for the angle variabel and how to get the correct color values.

Spektre
  • 49,595
  • 11
  • 110
  • 380
oknorton
  • 3
  • 2
  • You can use this [my Spectral colors](https://stackoverflow.com/a/22681410/2521214) for the coloring... The wanted rainbow coloring is weird should not the same colors be on radius instead of angle (90 deg rotation) but I guess you do not have such api (Gourard shading) at disposal so you use lines at angle instead ... Also the angle `hoek` is in degrees I do not code in Java but I would expect it should be in radians so try `hoek = 0.01745329251994329576923690768489*float(i);` instead – Spektre Mar 10 '22 at 09:39
  • also `x1,y1` is the same as `x2,y2` because you used the same angle, Why not set `x1=x2; y1=y2; x2=r*cos(ang); y2=r*sin(ang);` while you set x2,y2 to starting point before the loop... or increment the angle after computing x1,y1 with angle step – Spektre Mar 11 '22 at 07:12

0 Answers0