Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
| Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 26th, 2007, 07:14 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default loop array problem

Trying to get this checkerboard to loop and change colors based on the number inputted into start, step and stop. the problem I have no is that I get 2 errors.

incompatible types
found : void
required: java.awt.TextArea
numberArea[x] = numberArea[start & start++].setBackground(Color.magenta);

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);
            }
        }
}
    }

    // clears stop, stop, step
    void clearFields() {
        startField.setText("");
        stepField.setText("");
        stopField.setText("");
        startField.requestFocus();
        boardPanel.setBackground(Color.WHITE);
    }// end of clearFields() method

}// end of Checkerboard class
 
Old May 28th, 2007, 07:15 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hyderabad, A.P., India.
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,
the setBackground() method doesnt return anything its return type is void. When you call component.setBackground() then the background of that component gets changed you dont have to reassign it to some other variable.
Try changing the line
numberArea[x] = numberArea[start & start++].setBackground(Color.magenta);
to
numberArea[start & start++].setBackground(Color.magenta);

Regards,
Rakesh
 
Old June 15th, 2007, 06:49 AM
Authorized User
 
Join Date: Sep 2005
Location: bangalore, Karnataka, India.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vinay.bk
Default

U r trying to assign a textarea with the method i.e setbackground that return void. If you could use this way it would work fine
numberArea[step & step++].setBackground(Color.yellow);
numberArea[x] = numberArea[start & start++];




Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Array and Loop ublend Classic ASP Professional 2 November 21st, 2007 07:50 AM
Problem with loop and array katlees MySQL 1 July 5th, 2006 11:24 PM
Pass variables from array inside a loop dustygn PHP How-To 2 May 3rd, 2006 05:35 PM
create array for loop in FSO jfergy Classic ASP Professional 4 April 14th, 2006 09:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.