Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By doWhile

Thread: Paint is invalid type for variable paint.

  1. #1
    minibronya is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default Paint is invalid type for variable paint.

    I am making a game and when I type in "public void paint(Graphics g) {", it says "graphics is an invalid type for variable paint." How do I fix it? Here is my code.

    package maze;

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class board extends JPanel implements ActionListener {

    private Timer timer;
    public board() {
    timer = new Timer(25, this);
    timer.start();
    }


    public void actionPerformed(ActionEvent e) {
    repaint();


    public void paint(Graphics g) {
    super.paint(g);

    g.setColor(Color.red);
    g.fillRect(45, 60, 32, 32);

    }

    }
    }


    This is a snapshot of my screen.
    Paint is invalid type for variable paint.-code.png

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Paint is invalid type for variable paint.

    You can't have a method inside another method. paint method should not be placed inside the actionPerformed method. It should be like this:

    Java Code:
        ...
        ...
        public void actionPerformed(ActionEvent e) {
            repaint();
        }
    
        public void paint(Graphics g) {
            super.paint(g);
    
            g.setColor(Color.red);
            g.fillRect(45, 60, 32, 32);
    
        }
        ...
        ...
    Last edited by wsaryada; 05-25-2012 at 05:18 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Paint is invalid type for variable paint.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Paint is invalid type for variable paint.

    ...and do custom painting within paintComponent, not paint

    Painting in AWT and Swing
    quad64bit likes this.

Similar Threads

  1. Paint
    By ninjaturtlez in forum AWT / Swing
    Replies: 4
    Last Post: 12-17-2011, 05:15 AM
  2. paint and paintcomponent
    By wired-in=p in forum New To Java
    Replies: 3
    Last Post: 10-01-2011, 07:49 PM
  3. paint program
    By dewdadamnthang in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 12:40 PM
  4. Paint????
    By seanfmglobal in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 09:00 AM
  5. print variable with paint(Graphics g) ??
    By tghn2b in forum New To Java
    Replies: 10
    Last Post: 12-29-2008, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •