Component array and counter-controlled loop

I'm trying to do a program of a checker board

Page 1 of 1

5 Replies - 9060 Views - Last Post: 08 October 2009 - 05:09 AM Rate Topic: -----

#1 chave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-June 08

Component array and counter-controlled loop

Post icon  Posted 18 June 2008 - 05:21 AM

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

public class Checkerboard extends Frame implements ActionListener
{

	Panel boardPanel = new Panel();
	TextArea numberArea[] = new TextArea[16];
	Panel buttonPanel = new Panel();
	Button goBut = new Button("Go");
	Label startLabel = new Label("Start");
	Label stepLabel = new Label("Step");
	Label stopLabel = new Label("Stop");
	Panel inputPanel = new Panel();
	int start=0, stop=0, step=0;
	TextField startField = new TextField();
	TextField stepField = new TextField();
	TextField stopField = new TextField();


	public Checkerboard()
	{
		// set layouts for frame and two panels
		this.setLayout(new BorderLayout());
		boardPanel.setLayout(new GridLayout(4,4));
		inputPanel.setLayout(new GridLayout(2,3));
		buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

		// add components to board Panel
		for (int i = 0; i < numberArea.length; i++) {
			numberArea[i] = new TextArea(null, 3, 5, 3);
			numberArea[i].setText(Integer.toString(i));
			numberArea[i].setEditable(false);
			numberArea[i].setBackground(Color.WHITE);
			boardPanel.add(numberArea[i]);
		}

		// add components to input Panel
		inputPanel.add(startField);
		inputPanel.add(stepField);
		inputPanel.add(stopField);
		inputPanel.add(startLabel);
		inputPanel.add(stepLabel);
		inputPanel.add(stopLabel);

		//buttonPanel Component
		buttonPanel.add(goBut);

		// add panels to frame
		add(boardPanel, BorderLayout.NORTH);
		add(inputPanel, BorderLayout.CENTER);
		add(buttonPanel, BorderLayout.SOUTH);

		goBut.addActionListener(this);

		// overriding the windowClosing() method will allow the user to click the Close button
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

	}// end of constructor method

	public static void main(String[] args)
	{
		Checkerboard f = new Checkerboard();
		f.setBounds(50, 100, 300, 400);
		f.setTitle("Checkerboard Array");
		f.setVisible(true);
	}// end of main() method

	public void actionPerformed(ActionEvent e)
	{
		try
		{
			start = Integer.parseInt(startField.getText());
			step = Integer.parseInt(stepField.getText());
			stop = Integer.parseInt(stopField.getText());


		}catch(Exception exception)

		{
			return;
		}

		Object src = e.getSource();

		for (int x = start -1; x < stop; x = step)//you need to do the -1 because the array subscript values in Java all start
				//with zero, but humans use numbers starting with 1
				{
					//numberArea[x] = numberArea[start & start++].setBackground(Color.magenta);//..change the color for that square here

					if (src == goBut)
					{
					if (start >= 0 && start <= 15 && start < stop) {
						numberArea[x] = numberArea[start + start++].setBackground(Color.magenta);

					}
					else
					{
						JOptionPane
								.showMessageDialog(
										null,
										"You have entered an incorrect number or a number larger than 15. Please try again",
										"Error", JOptionPane.ERROR_MESSAGE);
					}

					if (step >= 0 && step <= 15 && start < stop) {
						numberArea[x] = numberArea[step + step++].setBackground(Color.yellow);
				}
			}
		}
	}
}











Hi guys, im struggling with this code. Its an assignment that i need to do. need to get coulours in the textboxes. If you use the following test data, in the start column 1, in the stop column 15, and in the step column 3. with this test data, box 1, 4, 7, 10, and 13 needs to be yellow. and the rest of the boxes needs to be magenta.
I get an error code of line 99 and 112. Please can you help me, and im a VERY BEGINNER

Is This A Good Question/Topic? 0
  • +

Replies To: Component array and counter-controlled loop

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,460
  • Joined: 07-April 08

Re: Component array and counter-controlled loop

Posted 18 June 2008 - 05:55 AM

these are the errors he's getting :

Checkerboard.java:100: incompatible types
found : void
required: java.awt.TextArea
numberArea[x] = numberArea[start + start++].setBackground(Color.magenta);
^
Checkerboard.java:113: incompatible types
found : void
required: java.awt.TextArea
numberArea[x] = numberArea[step + step++].setBackground(Color.yellow);
^
2 errors

Tool completed with exit code 1

i'll look into this some more but just wanted to iterate the errors. Remember to always put exact errors in your posts

This post has been edited by rgfirefly24: 18 June 2008 - 08:06 AM

Was This Post Helpful? 0
  • +
  • -

#3 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: Component array and counter-controlled loop

Posted 18 June 2008 - 08:12 AM

numberArea[x] = numberArea[start +  start++].setBackground(Color.magenta);



That piece of code doesn't make any sense to me. start + start++ << whats that?

and Its better if the setBackground(Color.magenta) was not used like that, it makes the code very complicated and I don't think it will work. Just an opinion. its better if you separate it.

Lastly seeing the code is well written; Atleast in my eyes, then I think your not such a n00b, your good.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Component array and counter-controlled loop

Posted 18 June 2008 - 03:02 PM

Mensahero is right
your code will be a lot easier if you just reset every body in yellow and do your magenta

	   for (int x = 0; x < numberArea.length; x++) 
		{
			numberArea[x].setBackground(Color.yellow);

		}
		for (int x = start - 1; x < stop; x += step) 
		{
			numberArea[x].setBackground(Color.magenta);

		}


Was This Post Helpful? 0
  • +
  • -

#5 chave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-June 08

Re: Component array and counter-controlled loop

Posted 23 June 2008 - 03:44 AM

Thanks alot for your replies. I really appreciate your input! I still cant get it to work properly, but i'm still working on it. So if i'm quiet for a bit, its because i'm trying to figure it out. Thanks again guys!!
Was This Post Helpful? 0
  • +
  • -

#6 cage2152  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-July 09

Re: Component array and counter-controlled loop

Posted 08 October 2009 - 05:09 AM

Quote

I got the code to work. There is only a problem with the sizing so just tinker with the parameters for the size.



 /*
	Chapter 5:  Using a Component Array and Counter-Controlled Loop
	Programmer: A.Logan
	Date:	   September 30, 2009
	Filename:   Checkerboard.java
	Purpose:	This projects uses 16 TextField components that change
			color to correspond with the start, stop, and step values
			entered by the user and to create a looging structure that
			allows the student to specify how many times the loop statements
			will be executed.
*/

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

public class Checkerboard extends Frame implements ActionListener
{
	//Declares the Panel that holds the Array
	Panel boardPanel = new Panel();
		TextField textArray[] = new TextField[16];

	//Declares the Panel that holds three fields and labels
	Panel inputPanel = new Panel();
		Label startLabel = new Label("Start");
		TextField startField = new TextField(2);
		Label stopLabel = new Label("Stop");
		TextField stopField = new TextField(2);
		Label stepLabel = new Label("Step");
		TextField stepField = new TextField(2);

	//Declares the Panel that holds the two buttons
	Panel buttonPanel = new Panel();
		Button goButton = new Button("Go");
		Button clearButton = new Button("Clear");

	//Declares the three int variables to recieve the start stop and step input
	int start = 0;
	int stop = 0;
	int step = 0;

 public Checkerboard()  //Create Checkerboard frame
	{
		for (int i=0; i<textArray.length; i++)
			   {
				   textArray[i]= new TextField();
				   textArray[i].setEditable(false);
				   textArray[i].setText(String.valueOf(i));
				   textArray[i].setBackground(Color.white);

						}

	   this.setLayout(new BorderLayout(2,2));
	   boardPanel.setLayout(new GridLayout(4,4,20,20));
	   inputPanel.setLayout(new GridLayout(2,3,20,20));
	   buttonPanel.setLayout(new GridLayout(1,2,5,5));

	   //add textFields to arrayPanel
	   for (int j=0;j<textArray.length;j++)
	   boardPanel.add(textArray[j]);


	inputPanel.add(startField);
	inputPanel.add(stopField);
	inputPanel.add(stepField);
	inputPanel.add(startLabel);
	inputPanel.add(stopLabel);
	inputPanel.add(stepLabel);

	buttonPanel.add(goButton);
	buttonPanel.add(clearButton);

	add(inputPanel, BorderLayout.CENTER);
	add(boardPanel, BorderLayout.NORTH);
	add(buttonPanel, BorderLayout.SOUTH);

	startField.addActionListener(this);
	stopField.addActionListener(this);
	stepField.addActionListener(this);
	goButton.addActionListener(this);
	clearButton.addActionListener(this);

	setVisible(true);
	startField.requestFocus();

	addWindowListener(new WindowAdapter()
	{
	public void windowClosing(WindowEvent e)
	{
					 System.exit(0);
		}
		}
	);
}//end Checkerboard frame

	public void actionPerformed(ActionEvent e)

		{
			String arg = e.getActionCommand();
			try
			{
				if(arg == "Go")
				{

	int start = Integer.parseInt(startField.getText());
	int stop = Integer.parseInt(stopField.getText());
	int step = Integer.parseInt(stepField.getText());

	for( int i=0; i<textArray.length; i++)

	textArray[i].setBackground(Color.blue);


	for(int i=start; i<textArray.length; i+=step)

	textArray[i].setBackground(Color.yellow);
				}
		   	}
			catch(Exception x)
			{
				JOptionPane.showMessageDialog(null,"Data Entry Error","Error",JOptionPane.INFORMATION_MESSAGE);
					arg = "Clear";
				}

				if(arg == "Clear")
				{
					startField.setText("");
					stopField.setText("");
					stepField.setText("");
					for(int i=0;i<textArray.length;i++)
						textArray[i].setBackground(Color.white);
				}
			}//end actionPerformed

	public static void main(String[] args)
	{
		Checkerboard f = new Checkerboard();
		f.setBounds(50,100,300,400);
		f.setTitle("Checkerboard Array");
		f.setVisible(true);
	}

}//end Checkerboard class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1