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 5 of 5

Thread: Balloon Applet Help [Simple for non-novice]

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

    Default Balloon Applet Help [Simple for non-novice]

    The applet allows you to move the balloon in a few directions, keeping the string in the bottom middle of the screen, which works fine. The problem started when I implimented the increase width/height buttons. When I increase the width and height, the balloon then falls through the south and east of the window about 1/2way when you try and push it up against the walls, and I cannot figure out why. Ty for the help.

    Here is the full code:

    package Balloon;
     
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
     
    public class balloon extends Applet
     
    implements ActionListener{   
     
        private static final long serialVersionUID = 1L; 
     
        Button north, west, south, east, northeast, northwest, southeast, southwest, center;
        Button increaseWidth, decreaseWidth, increaseHeight, decreaseHeight;
     
        public int displayWidth = 600;
        public int displayHeight = 600;
        public int boundX = displayWidth-50;
        public int boundY = displayHeight-76;
        public int startX = boundX/2;
        public int startY = boundY/2;
     
        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); 
     
            increaseWidth = new Button ("Increase Width");
            add (increaseWidth);
            increaseWidth.addActionListener (this); 
     
            decreaseWidth = new Button ("Decrease Width");
            add (decreaseWidth);
            decreaseWidth.addActionListener (this); 
     
            increaseHeight = new Button ("Increase Height");
            add (increaseHeight);
            increaseHeight.addActionListener (this); 
     
            decreaseHeight = new Button ("Decrease Height");
            add (decreaseHeight);
            decreaseHeight.addActionListener (this); 
     
        } // end Init 
     
        public void paint(Graphics g){
     
        	resize(displayWidth, displayHeight);
            setBackground(Color.BLACK);
            g.setColor(Color.WHITE);
            g.fillOval(startX,startY,50,70);
            g.drawLine(startX+25,startY+70,displayWidth/2, displayHeight);
     
        } //end Paint
     
        public void actionPerformed(ActionEvent click){
     
        		if (click.getSource()== west)
                doWest();
                else if (click.getSource()== northwest)
                doNorthwest();
                else if (click.getSource()== northeast)
                doNortheast();
                else if (click.getSource()== north)
                doNorth();
                else if (click.getSource()== center)
                doCenter();
                else if (click.getSource()== south)
                doSouth();
                else if (click.getSource()== southwest)
                doSouthwest();
                else if (click.getSource()== southeast)
                doSoutheast();
                else if (click.getSource()== east)
                doEast();
                else if (click.getSource()== increaseWidth)
                doincreaseWidth();
                else if (click.getSource()== decreaseWidth)
                dodecreaseWidth();
                else if (click.getSource()== increaseHeight)
                doincreaseHeight();
                else if (click.getSource()== decreaseHeight)
                dodecreaseHeight();
     
        		repaint();
     
        		if (CONSOLE_LOGGING)
                    System.out.println(startX + " " + startY);
     
        } //end ActionPerformed
     
        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);
        }
        public void doEast(){
     
            if (startX+10 <= boundX)
                startX+=10;
            else
                startX += Math.abs(boundX-startX);
        }
        public void doSouth(){
     
        	if (startY+10 <= boundY)
        		startY+=10;
        	else
        		startY += Math.abs(boundY-startY);
        }
        public void doNorthwest(){
     
        	doNorth();
            doWest();
        }
        public void doNortheast(){
     
            doNorth();
            doEast();
        }
        public void doSouthwest(){
     
            doSouth();
            doWest();
        }
        public void doSoutheast(){
     
            doSouth();
            doEast();
        }
        public void doincreaseWidth(){
     
        	displayWidth += 25;
        	boundX = displayWidth - 25;
     
        }
        public void dodecreaseWidth(){
     
        	displayWidth -= 25;
        	boundX = displayWidth - 25;
     
        }
        public void doincreaseHeight(){
     
        	displayHeight += 25;
        	boundY = displayHeight - 25;
     
        }
        public void dodecreaseHeight(){
     
        	displayHeight -= 25;
        	boundY = displayHeight - 25;
     
        }
        public void doCenter(){
     
        	startX=boundX/2;
            startY=boundY/2;
     
        }
    } //end ActionList


    --- Update ---

    I'm thinking the problem may lie within the increase/decrease height/width methods, but I've tried changing some numbers around and it hasn't helped.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Balloon Applet Help [Simple for non-novice]

    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Balloon Applet Help [Simple for non-novice]

    Quote Originally Posted by Norm View Post
    Same program, different problem. Another thing I noticed is that when you increase or decrease the width or height, and you try and re center the balloon with the center button, the balloon is slightly off to the right.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Balloon Applet Help [Simple for non-novice]

    Can you add some println statements for debugging that shows the problem?
    For example print out the balls position when it is centered then compare those values with what they should be for the ball to be where you want it to be. You should have manually computed the correct position so you can compare it to what the program computes.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Balloon Applet Help [Simple for non-novice]

    Fixed it, the problem was here:
     public void doincreaseWidth(){
     
        	displayWidth += 25;
        	boundX = displayWidth - 25;
     
        }
        public void dodecreaseWidth(){
     
        	displayWidth -= 25;
        	boundX = displayWidth - 25;
     
        }
        public void doincreaseHeight(){
     
        	displayHeight += 25;
        	boundY = displayHeight - 25;
     
        }
        public void dodecreaseHeight(){
     
        	displayHeight -= 25;
        	boundY = displayHeight - 25;

    I had to change the 25's to match the width and height of the balloon, like this:
     public void doincreaseWidth(){
     
        	displayWidth += 25;
        	boundX = displayWidth - 50;
     
        }
        public void dodecreaseWidth(){
     
        	displayWidth -= 25;
        	boundX = displayWidth - 50;
     
        }
        public void doincreaseHeight(){
     
        	displayHeight += 25;
        	boundY = displayHeight - 70;
     
        }
        public void dodecreaseHeight(){
     
        	displayHeight -= 25;
        	boundY = displayHeight - 70;
     
        }

    Thank's for your help though guys!

Similar Threads

  1. Simple Balloon Applet [Help!!!]
    By GMPoison in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2013, 06:46 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. Simple Applet Troubles
    By Veldimare in forum Java Applets
    Replies: 7
    Last Post: September 22nd, 2012, 05:19 PM
  5. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 10:22 PM