Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple Balloon Applet [Help!!!]

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Simple Balloon Applet [Help!!!]

    The bottom of the balloons string is supposed to stick to the bottom center (which it does). When you click a button, the balloon moves in that direction, simple. Problem is, for some reason, the balloon isn't stopping when it hits the east or south boundaries, but west and north work fine. Any reason why? Thank you!

    package Balloon_Applet;
     
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
     
    public class Balloon_Applet extends Applet
     
    implements ActionListener{  
     
        private static final long serialVersionUID = 1L; 
     
        Button north, west, south, east, northeast, northwest, southeast, southwest, center;
     
        public static final int DISPLAY_WIDTH = 600;
        public static final int DISPLAY_HEIGHT = 600;
     
        public int startX = DISPLAY_WIDTH/2;
        public int startY = DISPLAY_HEIGHT/2;
     
        public int boundX = DISPLAY_WIDTH;
        public int boundY = DISPLAY_HEIGHT;
     
        public static final boolean CONSOLE_LOGGING = true;
     
        public void init(){
     
            west = new Button ("West");
            add (west);
            west.addActionListener (this); 
     
            northwest = new Button ("Northwest");
            add (northwest);
            northwest.addActionListener (this); 
     
            northeast = new Button ("Northeast");
            add (northeast);
            northeast.addActionListener (this); 
     
            north = new Button ("North");
            add (north);
            north.addActionListener (this); 
     
            center = new Button ("Center");
            add (center);
            center.addActionListener (this);
     
            south = new Button ("South");
            add (south);
            south.addActionListener (this); 
     
            southwest = new Button ("Southwest");
            add (southwest);
            southwest.addActionListener (this);
     
            southeast = new Button ("Southeast");
            add (southeast);
            southeast.addActionListener (this); 
     
            east = new Button ("East");
            add (east);
            east.addActionListener (this);  
     
        } // end Init 
     
        public void paint(Graphics g){
     
                resize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
            setBackground(Color.CYAN);
            g.setColor(Color.BLACK);
            g.fillOval(startX,startY,50,70);
            g.drawLine(startX+25,startY+70,boundX-275,boundY);
     
        } //end Paint
     
        public void actionPerformed(ActionEvent clic){
     
                        if (clic.getSource()== west)
                doWest();
                else if (clic.getSource()== northwest)
                doNorthwest();
                else if (clic.getSource()== northeast)
                doNortheast();
                else if (clic.getSource()== north)
                doNorth();
                else if (clic.getSource()== center)
                doCenter();
                else if (clic.getSource()== south)
                doSouth();
                else if (clic.getSource()== southwest)
                doSouthwest();
                else if (clic.getSource()== southeast)
                doSoutheast();
                else if (clic.getSource()== east)
                doEast();
     
                        repaint();
     
                        if (CONSOLE_LOGGING)
                    System.out.println(startX + " " + startY);
     
        } //end ActionPerformed
     
        public void doWest(){
     
                    if (startX-10 >= 0)
                        startX-=10;
                else
                    startX -= Math.abs(0+startX);
        }
        public void doNorthwest(){
     
                doNorth();
                doWest();
        }
        public void doNortheast(){
     
                doNorth();
                doEast();
        }
        public void doNorth(){
     
                if (startY-10 >= 0)
                        startY-=10;
                else
                    startY -= Math.abs(0+startY);
        }
        public void doCenter(){
     
                startX=boundX/2;
                startY=boundY/2;
        }
        public void doSouth(){
     
                if (startY+10 <= boundY)
                        startY+=10;
                else
                   startY += Math.abs(boundY-startY);
        }
        public void doSouthwest(){
     
                doSouth();
                doWest();
        }
        public void doSoutheast(){
     
                doSouth();
                doEast();
        }
        public void doEast(){
     
                if (startX+10 <= boundX)
                        startX+=10;
                else
                   startX += Math.abs(boundX-startX);
        }
    } //end ActionListener


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Simple Balloon Applet [Help!!!]

    You should have been put an eye on the fact that the coordinates you are specifying in drawing an object are its top-left corner coordinates.
    So if you intended that the balloon should stop when its right edge just reach the end of the frame, you should say in condition that if startX<=boundX-balloonWidth.
    public void doEast(){
     
                if (startX+10 <= boundX-50/*balloonWidth*/)
                        startX+=10;
    //            else
    //               startX += Math.abs(boundX-startX);
        }
    the same to south since for there, y is also increasing.
    Hope this will help.

  3. The Following User Says Thank You to aba muhammad For This Useful Post:

    GMPoison (October 23rd, 2013)

  4. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Simple Balloon Applet [Help!!!]

    Quote Originally Posted by aba muhammad View Post
    You should have been put an eye on the fact that the coordinates you are specifying in drawing an object are its top-left corner coordinates.
    So if you intended that the balloon should stop when its right edge just reach the end of the frame, you should say in condition that if startX<=boundX-balloonWidth.
    public void doEast(){
     
                if (startX+10 <= boundX-50/*balloonWidth*/)
                        startX+=10;
    //            else
    //               startX += Math.abs(boundX-startX);
        }
    the same to south since for there, y is also increasing.
    Hope this will help.
    I changed the South and east (the two that aren't working):
    public void doSouth(){
     
                if (startY+10 <= boundY-70)
                        startY+=10;
                else
                   startY += Math.abs(boundY - startY);
        }

     public void doEast(){
     
                if (startX+10 <= boundX-50)
                        startX+=10;
                else
                   startX += Math.abs(boundX-startX);
        }

    What happens now is as soon as it hits the edge, it sort of "snaps" outside of the screen. I'm just curious as to why North and West look so different and work:

    public void doNorth(){
     
                if (startY-10 >= 0)
                        startY-=10;
                else
                    startY -= Math.abs(0+startY);
        }

    public void doWest(){
     
                    if (startX-10 >= 0)
                        startX-=10;
                else
                    startX -= Math.abs(0+startX);
        }

    Not sure why they use 0's and the others don't, but I can't seem to get it working right.

  5. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Simple Balloon Applet [Help!!!]

    sorry for being late!!!
    If you have a closer look at the code segment I posted, I commented "else".
    so it should be:
    public void doEast(){
     
                if (startX+10 <= boundX-50/*balloonWidth*/)
                        startX+=10;
    //            else
    //               startX += Math.abs(boundX-startX);
        }

Similar Threads

  1. java balloon project
    By srozo22 in forum Object Oriented Programming
    Replies: 12
    Last Post: September 19th, 2013, 03:02 PM
  2. Replies: 3
    Last Post: July 3rd, 2013, 08:25 AM
  3. [SOLVED] Simple applet, but wrong
    By infamous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2013, 06:38 PM
  4. Balloon code
    By Pinares in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 26th, 2013, 11:28 AM
  5. Balloon.java, what in the world do i do?
    By toterzuko in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 13th, 2010, 05:19 PM

Tags for this Thread