doing a program blinded

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #11
Apr 19th, 2008
Ezzarel you dont havta reply to this prolly think I am the biggest idiot living but I still am having problems with this program...I tried movin a bench of stuff around but just went back to this w\some minor changes and still havin problems

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Checkerboard extends Frame implements ActionListener
  8. {
  9. private Panel topPanel;
  10. private TextArea topDisplay[];
  11. private Panel bottomPanel;
  12. private TextField startField = new TextField(10);
  13. private TextField stopField = new TextField(10);
  14. private TextField stepField = new TextField(10);
  15. private Label startLabel = new Label ("Start");
  16. private Label stopLabel = new Label ("Stop");
  17. private Label stepLabel = new Label ("Step");
  18. private Button goButton[];
  19. private Button clearButton[];
  20. private boolean clearText;
  21. private boolean first;
  22. private int start;
  23. private int stop;
  24. private int step;
  25.  
  26. //constructor methods
  27. public Checkerboard()
  28.  
  29. {
  30. //set layouts for the Frame and Panels
  31. setLayout(new BorderLayout());
  32. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  33. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  34.  
  35. //construct components and initialize beginning values
  36. topPanel = new Panel();
  37. topDisplay = new TextArea[16];
  38. first = true;
  39. bottomPanel = new Panel();
  40. int start = 0;
  41. int stop = 0;
  42. int step = 0;
  43. bottomPanel.add(startField);
  44. bottomPanel.add(stopField);
  45. bottomPanel.add(stepField);
  46. bottomPanel.add(startLabel);
  47. bottomPanel.add(stopLabel);
  48. bottomPanel.add(stepLabel);
  49. clearText = true;
  50.  
  51. //construct the Display
  52. for(int i = 1; i <= 16; i++)
  53. {
  54. topDisplay[i] = new TextArea(null,3,5,3);
  55. }
  56.  
  57. //add components to frame
  58. add(topPanel, BorderLayout.NORTH);
  59. add(bottomPanel, BorderLayout.CENTER);
  60.  
  61. //set color to the panel
  62. //row 1
  63. for(int i = 1; i <= 4; i++)
  64. //set color for
  65. setBackground(Color.white);
  66.  
  67. //row 2
  68. for(int i = 5; i <= 8; i++)
  69. //set color for
  70. setBackground(Color.white);
  71.  
  72. //row 3
  73. for(int i = 9; i <= 12; i++)
  74. //set color for
  75. setBackground(Color.white);
  76.  
  77. //row 4
  78. for(int i = 13; i <= 16; i++)
  79. //set color for
  80. setBackground(Color.white);
  81.  
  82. //register the listener with each Button
  83. for(int i = 0; i < goButton.length; i++)
  84. goButton[i].addActionListener(this);
  85.  
  86. for(int i = 0; i < clearButton.length; i++)
  87. clearButton[i].addActionListener(this);
  88.  
  89. //allow the x to close the application
  90. addWindowListener(new WindowAdapter()
  91. {
  92. public void windowclosing(WindowEvent e)
  93. {
  94. System.exit(0);
  95. }
  96. } //end window adapter
  97. );
  98.  
  99. }
  100.  
  101. public static void main(String args[])
  102. {
  103. Checkerboard f = new Checkerboard();
  104. f.setTitle("Checkerboard Array");
  105. f.setBounds(50, 100, 300, 400);
  106. f.setLocationRelativeTo(null);
  107. f.setVisible(true);
  108.  
  109. } //end main
  110.  
  111. public void actionPerformed(ActionEvent e)
  112. {
  113. //test clear
  114. String arg = e.getActionCommand();
  115.  
  116. //clear button was clicked
  117. if(arg.equals("Clear"))
  118. {
  119. clearText = true;
  120. start = 0;
  121. stop = 0;
  122. step = 0;
  123. first = true;
  124. setBackground(Color.white);
  125. startField.requestFocus();
  126.  
  127. } //end the if clear
  128.  
  129. }//end action listener
  130. }//end class
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: doing a program blinded

 
0
  #12
Apr 19th, 2008
you really think that insulting people who try to help you is going to make them more likely to do your homework for you?
If so, think again...

Or don't you think at all before posting such crap?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,675
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: doing a program blinded

 
0
  #13
Apr 19th, 2008
First of all, can someone explain to me what he said?:
Ezzarel you dont havta reply to this prolly think I am the biggest idiot living but I still am having problems with this program...I tried movin a bench of stuff around but just went back to this w\some minor changes and still havin problems
Because my English are not that good

And it seems to me that Ezzarel has told you the solution. If you don't understand it then learn basic java and don't stuff like gui
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: doing a program blinded

 
0
  #14
Apr 19th, 2008
idiot kid is too lazy to do its own homework and wants us to do it for it.
That and/or it's utterly incapable of understanding what it's being told.
I vote for both of those at the same time.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #15
Apr 19th, 2008
keep your whining to yourself. Look I even talked with him about it and I was not calling him an idiot. He already helped me and I clearly said anyone but him only because I did not want to keep bothering him since he had done such a great job explaining my errors to me.

I think I made the correct changes by moving things around and initializing the buttons and panels (as long as I did it correctly) and getting a new message that I think has something to do with the TextArea. If this is just something with the action event then I am not worried about it because I am programming that today but if I am still messing something up with initialization point any ideas would be helpful.

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Checkerboard extends Frame implements ActionListener
  8. {
  9. private Panel topPanel;
  10. private TextArea topDisplay[];
  11. private Panel bottomPanel;
  12. private TextField startField = new TextField(10);
  13. private TextField stopField = new TextField(10);
  14. private TextField stepField = new TextField(10);
  15. private Label startLabel = new Label ("Start");
  16. private Label stopLabel = new Label ("Stop");
  17. private Label stepLabel = new Label ("Step");
  18. private Button goButton;
  19. private Button clearButton;
  20. private boolean clearText;
  21. private boolean first;
  22. private int start;
  23. private int stop;
  24. private int step;
  25.  
  26. //constructor methods
  27. public Checkerboard()
  28.  
  29. {
  30.  
  31. //construct components and initialize beginning values
  32. topPanel = new Panel();
  33. topDisplay = new TextArea[16];
  34.  
  35. first = true;
  36. bottomPanel = new Panel();
  37. int start = 0;
  38. int stop = 0;
  39. int step = 0;
  40. bottomPanel.add(startField);
  41. bottomPanel.add(stopField);
  42. bottomPanel.add(stepField);
  43. bottomPanel.add(startLabel);
  44. bottomPanel.add(stopLabel);
  45. bottomPanel.add(stepLabel);
  46. bottomPanel.add(goButton);
  47. goButton.addActionListener(this);
  48. bottomPanel.add(clearButton);
  49. clearButton.addActionListener(this);
  50. clearText = true;
  51.  
  52. //construct the Display
  53. for(int i = 1; i <= 16; i++)
  54. {
  55. topDisplay[i] = new TextArea(null,3,5,3);
  56. }
  57.  
  58. //set layouts for the Frame and Panels
  59. setLayout(new BorderLayout());
  60. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  61. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  62.  
  63. //add components to frame
  64. add(topPanel, BorderLayout.NORTH);
  65. add(bottomPanel, BorderLayout.CENTER);
  66.  
  67. //set color to the panel
  68. //row 1
  69. for(int i = 1; i <= 4; i++)
  70. //set color for
  71. setBackground(Color.white);
  72.  
  73. //row 2
  74. for(int i = 5; i <= 8; i++)
  75. //set color for
  76. setBackground(Color.white);
  77.  
  78. //row 3
  79. for(int i = 9; i <= 12; i++)
  80. //set color for
  81. setBackground(Color.white);
  82.  
  83. //row 4
  84. for(int i = 13; i <= 16; i++)
  85. //set color for
  86. setBackground(Color.white);
  87.  
  88.  
  89.  
  90. //allow the x to close the application
  91. addWindowListener(new WindowAdapter()
  92. {
  93. public void windowclosing(WindowEvent e)
  94. {
  95. System.exit(0);
  96. }
  97. } //end window adapter
  98. );
  99.  
  100. }
  101.  
  102. public static void main(String args[])
  103. {
  104. Checkerboard f = new Checkerboard();
  105. f.setTitle("Checkerboard Array");
  106. f.setBounds(50, 100, 300, 400);
  107. f.setLocationRelativeTo(null);
  108. f.setVisible(true);
  109.  
  110. } //end main
  111.  
  112. public void actionPerformed(ActionEvent e)
  113. {
  114. //test clear
  115. String arg = e.getActionCommand();
  116.  
  117. //clear button was clicked
  118. if(arg.equals("Clear"))
  119. {
  120. clearText = true;
  121. start = 0;
  122. stop = 0;
  123. step = 0;
  124. first = true;
  125. setBackground(Color.white);
  126. startField.requestFocus();
  127.  
  128. } //end the if clear
  129.  
  130. }//end action listener
  131. }//end class

Error in the console application says:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:625)
at java.awt.Container.add(Container.java:307)
at Checkerboard.<init>(Checkerboard.java:52)
at Checkerboard.main(Checkerboard.java:110)
Press any key to continue . . .





Could importing another java package resolve this
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,675
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: doing a program blinded

 
0
  #16
Apr 19th, 2008
bottomPanel.add(goButton);

Where do you do: goButton = new Button("some name"); ? If you don't do this then goButton is null.

Obviously you must initialize everything before you use it. And the compiler will tell you the lines where you try to use null objects
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: doing a program blinded

 
0
  #17
Apr 19th, 2008
so the lazy idiot kid has fallen into the normal pattern of the homework kiddo and started getting abusive.
How surprising, NOT.

You're going to make a lot of friends with that attitude, kiddo. It's really going to help you bully your way into friendships.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #18
Apr 19th, 2008
Ok I just wanna apologize to anyone I offended even the guy from the netherlands. I am not asking for someone to do my homework. These programming classes are the last 2 I have...one is java and the other is C++. I do not want to go into programming because I will be the first to admit I am completely horrible at it. I also do not want it to be called lazy because lazy is not caring and wanting someone to do it for me word for word and I do not want that. I have been working on this program all week and to me a forum is the last resolution for me if I am unable to resolve a problem because you guys especially in this forum have been really helpful for me to understand the material even the guy from the Netherlands.

With that being said I think I have gotten really close because I have gotten my program to run...the only problem is I only can see 3 boxes (they are not even numbered), 3 labels (start, stop,and step), and two giant sized buttons(go and clear)

I really hope this is just something small and could easily be corrected but does anyone see the error I am making in my program. I wanna think it has something to do with my program being out of order but I tried moving a bunch of things around and couldnt come across any changes.

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Checkerboard extends Frame implements ActionListener
  8. {
  9. private Panel topPanel;
  10. private TextArea topDisplay[];
  11. private Panel bottomPanel;
  12. private TextField startField = new TextField(10);
  13. private TextField stopField = new TextField(10);
  14. private TextField stepField = new TextField(10);
  15. private Label startLabel = new Label ("Start");
  16. private Label stopLabel = new Label ("Stop");
  17. private Label stepLabel = new Label ("Step");
  18. private Button goButton;
  19. private Button clearButton;
  20. private boolean clearText;
  21. private boolean first;
  22. private int start;
  23. private int stop;
  24. private int step;
  25.  
  26. //constructor methods
  27. public Checkerboard()
  28.  
  29. {
  30.  
  31. //construct components and initialize beginning values
  32. topPanel = new Panel();
  33. topDisplay = new TextArea[16];
  34.  
  35. goButton = new Button("Go");
  36. clearButton = new Button("Clear");
  37. first = true;
  38. bottomPanel = new Panel();
  39. int start = 0;
  40. int stop = 0;
  41. int step = 0;
  42. bottomPanel.add(startField);
  43. bottomPanel.add(stopField);
  44. bottomPanel.add(stepField);
  45. bottomPanel.add(startLabel);
  46. bottomPanel.add(stopLabel);
  47. bottomPanel.add(stepLabel);
  48. bottomPanel.add(goButton);
  49. goButton.addActionListener(this);
  50. bottomPanel.add(clearButton);
  51. clearButton.addActionListener(this);
  52. clearText = true;
  53.  
  54. //set layouts for the Frame and Panels
  55. setLayout(new BorderLayout());
  56. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  57. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  58.  
  59. //construct the Display
  60. for(int i = 1; i <= 15; i++)
  61. {
  62. topDisplay[i] = new TextArea(null, 3, 5, 3);
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. //set color to the panel
  70. //row 1
  71. for(int x = 1; x <= 4; x++)
  72. //set color for
  73. setBackground(Color.white);
  74.  
  75. //row 2
  76. for(int x = 5; x <= 8; x++)
  77. //set color for
  78. setBackground(Color.white);
  79.  
  80. //row 3
  81. for(int x = 9; x <= 12; x++)
  82. //set color for
  83. setBackground(Color.white);
  84.  
  85. //row 4
  86. for(int x = 13; x <= 16; x++)
  87. //set color for
  88. setBackground(Color.white);
  89. }
  90.  
  91.  
  92.  
  93. //add components to frame
  94. add(topPanel, BorderLayout.NORTH);
  95. add(bottomPanel, BorderLayout.CENTER);
  96.  
  97.  
  98. //allow the x to close the application
  99. addWindowListener(new WindowAdapter()
  100. {
  101. public void windowclosing(WindowEvent e)
  102. {
  103. System.exit(0);
  104. }
  105. } //end window adapter
  106. );
  107.  
  108. }
  109.  
  110. public static void main(String args[])
  111. {
  112. Checkerboard f = new Checkerboard();
  113. f.setTitle("Checkerboard Array");
  114. f.setBounds(50, 100, 300, 400);
  115. f.setLocationRelativeTo(null);
  116. f.setVisible(true);
  117.  
  118. } //end main
  119.  
  120. public void actionPerformed(ActionEvent e)
  121. {
  122. //convert data in TextField to int
  123. int start = Integer.parseInt(startField.getText());
  124. int stop = Integer.parseInt(stopField.getText());
  125. int step = Integer.parseInt(stepField.getText());
  126.  
  127. //test clear
  128. String arg = e.getActionCommand();
  129.  
  130. //clear button was clicked
  131. if(arg.equals("Clear"))
  132. {
  133. clearText = true;
  134. start = 0;
  135. stop = 0;
  136. step = 0;
  137. first = true;
  138. setBackground(Color.white);
  139. startField.requestFocus();
  140.  
  141. } //end the if clear
  142.  
  143. }//end action listener
  144.  
  145. }//end class
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #19
Apr 20th, 2008
FINALLY got what I needed to be done today now just working on the action...was wondering if anyone had a chance to take a look at what I have done so far to make sure everything looks good and was also wanting to know if anyone had an idea on how to put numbers on the boxes...

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Checkerboard extends Frame implements ActionListener
  8. {
  9. private Panel topPanel;
  10. private TextArea topDisplay[];
  11. private Panel bottomPanel;
  12. private TextField startField = new TextField(10);
  13. private TextField stopField = new TextField(10);
  14. private TextField stepField = new TextField(10);
  15. private Label startLabel = new Label ("Start");
  16. private Label stopLabel = new Label ("Stop");
  17. private Label stepLabel = new Label ("Step");
  18. private Button goButton;
  19. private Button clearButton;
  20. private boolean clearText;
  21. private boolean first;
  22. private int start;
  23. private int stop;
  24. private int step;
  25.  
  26. //constructor methods
  27. public Checkerboard()
  28.  
  29. {
  30.  
  31. //construct components and initialize beginning values
  32. topPanel = new Panel();
  33. topDisplay = new TextArea[16];
  34.  
  35. goButton = new Button("Go");
  36. clearButton = new Button("Clear");
  37. first = true;
  38. bottomPanel = new Panel();
  39. int start = 0;
  40. int stop = 0;
  41. int step = 0;
  42. bottomPanel.add(startField);
  43. bottomPanel.add(stopField);
  44. bottomPanel.add(stepField);
  45. bottomPanel.add(startLabel);
  46. bottomPanel.add(stopLabel);
  47. bottomPanel.add(stepLabel);
  48. bottomPanel.add(goButton);
  49. goButton.addActionListener(this);
  50. bottomPanel.add(clearButton);
  51. clearButton.addActionListener(this);
  52. clearText = true;
  53.  
  54. //set layouts for the Frame and Panels
  55. setLayout(new BorderLayout());
  56. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  57. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  58.  
  59. //construct the Display
  60. for(int i = 0; i <= 15; i++)
  61. {
  62. topDisplay[i] = new TextArea(null, 3, 5, 3);
  63. topPanel.add(topDisplay[i]);
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. //set color to the panel
  72. //row 1
  73. for(int x = 1; x <= 4; x++)
  74. //set color for
  75. setBackground(Color.white);
  76.  
  77. //row 2
  78. for(int x = 5; x <= 8; x++)
  79. //set color for
  80. setBackground(Color.white);
  81.  
  82. //row 3
  83. for(int x = 9; x <= 12; x++)
  84. //set color for
  85. setBackground(Color.white);
  86.  
  87. //row 4
  88. for(int x = 13; x <= 16; x++)
  89. //set color for
  90. setBackground(Color.white);
  91. }
  92.  
  93.  
  94.  
  95. //add components to frame
  96. add(topPanel, BorderLayout.NORTH);
  97. add(bottomPanel, BorderLayout.CENTER);
  98.  
  99.  
  100. //allow the x to close the application
  101. addWindowListener(new WindowAdapter()
  102. {
  103. public void windowClosing(WindowEvent e)
  104. {
  105. System.exit(0);
  106. }
  107. } //end window adapter
  108. );
  109.  
  110. }
  111.  
  112. public static void main(String args[])
  113. {
  114. Checkerboard f = new Checkerboard();
  115. f.setTitle("Checkerboard Array");
  116. f.setBounds(50, 100, 300, 400);
  117. f.setLocationRelativeTo(null);
  118. f.setVisible(true);
  119.  
  120. } //end main
  121.  
  122. public void actionPerformed(ActionEvent e)
  123. {
  124. //convert data in TextField to int
  125. int start = Integer.parseInt(startField.getText());
  126. int stop = Integer.parseInt(stopField.getText());
  127. int step = Integer.parseInt(stepField.getText());
  128.  
  129. for(int i = 1; i <=16; i++)
  130. setBackground(Color.blue);
  131.  
  132. for(int i = start; i <= stop; i++)
  133. setBackground(Color.yellow);
  134.  
  135. //test clear
  136. String arg = e.getActionCommand();
  137.  
  138. //clear button was clicked
  139. if(arg.equals("Clear"))
  140. {
  141. clearText = true;
  142. startField.setText("");
  143. stopField.setText("");
  144. stepField.setText("");
  145. first = true;
  146. setBackground(Color.white);
  147. startField.requestFocus();
  148.  
  149. } //end the if clear
  150.  
  151. }//end action listener
  152.  
  153. }//end class
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: doing a program blinded

 
0
  #20
Apr 20th, 2008
apologize for bothering you guys but I have one more question and no more bothering I promise...

It has to do with my go button...When I click it, the background turns to blue that is no problem but the issue is certain parts do not return to yellow...It is hard to explain withpout showing the instuctions so here are the instructions for the for loop

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

In the start stop and step fields in the example I have on my word document there shows 16 boxes looking like this

1 2 3 4
5 6 7 9
9 10 11 12
13 14 15 16

When I input 3 for start 15 for stop and 4 for step the boxes that are to be yellow are 3, 7, 11, 15...however they are all turning blue and was wondering if anyone could take a look at my loop and let me know if I am doing something wrong in it or something else.

  1.  
  2. //packages to import
  3. import javax.swing.JOptionPane;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class Checkerboard extends Frame implements ActionListener
  8. {
  9. private Panel topPanel;
  10. private TextArea topDisplay[];
  11. private Panel bottomPanel;
  12. private TextField startField = new TextField(10);
  13. private TextField stopField = new TextField(10);
  14. private TextField stepField = new TextField(10);
  15. private Label startLabel = new Label ("Start");
  16. private Label stopLabel = new Label ("Stop");
  17. private Label stepLabel = new Label ("Step");
  18. private Button goButton;
  19. private Button clearButton;
  20. private boolean clearText;
  21. private boolean first;
  22. private int start;
  23. private int stop;
  24. private int step;
  25.  
  26. //constructor methods
  27. public Checkerboard()
  28.  
  29. {
  30.  
  31. //construct components and initialize beginning values
  32. topPanel = new Panel();
  33. topDisplay = new TextArea[16];
  34.  
  35.  
  36. goButton = new Button("Go");
  37. clearButton = new Button("Clear");
  38. first = true;
  39. bottomPanel = new Panel();
  40. int start = 0;
  41. int stop = 0;
  42. int step = 0;
  43. bottomPanel.add(startField);
  44. bottomPanel.add(stopField);
  45. bottomPanel.add(stepField);
  46. bottomPanel.add(startLabel);
  47. bottomPanel.add(stopLabel);
  48. bottomPanel.add(stepLabel);
  49. bottomPanel.add(goButton);
  50. goButton.addActionListener(this);
  51. bottomPanel.add(clearButton);
  52. clearButton.addActionListener(this);
  53. clearText = true;
  54.  
  55. //set layouts for the Frame and Panels
  56. setLayout(new BorderLayout());
  57. topPanel.setLayout(new GridLayout(4, 4, 10, 10));
  58. bottomPanel.setLayout(new GridLayout(3, 3, 5, 5));
  59.  
  60. //construct the Display
  61. for(int i = 0; i <= 15; i++)
  62. {
  63. topDisplay[i] = new TextArea(null, 3, 5, 3);
  64. topDisplay[i].setText(String.valueOf(i+1));
  65. topDisplay[i].setEditable(false);
  66. topPanel.add(topDisplay[i]);
  67. //topPanel.setEditable("false");
  68. }
  69.  
  70. //add components to frame
  71. add(topPanel, BorderLayout.NORTH);
  72. add(bottomPanel, BorderLayout.CENTER);
  73.  
  74.  
  75. //allow the x to close the application
  76. addWindowListener(new WindowAdapter()
  77. {
  78. public void windowClosing(WindowEvent e)
  79. {
  80. System.exit(0);
  81. }
  82. } //end window adapter
  83. );
  84.  
  85. }
  86.  
  87. public static void main(String args[])
  88. {
  89. Checkerboard f = new Checkerboard();
  90. f.setTitle("Checkerboard Array");
  91. f.setBounds(50, 100, 300, 400);
  92. f.setLocationRelativeTo(null);
  93. f.setVisible(true);
  94.  
  95. } //end main
  96.  
  97. public void actionPerformed(ActionEvent e)
  98. {
  99. //convert data in TextField to int
  100. int start = Integer.parseInt(startField.getText());
  101. int stop = Integer.parseInt(stopField.getText());
  102. int step = Integer.parseInt(stepField.getText());
  103.  
  104. //test go
  105. String arg = e.getActionCommand();
  106.  
  107. //go button was clicked
  108. if(arg.equals("Go"))
  109. {
  110.  
  111. for(int i = 0; i <=16; i++)
  112. topDisplay[i].setBackground(Color.blue);
  113.  
  114. for(int i = start; i <= stop; step++)
  115. topDisplay[i].setBackground(Color.yellow);
  116.  
  117. } //end the if go
  118.  
  119.  
  120.  
  121. //clear button was clicked
  122. if(arg.equals("Clear"))
  123. {
  124. clearText = true;
  125. startField.setText("");
  126. stopField.setText("");
  127. stepField.setText("");
  128. first = true;
  129. setBackground(Color.white);
  130. startField.requestFocus();
  131.  
  132. } //end the if clear
  133.  
  134. }//end action listener
  135.  
  136. }//end class
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC