Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is paintComponent not drawing on the panel?

I am having some trouble getting my code to draw to my jpanel.

Everything runs correctly except the graphics drawing.

There are other answers saying that I need to set the size of the panel with setSize, but I already do that.

Other answers say that I need to call super.paintComponent(g);, but I do that as well.

What am I missing?

Here is my code:

Thanks in advance for any help.

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import static java.awt.Color.*;

public class Main extends JPanel implements KeyListener {

    //Declare all class vars.
    private Snake snake = new Snake(50,50,50);

    //Declare all Player global booleans
    private boolean leftPressed, rightPressed, upPressed, downPressed;

    //Declare all Gamestate gobal booleans
//    private boolean running = true;

    public static void main(String[] args){
        Main main = new Main();

        main.createFrame();
        SwingUtilities.invokeLater(main::customUpdate);
    }



    private void createFrame() {
        JFrame frame = new JFrame("Snaek");
        JPanel panel = new JPanel();
        frame.setResizable(false);
        frame.setSize(800,800);
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        panel.requestFocus();
        panel.addKeyListener(this);
        panel.setSize(new Dimension(frame.getWidth(), frame.getHeight()));
        Frame.getFrames();
    }

    private void customUpdate() {
        if(leftPressed) {
            snake.changeX(-1);
        } else if(upPressed) {
            snake.changeY(-1);
        } else if(downPressed) {
            snake.changeY(1);
        } else if(rightPressed) {
            snake.changeX(1);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(black);
        g.drawRect(snake.getX(), snake.getY(), snake.getSize(), snake.getSize());
        g.drawRect(100,100,200,200);
        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
            case 37:
//                System.out.println("Left?");
                leftPressed = true;
                break;
            case 38:
//                System.out.println("Up?");
                upPressed = true;
                break;
            case 39:
//                System.out.println("Right?");
                rightPressed = true;
                break;
            case 40:
                System.out.println("Down?");
                downPressed = true;
                break;
            case 27: //Escape
                System.out.println(snake.getX());
                break;
            case 50: //Space
//                spacePressed = true;
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println(e.getKeyCode());
        switch (e.getKeyCode()) {
            case 37:
//                System.out.println("Left?");
                leftPressed = false;
                break;
            case 38:
//                System.out.println("Up?");
                upPressed = false;
                break;
            case 39:
//                System.out.println("Right?");
                rightPressed = false;
                break;
            case 40:
//                System.out.println("Down?");
                downPressed = false;
                break;

        }
    }
}
like image 985
wanderer0810 Avatar asked Dec 23 '22 14:12

wanderer0810


1 Answers

You're creating a new JPanel here:

JPanel panel = new JPanel();

And then adding it to the JFrame

frame.add(panel);

Instead you should add your panel (Main) into it

frame.add(this);

Also, as stated by @VinceEmigh in the comments above, avoid calling repaint() in the paintComponent() method, otherwise you could fall into an endless loop, lagging or freezing in your app. Painting methods are for painting only.

Another tip, is to use KeyBindings instead of KeyListener or you could fall into your program not responding to the key events...

And I would also move frame.setVisible(true); line to the end of createFrame() method...

like image 107
Frakcool Avatar answered Jan 04 '23 04:01

Frakcool