"error: cannot find symbol" Symbol is inside a Switch statement - java

Here's my code that the error points to:
shipX = shipOne.getX();
EDIT2: Specifically it cannot find shipOne.
The Ship object, shipOne, is initialized inside a Switch statement not ten lines prior to the above code. I'm fairly certain that the error is appearing due to the object being initialized inside the Switch statement, and I don't know what to do about that. There are a total of eight Ship objects that are initialized via a loop and as such the above code needs to either be in a Switch statement or an If statement, both of which I'm sure would cause the same error. Is there an exception I can throw or something that would cause my program to run? The Ship objects do get initialized 100% of the time so there's no worry there.
EDIT: I was asked for the rest of the code so here it be:
(This isn't the entirety of it, as you can't see the return statement but the remainder isn't relevant to my problem).
public static String[][] createFleet()
{
//Initialize Variables
//Ship Variables
boolean isHoriz = true; //Is true if ship is Horizontal, false if Vertical
char ID;
int posX = 0; //Origin of the ship (x coordinate [row])
int posY = 0; //Origin of the ship (y coordinate [column])
int size = 0; //Either 2, 3, or 4
//Associated Variables
Random randomGen = new Random(); //Random number generator
String[][] board = new String[10][10]; //Gameboard, populated with ships
int orientationRand = 0; //If 0, Ship Horizontal. If 1, Ship Vertical
int posRand = 0; //Assigned to posX or posY
int sizeRand = 0; //If 0 <= x < 10, size = 2
//If 10 <= x < 40, size = 3
//If 40 <= x < 100, size = 4
//Create Ships
for (int i = 0; i < 8; i++)
{
//Size
sizeRand = randomGen.nextInt(100);
if (sizeRand < 10)
{
size = 2;
}
else if (sizeRand < 40)
{
size = 3;
}
else size = 4;
//Orientation & Position
orientationRand = randomGen.nextInt(2);
if (orientationRand == 0)
{
//Orientation
isHoriz = true;
//Position (horizontal)
//x position
switch (size)
{
case 2:
posRand = randomGen.nextInt(9);
posX = posRand;
break;
case 3:
posRand = randomGen.nextInt(8);
posX = posRand;
break;
case 4:
posRand = randomGen.nextInt(7);
posX = posRand;
break;
}
//y position
posRand = randomGen.nextInt(10);
posY = posRand;
}
else
{
//Orientation
isHoriz = false;
//Position (vertical)
//x position
posRand = randomGen.nextInt(10);
posX = posRand;
//y position
switch (size)
{
case 2:
posRand = randomGen.nextInt(9);
posY = posRand;
break;
case 3:
posRand = randomGen.nextInt(8);
posY = posRand;
break;
case 4:
posRand = randomGen.nextInt(7);
posY = posRand;
break;
}
}
//Assign Size, Orientation, and Position to each ship
switch (i)
{
case 0:
ID = 'A';
Ship shipOne = new Ship(posX, posY, size, isHoriz, ID);
break;
case 1:
ID = 'B';
Ship shipTwo = new Ship(posX, posY, size, isHoriz, ID);
break;
case 2:
ID = 'C';
Ship shipThree = new Ship(posX, posY, size, isHoriz, ID);
break;
case 3:
ID = 'D';
Ship shipFour = new Ship(posX, posY, size, isHoriz, ID);
break;
case 4:
ID = 'E';
Ship shipFive = new Ship(posX, posY, size, isHoriz, ID);
break;
case 5:
ID = 'F';
Ship shipSix = new Ship(posX, posY, size, isHoriz, ID);
break;
case 6:
ID = 'G';
Ship shipSeven = new Ship(posX, posY, size, isHoriz, ID);
break;
case 7:
ID = 'H';
Ship shipEight = new Ship(posX, posY, size, isHoriz, ID);
break;
}
}

In your code, 'ID' variable is not getting declared anywhere. You must declare variable before using it.

Initialize the objects to null before the switch or if statements. I think this should solve your problem.
It will be possible to provide a better solution if the entire code was posted.

Related

Java 2D Game Key Input (Best Practice?)

I'm brand new to game development and have chosen to start working on a 2D top-down scroller game. I'm using the Slick2D library for this game.
My question is about best practices for taking multiple direction input for sprite movement (UP + RIGHT = Diagonal)
Currently, I have a rather ugly looking if/elseif chain to read in the input from the keyboard that is then checked in the 'Mob' class to determine how the sprite will move. The current setup works just fine, but my question is if there is another, better way of going about taking multiple inputs for diagonals (or any combo of keys, for that matter)
Here is the main class's update method that reads the input (blooper is the instance of 'Mob'):
public void update(GameContainer container, StateBasedGame arg1, int delta) throws SlickException {
Input input = container.getInput();
if(input.isKeyDown(Input.KEY_RIGHT)) { //RIGHT
if(input.isKeyDown(Input.KEY_UP)){ //RIGHT + UP
blooper.direction = 2;
} else if(input.isKeyDown(Input.KEY_DOWN)){ //RIGHT + DOWN
blooper.direction = 3;
}
else {
blooper.direction = 1;
}
} else if(input.isKeyDown(Input.KEY_LEFT)){ //LEFT
if(input.isKeyDown(Input.KEY_UP)){ //LEFT + UP
blooper.direction = 5;
} else if(input.isKeyDown(Input.KEY_DOWN)){ //LEFT + DOWN
blooper.direction = 6;
} else{
blooper.direction = 4;
}
} else if(input.isKeyDown(Input.KEY_UP)){ //UP
if(input.isKeyDown(Input.KEY_RIGHT)){ //UP + RIGHT
blooper.direction = 8;
} else if(input.isKeyDown(Input.KEY_LEFT)){ //UP + LEFT
blooper.direction = 9;
} else{
blooper.direction = 7;
}
} else if(input.isKeyDown(Input.KEY_DOWN)){ //DOWN
if(input.isKeyDown(Input.KEY_RIGHT)){ //DOWN + RIGHT
blooper.direction = 11;
} else if(input.isKeyDown(Input.KEY_LEFT)){ //DOWN + LEFT
blooper.direction = 12;
} else{
blooper.direction = 10;
}
} else{
blooper.direction = -1;
}
blooper.update(delta);
}
And here is how that input is processed in the Mob class:
public class Mob {
private final int RIGHT = 1;
private final int RIGHTUP = 2;
private final int RIGHTDOWN = 3;
private final int LEFT = 4;
private final int LEFTUP = 5;
private final int LEFTDOWN = 6;
private final int UP = 7;
private final int UPRIGHT = 8;
private final int UPLEFT = 9;
private final int DOWN = 10;
private final int DOWNRIGHT = 11;
private final int DOWNLEFT = 12;
private final int IDLE = -1;
int direction = IDLE;
int x, y;
Image sprite;
public Mob() throws SlickException{
x = 20;
y = 20;
sprite = new Image("res/blooper.png");
}
public void update(int delta){
move();
}
public void draw(){
sprite.draw(x, y);
}
public void move(){
switch(direction){
case RIGHT:
x += 1;
break;
case RIGHTUP:
x += 1;
y -= 1;
break;
case RIGHTDOWN:
x += 1;
y += 1;
break;
case LEFT:
x -= 1;
break;
case LEFTUP:
x -= 1;
y -= 1;
break;
case LEFTDOWN:
x -= 1;
y += 1;
break;
case UP:
y -= 1;
break;
case UPRIGHT:
y -= 1;
x += 1;
break;
case UPLEFT:
y -= 1;
x -= 1;
break;
case DOWN:
y += 1;
break;
case DOWNRIGHT:
y += 1;
x += 1;
break;
case DOWNLEFT:
y += 1;
x -= 1;
break;
case IDLE:
//nothing
}
}
}
Like I said...this works, but doesn't seem like the best way to go about it. Any tips?
Make left/right and up/down movement independent. You (or the framework - I'm unfamiliar with it) seems to work with x/y coordinates, which are mathematically independent.
Thus make one function that treats the up/down movement, and one function that treats the left/right movement, and you can get rid of their 'combinations'. Is there a reason why the directions are positive integers? Try making left = -1, right = 1, and no x-direction movement 0, and then do the same for y. That should hopefully make it more intuitive.
Happy coding
Make a blooper.direction a bitwise, like:
blooper.direction=0;
if(input.isKeyDown(Input.KEY_RIGHT)) blooper.direction|=1;
if(input.isKeyDown(Input.KEY_LEFT)) blooper.direction|=2;
if(input.isKeyDown(Input.KEY_UP)) blooper.direction|=4;
if(input.isKeyDown(Input.KEY_DOWN)) blooper.direction|=8;
then in move do the move, like:
if ((blooper.direction&1)!=0) x+=1;
if ((blooper.direction&2)!=0) x-=1;
if ((blooper.direction&4)!=0) y-=1;
if ((blooper.direction&8)!=0) y+=1;

Java switch case delay

I'm programming a little 2d game. Acutally I'm busy with the jumping-function. I want my player to jump, when i press the up-arrow key. I managed to make him jumping, but there is a delay of at least one second. This dealy is not existing for other actions like moving left or right. Do you know where it comes from?
The code for jumping is in the keyReaction method.
public class John<I> extends AbstractGame<I> {
//lists
List<ImageObject<I>> floor = new ArrayList<>();
//variables
double jumpHeight = 0;
//boolean
boolean jump = false;
//fix values
int blockHeight = 40;
int blockWidth = 40;
public John(double width, double height) {
//getPlayer()
super(new ImageObject<>("player.png", new Vertex(120, height - 200), new Vertex(0, 0)), width, height);
//floor objects
for(int i=0; i<getWidth(); i+=blockWidth)
floor.add(new ImageObject<>("brick.png", new Vertex(i, getHeight() - blockHeight)));
//add arrays to canvas
getGOss().add(floor);
}
#Override
public void doChecks() {
for(ImageObject<I> f:floor) {
//gravity
if(!(jump) && !(getPlayer().touches(f)))
getPlayer().getMotion().y++;
//stop y-motion on ground
if(getPlayer().touches(f)){
getPlayer().getPos().y = getHeight() - blockHeight - getPlayer().getHeight();
getPlayer().getMotion().y = 0;}
}
}
#Override
public boolean isStopped(){
return false;
}
#Override
public void keyReaction(KeyCode keycode){
switch (keycode) {
//move left
case LEFT_ARROW:
getPlayer().getMotion().x = -2;
break;
case VK_A:
getPlayer().getMotion().x = -2;
break;
//move right
case RIGHT_ARROW:
getPlayer().getMotion().x = 2;
break;
case VK_D:
getPlayer().getMotion().x = 2;
break;
//jump
case UP_ARROW:
jump = true;
if(jumpHeight < 100){
jumpHeight += 5;
getPlayer().getPos().y -=5;
} else if (jumpHeight < 200){
jumpHeight +=5;
getPlayer().getPos().y +=5;
}
break;
case VK_W:
break;
case SPACE:
break;
}
}
#Override
public void keyReactionReleased(KeyCode keycode){
switch (keycode) {
//move left
case LEFT_ARROW:
getPlayer().getMotion().x = 0;
break;
case VK_A:
getPlayer().getMotion().x = 0;
break;
//move right
case RIGHT_ARROW:
getPlayer().getMotion().x = 0;
break;
case VK_D:
getPlayer().getMotion().x = 0;
break;
//jump
case UP_ARROW:
jump = false;
jumpHeight = 0;
break;
case VK_W:
jump = false;
break;
case SPACE:
break;
}
}
//test main
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new SwingScreen(new John<>(1800, 600)));
f.pack();
f.setVisible(true);
}
}
Thank you for your help.
Code with the if:
case UP_ARROW:
if(jumpHeight < 20){
jumpHeight += 5;
getPlayer().getPos().y = -5;
} else if (jumpHeight < 40){
jumpHeight +=5;
getPlayer().getPos().y = 5;
}
break;
Code without the if:
case UP_ARROW:
getPlayer().getPos().y = -5;
break;
KeyReaction:
setOnKeyPressed((ev)->{
logic.keyReaction(KeyCode.fromCode(ev.getCode().impl_getCode()));
ev.consume();
});
setOnKeyReleased((ev)->{
logic.keyReactionReleased(KeyCode.fromCode(ev.getCode().impl_getCode()));
ev.consume();
});
In your jumpheight method you have
jump = true;
if(jumpHeight < 100){
jumpHeight += 5;
getPlayer().getPos().y -=5;
} else if (jumpHeight < 200){
jumpHeight +=5;
getPlayer().getPos().y +=5;
}
break;
On the 4th line there, you have a -=5 for the pos. There is likely another method that prevents the ypos from going under 0. What happens then is you press the up arrow, it gets sent 40 times(200/5), eventually the jumpheight is greater than 200, and only then does the player start moving up.

Breaking an array into quadrants multiple times

My goal is that I have an image which spits out a bitmap. Now I want display the average color of the image as one giant pixel. This is a fairly simple task, just use bufferImage and get the bitmap which I take each red, green, and blue value, add it all up and then divide by the picture's resolution.
The thing is after doing this, I want to break the image into four quadrants and take the average color for each quadrant and display it. And again break each of the four quadrants and do the same. The issue I am facing is that I am using a recursive statement that does the following:
private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {
if(endHeight <= startHeight || endWidth <= startWidth) {
counter++;
return;
}
// get quadrant pixel average and display, I deleted this portion of the code just to keep things compact
getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
getBlockAverage((startHeight + endHeight)/2, endHeight, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
getBlockAverage(startHeight, (startHeight + endHeight)/2, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
getBlockAverage((startHeight+endHeight)/2, endHeight, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
}
It is quite easy to see that this is not what I want as the recursive statement will keep executing getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color); first until it is done and then move onto the next one. This is not what I want. I want the image to be broken down into 4 quadrants and then each of those quadrants gets broken down until all quadrants are broken down and continue.
So for example:
Starting off with 1 quadrants, break into 4. Now for quadrant 1, break that into 4, now quadrant 2, break that into 4, now quadrant 3, break that into 4, now quadrant 4, break that into 4.
Now that I am thinking about it, I feel like I should use some sort of for loop with a cap on the number of iterations but I am not sure how to do that.
I tend to agree with you. I think I would place this method into loop as well but also make the method return the average color for each quadrant into an single dimensional Array with the thought of each Array index is a quadrant number and and the actual element for that index contains the color for that particular quadrant. This way you can work with all the pertinent information that is acquired later on. I would at least get it going and then optimize it once it's working the way I want. Well, that's how I do it anyways :P
Of course I'm assuming throughout all this that the Quadrant dissection flow is something similar to what I show in the image below:
Here is what I would do:
Change the getBlockAverage() method so that it returns a Color...
private static Color getBlockAverage(int startHeight, int endHeight, int startWidth,
int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {
// get quadrant pixel average color and return it
// with whatever code you've been using....
return theQuadrantAverageColor;
}
then I would create another method which contains our loop, image quadrants dissectional dimensions, and calls to the getBlockAverage() method while the loop is well...looping and for every loop cycle place the returned color from the getBlockAverage() method into a per-established Color Array:
private static void getQuadrantsColorAverages(Color[] quadrantColors, BufferedImage img) {
// Decalre and Initialize required variables.
BufferedImage wrkImg = img;
BufferedImage blockImg = null; //?????
int imgWidth = wrkImg.getWidth();
int imgHeight = wrkImg.getHeight();
int startHeight = 0;
int endHeight = 0;
int startWidth = 0;
int endWidth = 0;
Color oldAvg = null;
int quadCount = 1;
// Start our loop and and continue it until our counter
// variable named quadCount goes over 20....
while (quadCount <= 20) {
// Handle dissectional dimensions (in pixels)
// for quadrants 1 to 20 as layed out within
// the supplied image to this forum post.
switch (quadCount) {
// Quadrant 1
case 1:
startHeight = 0; endHeight = (imgHeight / 2);
startWidth = 0; endWidth = (imgWidth / 2);
// Quadrant 2
case 2:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 3
case 3:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = 0; endWidth = (imgWidth / 2);
break;
// Quadrant 4
case 4:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 5
case 5:
startHeight = 0; endHeight = (imgHeight / 4);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 6
case 6:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 7
case 7:
startHeight = (endHeight + 1); endHeight = (imgHeight / 2);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 8
case 8:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 9
case 9:
startHeight = 0; endHeight = (imgHeight / 4);
startWidth = (endWidth + 1); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 10
case 10:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 11
case 11:
startHeight = (imgHeight / 4); endHeight = (imgHeight / 2);
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 12
case 12:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 13
case 13:
startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 14
case 14:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 15
case 15:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = 0; endWidth = (imgWidth / 4);
break;
// Quadrant 16
case 16:
startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
break;
// Quadrant 17
case 17:
startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 18
case 18:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
// Quadrant 19
case 19:
startHeight = (endHeight + 1); endHeight = imgHeight;
startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
break;
// Quadrant 20
case 20:
startWidth = (endWidth + 1); endWidth = imgWidth;
break;
}
// Maintain the oldAvg Color variable
oldAvg = getBlockAverage(startHeight, endHeight, startWidth,
endWidth, img, blockImg, oldAvg);
// We subtract 1 from quadCount below to accomodate
// our Array indexing which must start at 0.
quadrantColors[quadCount - 1] = oldAvg;
// increment our quadrant counter by 1.
quadCount++;
}
}
Then from somewhere in your application I would initiate all this like so:
// We declare our array to handle 20 elements since
// the image will be dissected into 20 quadrants.
Color[] quadrantColors = new Color[20];
BufferedImage img = null;
// Fill our Color Array...
getQuadrantsColorAverages(quadrantColors, img);
// Let's see what we managed to get....
for (int i = 0; i < quadrantColors.length; i++) {
Color clr = quadrantColors[i];
int red = clr.getRed();
int green = clr.getGreen();
int blue = clr.getBlue();
System.out.println("The average color for Quadrant #" +
(i + 1) + " is: RGB[" + red + "," + green + "," + blue + "]");
}
Well...that's it QQCompi. I hope it sort of helps you out a little.

JPanel not updating

I have a really odd problem where the JPanel I am making does not update every time. This is the method I use to update a JPanel which contains the map of my game. Calling on MapView creates a JPanel with the map inside it. However, sometimes the map refreshes with all the tiles in it displaying properly, and sometimes it only shows the tile in the top right.
public void updateMap(char[][] map){
remove(mapView);
revalidate();
repaint();
mapView = new MapView(logic, oCreator, 240, 240, map);
mapView.setBounds(40, 80, 240, 240);
add(mapView);
}
And the method inside the MapPanel class that draws the map:
public JPanel addMapPanel(char[][] map){
int mapLength = map.length;
int mapHeight = map[0].length;
int tileSize = Math.min((length / mapLength), (height / mapHeight));
absoluteMapHeight = tileSize * mapHeight;
absoluteMapLength = tileSize * mapLength;
JPanel mapFrame = new JPanel(new GridLayout(mapHeight, mapLength));
mapFrame.setBackground(oCreator.getColorMedium());
int counter = 0;
for(int yIndex = 0; yIndex < mapHeight; yIndex++){
for(int xIndex = 0; xIndex < mapLength; xIndex++){
char charAtPos = map[xIndex][yIndex];
JPanel tile;
//A tile is just a JPanel with the correct background.
switch(charAtPos){
//TODO Colours are placeholders.
case '.':
tile = createMapTile(Color.DARK_GRAY, tileSize);
break;
case '#':
tile = createMapTile(Color.black, tileSize);
break;
case 'G':
tile = createMapTile(Color.yellow, tileSize);
break;
case 'P':
tile = createMapTile(Color.MAGENTA, tileSize);
break;
case 'E':
tile = createMapTile(Color.RED, tileSize);
break;
default:
tile = createMapTile(Color.blue, tileSize);
}
mapFrame.add(tile);
counter++;
}
}
System.out.println(counter);
return mapFrame;
}
Sometimes it draws this: http://puu.sh/7Xcxo.png
And sometimes it draws the whole map: http://puu.sh/7XcAm.png
I am not sure why.

memory game java [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
memory game graphics java
I'm coding a program that is a 4x4 memory game. Within these 16 boxes will be a pair of integers from 0-7. I have all of that already randomized and coded correctly. Now I'm trying to figure out how to pair the colors with the corresponding integers each time the mouse clicks over the box.
Here most of the code. I know the logic for this game isn't started yet, but I'm more focused on the displayHit method and setColor method right now. Just posting the whole code because maybe I messed up somewhere else.
/*Sets the background of your memory board to black*/
public void init()
{
setSize(400,400);
setBackground(Color.BLACK);
buildBoard(4);
}
/*This is main in java applets
You may need to add (not change) a couple things in this method
*/
public void paint(Graphics canvas)
{
if(firstRun) //for the first run we need to build our random board
{
print2DArray(board);
buildBoard(4);
firstRun = false;
}
else // once our board is built we will display the game
{
displayGame(canvas);
if (mouseClicked) // if the mouse has been clicked
{
displayHit(canvas);//find which box the user clicked
mouseClicked = false;
}
}
}
/*
DO NOT change this method
determines if the mouse has been pressed
sets x and y Mouse to the location of the mouse arrow
redraws the image
*/
public boolean mouseDown(Event e, int x, int y )
{
mouseClicked = true;
xMouse = x;
yMouse = y;
repaint();
return true;
}
/*DO NOT change this method
redraws the scene
*/
public void update ( Graphics g )
{
paint(g);
}
/*
pre: none
post: build an array that holds the memory values for a board of size x size
the board will hold two of each int from 0 to size randomly placed in the array
*/
public void buildBoard(int s)
{
int a = 4;
for (int row = 0; row < a; row++)
for (int column = 0; column < a; column++)
{
board[row][column] = count++ % 8;
}
for(int row = 0; row < 4; row++)
for(int column = 0; column < 4; column ++)
{
int x = (int)Math.floor(Math.random()*4);
int y = (int)Math.floor(Math.random()*4);
temp = board[row][column];
board[row][column] = board[x][y];
board[x][y] = temp;
}
}
public static void print2DArray(int[][] arr)
{
for (int row = 0; row < arr.length; row++)
{
for (int col = 0; col < arr[row].length; col++)
{
System.out.print(arr[row][col] + " ");
}
System.out.println();
}
}
public void displayGame(Graphics canvas)
{
canvas.setColor(Color.WHITE);
for(int i =0; i < 400; i+= WIDTH)
for(int j = 0; j < 400; j+= WIDTH)
canvas.drawRect(i, j, WIDTH, WIDTH);
}
/*
Pre: xMouse and yMouse have been initialized
Post: A circle is displayed in the correct box on the screen
Currently the circle is displayed at the mouse location
*/
public void displayHit(Graphics g)
{
setColor(g);
centerHit(xMouse, xMouse);
g.fillOval(xMouse, yMouse, 40, 40);
}
public void setColor(Graphics g)
{
switch(temp)
{
case 0: g.setColor(Color.RED);
break;
case 1: g.setColor(Color.GREEN);
break;
case 2: g.setColor(Color.BLUE);
break;
case 3: g.setColor(Color.ORANGE);
break;
case 4: g.setColor(Color.CYAN);
break;
case 5: g.setColor(Color.MAGENTA);
break;
case 6: g.setColor(Color.PINK);
break;
case 7: g.setColor(Color.YELLOW);
break;
}
}
public void centerHit(int centerX, int centerY)
{
{
if ((xMouse > 0) && (xMouse <=100))
xMouse = 33;
else if ((xMouse > 100) && (xMouse <=200))
xMouse = 133;
else if ((xMouse > 200) && (xMouse <=300))
xMouse = 233;
else if ((xMouse > 300) && (xMouse <=400))
xMouse = 333;
}
{
if ((yMouse > 0) && (yMouse <=100))
yMouse = 33;
else if ((yMouse > 100) && (yMouse <=200))
yMouse = 133;
else if ((yMouse > 200) && (yMouse <=300))
yMouse = 233;
else if ((yMouse > 300) && (yMouse <=400))
yMouse = 333;
}
}
}
It looks like temp is never changed once buildBoard is called. Which is why you always see the same colour.
this code is fine
public void setColor(Graphics g)
{
switch(temp)
{
case 0: g.setColor(Color.RED);
break;
case 1: g.setColor(Color.GREEN);
break;
case 2: g.setColor(Color.BLUE);
break;
case 3: g.setColor(Color.ORANGE);
break;
case 4: g.setColor(Color.CYAN);
break;
case 5: g.setColor(Color.MAGENTA);
break;
case 6: g.setColor(Color.PINK);
break;
case 7: g.setColor(Color.YELLOW);
break;
}
}
but you are not setting temp anywhere in the code to choose which case/colour you want to use. you need to change temp when you click on a square either by hardcoding it or randomly generating a number between 0-7 to set the colour randomly

Categories