2017-02-17 40 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