Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Java programme to colour in a grid

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java programme to colour in a grid

    Hi this is my first post here.

    basically i want a programme to do teh following:

    1)create a 6x6 grid.
    2)ask the user to choose from 6 colours to fill a square
    3)test to see if one row or column has 5 colours in and autocomplete with the final colour. also it should not allow the user to click on any squares in that row or column if it is complete.
    4) if a square has already been filled in and gets clicked again it should go back to blank/empty colour.
    5) if a square is going to be coloured the same as one already in that row/column the square should be left blank and an error message displayed saying why and allowing the user to try again
    the starting pattern is to be read in from a file i.e.
    R1 C2 Green
    where R1 is 1st row and C2 is second column.


    I can create a 6x6 grid and open up a menu bar to ask for a colour but i can not get the button pressed to go into that colour. i also want the second window that opens to close when a colour is picked but dont know how to do that.

    I can get to grips with file reading but not the testing of rows columns and colouring them in from button clicks etc.

    the code belwo is what i have so far

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class ButtonGrid extends JFrame implements ActionListener {

    private int numClicks = 0;

    public static void main(String[] args) {
    int rowsGrid = 6;
    int colsGrid = 6;
    int sizeGrid = 600;

    ButtonGrid grid = new ButtonGrid(rowsGrid, colsGrid);
    grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    grid.setPreferredSize(new Dimension(sizeGrid, sizeGrid));
    grid.pack();
    grid.setLocationRelativeTo(null);
    grid.setVisible(true);
    }//end main

    public ButtonGrid(int rows, int cols) {
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(rows, cols));
    for(int j =0; j<rows; j++)
    for (int i = 0; i < cols; i++) {
    JButton button = new JButton("");
    button.addActionListener(this);
    button.setBackground(new Color(100,100,100));
    pane.add(button);
    }
    }//end buttonGrid

    public void actionPerformed(ActionEvent e) {
    numClicks++;
    int rows = 6;
    int cols = 6;
    int size = 300;
    ColourChoice choice = new ColourChoice(rows, cols);
    choice.setPreferredSize(new Dimension(size/2, size));
    choice.pack();
    choice.setVisible(true);
    }


    public class ColourChoice extends JFrame{

    public ColourChoice(int rows, int cols) {
    String[] colourName ={"RED","GREEN","BLUE","YELLOW","PURPLE","BROWN" };
    Color[] choices = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, new Color(102, 0, 102), new Color(102, 51, 0)};
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(rows, cols));
    for (int i = 0; i < choices.length; i++) {
    JButton button = new JButton(""+colourName[i]);
    button.setForeground(new Color(100,100,100));
    button.setBackground(choices[i]);
    pane.add(button);
    }
    }

    }//end choice class

    }//end class

    it creates a 6x6 grid and each time a button is pressed a new window opens with the colour choices.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Look at using a JDialog to prompt the user for his choices.

    Putting the button objects in an arraylist might be useful.
    The event object passed to the listener contains a reference to the source of the event and could be used to call its methods.
    Last edited by Norm; March 25th, 2012 at 11:34 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Hi ok thanks for that. I have now managed to get the squares to change colour. How will i now make them go back to default if they are clicked and they have been coloured in? thanks

    here is the code using at the moment

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class ButtonGridv2 extends JFrame implements ActionListener {
     
    	public static void main(String[] args) {
    		int rowsGrid = 6;
    		int colsGrid = 6;
    		int sizeGrid = 600;
     
    		ButtonGridv2 grid = new ButtonGridv2(rowsGrid, colsGrid);
    		grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		grid.setPreferredSize(new Dimension(sizeGrid, sizeGrid));
    		grid.pack();
    		grid.setLocationRelativeTo(null);
    		grid.setVisible(true);
     
    	}//end main  
     
    	public ButtonGridv2(int rows, int cols) {
    		int rowsGrid = 6;
    		int colsGrid = 6;
    		int size = 600;
    		JButton[][] buttons;
    		buttons = new JButton[rowsGrid][colsGrid];
    		Container pane = getContentPane();
    		pane.setLayout(new GridLayout(rows, cols));
    		for(int j =0; j<rows; j++)
    		for (int i = 0; i < cols; i++) {
    			buttons[j][i] = new JButton("");
    			buttons[j][i].setOpaque(true);
    			buttons[j][i].addActionListener(this);
    			buttons[j][i].setBackground(Color.GRAY);
    			pane.add(buttons[j][i]);
    			System.out.println(""+j+""+i);
    		}
    	}//end buttonGrid
     
    	public void actionPerformed(ActionEvent e) { 
     
    		int rows = 6;
    		int cols = 1;
    		int size = 300;
    		ColourChoicev2 choice = new ColourChoicev2(rows, cols, (JButton) e.getSource());
    		choice.setPreferredSize(new Dimension(size/2, size));
    		choice.pack();
    		choice.setVisible(true);
    	}//end actionperformed       
     
    	class ColourChoicev2 extends JFrame {
     
    		public JButton redButton;
    		public JButton yellowButton;
    		public JButton blueButton;
    		public JButton greenButton;
    		public JButton purpleButton;
    		public JButton brownButton;
    		public JPanel panel;
    		private JButton clickedButton;
     
    		public ColourChoicev2(int rows, int cols, JButton button)
    		{
    			redButton = new JButton("Red");
    			yellowButton = new JButton("Yellow");
    			blueButton = new JButton("Blue");
    			greenButton = new JButton("Green");
    			purpleButton = new JButton("Purple");
    			brownButton = new JButton("Brown");
     
    			clickedButton = button;
     
    			redButton.addActionListener(new RedButtonListener());
    			yellowButton.addActionListener(new YellowButtonListener());
    			blueButton.addActionListener(new BlueButtonListener());
    			greenButton.addActionListener(new GreenButtonListener());
    			purpleButton.addActionListener(new PurpleButtonListener());
    			brownButton.addActionListener(new BrownButtonListener());
     
    			panel = new JPanel();
     
    			panel.add(redButton);
    			panel.add(yellowButton);
    			panel.add(blueButton);
    			panel.add(greenButton);
    			panel.add(purpleButton);
    			panel.add(brownButton);
    			add(panel);
    			setVisible(true);
    		}
     
    		public class RedButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{        	
    				clickedButton.setBackground(Color.RED); 
    				dispose();
    			}
    		}
     
    		private class YellowButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{
    				clickedButton.setBackground(Color.YELLOW); 
    				dispose(); 
    			}
    		}
     
    		private class BlueButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{   
    				clickedButton.setBackground(Color.BLUE);    
    				dispose();          
    			}
    		}
     
    		private class GreenButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{    	    	
    				clickedButton.setBackground(Color.GREEN);   
    				dispose();
    			}
    		}
     
    		private class PurpleButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{    	    	
    				clickedButton.setBackground(new Color(102,0,102));
    				dispose();
    			}
    		}
     
    		private class BrownButtonListener implements ActionListener
    		{
    			public void actionPerformed(ActionEvent e)
    			{    	    	
    				clickedButton.setBackground(new Color(102,51,0));
    				dispose();
    			}
    		}
    	}//end colour choice
    }//end class

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    make them go back to default if they are clicked and they have been coloured in
    When do you want them to go back to the default? Is it when some event happens?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by Norm View Post
    When do you want them to go back to the default? Is it when some event happens?
    Hi I have managed to get the colouring in part to work but i am now confused on how to do the file reading. below is the new code that colours squares and SHOULD fill some in from the start. the file will be of the form
    R1 C2 Green
    R2 C3 Red
    etc

    but i do not know how to modify the filereader to colour in the correct square.

    here is the new code

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.util.*;
    import java.io.*;
     
    public class ButtonGridv3 extends JFrame implements ActionListener 
    {
        private ColourChoicev2 choice = null;
        private JButton[][] buttons = new JButton[6][6];
     
        public static void main(String[] args) 
        {
        	int rowsGrid = 6;
        	int colsGrid = 6;
        	int sizeGrid = 600;
        	ButtonGridv3 grid = new ButtonGridv3(rowsGrid, colsGrid);
        	grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	grid.setPreferredSize(new Dimension(sizeGrid, sizeGrid));
        	grid.pack();
        	grid.setLocationRelativeTo(null);
        	grid.setVisible(true);
     
        }//end main  
     
        public ButtonGridv3(int rows, int cols) 
        {
        	int rowsGrid = 6;
        	int colsGrid = 6;
        	int sizeGrid = 600;
        	Container pane = getContentPane();
        	pane.setLayout(new GridLayout(rows, cols));
        	for(int j =0; j<rows; j++)
        	    for (int i = 0; i < cols; i++) 
        	    {
        	    	buttons[j][i] = new JButton("");
        	    	buttons[j][i].setOpaque(true); 
        	    	buttons[j][i].setName("");      
        	    	buttons[j][i].addActionListener(this);      
        	    	buttons[j][i].setBackground(Color.darkGray);
        	    	pane.add(buttons[j][i]);
        	    }
        	    readStartFile("start.txt");
        }//end buttonGrid
     
        public void actionPerformed(ActionEvent e) 
        {
        	JButton button = (JButton) e.getSource();
     
        	//if ((button.getName()).equals(""))  //wont open colour window if button clicked is not default
        	//{
     
        	// this is to check if any previous window is showing or not
        	// if one found , dispose that and open a new one
        	// for the new button.
        	if ( choice != null && choice.isShowing())
        	    choice.dispose();
        	int rows = 6;
        	int cols = 1;
        	int size = 300;
        	choice = new ColourChoicev2(rows, cols, (JButton) e.getSource());
        	choice.setPreferredSize(new Dimension(size/3, size));
        	choice.pack();
        	choice.setVisible(true);
        	if ((button.getName()).equals("ColouredButton"))    
        	{
        	    button.setBackground(Color.darkGray);
        	    button.setName("");
        	    //}
        	}
        	/*else if ((button.getName()).equals("ColouredButton")) 
        	{
        	button.setBackground(Color.darkGray);
        	}*/
     
        }//end actionperformed    
     
        public void readStartFile(String fileName){
     
        	try{
        	    // Open the file that is the first 
        	    // command line parameter
        	    FileInputStream fstream = new FileInputStream("start.txt");
        	    // Get the object of DataInputStream
        	    DataInputStream in = new DataInputStream(fstream);
        	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
        	    String strLine;
        	    //Read File Line By Line
        	    while ((strLine = br.readLine()) != null)   {
        	    	System.out.println(strLine);
        	    }
        	    //Close the input stream
        	    in.close();
        	}catch (Exception e){//Catch exception if any
        	    System.err.println("Error: " + e.getMessage());
        	}
        }
     
        class ColourChoicev2 extends JFrame 
        {
    	public JButton redButton;
    	public JButton yellowButton;
    	public JButton blueButton;
    	public JButton greenButton;
    	public JButton purpleButton;
    	public JButton brownButton;
    	public JPanel panel;
    	private JButton clickedButton; 
     
    	public ColourChoicev2(int rows, int cols, JButton button)
    	{
    	    redButton = new JButton("Red");
    	    yellowButton = new JButton("Yellow");
    	    blueButton = new JButton("Blue");
    	    greenButton = new JButton("Green");
    	    purpleButton = new JButton("Purple");
    	    brownButton = new JButton("Brown");
     
    	    clickedButton = button;
     
    	    redButton.addActionListener(new RedButtonListener());
    	    yellowButton.addActionListener(new YellowButtonListener());
    	    blueButton.addActionListener(new BlueButtonListener());
    	    greenButton.addActionListener(new GreenButtonListener());
    	    purpleButton.addActionListener(new PurpleButtonListener());
    	    brownButton.addActionListener(new BrownButtonListener());
     
    	    panel = new JPanel();
     
    	    panel.add(redButton);
    	    panel.add(yellowButton);
    	    panel.add(blueButton);
    	    panel.add(greenButton);
    	    panel.add(purpleButton);
    	    panel.add(brownButton);
    	    add(panel);
    	}
     
    	private class RedButtonListener implements ActionListener
    	{
    	    public void actionPerformed(ActionEvent e)
    	    {
    	    	clickedButton.setBackground(Color.RED);
    	    	clickedButton.setName("ColouredButton");
    	    	dispose();
    	    }
    	}
     
    	private class YellowButtonListener implements ActionListener
    	{
    	    public void actionPerformed(ActionEvent e)
    	    {             
    	    	clickedButton.setBackground(Color.YELLOW);
    	    	clickedButton.setName("ColouredButton");
    	    	dispose();
    	    }
    	}
     
    	private class BlueButtonListener implements ActionListener
    	{
    	    public void actionPerformed(ActionEvent e)
    	    { 
    	    	clickedButton.setBackground(Color.BLUE);    
    	    	clickedButton.setName("ColouredButton");
    	    	dispose();          
    	    }
    	}
     
    	private class GreenButtonListener implements ActionListener
    	{
    	    public void actionPerformed(ActionEvent e)
    	    {
    	    	clickedButton.setBackground(Color.GREEN);   
    	    	clickedButton.setName("ColouredButton");
    	    	dispose();
    	    }
    	}
     
    	private class PurpleButtonListener implements ActionListener
    	{
    	    public void actionPerformed(ActionEvent e)
    	    {
     
    	    	clickedButton.setBackground(new Color(102,0,102));
    	    	clickedButton.setName("ColouredButton");
    	    	dispose();
    	    }
    	}
     
    	private class BrownButtonListener implements ActionListener
    	{
    	    public void actionPerformed(ActionEvent e)
    	    {
    	    	clickedButton.setBackground(new Color(102,51,0));
    	    	clickedButton.setName("ColouredButton");
    	    	dispose();
    	    }
    	}
        }
    }//end class

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    How do you determine the "correct" square to color?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Hi I will be determining the correct square by trying to parse the row and column info from the file as well as the colour and then using

    buttons[row][col].setBackground(colour)

    where row = the number after R
    column = number after C
    and colour = string after R1 C2 such as Green

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    You could use the String class's split method on the input String, then use the substring method on each substring to remove the leading R or C character. Then use the Integer class's parse method to convert the String to an int.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by Norm View Post
    You could use the String class's split method on the input String, then use the substring method on each substring to remove the leading R or C character. Then use the Integer class's parse method to convert the String to an int.
    ok thanks. so would that mean i would do something like

    while ((strLine = br.readLine()) != null)   {
                    strLine.split(" ");
                    strLine = strLine.substring(0, strLine.length() - 1)
     
                }

    thanks

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    You should read the API doc for the String class to see how to use its methods.
    For example the split method returns a value that you will use. Your code ignores what is returned.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by Norm View Post
    You should read the API doc for the String class to see how to use its methods.
    For example the split method returns a value that you will use. Your code ignores what is returned.
    ok so it should be something like

    while ((strLine = br.readLine()) != null)   {
                    strLine.split(" ");
                    strLine.removeCharAt(strLine[0]);
     
                }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    Not yet. You are still ignoring what the split method returns.

    Did what you posted compile? Try compiling before posting to see if the compiler likes your code.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by Norm View Post
    Not yet. You are still ignoring what the split method returns.

    Did what you posted compile? Try compiling before posting to see if the compiler likes your code.
    i removed the bottom line strLine.removeCharAt(strLine[0]);
    and it compiles fine.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    What happens to the value returned by the split() method?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by Norm View Post
    What happens to the value returned by the split() method?
    sorry i am not too sure . Can not get any code to compile on my laptop at the minute after trying to install jdk 7. no matter what program i try and run i just get the error could not find or load main class programName

  16. #16
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by why_always_me View Post
    sorry i am not too sure . Can not get any code to compile on my laptop at the minute after trying to install jdk 7. no matter what program i try and run i just get the error could not find or load main class programName
    managed to get jdk 7 working now. still slightly confused by the split() method though. is there any easier way of detecting the R,C and colour?

    thanks

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    The split() method would be the easiest.
    Other choices would be the indexOf and substring methods.

    To see what split() does, try writing a 3 line program with a sample input String. Call the split() method and print out contents of the returned array
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by Norm View Post
    The split() method would be the easiest.
    Other choices would be the indexOf and substring methods.

    To see what split() does, try writing a 3 line program with a sample input String. Call the split() method and print out contents of the returned array
    Hi I have now found out how to use the split method correctly. here is my piece of code used for an example as you suggested.
    public class SplitExample{
     
      public static void main(String args[]){
     
           String str = "one two three";
     
      /* delimiter */
      String delimiter = " ";
      /* given string will be split by the argument delimiter provided. */
      String[] temp = str.split(delimiter);
      /* print substrings */
      for(int i =0; i < temp.length ; i++)
        System.out.println(temp[i]);
      }
    }

    so how would i now get the second part of the first word in my case the 1 and the second part of the second word?
    thanks
    Last edited by why_always_me; March 28th, 2012 at 06:23 PM.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    Use the substring method to get parts of a String.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Mar 2012
    Posts
    21
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java programme to colour in a grid

    Quote Originally Posted by Norm View Post
    Use the substring method to get parts of a String.
    Hi so i tried using if statements to set my rNum variable and it never changes from 0? any ideas?

     public void readStartFile(String fileName){
         	 int rNum=0;
         	 int cNum=0;
     
            try{
                // Open the file that is the first 
                // command line parameter
                FileInputStream fstream = new FileInputStream("start.txt");
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)   {
                    String[] temp = strLine.split(" ");
                    if(temp[0] == "R1"){
                    rNum =1;
                    System.out.println(""+rNum);
                    }else if(temp[0] == "R2"){
                    rNum =1;
                    }else if(temp[0] == "R3"){
                    rNum =2;
                    }else if(temp[0] == "R4"){
                    rNum =3;
                    }else if(temp[0] == "R5"){
                    rNum =4;
                    }else if(temp[0] == "R6"){
                    rNum =5;
                    }
                    System.out.println(""+rNum);
     
                }
                //Close the input stream
                in.close();
            }catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        }

    thanks

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java programme to colour in a grid

    You should use the equals() method do compare Strings not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to call a java programme from an applet?
    By prativa in forum Java Applets
    Replies: 2
    Last Post: December 19th, 2011, 11:01 AM
  2. HELP! HOW TO PRINT A GRID IN JAVA (NO GUI...)
    By imicrothinking in forum Java Theory & Questions
    Replies: 0
    Last Post: October 19th, 2011, 08:39 AM
  3. Java jar programme with a microcontroller
    By bczm8703 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 11th, 2011, 08:01 AM
  4. Replies: 10
    Last Post: November 16th, 2010, 12:12 AM
  5. access cisco router with java programme
    By vigneswara in forum Java Theory & Questions
    Replies: 1
    Last Post: May 11th, 2010, 01:36 AM