Welcome to Dream.In.Code
Become a Java Expert!

Join 150,105 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,890 people online right now. Registration is fast and FREE... Join Now!




Component array and counter-controlled loop

 
Reply to this topicStart new topic

Component array and counter-controlled loop, I'm trying to do a program of a checker board

chave
18 Jun, 2008 - 04:21 AM
Post #1

New D.I.C Head
*

Joined: 18 Jun, 2008
Posts: 2

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
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Component Array And Counter-controlled Loop
18 Jun, 2008 - 04:55 AM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
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 Jun, 2008 - 07:06 AM
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Component Array And Counter-controlled Loop
18 Jun, 2008 - 07:12 AM
Post #3

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
CODE

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.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Component Array And Counter-controlled Loop
18 Jun, 2008 - 02:02 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Mensahero is right
your code will be a lot easier if you just reset every body in yellow and do your magenta

CODE

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

        }

User is offlineProfile CardPM
+Quote Post

chave
RE: Component Array And Counter-controlled Loop
23 Jun, 2008 - 02:44 AM
Post #5

New D.I.C Head
*

Joined: 18 Jun, 2008
Posts: 2

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!!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:53AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month