Results 1 to 3 of 3
  1. #1
    jdevmiller is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Unhappy Objects in Array Not Changing Color

    Hello, I am in the process of learning Java and am currently working with arrays. I am having some issues with the object arrays I am working with. I am trying to use an array of GOvals (the circle class from the AWT Java package). I know this may be an outdated library but it is required with the textbook I am using.
    Anyways, I am trying to make the game "21" (see Games people play | plus.maths.org for basic rules). I am creating an AI in the game to compete against the human player. I want to indicate that the computer is making a move by turning the colors of the coins displayed onscreen from red to yellow before deleting them. My problem is that the coins do not change color until after the makeMove() method finishes, after it also has called the method to remove the coins. This results in a pause (for the color change to be noticed by the player), and then the coins being removed without the color being changed. If I remove the removeCoins(startIndex) method call, the colors of the coins change no problem. Should I be using an ArrayList instead of an array or is there some other solution to this problem?

    NOTE: "startIndex" stores the index of the selected coin in a line of coins, setting the coin to remove last.
    "lastIndex" keeps track of the last visible coin in the array.
    These are both instance variables.

    Java Code:
      //Removes the specified number of coins from the end of the array
    	//@param int startIndex removes the coin at this index and all subsequent coins.
    	//@param boolean computerMove changes the color of the coins before removing them to make it obvious to the player what move the computer is making.
    	private void makeMove(int startIndex, boolean computerMove){		
    		if (computerMove == true) changeCoinColor(startIndex, Color.YELLOW);
    		removeCoins(startIndex);
    		lastIndex = startIndex - 1;
    		//TODO problem here. Coin does not change color until after method finishes
    	}
    	
    	//Changes the colors of the coins
    	private void changeCoinColor(int startIndex, Color color) {
    		for (int i = startIndex; i <= lastIndex; i++){
    			coinArray[i].setFillColor(color);
    		}		
    		pause(500); //Pauses to let the player notice that the computer has made a move
    	}
    	
    	//Removes the coins
    	private void removeCoins(int startIndex) {
    		for (int i = startIndex; i <= lastIndex; i++) {
    			remove(coinArray[i]);
    		}
    	}
    Last edited by jdevmiller; 10-22-2013 at 03:53 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,039
    Rep Power
    6

    Default Re: Objects in Array Not Changing Color

    It's not really possible to know what is going on because you have only provided a fragment of the application. Providing an Short, Self Contained, Correct Example (less than 100 lines) that demonstrates the problem would help. You might try issuing a repaint() before the pause(500) statement.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jdevmiller is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Objects in Array Not Changing Color

    OK here is the code. Sorry it is pretty condensed but not incredibly small as pretty much all of the code is necessary to properly display the code. The computer AI is not coded yet so I just set the first if statement in "makeMove()" (around line 75) to true so it always runs (for debugging). I tried using a repaint() command but it did not work. If you need the acm library you can find it here acm.jar.
    Java Code:
    import acm.program.*;
    import acm.graphics.*;
    import java.awt.Color;
    import java.awt.event.*;
    
    public class nim extends GraphicsProgram{
    	
    	//Named constants
    	public static final int NUM_COINS = 21;
    	public static final int BLINK_MS = 750;
    	public static final int NUM_BLINKS = 10;
    	
    	public void run() {
    		setupGame();
    		addMouseListeners();
    	}
    	
    	//Sets up the game by displaying the coins on the screen
    	private void setupGame() {
    		double coinSpacing = getWidth() / (NUM_COINS +1);
    		double coinDiameter = coinSpacing * .9;
    		double coinY = (getHeight() / 2) - (coinDiameter / 2);
    		double coinX = coinDiameter - (coinDiameter / 2);
    		for (int i = 0; i < NUM_COINS; i++) {
    			coinArray[i] = new GOval(coinX, coinY, coinDiameter, coinDiameter);
    			coinArray[i].setFilled(true);
    			coinArray[i].setFillColor(Color.RED);
    			add(coinArray[i]);
    			coinX += coinSpacing;
    		}		
    	}
    	
    	//Responds to clicking on circles
    	public void mouseClicked(MouseEvent e) {
    		if (gameOver == false) {
    			lastClicked = new GPoint(e.getX(), e.getY());
    			clickedObject = getElementAt(lastClicked);
    			/* ObjectIndex is Used to store the index of the object that was clicked, 
    			which is then checked with the number of objects remaining to see if it is a valid move. */
    			int objectIndex = lastIndex + 1;
    			if (clickedObject != null) {
    				objectIndex = getObjectIndex(clickedObject);
    			}
    			if (objectIndex < 0) displayErrorMessage(01); //For debugging purposes
    			/* Checks the index number against number of coins left to determine
    			 * if a valid choice was made */
    			if (objectIndex > lastIndex - 3) {
    				makeMove(objectIndex, true);
    				playerJustMoved = true;
    			} 
    			if (playerJustMoved == true) computerMove();
    		}
    		
    	}
    	
    	//Checks to see if the player has won or lost
    	private void checkForWin() {
    		if (lastIndex == 0 && playerJustMoved == true) {
    			displayWinMessage();
    			gameOver = true;
    		}
    		
    		if (lastIndex == 0 && playerJustMoved == false) {
    			displayLoseMessage();
    			gameOver = true;
    		}
    	
    	}
    	
    	
    	//Removes the specified number of coins from the end of the array
    	//@param int startIndex removes the coin at this index and all subsequent coins.
    	//@param boolean computerMove changes the color of the coins before removing them to make it obvious to the player what move the computer is making.
    	private void makeMove(int startIndex, boolean computerMove){
    		if (computerMove == true) changeCoinColor(startIndex, Color.YELLOW);
    		removeCoins(startIndex);
    		lastIndex = startIndex - 1;
    		//TODO problem here. Coin does not change color until after method finishes
    	}
    	
    	//Changes the colors of the coins
    	private void changeCoinColor(int startIndex, Color color) {
    		for (int i = startIndex; i <= lastIndex; i++){
    			coinArray[i].setFillColor(color);
    		}		
    		repaint();
    		pause(500);
    	}
    	
    	//Removes the coins
    	private void removeCoins(int startIndex) {
    		for (int i = startIndex; i <= lastIndex; i++) {
    			remove(coinArray[i]);
    		}
    	}
    	
    	//Returns the index of the object selected
    	private int getObjectIndex(GObject obj) {
    		int indexNo = -1;
    		if (obj != null) {
    			for (int i = 0; i <= lastIndex; i++) {
    				if (obj == coinArray[i]) indexNo = i;
    			}
    		}
    		
    		return indexNo; 
    	}
    
    	//The AI code for the computer
    	private void computerMove() {
    		checkForWin();
    		playerJustMoved = false; // Records if the player has just made a move. Used in the checkForWin() mehtod.
    		//Only makes a move when the game is not over
    		if (lastIndex != 1 && lastIndex != 0) {
    			//TODO write the AI code - strategy: remove coins so there is a multiple of 4 left
    		}
    		checkForWin();
    	}
    	
    	//The message displayed for the loser
    	private void displayLoseMessage() {
    		display("I'm sorry, you have lost!");
    	}
    	
    	//The message displayed for the winner
    	private void displayWinMessage(){
    		display("Congratulations! You have won!");
    	}
    	
    	//Displays a selected error message
    	private void displayErrorMessage(int errorCode) {
    		switch (errorCode) {
    		case 01:
    			display("Error 01: object index not retrieved");
    			break;
    		default:
    			display("Unknown error!");
    			break;
    		}
    				
    	}
    	
    	
    	//Displays a label of text in the center of the window
    	private void display(String text) {
    		GLabel label = new GLabel(text);
    		double labelX = (getWidth() / 2) - (label.getWidth() / 2);
    		double labelY = (getHeight() / 4) - (label.getAscent() / 2);
    		label.setLocation(labelX, labelY);
    		add(label);		
    	}
    	
    	//Instance Variables 
    	private int lastIndex = NUM_COINS - 1;
    	GOval[] coinArray = new GOval[NUM_COINS];
    	private GPoint lastClicked;
    	private boolean playerJustMoved;
    	private GObject clickedObject;
    	private boolean gameOver = false;
    }
    Last edited by jdevmiller; 10-22-2013 at 05:37 AM.

Similar Threads

  1. Changing Image color
    By Frecow in forum Java 2D
    Replies: 0
    Last Post: 04-04-2011, 11:16 AM
  2. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 06:54 AM
  3. Changing text color in SWT
    By ourimaler in forum SWT / JFace
    Replies: 1
    Last Post: 06-02-2010, 02:08 PM
  4. Color-changing model
    By higuchi in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 08:29 AM
  5. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •