reboundpanel.java help!
Results 1 to 3 of 3

Thread: reboundpanel.java help!

  1. #1
    Member
    Join Date
    Aug 2003
    Posts
    38

    reboundpanel.java help!

    hi everyone!
    there are a few questions that I'd like to ask regarding a particular problem.
    I havent seen an entry like this before... so if this is inappropriate, please delete this thread!

    here's the program code....


    //********************************************************************
    // ReboundPanel.java Author: Lewis/Loftus
    //
    // Represents the primary panel for the Rebound program.
    //********************************************************************

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ReboundPanel extends JPanel
    {
    private final int WIDTH = 300, HEIGHT = 100;
    private final int DELAY = 20, IMAGE_SIZE = 35;

    private ImageIcon image;
    private Timer timer;
    private int x, y, moveX, moveY;

    //-----------------------------------------------------------------
    // Sets up the panel, including the timer for the animation.
    //-----------------------------------------------------------------
    public ReboundPanel()
    {
    timer = new Timer(DELAY, new ReboundListener());

    image = new ImageIcon ("happyFace.gif");

    x = 0;
    y = 40;
    moveX = moveY = 3;

    setPreferredSize (new Dimension(WIDTH, HEIGHT));
    setBackground (Color.black);
    timer.start();
    }

    //-----------------------------------------------------------------
    // Draws the image in the current location.
    //-----------------------------------------------------------------
    public void paintComponent (Graphics page)
    {
    super.paintComponent (page);
    image.paintIcon (this, page, x, y);
    }

    //*****************************************************************
    // Represents the action listener for the timer.
    //*****************************************************************
    private class ReboundListener implements ActionListener
    {
    //--------------------------------------------------------------
    // Updates the position of the image and possibly the direction
    // of movement whenever the timer fires an action event.
    //--------------------------------------------------------------
    public void actionPerformed (ActionEvent event)
    {
    x += moveX;
    y += moveY;

    if (x <= 0 || x >= WIDTH-IMAGE_SIZE)
    moveX = moveX * -1;

    if (y <= 0 || y >= HEIGHT-IMAGE_SIZE)
    moveY = moveY * -1;

    repaint();
    }
    }
    }


    Questions I'd like to ask....

    1. How do I move the smiley diagonally? I've tried experimenting with the moveX and moveY values, but to no avail. So, I don't really understand my java well...

    2. At this stage, the smiley will run out of the window (right and bottom sides). Smiley should bounce back as soon as it touches the 4 sides of the window.
    Write down the steps you take to solve this problem.

    3. Using array implement 2 bouncing smileys. does this mean just declaring the necessary variables etc to get a second smiley??

    4. Implement such that when i click the mouse on this window, the 2 bouncing smileys will stop. The next clicked will make them move again.

    can anyone be patient & kind to guide me along? thanks much.

    ps. the alightment might be slightly altered due to the textbox.

  2. #2
    Senior Member
    Join Date
    Oct 2005
    Posts
    106

    Cool

    I actually had to do this program before, but I used objectdraw rather than swing.

    The trick is that the action performed doesn't need to use "if...else..." as you implemented it.

    The solution that I would use is create two booleans "moveXRight" and "moveYUp" or something to those effects.

    What you do then is IF x == WIDTH-IMAGE_SIZE THEN moveXRight = false. You also need another one IF x==0 THEN moveXRight = true.

    //checks if it reaches a side
    //if it has reached a side, it changes direction
    //otherwise it keeps going in its direction
    if((moveXRight == false && moveX>0)||(moveXRight == true && moveX<0))
    moveX*=-1;

    Then you do the same thing with moveY.

    In my program, I made the ball move randomly in the X direction and randomly in the Y direction (though the random number was between 0 and 1 so I had control over the direction ).

    You would have to implement this in the actionPerformed method.

    Goodluck!

  3. #3
    Member
    Join Date
    Aug 2003
    Posts
    38
    thanks for replying! i'll try that soon.
    [glowpurple]First you must decide. Then you must follow through.\" - Lacus Clyne[/glowpurple]

Posting Permissions

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

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides