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 9283111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:32:49+00:00 2026-06-18T18:32:49+00:00

This is a grade 12 object oriented programming project. I have a class called

  • 0

This is a grade 12 object oriented programming project.

I have a class called Ball to construct my ball object.

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

public class Ball{
    private double xPos;
    private double yPos;
    private int direction;
    public Ball(int ixPos, int iyPos, int idirection){
        xPos = ixPos;
        yPos = iyPos;
        direction = idirection;
    }
    public int returnX(){
        return (int)xPos;
    }
    public int returnY(){
        return (int)yPos;
    }
    public int returnDirection(){
        return direction;
    }
    public void move(){
        xPos += 1*Math.cos(Math.toRadians(direction));
        yPos -= 1*Math.sin(Math.toRadians(direction));
    }
    public void collide(int collideWith){
        if(collideWith==1){//collide with left wall
            if(90<direction && direction<180){
                direction = 180-direction;
            }
            if(180<direction && direction<270){
                direction = 540-direction;
            }
        }
        if(collideWith==2){//collide with right wall
            if(0<direction && direction<90){
                direction = 180-direction;
            }
            if(270<direction && direction<360){
                direction = 540-direction;
            }
        }
        if(collideWith==3){//collide with up wall
            if(0<direction && direction<90){
                direction = 360-direction;
            }
            if(90<direction && direction<180){
                direction = 360-direction;
            }
        }
        if(collideWith==4){//collide with down wall
            direction = 360-direction;
        }
    }
    public void collidePaddle(int collidePos){
        if(collidePos!=50 && collidePos!=0){
            direction = (50-collidePos)*180/50;
        }
    }
}

As you can see in the “move” function, right now the ball is going at a very low speed. But i need the ball to go faster. If I change the 1 into something like, 5, there would be a problem. In my main class where it checks if the ball is hitting the wall or blocks to change direction, the ball would go into the wall or the blocks if the amount of pixels the ball can move each time is greater than 1.

To me it seems like there’s no way to solve this problem, no idea where I would start thinking, is there a better way of checking collide or something?

Thank you.

  • 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-18T18:32:50+00:00Added an answer on June 18, 2026 at 6:32 pm

    Instead of using absolute checks in your collidePaddle check, you should allow for ranges.

    For example…

    public void collidePaddle(int collidePos){
        if (collidePos >= 50) {
            direction = (50-collidePos)*180/50;
            // Correct the position of the ball to meet the minimum requirements
            // of the collision...
        } else if (collidePos <= 0) {
            direction = (50-collidePos)*180/50;
            // Correct the position of the ball to meet the minimum requirements
            // of the collision...
        }
    }
    

    (Sorry, I’m having fun working out your code ;))

    This will allow the ball the “pass” beyond the these points within a virtual context, but if you correct the position to componsate, it should make no difference…when it’s rendered…

    Updated

    Here’s a REALLY SIMPLE example of what I’m talking about…

    public class SimpleBouncyBall {
    
        public static void main(String[] args) {
            new SimpleBouncyBall();
        }
    
        public SimpleBouncyBall() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new CourtPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class CourtPane extends JPanel {
    
            private Ball ball;
            private int speed = 5;
    
            public CourtPane() {
                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Rectangle bounds = new Rectangle(new Point(0, 0), getSize());
                        if (ball == null) {
                            ball = new Ball(bounds);
                        }
                        speed = ball.move(speed, bounds);
                        repaint();
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g); 
                if (ball != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    Point p = ball.getPoint();
                    g2d.translate(p.x, p.y);
                    ball.paint(g2d);
                    g2d.dispose();
                }
            }
    
        }
    
        public class Ball {
    
            private Point p;
            private int radius = 12;
    
            public Ball(Rectangle bounds) {                
                p = new Point();
                p.x = 0;
                p.y = bounds.y + (bounds.height - radius) / 2;                
            }
    
            public Point getPoint() {
                return p;
            }
    
            public int move(int speed, Rectangle bounds) {                
                p.x += speed;
                if (p.x + radius >= (bounds.x + bounds.width)) {                    
                    speed *= -1;
                    p.x = ((bounds.x + bounds.width) - radius) + speed;                    
                } else if (p.x <= bounds.x) {
                    speed *= -1;
                    p.x = bounds.x + speed;                    
                }
                p.y = bounds.y + (bounds.height - radius) / 2;                
                return speed;                
            }
    
            public void paint(Graphics2D g) {
                g.setColor(Color.RED);
                g.fillOval(0, 0, radius, radius);
            }            
        }        
    }
    

    My move method doesn’t care if you’ve past the boundaries as it will reposition the ball back to sit within side those boundaries

    public int move(int speed, Rectangle bounds) {                
        // Apply the delta
        p.x += speed;
        // Have we passed beyond the right side??
        if (p.x + radius >= (bounds.x + bounds.width)) {                    
            speed *= -1;
            p.x = ((bounds.x + bounds.width) - radius) + speed;                    
        // Have we past beyond the left side??
        } else if (p.x <= bounds.x) {
            speed *= -1;
            p.x = bounds.x + speed;                    
        }
        p.y = bounds.y + (bounds.height - radius) / 2;                
        return speed;                
    }
    

    Play around with the speed and see what you get 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that looks something like this: public class Student { public
Assume I have a business object like this, class Employee { public string name;
I have this code struct Student { char name[48]; float grade; int marks[10,5]; char
I have created a class that can contain several instances of an object, all
I have this coding inside my java file . This code basically receives an
I have a JOptionPane popup in my applet normally, a-la: Object[] options = {Grade,
I have the following code: Code 1 class Student { int no; char grade[M+1];
I have a webservice which return this kind of json object : { dossiers:
This part of my code is used to award a grade to a student
I've got table with two columns: name and grade. It looks sth like this:

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.