BouncingBall multiWindows
Budget: €30 – €250 EUR
I want to create a Java application that displays a ball bouncing between multiple windows. There will be a server that accepts TCP connections from clients, a game manager that keeps track of the clients and sends the ball’s coordinates to all clients, and the clients, which are windows created using Java Swing.
The ball’s code should be similar to the following:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BouncingBall extends JPanel implements ActionListener {
private int ballX = 50; // Initial x position of the ball
private int ballY = 50; // Initial y position of the ball
private int ballDiameter = 30; // Diameter of the ball
private int ballDeltaX = 2; // Horizontal speed of the ball
private int ballDeltaY = 3; // Vertical speed of the ball
private Timer timer;
protected void startGame() {
timer = new Timer(10, this); // Sets a timer to update the ball's position
timer.start();
}
// Sets the initial position of the ball
protected void setXY(int x, int y) {
ballX = x;
ballY = y;
}
// Gets the current position of the ball
protected String getXY() {
return ballX + "," + ballY;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(ballX, ballY, ballDiameter, ballDiameter); // Draws the ball
}
@Override
public void actionPerformed(ActionEvent e) {
// Updates the ball's position
ballX += ballDeltaX;
ballY += ballDeltaY;
// Boundary checks for bouncing
if (ballX < 0 || ballX + ballDiameter > getWidth()) {
ballDeltaX = -ballDeltaX; // Inverts horizontal direction
}
if (ballY < 0 || ballY + ballDiameter > getHeight()) {
ballDeltaY = -ballDeltaY; // Inverts vertical direction
}
repaint(); // Redraws the window
}
}
The program should allow the user to input a number of rows n and columns m, and it will generate that number of clients, arranging them in an n x m grid. The ball will be unique across the entire program and will move through the windows as if they were a single continuous window. It will bounce off the outermost walls of the matrix of windows.
If a window is removed, the program must adjust the layout by shrinking the matrix by one window and redistributing the remaining windows accordingly.
The ball’s code should be similar to the following:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BouncingBall extends JPanel implements ActionListener {
private int ballX = 50; // Initial x position of the ball
private int ballY = 50; // Initial y position of the ball
private int ballDiameter = 30; // Diameter of the ball
private int ballDeltaX = 2; // Horizontal speed of the ball
private int ballDeltaY = 3; // Vertical speed of the ball
private Timer timer;
protected void startGame() {
timer = new Timer(10, this); // Sets a timer to update the ball's position
timer.start();
}
// Sets the initial position of the ball
protected void setXY(int x, int y) {
ballX = x;
ballY = y;
}
// Gets the current position of the ball
protected String getXY() {
return ballX + "," + ballY;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLUE);
g.fillOval(ballX, ballY, ballDiameter, ballDiameter); // Draws the ball
}
@Override
public void actionPerformed(ActionEvent e) {
// Updates the ball's position
ballX += ballDeltaX;
ballY += ballDeltaY;
// Boundary checks for bouncing
if (ballX < 0 || ballX + ballDiameter > getWidth()) {
ballDeltaX = -ballDeltaX; // Inverts horizontal direction
}
if (ballY < 0 || ballY + ballDiameter > getHeight()) {
ballDeltaY = -ballDeltaY; // Inverts vertical direction
}
repaint(); // Redraws the window
}
}
The program should allow the user to input a number of rows n and columns m, and it will generate that number of clients, arranging them in an n x m grid. The ball will be unique across the entire program and will move through the windows as if they were a single continuous window. It will bounce off the outermost walls of the matrix of windows.
If a window is removed, the program must adjust the layout by shrinking the matrix by one window and redistributing the remaining windows accordingly.
Related categories:
Business, Accounting, Human Resources & Legal
Software Architecture
Socket IO
Swing (Java)