Java 小程序 : moving polygon using keylistener

标签 java applet 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>

最佳答案

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

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) {
   }
}

关于Java 小程序 : moving polygon using keylistener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9550022/

相关文章:

Java 可以只将输入/输出放在主方法中吗?

java - 在 nUnit 中,Hamcrest 的 Matchers.containsInAnyOrder(Matcher... matchers) 等价于什么?

java - AWT-EventQueue-1 NullExceptionPointer

java - 添加了 KeyListener 但按下时 keyEvent 不起作用,是什么问题?

java - Hibernate 通过非 id 列关联两个实体

java - 用java编写哈希函数的最佳实践是什么?

java - 制作小程序

Java:透明TextArea +绘制背景

java - 如何向 JFrame 添加 key 监听器?

android - EditText OnKeyListener 不工作