Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help for java game
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
KeyLimePie




PostPosted: 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
Sponsor
sponsor
DemonWasp




PostPosted: 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




PostPosted: 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. Smile

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




PostPosted: 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




PostPosted: 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. Smile I've learned a lot from you! Very Happy
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 20 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: