+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Double buffering, movement, and collision detection.

  1. #11
    lawrs is offline Newbie
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Re: Double buffering, movement, and collision detection.

    Hi farrel, thank you for this tutorial. I'm a pure newbie at Java, and am trying to learn how to place images instead of the rectangles. Also, I've looked around on how to set the background of the JFrame( or is it JPanel ?) , but I get confused on which class to include/create the background in. Would really appreciate it if you could show how to use an img for background and for the rectangles, based on this tutorial.

    Also I tried adding another rect ( enemy2 = new Entity) and added the collision codes respectively, but it still doesn't detect.

    ///////////////////////////////////////////////
    public void checkCollision()
    {
    if (player.getBounds().intersects(enemy.getBounds()))
    player.collision = true;
    if (player.getBounds().intersects(enemy2.getBounds()) )
    player.collision = true;
    else
    player.collision = false;

    ///////////////////////////////////////////////

    What makes it worse, the first "enemy" doesn't detect collision too once I add collision codes for "enemy2". Could you explain how about doing so if I would like to add say.. 4 more enemies? Thank you in advance!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Re: Double buffering, movement, and collision detection.

    Quote Originally Posted by lawrs View Post
    Hi farrel, thank you for this tutorial. I'm a pure newbie at Java, and am trying to learn how to place images instead of the rectangles. Also, I've looked around on how to set the background of the JFrame( or is it JPanel ?) , but I get confused on which class to include/create the background in. Would really appreciate it if you could show how to use an img for background and for the rectangles, based on this tutorial.

    Also I tried adding another rect ( enemy2 = new Entity) and added the collision codes respectively, but it still doesn't detect.

    ///////////////////////////////////////////////
    public void checkCollision()
    {
    if (player.getBounds().intersects(enemy.getBounds()))
    player.collision = true;
    if (player.getBounds().intersects(enemy2.getBounds()) )
    player.collision = true;
    else
    player.collision = false;

    ///////////////////////////////////////////////

    What makes it worse, the first "enemy" doesn't detect collision too once I add collision codes for "enemy2". Could you explain how about doing so if I would like to add say.. 4 more enemies? Thank you in advance!
    You have to understand that code I posted is very sloppy. There are better ways to check for collisions and to draw. The code is meant to show basic principles.

    You can add images for the enemies by creating an Image like so:
    Image enemyImage = Toolkit.getDefaultToolkit().getImage("enemy.jpg");

    Stick that somewhere at the top of the DrawPanel class, and then find the drawBuffer() method and instead of the code that draws the player rectangle ' b.fillRect(x,x,x,x)', use b.drawImage(enemyImage, player.getX(), player.getY(), null);

    As far as you adding extra enemies, duplicating what I did for the first two, if done correctly, will produce the same results. In checkCollision(), you need to make sure that every enemy is checking for each other.

    PM your code to me, or make it available for download somewhere, and I'll look at it for you when I have time.

  4. #13
    lawrs is offline Newbie
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Re: Double buffering, movement, and collision detection.

    Thank you again farrell2k.

    I managed to add more enemies to this, and the collisions now work by using "else if" repeatedly. Can you explain to me how I can change the background of the Panel as well ? Thank you thank you.

  5. #14
    farrell2k is offline Learning Programmer
    Join Date
    Mar 2009
    Posts
    60
    Rep Power
    11

    Re: Double buffering, movement, and collision detection.

    Quote Originally Posted by lawrs View Post
    Thank you again farrell2k.

    I managed to add more enemies to this, and the collisions now work by using "else if" repeatedly. Can you explain to me how I can change the background of the Panel as well ? Thank you thank you.
    drawBuffer() this sets the background. Change the color, or if you like, use an image with drawImage() instead of fillRect()
    b.setColor(Color.black);
    b.fillRect(0,0,800,600);

  6. #15
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Thumbs up Re: Double buffering, movement, and collision detection.

    Thanks a ton @All .... nice ideas

  7. #16
    mrpawn is offline Newbie
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Re: Double buffering, movement, and collision detection.

    This is a cool tutorial, but i have one problem. my java compiler can't find Graphics2D. i copied the code exactly but when it gets to "Graphics2D b = buffer.createGraphics();" i keep getting a cannot find symbol error. please help. i even tried imported the Graphics class and Graphics2D class.

  8. #17
    kjbmarr is offline Newbie
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Re: Double buffering, movement, and collision detection.

    use the package= import java.awt.*;

+ Reply to Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Similar Threads

  1. collision detection in pygame
    By BRUTAL in forum Python
    Replies: 3
    Last Post: 02-05-2011, 11:26 PM
  2. Collision Detection
    By cocoa10 in forum C and C++
    Replies: 8
    Last Post: 10-26-2010, 09:41 PM
  3. Collision Detection
    By Flezria in forum Java Help
    Replies: 4
    Last Post: 02-06-2010, 05:41 AM
  4. Image Double Buffering!
    By Bioshox in forum Visual Basic Programming
    Replies: 3
    Last Post: 02-05-2010, 07:30 AM
  5. How to convert my code for double buffering?
    By Overv in forum C and C++
    Replies: 2
    Last Post: 09-24-2008, 02:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts