I cant seem to get the method correct. Highlighted in bold is what im having pro
ID: 3758269 • Letter: I
Question
I cant seem to get the method correct. Highlighted in bold is what im having problems with.
Instructions:
To show the ball in a new position, we need to first erase it from it old location. We do this by drawing the ball in its original position using the background color. Make a class constant BACKGROUND_COLOR and initialize it to Color.WHITE. Make class variables ballVelocityX and ballVelocityY and initialize these to 1 in the main method.
Write the method:
public static void moveBall(Graphics g)
that draws the ball in the background color, than adds ballVelocityX to ballX and adds ballVelocityY to ballY and draws the ball with its usual color. Call this method in the startGame loop.
Modify moveBall so that if the value of ballX is greater than the width of the panel, or ballY is greater than the height of the panel, the position is set back to the center. Do not change the ball velocity.
What I have:
import java.awt.*;
public class Project2 {
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(500, 400);
panel.setBackground(Color.WHITE);
Graphics g = panel.getGraphics( );
g.setColor(Color.BLACK);
g.drawString("Project by: ", 10, 15);
int PANEL_WIDTH = 500;
int PANEL_HEIGHT = 400;
int BALL_SIZE = 10;
Color BALL_Color = Color.RED;
int ballX = 250;
int ballY = 200;
g.setColor(Color.RED);
g.fillOval(250, 200, 20, 20);
Color BACKGROUND_COLOR = Color.WHITE;
int ballVelocityX = 1;
int ballVelocityY = 1;
startGame(panel, g);
}
public static void startGame(DrawingPanel panel, Graphics g) {
int x = 0;
int y = 270;
int deltaX = 1;
int deltaY = -3;
for (int t = 0; t <= 1000; t++) {
panel.sleep(50);
}
}
public static void drawBall(Graphics g, Color c){
g.setColor(Color.RED);
g.fillOval(250, 200, 20, 20);
}
// moving the ball
public static void moveBall(Graphics g){
g.setColor(Color.WHITE);
g.fillOval(ballX + ballVelocityX, ballY + ballVelocityY, 20, 20);
}
}
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameTest extends JFrame implements ActionListener
{
private GamePanel gamePanel = new GamePanel();
private JButton startButton = new JButton("Start");
private JButton quitButton = new JButton("Quit");
private JButton pauseButton = new JButton("Pause");
private boolean running = false;
private boolean paused = false;
private int fps = 60;
private int frameCount = 0;
public GameTest()
{
super("Fixed Timestep Game Loop Test");
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new GridLayout(1,2));
p.add(startButton);
p.add(pauseButton);
p.add(quitButton);
cp.add(gamePanel, BorderLayout.CENTER);
cp.add(p, BorderLayout.SOUTH);
setSize(500, 500);
startButton.addActionListener(this);
quitButton.addActionListener(this);
pauseButton.addActionListener(this);
}
public static void main(String[] args)
{
GameTest gt = new Game
Test();
gt.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object s = e.getSource();
if (s == startButton)
{
running = !running;
if (running)
{
startButton.setText("Stop");
runGameLoop();
}
else
{
startButton.setText("Start");
}
}
else if (s == pauseButton)
{
paused = !paused;
if (paused)
{
pauseButton.setText("Unpause");
}
else
{
pauseButton.setText("Pause");
}
}
else if (s == quitButton)
{
System.exit(0);
}
}
public void runGameLoop()
{
Thread loop = new Thread()
{
public void run()
{
gameLoop();
}
};
loop.start();
}
private void gameLoop()
{
final double GAME_HZT = 30.0;
final double TIME_UPDATES = 1000000000 / GAME_HZT;
final int BEFORE_RENDER = 5;
double UpdateTime = System.nanoTime();
double RenderTime = System.nanoTime();
final double TARGET_FPS = 60;
final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
int lastSecondTime = (int) (UpdateTime / 1000000000);
while (running)
{
double now = System.nanoTime();
int updateCount = 0;
if (!paused)
{
while( now - UpdateTime > TIME_UPDATES && updateCount < BEFORE_RENDER )
{
updateGame();
lastUpdateTime += TIME_UPDATES;
updateCount++;
}
do an insane number of catchups.
EXACT time, you would get rid of this.
if ( now - UpdateTime > TIME_UPDATES)
{
UpdateTime = now - TIME_UPDATES;
}
float interpolation = Math.min(1.0f, (float) ((now - UpdateTime) / TIME_UPDATES) );
drawGame(interpolation);
RenderTime = now;
int thisSecond = (int) (UpdateTime / 1000000000);
if (thisSecond > lastSecondTime)
{
System.out.println("NEW SECOND " + thisSecond + " " + frameCount);
fps = frameCount;
frameCount = 0;
lastSecondTime = thisSecond;
}
while ( now - RenderTime < TARGET_TIME_BETWEEN_RENDERS && now - UpdateTime < TIME_UPDATES)
{
Thread.yield();
try {Thread.sleep(1);} catch(Exception e) {}
now = System.nanoTime();
}
}
}
}
private void updateGame()
{
gamePanel.update();
}
private void drawGame(float interpolation)
{
gamePanel.setInterpolation(interpolation);
gamePanel.repaint();
}
private class GamePanel extends JPanel
{
float interpolation;
float ballX, ballY, lastBallX, lastBallY;
int ballWidth, ballHeight;
float ballXVel, ballYVel;
float ballSpeed;
int lastDrawX, lastDrawY;
public GamePanel()
{
ballX = lastBallX = 100;
ballY = lastBallY = 100;
ballWidth = 25;
ballHeight = 25;
ballSpeed = 25;
ballXVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
ballYVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
}
public void setInterpolation(float interp)
{
interpolation = interp;
}
public void update()
{
lastBallX = ballX;
lastBallY = ballY;
ballX += ballXVel;
ballY += ballYVel;
if (ballX + ballWidth/2 >= getWidth())
{
ballXVel *= -1;
ballX = getWidth() - ballWidth/2;
ballYVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
}
else if (ballX - ballWidth/2 <= 0)
{
ballXVel *= -1;
ballX = ballWidth/2;
}
if (ballY + ballHeight/2 >= getHeight())
{
ballYVel *= -1;
ballY = getHeight() - ballHeight/2;
ballXVel = (float) Math.random() * ballSpeed*2 - ballSpeed;
}
else if (ballY - ballHeight/2 <= 0)
{
ballYVel *= -1;
ballY = ballHeight/2;
}
}
public void paintComponent(Graphics g)
{
g.setColor(getBackground());
g.fillRect(lastDrawX-1, lastDrawY-1, ballWidth+2, ballHeight+2);
g.fillRect(5, 0, 75, 30);
g.setColor(Color.RED);
int drawX = (int) ((ballX - lastBallX) * interpolation + lastBallX - ballWidth/2);
int drawY = (int) ((ballY - lastBallY) * interpolation + lastBallY - ballHeight/2);
g.fillOval(drawX, drawY, ballWidth, ballHeight);
lastDrawX = drawX;
lastDrawY = drawY;
g.setColor(Color.BLACK);
g.drawString("FPS: " + fps, 5, 10);
frameCount++;
}
}
private class Ball
{
int width, height;
float xVelocity, yVelocity;
float speed;
float x, y, lastX, lastY;
public Ball()
{
width = (int) (Math.random() * 50 + 10);
height = (int) (Math.random() * 50 + 10);
x = (float) (Math.random() * (gamePanel.getWidth() - width) + width/2);
y = (float) (Math.random() * (gamePanel.getHeight() - height) + height/2);
lastX = x;
lastY = y;
xVelocity = (float) Math.random() * speed*2 - speed;
yVelocity = (float) Math.random() * speed*2 - speed;
}
public void update()
{
lastX = x;
lastY = y;
x += xVelocity;
y += yVelocity;
if (x + width/2 >= gamePanel.getWidth())
{
xVelocity *= -1;
x = gamePanel.getWidth() - width/2;
yVelocity = (float) Math.random() * speed*2 - speed;
}
else if (x - width/2 <= 0)
{
xVelocity *= -1;
x = width/2;
}
if (y + height/2 >= gamePanel.getHeight())
{
yVelocity *= -1;
y = gamePanel.getHeight() - height/2;
xVelocity = (float) Math.random() * speed*2 - speed;
}
else if (y - height/2 <= 0)
{
yVelocity *= -1;
y = height/2;
}
}
public void draw(Graphics g)
{
}
}
}
public void paintComponent(Graphics g)
{
g.setColor(getBackground());
g.fillRect(lastDrawX-1, lastDrawY-1, ballWidth+2, ballHeight+2);
g.fillRect(5, 0, 75, 30);
g.setColor(Color.RED);
int drawX = (int) ((ballX - lastBallX) * interpolation + lastBallX - ballWidth/2);
int drawY = (int) ((ballY - lastBallY) * interpolation + lastBallY - ballHeight/2);
g.fillOval(drawX, drawY, ballWidth, ballHeight);
lastDrawX = drawX;
lastDrawY = drawY;
g.setColor(Color.BLACK);
g.drawString("FPS: " + fps, 5, 10);
frameCount++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.