有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java小程序:使用keylistener移动多边形

在fillPolygon/drawPolygon上使用KeyListener时似乎有问题。我不能使用KeyListener移动多边形,但我可以移动其他图形。以下是java代码:

import java.awt.Color;
import java.awt.Graphics;
import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Exer09Laggui extends Applet implements KeyListener {

   int width;
   int height;
   int carLength = 200;
   int carWidth = 75;

   int nPoints = 4;

   // for body
   int body_x = 0;
   int body_y = 0;

   // for front windshield
   // x coordinates
   int frontWS_x1 = 125;
   int frontWS_x2 = 125;
   int frontWS_x3 = 145;
   int frontWS_x4 = 145;
   // y coordinates
   int frontWS_y1 = 62;
   int frontWS_y2 = 10;
   int frontWS_y3 = 5;
   int frontWS_y4 = 67;

   int[] xPoints1 = { frontWS_x1, frontWS_x2, frontWS_x3, frontWS_x4 };
   int[] yPoints1 = { frontWS_y1, frontWS_y2, frontWS_y3, frontWS_y4 };

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground(Color.GRAY);
      addKeyListener(this);
   }

   public void paint(Graphics g) {
      // for the car body
      g.setColor(Color.BLACK);
      // fillRoundRect(int x, int y, int width, int height, int arcWidth, int
      // arcHeight)
      g.fillRoundRect(body_x, body_y, carLength, carWidth, 30, 30);

      // for the front windshield
      // fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
      g.setColor(Color.white);
      g.drawPolygon(xPoints1, yPoints1, nPoints);
      g.setColor(new Color(200, 200, 255));
      g.fillPolygon(xPoints1, yPoints1, nPoints);
   }

   public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_DOWN) {
         // for body
         body_y += 5;
         // for front windshield
         frontWS_y1 += 5;
         frontWS_y2 += 5;
         frontWS_y3 += 5;
         frontWS_y4 += 5;
      }
      if (e.getKeyCode() == KeyEvent.VK_UP) {
         // for body
         body_y -= 5;
         // for front windshield
         frontWS_y1 -= 5;
         frontWS_y2 -= 5;
         frontWS_y3 -= 5;
         frontWS_y4 -= 5;
      }
      if (e.getKeyCode() == KeyEvent.VK_LEFT) {
         // for body
         body_x -= 5;
         // for front windshield
         frontWS_x1 -= 5;
         frontWS_x2 -= 5;
         frontWS_x3 -= 5;
         frontWS_x4 -= 5;
      }
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         // for body
         body_x += 5;
         // for front windshield
         frontWS_x1 += 5;
         frontWS_x2 += 5;
         frontWS_x3 += 5;
         frontWS_x4 += 5;
      }
      repaint();
   }

   public void keyReleased(KeyEvent e) {
   }

   public void keyTyped(KeyEvent e) {
   }
}

这是html。。。我不知道怎么了,请帮忙

    <html>

<body>

    <applet code   = 'Exer09Laggui.class'
            width  = '1340'
            height = '635'/>
</body>

    </html>

共 (2) 个答案

  1. # 1 楼答案

    这是一个焦点问题,关键是在小程序可见后将焦点集中到它上,后一部分是棘手的部分,因为仅仅在init()方法中调用requestFocus()是行不通的。您可以在init中的Swing计时器中执行此操作,或覆盖setVisible,如下所示,即kludge 1或kludge 2:

    import java.applet.Applet;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JOptionPane;
    
    public class Exer09Laggui extends Applet implements KeyListener {
       private boolean firstSetVisible = true;
    
       public void init() {
          addKeyListener(this);
    
          // kludge one:
          int timerDelay = 400;
          new javax.swing.Timer(timerDelay , new ActionListener() {
    
             public void actionPerformed(ActionEvent evt) {
                Exer09Laggui.this.requestFocusInWindow();
                ((javax.swing.Timer)evt.getSource()).stop();
             }
          }).start();
       }
    
       // kludge two
       public void setVisible(boolean b) {
          super.setVisible(b);
          if (firstSetVisible) {
             requestFocusInWindow();
             firstSetVisible = false;
          }
       }
    
       public void keyPressed(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_DOWN) {
             JOptionPane.showMessageDialog(this, "down");
          }
          if (e.getKeyCode() == KeyEvent.VK_UP) {
             JOptionPane.showMessageDialog(this, "up");
          }
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
             JOptionPane.showMessageDialog(this, "left");
          }
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             JOptionPane.showMessageDialog(this, "right");
          }
       }
    
       public void keyReleased(KeyEvent e) {
       }
    
       public void keyTyped(KeyEvent e) {
       }
    }
    

    调试这类东西时的另一个技巧是尽可能简化代码,以便只保留演示错误所需的基本要素。哪种代码对我们来说更容易阅读和理解,你的代码,还是我的更简单的代码,你会看到为什么这对我们有帮助。这对你也有帮助,因为你消除了可能使问题复杂化的事情,从而只处理纯粹的问题

    编辑
    要移动“挡风玻璃”,需要更新挡风玻璃用于绘制的数据。这些数据是什么?两个int[]数组,xPoints1和yPoints1。因为你的代码没有改变这些,所以挡风玻璃将保持原位。您需要添加以下内容:

    public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_DOWN) {
         // for body
         body_y += 5;
         // for front windshield
         frontWS_y1 += 5;
         frontWS_y2 += 5;
         frontWS_y3 += 5;
         frontWS_y4 += 5;
      }
      if (e.getKeyCode() == KeyEvent.VK_UP) {
         // for body
         body_y -= 5;
         // for front windshield
         frontWS_y1 -= 5;
         frontWS_y2 -= 5;
         frontWS_y3 -= 5;
         frontWS_y4 -= 5;
      }
      if (e.getKeyCode() == KeyEvent.VK_LEFT) {
         // for body
         body_x -= 5;
         // for front windshield
         frontWS_x1 -= 5;
         frontWS_x2 -= 5;
         frontWS_x3 -= 5;
         frontWS_x4 -= 5;
      }
      if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         // for body
         body_x += 5;
         // for front windshield
         frontWS_x1 += 5;
         frontWS_x2 += 5;
         frontWS_x3 += 5;
         frontWS_x4 += 5;
      }
    
      // ***** note added code *****
      xPoints1 = new int[]{ frontWS_x1, frontWS_x2, frontWS_x3, frontWS_x4 };
      yPoints1 = new int[]{ frontWS_y1, frontWS_y2, frontWS_y3, frontWS_y4 };
    
      repaint();
    }
    
  2. # 2 楼答案

    数组值不会自动更新,因为它们是基本体,而不是对象

    Exer09Laggui

    // <applet code='Exer09Laggui' width=400 height=200></applet>
    import java.awt.Color;
    import java.awt.Graphics;
    import java.applet.Applet;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class Exer09Laggui extends Applet implements KeyListener {
    
       int width;
       int height;
       int carLength = 200;
       int carWidth = 75;
    
       int nPoints = 4;
    
       // for body
       int body_x = 0;
       int body_y = 0;
    
       // for front windshield
       // x coordinates
       int frontWS_x1 = 125;
       int frontWS_x2 = 125;
       int frontWS_x3 = 145;
       int frontWS_x4 = 145;
       // y coordinates
       int frontWS_y1 = 62;
       int frontWS_y2 = 10;
       int frontWS_y3 = 5;
       int frontWS_y4 = 67;
    
       int[] xPoints1 = { frontWS_x1, frontWS_x2, frontWS_x3, frontWS_x4 };
       int[] yPoints1 = { frontWS_y1, frontWS_y2, frontWS_y3, frontWS_y4 };
    
       public void init() {
          width = getSize().width;
          height = getSize().height;
          setBackground(Color.GRAY);
          addKeyListener(this);
          setFocusable(true);
          requestFocusInWindow();
       }
    
       public void paint(Graphics g) {
          // for the car body
          g.setColor(Color.BLACK);
          // fillRoundRect(int x, int y, int width, int height, int arcWidth, int
          // arcHeight)
          g.fillRoundRect(body_x, body_y, carLength, carWidth, 30, 30);
    
          // for the front windshield
          // fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
          g.setColor(Color.white);
          g.drawPolygon(xPoints1, yPoints1, nPoints);
          g.setColor(new Color(200, 200, 255));
          g.fillPolygon(xPoints1, yPoints1, nPoints);
       }
    
       public void keyPressed(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_DOWN) {
             // for body
             body_y += 5;
             // for front windshield
             frontWS_y1 += 5;
             frontWS_y2 += 5;
             frontWS_y3 += 5;
             frontWS_y4 += 5;
          }
          if (e.getKeyCode() == KeyEvent.VK_UP) {
             // for body
             body_y -= 5;
             // for front windshield
             frontWS_y1 -= 5;
             frontWS_y2 -= 5;
             frontWS_y3 -= 5;
             frontWS_y4 -= 5;
          }
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
             // for body
             body_x -= 5;
             // for front windshield
             frontWS_x1 -= 5;
             frontWS_x2 -= 5;
             frontWS_x3 -= 5;
             frontWS_x4 -= 5;
          }
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             // for body
             body_x += 5;
             // for front windshield
             frontWS_x1 += 5;
             frontWS_x2 += 5;
             frontWS_x3 += 5;
             frontWS_x4 += 5;
          }
           xPoints1[0] = frontWS_x1;
           xPoints1[1] = frontWS_x2;
           xPoints1[2] = frontWS_x3;
           xPoints1[3] = frontWS_x4;
           yPoints1[0] = frontWS_y1;
           yPoints1[1] = frontWS_y2;
           yPoints1[2] = frontWS_y3;
           yPoints1[3] = frontWS_y4;
          repaint();
       }
    
       public void keyReleased(KeyEvent e) {
       }
    
       public void keyTyped(KeyEvent e) {
       }
    }