生成随机颜色

时间:2015-07-08 19:02:03

标签: java

我无法让我的鱼充满随机颜色,但无论我做了什么,它都没有改变我的鱼。这是代码。

import java.awt.Color;
import java.util.Random;
import uwcse.graphics.*;

/**
 * A fish in a graphics window
 */

public class Fish {

    // The graphics window the fish belongs to
    private GWindow window;

    // The location of the fish
    private int x;

    private int y;

    // Scale of the drawing of the fish
    private double scale;

    private Color color;

    /**
     * Draws a Fish in a graphics window
     * 
     * @param x
     *            the x coordinate of the location of the fish
     * @param y
     *            the y coordinate of the location of the fish
     * @param scale
     *            the scale of the drawing of the fish
     * @param window
     *            the graphics window the fish belongs to
     */
    public Fish(int x, int y, double scale, GWindow window) {
        // Initialize the instance fields
        this.x  = x;
        this.y = y;
        this.scale = scale;
        this.window = window;
        this.color = randomColor();




        // Draw the fish
        this.draw();
    }
    private Color randomColor(){
        Random random = new Random();
        int R = random.nextInt(256);
        int G = random.nextInt(256);
        int B = random.nextInt(256);
        System.out.println(R + " " + B + " " + G);
        return new Color(R,B,G);

    }


    /**
     * Draws this fish
     */
    private void draw() {

        int Fsize = (int)(this.scale*15);

        Oval O1 = new Oval(this.x - Fsize /3,                          
                           this.y - Fsize /7 ,
                           Fsize + 3*Fsize/3,                          
                           Fsize  + Fsize/5,                           
                           Color.CYAN,true);        
        Triangle tail1 =new Triangle(this.x - Fsize,                                    
                                     this.y,                                     
                                     this.x - 2/Fsize,                                   
                                     this.y + Fsize/2,                                   
                                     this.x - Fsize,                                     
                                     this.y + Fsize,Color.CYAN,true);
        Line eye1 = new Line(this.x + 12*Fsize/10,                          
                            this.y + 5*Fsize/10  ,                          
                            this.x + 12*Fsize/10,                           
                            this.y + 5* Fsize /10,                          
                            Color.BLACK);   
        Oval O2 = new Oval(this.x - 37*Fsize/5,                
                   this.y+ 27*Fsize/5 ,                
                   Fsize + 2*Fsize/3, 
                   Fsize  + Fsize/5,                   
                   Color.CYAN,true);        
        Line eye2 = new Line(                
                this.x - 35*Fsize/5,                 
                this.y + 30*Fsize/5,                
                this.x - 35*Fsize/5,                 
                this.y + 30*Fsize/5, Color.BLACK);                      
        Triangle tail2 =new Triangle(this.x - Fsize*5 ,             
                 this.y + 13*Fsize/2,                
                 this.x - 10*Fsize/2,                
                 this.y + 11*Fsize/2,                
                 this.x - Fsize*6,               
                 this.y + Fsize*6,Color.CYAN,true);         


        this.window.add(O1);
        this.window.add(O2);
        this.window.add(tail2);
        this.window.add(tail1);
        this.window.add(eye1);
        this.window.add(eye2);


    }

}

`

2 个答案:

答案 0 :(得分:3)

您正在使用颜色常量变量。而不是Color.CYAN使用您的随机颜色

答案 1 :(得分:1)

正如@Michelle所说,在定义鱼类部件的颜色时,您实际上并未使用randomColor()方法或this.color的值,该值使用randomColor()的值初始化在您的draw()方法中。请尝试以下修改。

import java.awt.Color; import java.util.Random; import uwcse.graphics.*;

/** * A fish in a graphics window */

public class Fish {

// The graphics window the fish belongs to
private GWindow window;

// The location of the fish
private int x;

private int y;

// Scale of the drawing of the fish
private double scale;

private Color color;






/**
 * Draws a Fish in a graphics window
 * 
 * @param x
 *            the x coordinate of the location of the fish
 * @param y
 *            the y coordinate of the location of the fish
 * @param scale
 *            the scale of the drawing of the fish
 * @param window
 *            the graphics window the fish belongs to
 */
public Fish(int x, int y, double scale, GWindow window) {
    // Initialize the instance fields
    this.x  = x;
    this.y = y;
    this.scale = scale;
    this.window = window;
    this.color = randomColor();




    // Draw the fish
    this.draw();
}randomColor()
private Color randomColor(){
    Random random = new Random();
    int R = random.nextInt(256);
    int G = random.nextInt(256);
    int B = random.nextInt(256);
    System.out.println(R + " " + B + " " + G);
    return new Color(R,B,G);

}


/**
 * Draws this fish
 */
private void draw() {

    int Fsize = (int)(this.scale*15);

    Oval O1 = new Oval(this.x - Fsize /3,                          
                       this.y - Fsize /7 ,
                       Fsize + 3*Fsize/3,                          
                       Fsize  + Fsize/5,                           
                       randomColor(),true);        
    Triangle tail1 =new Triangle(this.x - Fsize,                                    
                                 this.y,                                     
                                 this.x - 2/Fsize,                                   
                                 this.y + Fsize/2,                                   
                                 this.x - Fsize,                                     
                                 this.y + Fsize,randomColor(),true);
    Line eye1 = new Line(this.x + 12*Fsize/10,                          
                        this.y + 5*Fsize/10  ,                          
                        this.x + 12*Fsize/10,                           
                        this.y + 5* Fsize /10,                          
                        randomColor());   
    Oval O2 = new Oval(this.x - 37*Fsize/5,                
               this.y+ 27*Fsize/5 ,                
               Fsize + 2*Fsize/3, 
               Fsize  + Fsize/5,                   
               randomColor(),true);        
    Line eye2 = new Line(                
            this.x - 35*Fsize/5,                 
            this.y + 30*Fsize/5,                
            this.x - 35*Fsize/5,                 
            this.y + 30*Fsize/5, randomColor());                      
    Triangle tail2 =new Triangle(this.x - Fsize*5 ,             
             this.y + 13*Fsize/2,                
             this.x - 10*Fsize/2,                
             this.y + 11*Fsize/2,                
             this.x - Fsize*6,               
             this.y + Fsize*6,randomColor(),true);         


    this.window.add(O1);
    this.window.add(O2);
    this.window.add(tail2);
    this.window.add(tail1);
    this.window.add(eye1);
    this.window.add(eye2);


}

}

或者如果你想要整条鱼是相同的颜色尝试类似下面的修改

   import java.awt.Color; import java.util.Random; import uwcse.graphics.*;

    /** * A fish in a graphics window */

    public class Fish {

    // The graphics window the fish belongs to
    private GWindow window;

    // The location of the fish
    private int x;

    private int y;

    // Scale of the drawing of the fish
    private double scale;

    private Color color;






    /**
     * Draws a Fish in a graphics window
     * 
     * @param x
     *            the x coordinate of the location of the fish
     * @param y
     *            the y coordinate of the location of the fish
     * @param scale
     *            the scale of the drawing of the fish
     * @param window
     *            the graphics window the fish belongs to
     */
    public Fish(int x, int y, double scale, GWindow window) {
        // Initialize the instance fields
        this.x  = x;
        this.y = y;
        this.scale = scale;
        this.window = window;
        this.color = randomColor();




        // Draw the fish
        this.draw();
    }randomColor()
    private Color randomColor(){
        Random random = new Random();
        int R = random.nextInt(256);
        int G = random.nextInt(256);
        int B = random.nextInt(256);
        System.out.println(R + " " + B + " " + G);
        return new Color(R,B,G);

    }


    /**
     * Draws this fish
     */
    private void draw() {

        int Fsize = (int)(this.scale*15);

        Oval O1 = new Oval(this.x - Fsize /3,                          
                           this.y - Fsize /7 ,
                           Fsize + 3*Fsize/3,                          
                           Fsize  + Fsize/5,                           
                           this.color,true);        
        Triangle tail1 =new Triangle(this.x - Fsize,                                    
                                     this.y,                                     
                                     this.x - 2/Fsize,                                   
                                     this.y + Fsize/2,                                   
                                     this.x - Fsize,                                     
                                     this.y + Fsize,this.color,true);
        Line eye1 = new Line(this.x + 12*Fsize/10,                          
                            this.y + 5*Fsize/10  ,                          
                            this.x + 12*Fsize/10,                           
                            this.y + 5* Fsize /10,                          
                            this.color);   
        Oval O2 = new Oval(this.x - 37*Fsize/5,                
                   this.y+ 27*Fsize/5 ,                
                   Fsize + 2*Fsize/3, 
                   Fsize  + Fsize/5,                   
                   this.color,true);        
        Line eye2 = new Line(                
                this.x - 35*Fsize/5,                 
                this.y + 30*Fsize/5,                
                this.x - 35*Fsize/5,                 
                this.y + 30*Fsize/5, this.color);                      
        Triangle tail2 =new Triangle(this.x - Fsize*5 ,             
                 this.y + 13*Fsize/2,                
                 this.x - 10*Fsize/2,                
                 this.y + 11*Fsize/2,                
                 this.x - Fsize*6,               
                 this.y + Fsize*6,this.color,true);         


        this.window.add(O1);
        this.window.add(O2);
        this.window.add(tail2);
        this.window.add(tail1);
        this.window.add(eye1);
        this.window.add(eye2);


    }

    }
相关问题