Search code examples
javaswingvariablesgraphicspaintcomponent

fillRect Graphics error


public class paintObjects extends JPanel{

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Rectangle r = new Rectangle(25, 25, 100, 100); 
        g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
        g.setColor(Color.BLUE);

        }

I have an error on fillRect and when I hover over it, it says

The method fillRect(int, int, int, int) in the type Graphics is not applicable for the arguments (double, double, double, double

but I'm not sure how to fix this, if anyone could tell me how to. I'd be thankful.


Solution


  •    public abstract void fillRect(int x, int y, int width, int height)
    


    this is the method with parameter list

    you need to cast double values to int because only int accept by method.

    r.getX() returns double so cast it to int and pass to the method

     g.fillRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getHeight());