A
A
Abaddon882021-05-10 18:16:56
Java
Abaddon88, 2021-05-10 18:16:56

PaintComponent refuses to work, cannot resolve symbol "g" how to fix?

Good evening. I ran into one annoying problem. Sat watched a recording of a webinar on the topic of creating a Minesweeper application in Java. When it came to graphics, I wrote the code as indicated in the video, but Idea does not accept it. It seems that all the libraries I have connected. I have already climbed everything I could, reinstalled the 16th version of the JDK and everything is useless. Help as soon as possible, this is important.

private void initPanel ()
    {
        panel = new JPanel();
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponents(g);
                for (Box box : Box.values())
                    g.drawImage ((Image)box.image, box.ordinal() * IMAGE_SIZE, 0, this);
            }
        };
        panel.setPreferredSize (new Dimension(COLS * IMAGE_SIZE, ROWS * IMAGE_SIZE));
        add (panel);
    }

Above is a code snippet where exactly the error comes out. Next, I will give the code of the entire class for a general understanding of what is happening in general:
import javax.swing.*;
import java.awt.*;
import Sweeper.Box;

public class JavaSweeper extends JFrame
{
    private JPanel panel;
    private final int COLS = 15;
    private final int ROWS = 1;
    private final int IMAGE_SIZE = 50;

    public static void main (String [] args)
    {
        new JavaSweeper(); //Создание экземпляра программы
    }

    private JavaSweeper ()
    {
        setImages();
        initPanel();
        initFrame(); //Вызов инициализации фрейма
    }

    private void initPanel ()
    {
        panel = new JPanel();
        {
            @Override
            protected void paintComponent(Graphics g)
            {
                super.paintComponents(g);
                for (Box box : Box.values())
                    g.drawImage ((Image)box.image, box.ordinal() * IMAGE_SIZE, 0, this);
            }
        };
        panel.setPreferredSize (new Dimension(COLS * IMAGE_SIZE, ROWS * IMAGE_SIZE));
        add (panel);
    }

    //Подготовка заготовок
    private void initFrame ()
    {
        pack ();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Java Sweeper");
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);


    }

    private void setImages ()
    {
       for (Box box : Box.values())
           box.image = getImage(box.name());
    }

    private Image getImage (String name)
    {
        String filename = "img/" + name.toLowerCase() + ".png";
        ImageIcon icon = new ImageIcon(getClass().getResource(filename));
        return icon.getImage();
    }

}

60994e5c97a1a442885788.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-05-10
@Abaddon88

panel = new JPanel(); -- Here's an extra semicolon

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question