Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2007

Tip: Looking for answers? Try searching our database.

2 Basic Questions....

Thread view: 
TheBigPJ - 18 Nov 2007 14:59 GMT
Ive done alot of reading to get some basic applet creating knowledge,
however I have 2 questions: What happens when a shape goes off and you
cant see it anymore? Is it possible to destroy is when it leaves the
screen or do I need to reuse it?

And how would I create an array of rectangles?

I want to just go forward/ or backwards for what seems forever, run my
applet and you'll see what i mean.

My Code so far:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class KeyboardTest extends Applet implements KeyListener {

int keyCode;
int x = 100;
int y = 100;
int width = 75;
int height = 100;

public void init() {
 addKeyListener(this);
}

public void paint(Graphics g) {
 g.drawRect (x-50,y-50,width+100,height+100);
 g.drawRect (x,y,width,height);
 g.drawRect (x+50,y+50,width-100,height-100);
}

public void keyPressed(KeyEvent e) {
 keyCode = e.getKeyCode();
  if(keyCode == 38)
  {
   x--;
   y--;
   width = width +2;
   height = height +2;
  }
  else if(keyCode == 40)
  {
   x++;
   y++;
   width = width -2;
   height = height -2;
  }
 repaint();
}

public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }

}
Knute Johnson - 18 Nov 2007 18:25 GMT
> Ive done alot of reading to get some basic applet creating knowledge,
> however I have 2 questions: What happens when a shape goes off and you
> cant see it anymore?

If you draw outside the bounds of your component nothing happens.

 Is it possible to destroy is when it leaves the
> screen or do I need to reuse it?

In your case there is nothing to destroy.  But yes you can remove all
references to an object and it will get garbage collected.

> And how would I create an array of rectangles?

Rectangle[] rects = new Rectangle[1000000];

Remember though that that only creates the array object to hold some
Rectangles.  You need to explicitly create each rectangle;

for (int i=0; i<rects.length; i++)
    rects[i] = new Rectangle();

> I want to just go forward/ or backwards for what seems forever, run my
> applet and you'll see what i mean.

Go to my website, www.knutejohnson.com, and look at the Shapes2 applet
code and at the code for the Asteroids game.  The Shapes2 applet does
simple drawing.  The Asteroids game creates some objects, Rocks, Bullets
and a Ship and moves them around on a panel.  When Rocks and Bullets
objects leave the drawable area, they are destroyed by removing them
from the ArrayList they normally occupy.

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 18 Nov 2007 20:59 GMT
On Sun, 18 Nov 2007 06:59:32 -0800 (PST), TheBigPJ
<TheBigPJ@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>What happens when a shape goes off and you
>cant see it anymore?

Whenever you use a draw routine, it examines the current clipregion
and only draws dots inside that rectangle. The rest is irrelevant. It
only exists in your head.  It never gets drawn anywhere.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 18 Nov 2007 21:02 GMT
On Sun, 18 Nov 2007 06:59:32 -0800 (PST), TheBigPJ
<TheBigPJ@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>And how would I create an array of rectangles?

There are at least two ways to interpret your question.

1. how do I draw a spray of rectangles on the screen?

Just use drawRect over and over at different spots using different
colours.

see http://mindprod.com/jgloss/canvas.html

2. how do I create a non-visible
Rectangle[] rects
list of Rectangle objects with rectangle co-ordinates.?

See http://mindprod.com/jgloss/array.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

TheBigPJ - 20 Nov 2007 12:50 GMT
How would I take two keys as an input at once?

Like say I want to take the keyCode and 38 to go forward, and if I
push 39 I go left while going forward. How would I go about doing
that? Or do I just invoke key released as well somehow?

Thanks,
Peter
Hendrik Maryns - 20 Nov 2007 13:50 GMT
TheBigPJ schreef:
> How would I take two keys as an input at once?
>
> Like say I want to take the keyCode and 38 to go forward, and if I
> push 39 I go left while going forward. How would I go about doing
> that? Or do I just invoke key released as well somehow?

Ummm…

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

H.
Signature

Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

TheBigPJ - 20 Nov 2007 21:35 GMT
Ok fair point, my question was a bit bland.

Anyone know a good site for basic game maths? Game geometry
etc...etc...

Thanks,
Peter


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.