2017-02-17 37 views
-1

好的我想创建一个射箭风格的目标与同心圆各有不同的颜色,但事情是,我不能用不同的颜色填充每个圆圈,如果我填写一个特定的颜色,然后移动到下一个,然后连上一个圆圈的颜色也会改变为另一个圆圈的颜色..我如何用不同的颜色填充它们?这里是我的代码同心圆在java中的颜色

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

    } 
+2

反向绘制圆的顺序,您绘制在较小的圆更大的圆。并且使用'fillOval'来简化。 –

+1

自定义绘画应通过重写'paintComponent(...)'不绘制(...)来完成。 – camickr

回答

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