Results 1 to 2 of 2
  1. #1
    HbJgd's Avatar
    HbJgd is offline Member
    Join Date
    Mar 2011
    Location
    Doesn't matter
    Posts
    10
    Rep Power
    0

    Default Need help adding an actionListener

    Hello,
    I currently need help with my code in adding an ActionListener that would change each of these rectangles in the grid from either yellow to gray or vice-versa. In order to change these I need to change the number from 1 to - 1, or from -1 to 1. I have no clue on how to use an action Listener even though I've been told it's fairly easy.

    yellow or 1 = ON
    gray or -1 = OFF

    If you could help me out it would be greatly appreciated.

    Java Code:
    /*
     	@author: Jack Davis
     	Objective of the Game:
     		To systematically change all of the lights to on.
     		When you select a square it will turn all of the lights around that light
     		not diagonal just beside it to either On or Off, according to it's previous state.
     		There is a reset Button on the bottom right corner of the game that re-Initializes 
     		the game and allows you to start over with all of the lights in the Off state of being.
     		The two counters on the right hand side of the screen display the number of On and Off
     		lights that are currently on the board.
     		When you have turned all 25 of the lights to the On orientation, there will be a message
     		that pops up and says "Congratulations!".
     */
    
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    import java.net.*;
    
    public class EveryLight extends Applet
    {
    	private final int ON = 1;
    	private final int OFF = -1;
    	private final int WIDTH = 200;
    	public int light[][];
    	public int countOn = 0;
    	public int countOff = 0;
    	public int end = 0;
    	
    	
    	//Starts the game by setting the background to white and initializing all
    	//of the lights in the grid.
    	public void startGame()
    	{	
    		initializeLights();
    		setBackground(Color.white);
    	}
    	
    	//Set all of the lights in the grid to OFF for the beginning of the game,
    	//and then counts the lights that are ON or OFF.
    	public void initializeLights()
    	{
    		light = new int[5][5];
    		for (int l = 0; l < 5; l++)
    		{
    			for(int i = 0; i < 5; i++)
    			{
    				light[l][i] = OFF;
    			}
    		}
    		countOnAndOff();
    	}
    	
    	//Makes the board, Initializes the lights once again, and then draws
    	//the lights in order.
    	public void paint(Graphics g) 
    	{
    		initializeLights();
            g.setColor(Color.blue);
            g.fill3DRect(210, 165, 60, 20, true);
            g.setColor(Color.red);
            g.drawString("RESTART", 214, 180);
            makeBoard(g);
            for (int i = 0; i < 5; i++)
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      if (light[i][j]  != 0) 
                      {
                           drawLight(i, j, g);
                      }
                 }
            }
            drawOnAndOffCounters(g);
       }
    	
    	//Sets up the lights to be approximately 200 wide, and assigns each light to
    	//be either gray or yellow according to what it currently is set as.
    	public void drawLight(int column, int row, Graphics g) 
    	{
            if (light[row][column] == ON) 
            {
                 g.setColor(Color.yellow);
            } 
            else if (light[row][column] == OFF) 
            {
                 g.setColor(Color.gray);
            }
            g.fillRect(column * WIDTH / 5 + 2, row * WIDTH / 5 + 2, WIDTH / 5 - 3, WIDTH / 5 - 3);
       }
    	
    	//Creates the board by drawing all of the necessary lines.
    	public void makeBoard (Graphics graph)
    	{
    		graph.setColor(Color.blue);
    		graph.drawLine(0, 0, 0, WIDTH);
    		graph.drawLine(WIDTH, 0, WIDTH, WIDTH);
    		graph.drawLine(0, 0, WIDTH, 0);
    		graph.drawLine(0, WIDTH, WIDTH, WIDTH);
    		for (int i = 1; i < 5; i++)
    		{
    			graph.drawLine(WIDTH * i / 5, 0, WIDTH * i / 5, WIDTH);
    			graph.drawLine(0, WIDTH * i / 5, WIDTH, WIDTH * i / 5);
    		}
    	}
    	
    	//Counts and total the on and off lights.
    	public void countOnAndOff ()
    	{
    		countOn = 0;
            countOff = 0;
            for (int i = 0; i < 5; i++) 
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      if(light[i][j] == ON) 
                    	  countOn++;
                      if(light[i][j] == OFF) 
                    	  countOff++;
                 }
            }
            if (countOn == 25) 
            {
                 winGame(getGraphics());
            }
    	}
    	
    	//Wins the game and displays Congratulations!
    	public void winGame (Graphics g)
    	{
    		update(getGraphics());
            for (int i = 0; i < 5; i++) 
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      light[i][j] = OFF;
                      update(getGraphics());
                 }
            }
            for (int i = 0; i < 5; i++) 
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      light[i][j] = ON;
                      update(getGraphics());
                 }
            }
            g.setColor(Color.yellow);
            g.fillRect(65, 98, 80, 10);
            g.setColor(Color.black);
            g.drawString("Congratulations!!", 66, 106);
            g.setColor(Color.red);
            g.drawString("Congratulations!!", 65, 105);
            end = 1;
    	}
    	
    	//Displays the number of on and off lights and sets up the LightOn, and 
    	//LightOff displays.
    	public void drawOnAndOffCounters (Graphics graph)
    	{
    		graph.setColor(Color.white);
            graph.fill3DRect(WIDTH+15, 50, 30,20, false);
            graph.fill3DRect(WIDTH+15, 110, 30,20, false);
            graph.setColor(Color.gray);
            graph.fill3DRect(WIDTH+5, 85, 20,20, true);
            graph.setColor(Color.yellow);
            graph.fill3DRect(WIDTH+5, 20, 20,20, true);
            graph.setColor(Color.black);
            graph.drawString("LightOn", WIDTH+30, 35);
            graph.drawString("LightOff", WIDTH+30, 100);
            graph.drawString(Integer.toString(countOn), WIDTH+20, 65);
            graph.drawString(Integer.toString(countOff), WIDTH+20, 125);
    	}
    	
    	//Overrides the Graphic Class mouseUp method.
        public boolean mouseUp(Event event, int x, int y) 
        {
             if (end == 0) 
             {
                  init();
                  update(getGraphics());
             } 
             else 
             {
                 if (x > 210 && x < 270 && y > 165 && y < 185) 
                 {
                	 init();
                      update(getGraphics());
                 }
                 int column = (int)(x / (WIDTH / 4 + 1));
                 int row    = (int)(y / (WIDTH / 4 + 1));
             	 light[row][column] = -light[row][column];
             	  
             	 if(row < 4)
                   	   light[row+1][column] = -light[row+1][column];
             	 if(row > 0)
                       light[row-1][column] = -light[row-1][column];
             	 if(column < 4)
                       light[row][column+1] = -light[row][column+1];
                 if(column > 0)
                       light[row][column-1] = -light[row][column-1];
                  
                 countOnAndOff();
                 if (end == 1) 
                 {
                      update(getGraphics());
                 }
             }
             return true;
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    There is a discussion in the Java tutorial on how to use listeners with examples>
    Go to this site and Find Listener and read up
    The Really Big Index

    The code you have posted must be VERY OLD and uses many poor coding techniques. Especially the usage of graphics.
    Last edited by Norm; 06-12-2011 at 05:52 PM.

Similar Threads

  1. Need some help with ActionListener error
    By DaBananaboat in forum New To Java
    Replies: 2
    Last Post: 05-04-2011, 01:29 PM
  2. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  3. adding a actionListener but not using inner class
    By hariza in forum AWT / Swing
    Replies: 2
    Last Post: 10-08-2010, 07:24 AM
  4. How to access the ActionListener
    By jboy in forum New To Java
    Replies: 3
    Last Post: 10-15-2009, 06:04 PM
  5. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM

Posting Permissions

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