0

我有一个游戏,其中两个椭圆出现在 JFrame 上,一个由 a - s - d - w 键控制,另一个由箭头键控制。这是我的运行方法:

public void run() {
        while (animator != null) {
            repaint();

            player1.move(player1.direction);
            player2.move(player2.direction);
            try {  
                Thread.sleep(100);  
            } catch (InterruptedException e) {  
                break;  
            }  
        }
}

这是我的个人球员课程。这是玩家 1 类的paintComponent 方法。(玩家 1 和玩家 2 相同)变量 shape、xPos、yPos、size 和 Color 都很好。(它们依附于值)

public void paintComponent(Graphics g) { 
    Graphics2D g2 = (Graphics2D)g;

    switch(Shape) {
    case 1: Ellipse2D.Double ball = new Ellipse2D.Double(xPos, yPos, size, size);         
    g2.setPaint(Color);
    g2.draw(ball);
    g2.fill(ball);
    break;

    case 2: Rectangle2D.Double rectangle = new Rectangle2D.Double(xPos, yPos, size, size);
    g2.setPaint(Color);
    g2.draw(rectangle);
    g2.fill(rectangle);
    break;
    }   
}       

我通过使用一个名为 KeyController 的类来控制椭圆的方向。这是该程序的示例部分;其余的都是一样的(记住这只是一个摘录):

public void keyPressed(KeyEvent e) {
    // Player 1 Left
    if(e.getKeyCode() == KeyEvent.VK_A){
        Player1.setDirection(270);
        System.out.println("A pressed");
    }

    // Player 1 Down
    if(e.getKeyCode() == KeyEvent.VK_S){
        Player1.setDirection(180);
        System.out.println("S pressed");
    }

    if(e.getKeyCode() == KeyEvent.VK_LEFT){
        Player2.setDirection(270);
        System.out.println("LEFT pressed");
    }

    if(e.getKeyCode() == KeyEvent.VK_DOWN){
        Player2.setDirection(180);
        System.out.println("DOWN pressed");
    }

程序的其余部分运行良好。(它有一个 keyTyped 和一个 keyPressed)

现在我的问题是,当我运行我的程序时,我看到一个椭圆并且所有键都可以控制它,包括 a - s - d - w 键和箭头键。

如果您需要更多代码,请询问我。(我有一个主要方法,它工作正常。)

如何让 Ellipse 响应控件?

4

1 回答 1

0

这是一个用键移动圆圈的小例子:

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

    controls.add ( new JComponent ()
    {
        private Point location = new Point ( 250, 250 );
        private int speed = 5;

        {
            setFocusable ( true );
            requestFocusInWindow ();
            addKeyListener ( new KeyAdapter ()
            {
                public void keyPressed ( KeyEvent e )
                {
                    boolean changed = false;
                    if ( e.getKeyCode () == KeyEvent.VK_W || e.getKeyCode () == KeyEvent.VK_UP )
                    {
                        location.y -= speed;
                        changed = true;
                    }
                    else if ( e.getKeyCode () == KeyEvent.VK_A ||
                            e.getKeyCode () == KeyEvent.VK_LEFT )
                    {
                        location.x -= speed;
                        changed = true;
                    }
                    else if ( e.getKeyCode () == KeyEvent.VK_S ||
                            e.getKeyCode () == KeyEvent.VK_DOWN )
                    {
                        location.y += speed;
                        changed = true;
                    }
                    else if ( e.getKeyCode () == KeyEvent.VK_D ||
                            e.getKeyCode () == KeyEvent.VK_RIGHT )
                    {
                        location.x += speed;
                        changed = true;
                    }
                    if ( changed )
                    {
                        repaint ();
                    }
                }
            } );
        }

        protected void paintComponent ( Graphics g )
        {
            Graphics2D g2d = ( Graphics2D ) g;
            g2d.setRenderingHint ( RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON );

            g2d.setPaint ( Color.BLACK );
            g2d.drawOval ( location.x - 10, location.y - 10, 20, 20 );
        }
    } );

    controls.setSize ( 500, 500 );
    controls.setLocationRelativeTo ( null );
    controls.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    controls.setVisible ( true );
}

使用简单的 Key 侦听器时,请确保该组件具有焦点 - 否则它根本不会收到任何键事件。因此,有时您必须强制更改焦点组件:

component.requestFocusInWindow ();
于 2012-05-02T08:56:00.747 回答