Answer the question
In order to leave comments, you need to log in
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);
}
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();
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question