Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8795507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:24:04+00:00 2026-06-13T23:24:04+00:00

Sorry for the awful title. The purpose of the Java applet is as such:

  • 0

Sorry for the awful title. The purpose of the Java applet is as such: A ball is bouncing around the screen. The size and speed of this ball can be changed via scrollbars. The user can press and drag the mouse on the screen to draw rectangles. The ball will bounce off of these rectangles as well. The bounds of these rectangles are stored in a vector. When a rectangle is clicked, it (and all other rectangles at that point) are removed from the vector (and the screen).

The problem I’m having is two-fold: One, when I click a rectangle to remove it, it doesn’t get removed, but that can be solved later.

Two: The ball doesn’t bounce off of the rectangles like it’s supposed to. When I draw a rectangle in either the same row or column as the ball, the ball bounces around inside of a tiny rectangle, like it’s stuck.

Here’s my code to detect if the ball is hitting the boundaries of either the applet or any of the rectangles:

  public void move()
  {
    //if it will hit the right or left boundary, flip the x direction and set it
    if (loc.x+size >= boundx || loc.x <= 0)
    { dx *= -1; }
    //if it will hit the top or bottom boundray, flip the y direction and set it
    if (loc.y+size >= boundy || loc.y <= 0)
    { dy *= -1; }


    for (int i = 0; i < r.size(); i++)
    {

      temp = new Rectangle(r.elementAt(i));
      int rx = temp.x;
      int ry = temp.y;
      int rh = temp.height;
      int rw = temp.width;
      //If the ball hits either side of the rectangle, change the x direction
      if((loc.x > rx && loc.x > ry && loc.x < (ry + rh))||(loc.x < (rx + rw) && loc.x > rx && loc.x <(ry + rh)))
        {dx *= -1;}
      //If the ball hits either the top or bottom, change the y direction
      if((loc.y > ry && loc.y > rx && loc.y < (rx + rw))||(loc.y < (ry + rh) && loc.y > ry && loc.y <(rx + rw)))
        {dy *= -1;}
    }
    //Increment or decrement the location of the ball based on the X and Y directions.
    loc.x += dx;
    loc.y += dy;
  }

If you want to view the code in its entirety, it’s here: http://ideone.com/R1hpBx

Thanks in advance for all the wonderful help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:24:06+00:00Added an answer on June 13, 2026 at 11:24 pm

    I finally found a edge detection system I like…

    Spinning ball of doom

    Basically, the magic happens here…

    // Detect if we collided with any one (collision is the rectangle, bounds is us)
    if (collision.intersects(bounds)) {
        // Determine the intersect of the collision...
        insect = collision.intersection(bounds);
    
        // Flags...
        boolean vertical = false;
        boolean horizontal = false;
        boolean isLeft = false;
        boolean isTop = false;
    
        // Left side...
        if (insect.x == collision.x) {
            horizontal = true;
            isLeft = true;
        // Right side
        } else if (insect.x + insect.width == collision.x + collision.width) {
            horizontal = true;
        }
        // Top
        if (insect.y == collision.y) {
            vertical = true;
            isTop = true;
        // Bottom
        } else if (insect.y + insect.height == collision.y + collision.height) {
            vertical = true;
        }
    
        // Technically, we can really only collide with a single edge...more or less
        if (horizontal && vertical) {
            // Basically, we try and give precedence to the longer edge...
            if (insect.width > insect.height) {
                horizontal = false;
            } else {
                vertical = false;
            }
        }
    
        // We collided with a horizontal side...
        if (horizontal) {
            dx *= -1;
            // Move the ball to the approriate edge so we don't get caught...
            if (isLeft) {
                bounds.x = collision.x - bounds.width;
            } else {
                bounds.x = collision.x + collision.width;
            }
        // We collided with a vertical side...
        } else if (vertical) {
            dy *= -1;
            // Move the ball to the approriate edge so we don't get caught...
            if (isTop) {
                bounds.y = collision.y - bounds.height;
            } else {
                bounds.y = collision.y + collision.height;
            }
        }
    
    }
    

    Now, I only have a single obstacle, but I doubt it would take much effort to get it working with a series of obstacles… 😉

    public class TestBouncingBall {
    
        private Rectangle insect;
    
        public static void main(String[] args) {
            new TestBouncingBall();
        }
    
        public TestBouncingBall() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new BallPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class BallPane extends JLayeredPane {
    
            private Ball ball;
            private Timer timer;
            private Rectangle world;
    
            public BallPane() {
    
    //            world = new Rectangle(random(400), random(400), random(100), random(100));
                world = new Rectangle(100, 100, 200, 200);
    
                ball = new Ball();
                ball.setSize(ball.getPreferredSize());
                ball.setLocation(10, 10);
    
                add(ball);
    
                timer = new Timer(16, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        ball.move(getBounds(), world);
                        invalidate();
                        repaint();
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
    
            }
    
            protected int random(int max) {
    
                return (int) Math.round(Math.random() * max);
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.setColor(Color.GRAY);
                g2d.fill(world);
                if (insect != null) {
                    g2d.setColor(Color.RED);
                    g2d.fill(insect);
                }
                g2d.dispose();
            }
        }
    
        public class Ball extends JPanel {
    
            public int maxSpeed = 10;
            private BufferedImage beachBall;
            private int dx = 10 - (int)Math.round(Math.random() * (maxSpeed * 2)) + 1;
            private int dy = 10 - (int)Math.round(Math.random() * (maxSpeed * 2)) + 1;
            private int spin = 20;
            private int rotation = 0;
    
            public Ball() {
                try {
                    beachBall = ImageIO.read(getClass().getResource("/ball.png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                setOpaque(false);
            }
    
            @Override
            public Dimension getPreferredSize() {
                Dimension size = beachBall == null ? new Dimension(48, 48) : new Dimension(beachBall.getWidth(), beachBall.getHeight());
                size.width += 4;
                size.height += 4;
                return size;
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (beachBall != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
                    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
                    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
                    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    
                    int x = (getWidth() - beachBall.getWidth()) / 2;
                    int y = (getHeight() - beachBall.getHeight()) / 2;
                    AffineTransform transform = g2d.getTransform();
                    AffineTransform at = new AffineTransform();
                    at.translate(getX(), getY());
                    at.rotate(Math.toRadians(rotation), getWidth() / 2, getHeight() / 2);
                    g2d.setTransform(at);
                    g2d.drawImage(beachBall, x, y, this);
                    g2d.setTransform(transform);
                    g2d.dispose();
                }
            }
    
            public void move(Rectangle world, Rectangle collision) {
                Rectangle bounds = getBounds();
                bounds.x += dx;
                bounds.y += dy;
    
                if (bounds.x < 0) {
                    bounds.x = 0;
                    dx *= -1;
                }
                if (bounds.y < 0) {
                    bounds.y = 0;
                    dy *= -1;
                }
                if (bounds.x + bounds.width > world.width) {
                    bounds.x = world.width - bounds.width;
                    dx *= -1;
                }
                if (bounds.y + bounds.height > world.height) {
                    bounds.y = world.height - bounds.height;
                    dy *= -1;
                }
    
                if (collision.intersects(bounds)) {
                    insect = collision.intersection(bounds);
    
                    boolean vertical = false;
                    boolean horizontal = false;
                    boolean isLeft = false;
                    boolean isTop = false;
    
                    if (insect.x == collision.x) {
                        horizontal = true;
                        isLeft = true;
                    } else if (insect.x + insect.width == collision.x + collision.width) {
                        horizontal = true;
                    }
                    if (insect.y == collision.y) {
                        vertical = true;
                        isTop = true;
                    } else if (insect.y + insect.height == collision.y + collision.height) {
                        vertical = true;
                    }
    
                    if (horizontal && vertical) {
                        if (insect.width > insect.height) {
                            horizontal = false;
                        } else {
                            vertical = false;
                        }
                    }
    
                    System.out.println("v = " + vertical + "; h = " + horizontal);
    
                    if (horizontal) {
                        dx *= -1;
                        if (isLeft) {
                            bounds.x = collision.x - bounds.width;
                        } else {
                            bounds.x = collision.x + collision.width;
                        }
                    } else if (vertical) {
                        dy *= -1;
                        if (isTop) {
                            bounds.y = collision.y - bounds.height;
                        } else {
                            bounds.y = collision.y + collision.height;
                        }
                    }
    
                }
    
                rotation += spin;
    
                setBounds(bounds);
                repaint();
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the awful title, I couldn't think of anything better. Basically I have
Sorry for this simple question, but I can't solve it... There is an example:
I'm sorry, the title's awful; however, I couldn't think of any better way to
Sorry this is such a long question. Ive been doing lots of research lately
Sorry, I'm new to SVN and I looked around a little for this. How
I make a DLL-Java communication correspondingly this post . My compilation configuration you can
(Sorry for the not-so-good-title, I'm not sure how my problem can be solved, thus
Sorry if this question's been asked before, but I can't seem to find an
Sorry, but I can't solve this issue by myself.. For example, I have a
Sorry for question but I can't find answer anywhere on internet. I couldn't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.