Andrei
Andrei

Reputation: 17

My paint method gives me an error and I don't know why

In eclipse the paint is underlined and so it the parentheses after the g in (Graphics g). For paint I am getting that void is an invalid type for the variable paint, but this is the exact way it is written in my examples.

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JApplet;


public class Olympic extends JApplet{

    public void init(){

        getContentPane().setBackground(Color.WHITE);

        public void paint(Graphics g){

            super.paint(g);

            g.setColor(Color.BLUE);
            g.drawOval(0,0,10,10);


        }

    }

}

Thanks!

Upvotes: 0

Views: 476

Answers (1)

Xavier Delamotte
Xavier Delamotte

Reputation: 3599

It's because you declared the paint method inside the method init. It should be:

public class Olympic extends JApplet{

    public void init(){
        getContentPane().setBackground(Color.WHITE);    
    }
    public void paint(Graphics g){
        super.paint(g);
        g.setColor(Color.BLUE);
        g.drawOval(0,0,10,10);
    }
}

Upvotes: 2

Related Questions