简体   繁体   中英

letting caller function choose a color java

Hi I am trying to let the caller function choose the color of the drawing but failing.

Car car = new Car(400, 300, 15, Color.red)

and here is my car object

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

edit. code compiles properly but doesn't change the color PS. i did not include some of the methods in the car object here.

You're setting the color of the Graphics context AFTER you've finished painting the object...

The order in which you do things is important, for example, something like

graphics.setColor(color);
graphics.drawPolyline(xCoordOfStar, yCoordOfStar, 11);
graphics.fillPolygon(xCoordOfStar, yCoordOfStar, 11);

should set the color and then, using that color, draw you shapes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM