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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:17:32+00:00 2026-05-19T11:17:32+00:00

This is an excerise i have to complete for a uni course, its not

  • 0

This is an excerise i have to complete for a uni course, its not a marked assignment and i could do with a bit of help. I can get the ball to appear on the screen and bounce of the sides, it doesnt matter at the moment if it falls through the bottom of the screen and i can get the paddle to appear on the screen at different times but i cant get them both to appear at the same time. Help please

Here are my classes

MainClass

package movingball;

public class Main
{
    public  static  void  main  (String []args)
    {
        MovingBall  world  =  new  MovingBall("Moving  Ball");
        world.setVisible(true);
        world.move();
    }
}

BallClass

package movingball;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class Ball
{
    private  final  int  RADIUS  = 10;

    private  Point  pos;
    private  Color  ballColour;
    private int yChange = 2;
    private int xChange = 1;
    private  int  height,  width;
    private int change;

    public  Ball  (int  frameWidth,  int  frameHeight)
    {
        change = 3;
        ballColour = Color.RED;
        width  = frameWidth;
        height  = frameHeight;
        pos  =  new  Point();
        pos.x  = (int)(Math.random() * (width - RADIUS)) +  RADIUS;
        pos.y  = (int)(Math.random() * (height/2 - RADIUS)) +  RADIUS;
    }

    //There are lots of ways you can updateBallState 
    //Note that the menu bar occupies some of the visible space
    public void move()
    {
        if(pos.y < RADIUS)
        {
           yChange = - yChange;
        }
        if(pos.x < RADIUS)
        {
           xChange = -xChange;
        }
        if(pos.x > width - RADIUS)
        {
           xChange = -xChange;
        }
        if(pos.y < height - RADIUS)
        {
           pos.translate(xChange, yChange);
        }
        if(pos.x < width - RADIUS)
        {
           pos.translate(xChange, yChange);
        }
    }

     public void updateBallState()
    {
        if (pos.y + change < height - 3*RADIUS)
        {
            pos.translate(0, change);
            // change++; //accelerate
        }
    }

    //This method can be called with a provided graphics context
    //to draw the ball in its current state 
    public void draw(Graphics  g)
    {
        g.setColor(ballColour);
        g.fillOval(pos.x - RADIUS,  pos.y - RADIUS,  2*RADIUS, 2*RADIUS);
    }  

    public void bounce()
    {
       yChange = -yChange;
       pos.translate(xChange, yChange);
    }

    public Point getPosition()
    {
       return pos;
    }
}

BallGame

package movingball;

import java.awt.Graphics;
import java.awt.event.*;

public  class  BallGame  extends  MovingBall
{

    private Paddle myPaddle = new Paddle(FRAME_WIDTH, FRAME_HEIGHT);


    public  BallGame(String  title)
    {
        super(title);
        addKeyListener(new KeyList());    
    }

    public void paint(Graphics g)
    {
       super.paint(g);
       myPaddle.paint(g);
       if(isContact())
       {
          myBall.bounce();
       }
       else
       {
          myPaddle.paint(g);
       }
    }

    public boolean isContact()
    {
       if (myPaddle.area().contains(myBall.getPosition()))
       {
          return true;
       }
       else
       {
          return false;
       }
    }

    public class KeyList extends KeyAdapter
    {
       public void keyPressed(KeyEvent k)
       {
          if(k.getKeyCode() == KeyEvent.VK_LEFT)
          {
             myPaddle.moveLeft();
          }
          if(k.getKeyCode() == KeyEvent.VK_RIGHT)
          {
             myPaddle.moveRight();
          }
       }
    }
}

MovingBall class

package movingball;

import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;

public  class  MovingBall  extends  JFrame
{
    protected  final  int  FRAME_WIDTH  = 240;
    protected  final  int  FRAME_HEIGHT  = 320;
    protected Ball myBall = new Ball(FRAME_WIDTH, FRAME_HEIGHT);

    public  MovingBall  (String  title)
    {
        super(title);

        setSize(FRAME_WIDTH,  FRAME_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    }

    public void paint(Graphics g)
    {
       super.paint(g);
       myBall.draw(g);  
    }

    public void move()
    {
       while(true)
       {
          myBall.move();
          repaint();
          try
          {
             Thread.sleep(50);
          }
          catch(InterruptedException e)
          {
             System.exit(0);
          }
       }
    }
}

Paddle class

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


public class Paddle
{
   private Color paddleColour = Color.GREEN;
   private int x,y;
   private int paddleWidth = 20;
   private int paddleHeight = 10;
   private int move = 5;


   /** Creates a new instance of Paddle */
   public Paddle(int frameWidth, int frameHeight)
   {

      x =(int)(Math.random()*(frameWidth - paddleWidth));
      y = frameHeight - paddleHeight;

   }

   public void moveRight()
   {
      x = x + move;
   }

   public void moveLeft()
   {
      x = x - move;
   }

   public Rectangle area()
   {
      return new Rectangle(x,y, paddleWidth, paddleHeight);
   }

   public void paint(Graphics g)
   {
      g.setColor(paddleColour);
      g.fillRect(x,y,paddleWidth, paddleHeight);
   }
}
  • 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-05-19T11:17:33+00:00Added an answer on May 19, 2026 at 11:17 am

    Here are a couple of pointers to get you started. I have a lot of things to suggest but I’ll just say this to get you further than you are now. You want your JFrame to be double-buffered. That’s the first step. To do this, create a new buffer strategy for your JFrame after making it visible:

    world.setVisible(true);
    world.createBufferStrategy(2);
    

    As for why your Paddle isn’t painting? You’re going to kick yourself. Look at this line:

    MovingBall  world  =  new  MovingBall("Moving  Ball");
    

    You’re not actually creating a BallGame, which is where the logic for painting the paddle is contained! (BallGame.paint()) Try:

    BallGame world  =  new  BallGame("Moving  Ball");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem has been bugging me since forever. I have an array and in
This is a weired problem, I have implemented simple quick sort as follows.. static
This should be exceptionally simple. I have a zoo object with 500 times series
In this question I'm not asking how to do it but HOW IS IT
EDIT Apologies if the original unedited question is misleading. This question is not asking
Update the purpose of this excerise is to eliminate passing the @RegModifiedDateTime again what
At the moment it is just an intellectual excercise for I do not have
maybe its late or something, but this is freaking me out. Basicly im writing
I have a school assignment which consists of programming a scanner/lexical analyzer for a
EDIT: If you can't be bothered to read this mammoth question, I've put a

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.