| | |
doing a program blinded
![]() |
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
I was sick the day on our review for this programming assignment so I am in kind of a dillema with a jerk for a professor...What I am going to do is after each section show you guys what I have and I don't think I should have too many problems but would it be possible for feedback. If not thats ok I was just seeing if that is possible. This is what I have so far from completing section 2 and I think I got it done right just making sure.....
this is my instructions:
This will incorporate arrays, for loops, and Frames all in one.
Create a panel containing an array of 16 TextArea components that change color to correspond with the start, stop, and step values entered by the user. Perform the following tasks to create the Checkerboard Array application shown below. When the user enters the start, stop, and step fields and then clicks the Go button, the results are also shown below.
1. Call your application Checkerboard.java
2. You will need the following variables… declare them as private:
a. 16 component TextArea array
b. a Panel to hold the array
c. 3 TextField components with length of 10
d. 3 int variables to receive the start, stop, and step values
e. 3 Labels to display the words Start, Stop, and Step
f. a Go button
g. a Clear button
h. a Panel to hold the 3 TextFields, 3 Labels, and the 2 Buttons
3. Create a constructor method to:
a. construct each of the components declared above and initializes the start, stop, and step variables to zero (when constructing the TextArea components, use the following parameters: null, 3, 5, 3)
b. set the Frame layout to BorderLayout
c. write a for loop to loop the array and set each of the 16 TextArea components in that array so they cannot be edited. In the same loop, set each of the TextArea components text to be 1 more than the index number. Also in this same loop, set the background of each of the TextArea components to white.
d. set the Panel for the TextArea components to GridLayout with 4 rows, 4 columns, and both gaps set to 10
e. set the Panel for the TextFields, Labels, and button to GridLayout with 3 rows, 3 columns, and both gaps set to 5
f. add the components to their respective Panels
g. make the buttons clickable
h. place the Panels in the Frame… put one in the NORTH and one in the CENTER
i. Enter the addWindowListener() method described in the chapter… this is the method that overrides the click of the X so it terminates the application
4. In your actionPerformed() method:
a. convert the data in your TextFields to int and store them in the variables declared above
b. write a loop that goes through the array setting every background color to blue
c. write another loop that’s based on the user inputs. Each time the loop is executed, change the background color to yellow (so… start your loop at the user’s specified starting condition. You’ll stop at the user’s specified stopping value. You’ll change the fields to yellow every time you increment your loop based on the step value. REMEMBER: Your displayed values are 1 off from your index numbers!!)
5. Write a main() method that creates an instance of the Checkerboard Frame.
a. set the bounds for the frame to 50, 100, 300, 400
b. set the title bar caption to Checkerboard Array
c. use the setVisible() method to display the application Frame during execution
6. After you get all of this complete, include error handling to make sure:
a. the values entered in the TextFields are valid integers
b. the start value is greater than or equal to 1 and less than or equal to 16
c. the stop value is greater than or equal to 1 and less than or equal to 16
d. the step value is greater than or equal to 1 and less than or equal to 16
e. the start condition is less than the stop condition
f. when an error occurs, give an error message in a dialog box that is specific to their error, remove the text from the invalid field, and put the cursor in that field so the user has a chance to re-enter… this can be accomplished by using multiple try/catch statements
g. only change the colors if the numbers are valid
7. Create a clear button as seen in the example below. This button should:
a. clear out all 3 TextFields
b. change the background color of all TextArea array elements to white
c. put the cursor in the start field
java Syntax (Toggle Plain Text)
//packages to import import java.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel = new Panel(); TextArea topDisplay[] = new TextArea[16]; private Panel bottomPanel = new Panel(); private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton; private Button clearButton; private int start; private int stop; private int step; public Checkerboard() { }//end action listener }//end class
this is my instructions:
This will incorporate arrays, for loops, and Frames all in one.
Create a panel containing an array of 16 TextArea components that change color to correspond with the start, stop, and step values entered by the user. Perform the following tasks to create the Checkerboard Array application shown below. When the user enters the start, stop, and step fields and then clicks the Go button, the results are also shown below.
1. Call your application Checkerboard.java
2. You will need the following variables… declare them as private:
a. 16 component TextArea array
b. a Panel to hold the array
c. 3 TextField components with length of 10
d. 3 int variables to receive the start, stop, and step values
e. 3 Labels to display the words Start, Stop, and Step
f. a Go button
g. a Clear button
h. a Panel to hold the 3 TextFields, 3 Labels, and the 2 Buttons
3. Create a constructor method to:
a. construct each of the components declared above and initializes the start, stop, and step variables to zero (when constructing the TextArea components, use the following parameters: null, 3, 5, 3)
b. set the Frame layout to BorderLayout
c. write a for loop to loop the array and set each of the 16 TextArea components in that array so they cannot be edited. In the same loop, set each of the TextArea components text to be 1 more than the index number. Also in this same loop, set the background of each of the TextArea components to white.
d. set the Panel for the TextArea components to GridLayout with 4 rows, 4 columns, and both gaps set to 10
e. set the Panel for the TextFields, Labels, and button to GridLayout with 3 rows, 3 columns, and both gaps set to 5
f. add the components to their respective Panels
g. make the buttons clickable
h. place the Panels in the Frame… put one in the NORTH and one in the CENTER
i. Enter the addWindowListener() method described in the chapter… this is the method that overrides the click of the X so it terminates the application
4. In your actionPerformed() method:
a. convert the data in your TextFields to int and store them in the variables declared above
b. write a loop that goes through the array setting every background color to blue
c. write another loop that’s based on the user inputs. Each time the loop is executed, change the background color to yellow (so… start your loop at the user’s specified starting condition. You’ll stop at the user’s specified stopping value. You’ll change the fields to yellow every time you increment your loop based on the step value. REMEMBER: Your displayed values are 1 off from your index numbers!!)
5. Write a main() method that creates an instance of the Checkerboard Frame.
a. set the bounds for the frame to 50, 100, 300, 400
b. set the title bar caption to Checkerboard Array
c. use the setVisible() method to display the application Frame during execution
6. After you get all of this complete, include error handling to make sure:
a. the values entered in the TextFields are valid integers
b. the start value is greater than or equal to 1 and less than or equal to 16
c. the stop value is greater than or equal to 1 and less than or equal to 16
d. the step value is greater than or equal to 1 and less than or equal to 16
e. the start condition is less than the stop condition
f. when an error occurs, give an error message in a dialog box that is specific to their error, remove the text from the invalid field, and put the cursor in that field so the user has a chance to re-enter… this can be accomplished by using multiple try/catch statements
g. only change the colors if the numbers are valid
7. Create a clear button as seen in the example below. This button should:
a. clear out all 3 TextFields
b. change the background color of all TextArea array elements to white
c. put the cursor in the start field
Well I have to admit I usually use swing components rather than awt components for my GUIs. Has your instructor specifically asked you to use awt components? Changing to swing is easy - import javax.swing.* and add a J to the front of each of your GUI components. Other than that, looks like number 2 is done for you...
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
Really got a lot done today...I finished up with constructor, main, and clear button and just trying to verify everything is right. Also I am getting one error and I am sure it is something minor just having a hard time trying to figure out what it is so could anyone if they have time look at what I have along with the instructions and just make sure I have everything done right with the exception of action performed and the error I am getting is...
H:\JAVA\Checkerboard.java:102: cannot resolve symbol
symbol : method setSize (int,int,int,int)
location: class Checkerboard
f.setSize(50, 100, 300, 400);
^
code is:
H:\JAVA\Checkerboard.java:102: cannot resolve symbol
symbol : method setSize (int,int,int,int)
location: class Checkerboard
f.setSize(50, 100, 300, 400);
^
code is:
java Syntax (Toggle Plain Text)
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextField topDisplay; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton[]; private Button clearButton[]; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct components and initialize beginning values topDisplay = new TextField(16); topDisplay.setEditable(false); topPanel = new Panel(); bottomPanel = new Panel(); bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //set color to the panel //row 1 for(int i = 1; i <= 4; i++) //set color for setBackground(Color.white); //row 2 for(int i = 5; i <= 8; i++) //set color for setBackground(Color.white); //row 3 for(int i = 9; i <= 12; i++) //set color for setBackground(Color.white); //row 4 for(int i = 13; i <= 16; i++) //set color for setBackground(Color.white); //register the listener with each Button for(int i = 0; i < goButton.length; i++) goButton[i].addActionListener(this); for(int i = 0; i < clearButton.length; i++) clearButton[i].addActionListener(this); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowclosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setSize(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //test clear String arg = e.getActionCommand(); //clear button was clicked if(arg.equals("Clear")) { clearText = true; start = 0; stop = 0; step = 0; first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
You just used the wrong method call for the Frame sizing - easy to fix. setSize() will take either a (width,height) or a Dimension argument. There is another method setBounds() that takes (x,y,width,height), which is probably the one you are trying to use.
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
yea that worked fine...one other quick question...i tried to run my code and it will not let me but compiled fine...is there something I did wrong with my code or something that I just have not programmed yet because it looks fine on my end...
java Syntax (Toggle Plain Text)
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextField topDisplay; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton[]; private Button clearButton[]; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct components and initialize beginning values topDisplay = new TextField(16); topDisplay.setEditable(false); topPanel = new Panel(); bottomPanel = new Panel(); bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //set color to the panel //row 1 for(int i = 1; i <= 4; i++) //set color for setBackground(Color.white); //row 2 for(int i = 5; i <= 8; i++) //set color for setBackground(Color.white); //row 3 for(int i = 9; i <= 12; i++) //set color for setBackground(Color.white); //row 4 for(int i = 13; i <= 16; i++) //set color for setBackground(Color.white); //register the listener with each Button for(int i = 0; i < goButton.length; i++) goButton[i].addActionListener(this); for(int i = 0; i < clearButton.length; i++) clearButton[i].addActionListener(this); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowclosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //test clear String arg = e.getActionCommand(); //clear button was clicked if(arg.equals("Clear")) { clearText = true; start = 0; stop = 0; step = 0; first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
What error message do you get when you try to run the program?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. The null pointer error (with line number noted) indicates this is the problem NullPointerException indicates you tried to call a method or access a variable on an object reference that has not been initialized (it's still null). Your initialization of "topPanel" occurs 6 lines below that code, so the solution should be apparent.
java Syntax (Toggle Plain Text)
topPanel.setLayout(new GridLayout(4, 4, 10, 10));
Last edited by Ezzaral; Apr 18th, 2008 at 11:58 am.
"I was sick the day on our review for this programming assignment so I am in kind of a dillema with a jerk for a professor..."
Bad attitude. You're "professor" (more likely a highschool teacher if that) is no jerk. You're mistaken if you think he's going to allow you to fall behind on your studies and give you full marks for homework not done and material not studied just because you had a headache the night before.
Bad attitude. You're "professor" (more likely a highschool teacher if that) is no jerk. You're mistaken if you think he's going to allow you to fall behind on your studies and give you full marks for homework not done and material not studied just because you had a headache the night before.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
This is the error I am getting in the command line console.....
Exception in thread "main" java.lang.NullPointerException
at Checkerboard.<init><Checkerboard.java:39>
at Checkerboard.main<Checkerboard.java:100>
Press any key to continue
my code is:
Exception in thread "main" java.lang.NullPointerException
at Checkerboard.<init><Checkerboard.java:39>
at Checkerboard.main<Checkerboard.java:100>
Press any key to continue
my code is:
java Syntax (Toggle Plain Text)
//packages to import import javax.swing.JOptionPane; import java.awt.*; import java.awt.event.*; public class Checkerboard extends Frame implements ActionListener { private Panel topPanel; private TextField topDisplay; private Panel bottomPanel; private TextField startField = new TextField(10); private TextField stopField = new TextField(10); private TextField stepField = new TextField(10); private Label startLabel = new Label ("Start"); private Label stopLabel = new Label ("Stop"); private Label stepLabel = new Label ("Step"); private Button goButton[]; private Button clearButton[]; private boolean clearText; private boolean first; private int start; private int stop; private int step; //constructor methods public Checkerboard() { //set layouts for the Frame and Panels setLayout(new BorderLayout()); topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5)); //construct components and initialize beginning values topDisplay = new TextField(16); topDisplay.setEditable(false); topPanel = new Panel(); bottomPanel = new Panel(); bottomPanel.add(startField); bottomPanel.add(stopField); bottomPanel.add(stepField); bottomPanel.add(startLabel); bottomPanel.add(stopLabel); bottomPanel.add(stepLabel); //add components to frame add(topPanel, BorderLayout.NORTH); add(bottomPanel, BorderLayout.CENTER); //set color to the panel //row 1 for(int i = 1; i <= 4; i++) //set color for setBackground(Color.white); //row 2 for(int i = 5; i <= 8; i++) //set color for setBackground(Color.white); //row 3 for(int i = 9; i <= 12; i++) //set color for setBackground(Color.white); //row 4 for(int i = 13; i <= 16; i++) //set color for setBackground(Color.white); //register the listener with each Button for(int i = 0; i < goButton.length; i++) goButton[i].addActionListener(this); for(int i = 0; i < clearButton.length; i++) clearButton[i].addActionListener(this); //allow the x to close the application addWindowListener(new WindowAdapter() { public void windowclosing(WindowEvent e) { System.exit(0); } } //end window adapter ); } public static void main(String args[]) { Checkerboard f = new Checkerboard(); f.setTitle("Checkerboard Array"); f.setBounds(50, 100, 300, 400); f.setLocationRelativeTo(null); f.setVisible(true); } //end main public void actionPerformed(ActionEvent e) { //test clear String arg = e.getActionCommand(); //clear button was clicked if(arg.equals("Clear")) { clearText = true; start = 0; stop = 0; step = 0; first = true; setBackground(Color.white); startField.requestFocus(); } //end the if clear }//end action listener }//end class
I already explained the reason for that error - you have not yet initialized your panels when you call The code that creates those panels is several lines below those calls
Java Syntax (Toggle Plain Text)
topPanel.setLayout(new GridLayout(4, 4, 10, 10)); bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
Java Syntax (Toggle Plain Text)
topPanel = new Panel(); bottomPanel = new Panel();
![]() |
Similar Threads
- My URLs are blinded by their cloaks! (Geeks' Lounge)
Other Threads in the Java Forum
- Previous Thread: First, best, worst fit java program - need help debugging
- Next Thread: Curved vertex for swing containers.
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javaarraylist javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows






