🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #7695

Problems with an Applet ..... please help!

#7695
From: Ritz
Date: Mon, 28 May 2001 09:02
79 lines
3183 bytes
Hi All,

I'm trying to do this simple exercise but am experiencing some annoying
problems which I can't seem to find the answer to.
I have to create an applet that displays a cursor. You can move the cursor
around in the applet and if you press any of the other keys the cursor will
change to that letter/number/symbol.
The cursor is not supposed to go outside the applet - in other words it has
to be in full view at all times. This is my problem......whenever it comes
to the top or the right side it continues out of the picture (when it comes
to the bottom or the left side it stops). It seems that I'm doing something
wrong with the 'getWidth() and getHeight() but what?
Do I need to include some security in this code or what am I doing wrong? I
have included this very simple code below and I would appreciate any help
you could give me.

Cheers.
Anders

package Exercise10;

import java.applet.Applet;
import java.awt.*;

public class SimpleKey extends Applet
{

   char current_key;
   int current_x, current_y;
   Font theFont;

   public void init()
   {
      setBackground(Color.white);
      theFont = new Font("Courier", Font.BOLD, 30);
      setFont(theFont);
      current_x = 150;
      current_y = 100;
   }

   public boolean keyDown(Event evt, int key)
   {
      switch(key)
      {
         case Event.LEFT: current_x = current_x - 5; break;
         case Event.RIGHT: current_x = current_x + 5; break;
         case Event.UP: current_y = current_y - 5; break;
         case Event.DOWN: current_y = current_y + 5; break;
         default: current_key = (char) key;
      }
      repaint();
      return true;
   }

   public void paint(Graphics g)
   {
      g.setColor(Color.blue);

      if(current_x < 0)
      {
         current_x = 0;
      }
      else if(current_x > this.getSize().width)
      {
         current_x = this.getSize().width;
      }

      if(current_y < 0)
      {
         current_y = 0;
      }
      else if(current_y > this.getSize().height)
      {
         current_y = this.getSize().height;
      }

      g.drawString(String.valueOf(current_key), current_x, current_y);
   }
}
Technical Details
Message-ID: <9et47t$s8g$1@neptunium.btinternet.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!dispose.news.demon.net!demon!btnet-peer0!btnet-feed5!btnet!mendelevium.btinternet.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,7e98b5e2e88dbeba,start
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-05-28 02:07:05 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!dispose.news.demon.net!demon!btnet-peer0!btnet-feed5!btnet!mendelevium.btinternet.com!not-for-mail
From: "Ritz" <chill_winston@hotmail.com>
Newsgroups: borland.public.jbuilder.java.language
Subject: Problems with an Applet ..... please help!
Date: Mon, 28 May 2001 10:02:59 +0100
Organization: BT Internet
Lines: 81
Message-ID: <9et47t$s8g$1@neptunium.btinternet.com>
Reply-To: "Ritz" <chill_winston@hotmail.com>
NNTP-Posting-Host: host213-122-81-216.btinternet.com
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200