同心圆与java中的颜色

时间:2017-02-17 11:00:03

标签: java arrays eclipse swing

好吧我正在尝试用同心圆创建一个射箭风格的目标,每个都有不同的颜色,但事实是,我不能用不同的颜色填充每个圆圈,如果我填充一个特定的颜色,并继续前进接下来,即使是前一个圆圈也会将其颜色更改为另一个圆圈的颜色..如何用不同的颜色填充它们?这是我的代码

public void paint(Graphics g){

            int fontSize = 20;

            g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));
            g.setColor(Color.yellow);
            g.drawArc(250, 150, 50, 50, 0, 360);
            g.fillArc(250, 150, 50, 50, 0, 360);
            g.setColor(Color.red);

            g.drawArc(230, 130, 90, 90, 0, 360);
            g.setColor(Color.blue);
            g.drawArc(210, 110, 130, 130, 0, 360);
            g.fillArc(210, 110, 130, 130, 0, 360);
            g.setColor(Color.black);
            g.drawArc(190, 90, 170, 170, 0, 360);
            g.fillArc(190, 90, 170, 170, 0, 360);

    }

1 个答案:

答案 0 :(得分:0)

这是一个JavaFX解决方案:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class Lab8 extends Application {

    private Pane pane;

    @Override
    public void start(Stage primaryStage) throws Exception {
        pane = new Pane();

        int pwidth = 425;
        int pheight = 425;
        int cx = 210;
        int cy = 210;

        Rectangle r = new Rectangle();
        r.setX(0);
        r.setY(0);
        r.setWidth(pwidth);
        r.setHeight(pheight);
        r.setFill(Color.web("#00ffff"));
        pane.getChildren().add(r);  

        Ellipse c0 = new Ellipse(cx,cy,205,205);
        c0.setFill(Color.WHITE);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0);

        c0 = new Ellipse(cx,cy,185,185);
        c0.setFill(Color.WHITE);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0);     

        c0 = new Ellipse(cx,cy,165,165);
        c0.setFill(Color.BLACK);
        c0.setStroke(Color.WHITE);
        pane.getChildren().add(c0);     

        c0 = new Ellipse(cx,cy,145,145);
        c0.setFill(Color.BLACK);
        c0.setStroke(Color.WHITE);
        pane.getChildren().add(c0);                 

        c0 = new Ellipse(cx,cy,125,125);
        c0.setFill(Color.BLUE);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0); 

        c0 = new Ellipse(cx,cy,105,105);
        c0.setFill(Color.BLUE);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0); 

        c0 = new Ellipse(cx,cy,85,85);
        c0.setFill(Color.RED);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0); 

        c0 = new Ellipse(cx,cy,65,65);
        c0.setFill(Color.RED);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0); 

        c0 = new Ellipse(cx,cy,45,45);
        c0.setFill(Color.YELLOW);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0); 

        c0 = new Ellipse(cx,cy,25,25);
        c0.setFill(Color.YELLOW);
        c0.setStroke(Color.BLACK);
        pane.getChildren().add(c0); 

        c0 = new Ellipse(cx,cy,5,5);
        c0.setFill(Color.BLACK);
        c0.setStroke(Color.WHITE);
        pane.getChildren().add(c0);         

        Scene scene = new Scene(pane,pwidth,pheight);
        primaryStage.setTitle("Target");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            public void handle(WindowEvent we) {
                //stage is closing
                System.out.println("Bye.");
            }
        });
    }

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

enter image description here