为什么paintComponent没有在面板上绘图?

时间:2017-09-07 17:26:48

标签: java swing graphics

我在将代码绘制到我的jpanel时遇到了一些麻烦。

除了图形绘制外,一切都正常运行。

还有其他答案说我需要使用setSize设置面板的大小,但我已经这样做了。

其他答案说我需要致电super.paintComponent(g);,但我也这样做。

我错过了什么?

这是我的代码:

提前感谢您的帮助。

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;

        }
    }
}

2 个答案:

答案 0 :(得分:5)

您在此处创建了新的JPanel

JPanel panel = new JPanel();

然后将其添加到JFrame

frame.add(panel);

相反,您应该将面板(Main)添加到其中

frame.add(this);

另外,正如@VinceEmigh在上述评论中所述,请避免在repaint()方法中调用paintComponent(),否则您可能会陷入无限循环,滞后或冻结您的应用。绘画方法仅用于绘画。

另一个提示是,使用KeyBindings代替KeyListener,否则您可能会陷入不响应关键事件的程序......

我还会将frame.setVisible(true);行移至createFrame()方法的结尾...

答案 1 :(得分:4)

您正在为JFrame添加默认面板。您想要添加自定义JPanel。