CODE
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