繁体   English   中英

如何在一个类中编辑JPanel图形?

[英]How can I edit JPanel Graphics in one class?

因此,我试图在JFrame上对蛇进行编程,并在JPanel上进行所有图形处理(移动“蛇”,随机生成食物等)。 我正处于起步阶段,因此我现在要做的就是使用箭头键在框架上移动一个黑色正方形。 我在Panel类中的while循环不会被Snake类中的按键操作打断,因此是否可以用我的所有其他代码来编辑同一类中的JPanel图形?

这是所有代码。 底部的Panel类遵循我在此处找到的模板。

public class Snake {

    // panel width and height
    static int pW; 
    static int pH;

    static int x = 10;
    static int y = 10;

    static int k;

    static JFrame frame = new JFrame("SNAKE");

    // getters for panel class
    public int getPW() { return pW; }
    public int getPH() { return pH; }
    public int getX()  { return x;  }  
    public int getY()  { return y;  }

    public static void main(String[] args) {

        // get screen dimensions
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int sH = (int) screenSize.getHeight();
        int sW = (int) screenSize.getWidth();

        pW = (int) sW/2;
        pH = (int) sH/2;

        // initialize frame
        frame.setSize    (pW/1,pH/1);
        frame.setLocation(pW/2,pH/2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addKeyListener( new KeyAdapter() {

            public void keyPressed(KeyEvent e) {

                k = e.getKeyCode();

                switch(k) {

                case 38: /* y -= square size */ break; // up
                case 40: /* y += square size */ break; // down 
                case 37: /* x -= square size */ break; // left
                case 39: /* x += square size */ break; // right
                case 27: System.exit(0);

                }

            }
        });

        Panel panel = new Panel();

        frame.add(panel);

        frame.setVisible(true);

    }

}

class Panel extends JPanel {

    Snake snake = new Snake();

    //square size and separation between squares
    int sep = 0;
    int size = 50;

    // initial location of square on the panel/frame
    int x = sep + size;
    int y = sep + size;

    // holding values to check if x or y have changed
    int xH = x;
    int yH = x;

    public void paint(Graphics g) {

        int pW = snake.getPW();
        int pH = snake.getPH();

        int i; int o;

        Color on  = Color.BLACK;
        Color off = Color.GRAY;

        // gray background
        g.setColor(Color.GRAY);
        g.fillRect(0,0,pW,pH);

        // black square initialization
        g.setColor(Color.BLACK);
        g.fillRect(x, y, size, size);

        /* this loop is supposed to check if the black
         * rectangle has moved by repeatedly grabbing x & y
         * values from the Snake class. When a key is pressed
         * and the values change, a gray rectangle is placed at the old location 
         * and a black one is placed at the new location.
         * 
         * When I run the program, I get stuck in this while loop.
         * If I had the while loop in the same class I check for keys,
         * I don't think I would have this problem
         */

        while(true) {

            x = snake.getX();
            y = snake.getY();

            if(x != xH || y != yH) {

            g.setColor(off);
            g.fillRect(xH, yH, size, size);
            g.setColor(on);
            g.fillRect(snake.getX(), snake.getY(), size, size);

            xH = x;
            yH = y;

        }}

    }   
}

绘画方法中永远不应该有while(true)循环。 这只会导致无限循环,并且您的GUI将无法响应事件。

相反,您需要向蛇类添加方法以移动蛇。 因此,当按下箭头键之一时,您将更新蛇的起始位置。 然后,当Swing调用paintComponent()方法时,该方法将调用repaint(),而蛇将重新绘制自身。

因此,您的绘画代码应覆盖paintComponent()而不是paint(),并且应调用super.paintComponent(g)作为方法中的第一条语句。

不要将您的自定义类称为“面板”,有一个带有该名称的AWT类。 使您的班级名称更具描述性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM