Go Back   Webmaster Centre News and Discussion Forum > Website Developement > Web Programming

Web Programming Post your questions, tips and opinions on web scripting, programming languages. Subject areas include Java, Java Script, PHP, ASP, .Net, XML, Perl, HTML, Flash,etc

Reply
 
LinkBack Thread Tools Display Modes
Old 04-28-2008, 08:26 PM   #1 (permalink)
Junior Member
 
FRANK's Avatar
 
Default Collision detection in JAVA (using Jcreator to impement simulation)?

Hi, the following code shows my image moving around the screen but I cannot seem to stop it from hitting the edge of the panel. Can anyone add to the end of the code... much appreciated

import javax.swing.JPanel;
import java.awt.Graphics;


class robotpanel extends JPanel

{

private robot ted;
private Thread robothread;


public robotpanel()
{
ted=new robot(this);
robothread = new Thread(ted);
robothread.start();
}


public void paintComponent(Graphics g)
{
super.paintComponent(g);
System.out.println(this.getWidth()+" " +this.getHeight());
g.fillRect(ted.getX(),ted.getY(),20,20);
}
}

^^^ I need to add the code at the end to stop "ted" hitting the edge. Please advise. Cheers
FRANK is offline   Reply With Quote
Old 04-28-2008, 08:26 PM   #2 (permalink)
Junior Member
 
Default

The way to solve this is to have a function that calls itself--lets say every millisecond--to check the value of a certain variable. For example your "ted" could have two variables, x, and y, initialized at (0,0) that you either add to or subtract from as "ted" moves left and right, up and down. While this is happening, the function that calls itself is checking the values of x and y. You tell this function to ignore any calls to move "ted" up/down left/right by assigning a maximum value of x and a maximum value of y. I did this in JavaScript so you should be able to do it in full fledged Java.
Marlek D is offline   Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change the value of a select box with Java Script? Jamaal E Web Programming 0 05-01-2008 01:30 PM
can anyone write the following in java language? geekguy Web Programming 1 05-01-2008 06:20 AM
myspace/javascript/java helpppp? macey. Web Programming 0 05-01-2008 02:24 AM
Connect four in JAVA?!? xOx Frenchie xOx Web Programming 0 04-29-2008 09:48 AM
JAVA help? swimmerxc Web Programming 0 04-28-2008 08:23 PM



All times are GMT +1. The time now is 11:40 AM.
Powered by vBulletin® Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Style Provided By: Wrestling Clique - Wrestling Forums

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44