How do I call a method using Ball_Color? Everytime I compile, it says that it ca
ID: 3758165 • Letter: H
Question
How do I call a method using Ball_Color? Everytime I compile, it says that it cannot find a variable for "c" on line 12. Any help is greatly appreciated!
Here are the detailed intructions:
The ball will be displayed as a solid red circle. It will start in the middle of the screen. Make class constants BALL_SIZE initialized to 10 and BALL_COLOR initialized to Color.RED.
BALL_SIZE is the diameter of the ball. Make integer class variables ballX and ballY which represent the position of the center of the ball.
Write a method:
public static void drawBall(Graphics g, Color c);
that draws the ball at the position given by ballX and ballY.
Use g.fillOval but remember that this needs to be given the upper left corner of the bounding box of the ball, while the variables represent the center of the ball.
In the main method, initialize ballX and ballY to correspond to the center of the panel. Then call drawBall using BALL_COLOR as the color.
Here is what i currently 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 2 by: Name", 10, 15);
drawBall(panel, c);
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);
}
}
Explanation / Answer
If you observe, you are passing an argument of type Color to drawBall(that is c) which you have not initialized. you need to initialize c with some value
public static void drawBall(Graphics g, Color c){
g.setColor(c);
g.fillOval(250, 200, 20, 20);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.