Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 让调用者函数选择颜色_Java_Oop_Colors - Fatal编程技术网

Java 让调用者函数选择颜色

Java 让调用者函数选择颜色,java,oop,colors,Java,Oop,Colors,嗨,我试图让调用函数选择图形的颜色,但失败了 Car car = new Car(400, 300, 15, Color.red) 这是我的车 public class Car{ private Color color; public Star(double x, double y, double radius, Color color) { // We can call other methods inside the const

嗨,我试图让调用函数选择图形的颜色,但失败了

Car car = new Car(400, 300, 15, Color.red)
这是我的车

    public class Car{
    private Color color;



        public Star(double x, double y, double radius, Color color) {
            // We can call other methods inside the constructor
            setStarCoordinates(x, y, radius);
            this.color = color;
        }
public void draw(Graphics2D graphics){
        graphics.drawPolyline(xCoordOfStar, yCoordOfStar, 11);
        graphics.fillPolygon(xCoordOfStar, yCoordOfStar, 11);
        graphics.setColor(color);
    }
}
编辑。代码可以正确编译,但不会更改颜色 另外,我没有在这里的car对象中包含一些方法。

在完成绘制对象后,您正在设置图形上下文的颜色

你做事的顺序很重要,例如

graphics.setColor(color);
graphics.drawPolyline(xCoordOfStar, yCoordOfStar, 11);
graphics.fillPolygon(xCoordOfStar, yCoordOfStar, 11);
应该设置颜色,然后使用该颜色绘制形状