I've created a class with three methods. I originally started with slightly different coding, and have changed it after looking at several videos and questions until I ended up with this. For some reason, the square I am trying to make simply will not appear on my screen. Please make any suggestions you have. I started relatively recently, which I'm sure is obvious. Please excuse any conventional errors I have made. As a side note, I figured out that the problem is that the method "paint" isn't being called. As I'm new I have no idea how to call it after trying a few different ways. If you have any clarifying questions about my code please ask. Thanks!
public class TheGame extends JPanel {
public void screen() {
JFrame f = new JFrame();
f.setTitle("Grid Game");
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setBounds(250, 250, 500, 500);
f.add(this);
f.setVisible(true);
}
public void paint(Graphics2D g) {
g.setColor(Color.BLACK);
g.fillRect(50, 50, 200, 200);
}
public static void main(String[] args) {
TheGame game = new TheGame();
game.screen();
}
You need to override
the paintComponent()
method:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(50, 50, 200, 200);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.