Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:51:36+00:00 2026-05-30T21:51:36+00:00

There seems to be a problem when using KeyListener on fillPolygon/drawPolygon. I can’t move

  • 0

There seems to be a problem when using KeyListener on fillPolygon/drawPolygon. I can’t move polygons using KeyListener, but I can move other graphics. Here is the java code:

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

and here is the html… I don’t know what is wrong, Please Help…

    <html>

<body>

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

    </html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:51:37+00:00Added an answer on May 30, 2026 at 9:51 pm

    The array values don’t update automatically, because they are primitives, not objects.

    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) {
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a custom thread pool utility, but there seems to be a problem
I am using timeago jQuery plugin for my blog, but there seems to be
Sorry for the super dumb question, but is there any problem using the Android/Eclipse
I'm trying to retrieve images from wikimedia using the existing api, but there seems
Seems there's nothing to do… using the combo prototype/lowpro, no problems at all with
There seems to be a problem when virtualenv is used in PowerShell. When I
So there seems to be this problem with GNU Make's $(wildcard) function keeping a
Hi i encountered this problem whereby when i initialized my String[], there seems to
On this page, there seems to be a small problem... there's a sliver of
Trying to solve a very simple problem using mvvm-light, but after days of sifting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.