Posted: Thu Jun 06, 2013 12:20 pm Post subject: Re: RE:Need help for java game
DemonWasp @ Wed Jun 05, 2013 2:09 pm wrote:
Your code seems to have all the critical bits there (even if it is a bit messy).
You are right, however, that you're checking the line, rather than the line segment. This is my fault: I told you to use ptLineDist when I should have said ptSegDist. If you look carefully at http://docs.oracle.com/javase/6/docs/api/java/awt/geom/Line2D.html , you'll see that ptSegDist will find the distance to the line segment, rather than the infinite-extension of the line.
(Sorry!)
Thank you! I fixed my game. Is there a way to check for the other lines without repeating it?
Sponsor Sponsor
DemonWasp
Posted: Thu Jun 06, 2013 12:37 pm Post subject: RE:Need help for java game
Yes, of course there is!
Instead of:
Java:
Line2D line1 = new Line2D.Double ( ... );
Line2D line2 = new Line2D.Double ( ... );
You should be putting all of your lines (and any other repeated objects) into a collection of some kind. There are a few basic types (List, Set, Map), but the best one for this scenario is a List:
Java:
// Note: this syntax is for Java versions prior to 1.5. I'll show the newer version below...
List lines = new ArrayList();
lines.add ( new Line2D.Double ( ... ) );
lines.add ( new Line2D.Double ( ... ) );
// Then later on, you can check the ball against all lines pretty easily by checking them one at a time:
for ( int i = 0; i < lines.size(); ++i ) {
Line2D line = lines.get(i);
// Fill in your "check ball against line" code here (exactly once, not more!).
// Make sure you're checking against the 'line' variable I introduce in the line above, not L1 or something else.
}
Of course, if you're using a newer version of Java, such as 1.7 or better, the syntax gets a bit better:
Java:
List<Line2D> lines = new ArrayList<>();
lines.add ( new Line2D.Double ( ... ) );
lines.add ( new Line2D.Double ( ... ) );
// Then for each line in lines, check that line against the ball:
for ( Line2D line : lines ) {
// Fill in your "check ball against line" code here...
}
KeyLimePie
Posted: Sun Jun 09, 2013 9:42 pm Post subject: Re: RE:Need help for java game
DemonWasp @ Thu Jun 06, 2013 12:37 pm wrote:
Yes, of course there is!
Instead of:
Java:
Line2D line1 = new Line2D.Double ( ... );
Line2D line2 = new Line2D.Double ( ... );
You should be putting all of your lines (and any other repeated objects) into a collection of some kind. There are a few basic types (List, Set, Map), but the best one for this scenario is a List:
Java:
// Note: this syntax is for Java versions prior to 1.5. I'll show the newer version below...
List lines = new ArrayList();
lines.add ( new Line2D.Double ( ... ) );
lines.add ( new Line2D.Double ( ... ) );
// Then later on, you can check the ball against all lines pretty easily by checking them one at a time:
for ( int i = 0; i < lines.size(); ++i ) {
Line2D line = lines.get(i);
// Fill in your "check ball against line" code here (exactly once, not more!).
// Make sure you're checking against the 'line' variable I introduce in the line above, not L1 or something else.
}
Of course, if you're using a newer version of Java, such as 1.7 or better, the syntax gets a bit better:
Java:
List<Line2D> lines = new ArrayList<>();
lines.add ( new Line2D.Double ( ... ) );
lines.add ( new Line2D.Double ( ... ) );
// Then for each line in lines, check that line against the ball:
for ( Line2D line : lines ) {
// Fill in your "check ball against line" code here...
}
Thank you! I added the list into my game and it checks for all the lines in my game.
I want to clear all the lines on the screen after the collision occurs. I tried to use GC.dispose (); but it displays an error if I add it to my if dist statement. How do I dispose the frame if the ball touches the lines (lose) and the white box (win) so that I can make a new frame?
code:
public static void main (String[] args) // transitions from splash screen to game
{
PCircle PC = new PCircle ();
GC.setSize (800, 600);
int x = ((1024 - 640) / 2);
int y = ((768 - 480) / 2);
GC.setLocation (x, y);
GC.add (PC); // new frame for game content
GC.setVisible (true);
}
public void paint (Graphics g)
{
g.setColor (Color.white); // white color box
g.fillRect (220, 360, 20, 20); // draws the box
}
DemonWasp
Posted: Sun Jun 09, 2013 10:32 pm Post subject: Re: RE:Need help for java game
KeyLimePie @ Sun Jun 09, 2013 9:42 pm wrote:
I want to clear all the lines on the screen after the collision occurs. I tried to use GC.dispose (); but it displays an error if I add it to my if dist statement. How do I dispose the frame if the ball touches the lines (lose) and the white box (win) so that I can make a new frame?
I'm not a Swing expert, but I think you want to call .dispose() on the JFrame. Is 'GC' a JFrame object? If it is, can you post the exact error you're encountering?
If not, can you post your whole source code so that I can run it?
KeyLimePie
Posted: Tue Jun 11, 2013 3:30 am Post subject: Re: RE:Need help for java game
DemonWasp @ Sun Jun 09, 2013 10:32 pm wrote:
KeyLimePie @ Sun Jun 09, 2013 9:42 pm wrote:
I want to clear all the lines on the screen after the collision occurs. I tried to use GC.dispose (); but it displays an error if I add it to my if dist statement. How do I dispose the frame if the ball touches the lines (lose) and the white box (win) so that I can make a new frame?
I'm not a Swing expert, but I think you want to call .dispose() on the JFrame. Is 'GC' a JFrame object? If it is, can you post the exact error you're encountering?
If not, can you post your whole source code so that I can run it?
My game works now.
Thank you for helping me. I've learned a lot from you!