Results 1 to 8 of 8
- 01-18-2012, 11:18 AM #1
Member
- Join Date
- Jan 2012
- Location
- Kampala, Uganda
- Posts
- 3
- Rep Power
- 0
How to call methods in a for loop-- trying to draw a string to count each buton click
Hello guyz, I am trying to write a code that should count the number of clicks on each button, but my problem is I have failed to include the paint methods in the iteration that creates the buttons, if I write the methods outside the for loop and the main method it does not complain, but ofcourse only prints 1 count that increments each time each button is clicked. yet I want it to count for only respective button.
The error I get when I place my mouse over paint in eclipse is "void is an invalid type for the variable pain", actionperformed "void is an invalid type for the variable actionPerformed", getcount "Syntax error on token "int", @ expected" and increasecount "Syntax error on token "void", @ expected"
Or is there a better way of doing this?
Here's my code!
Java Code:import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.JButton; import javax.swing.JOptionPane; public class CountClick extends Applet implements ActionListener { /** * */ private static final long serialVersionUID = 1L; private int count = 0; public void init() { Button[] b = new Button[10]; for(int i = 0; i < b.length; i++) { b[i] = new Button(); b[i].addActionListener(this); add(b[i]); public void paint(Graphics g) { g.drawString("Count: " + getCount(), 20, 20); } public void actionPerformed(ActionEvent e) { increaseCount(); repaint(); } public synchronized int getCount() { return count; } public synchronized void increaseCount() { count++; } } } }Last edited by Norm; 01-18-2012 at 01:13 PM. Reason: added code tags
- 01-18-2012, 01:17 PM #2
Re: How to call methods in a for loop-- trying to draw a string to count each buton c
Please copy and paste here the full text of the errors from the compiler. Your edited messages from the IDE leave off important information.
Please copy full text of error message and paste it here. Here is a sample:
Check the {}s to be sure the method definitions are not inside of other method definitions.Java Code:TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^
- 01-18-2012, 02:01 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How to call methods in a for loop-- trying to draw a string to count each buton c
Indent your code properly and you'll see you've almost certainly missed some brackets somewhere.
A collection of compilation errors like that is pretty much the compiler saying "I haven't got a bloody clue what you've asked me to compile, here".
- 01-18-2012, 02:42 PM #4
Member
- Join Date
- Jan 2012
- Location
- Kampala, Uganda
- Posts
- 3
- Rep Power
- 0
Re: How to call methods in a for loop-- trying to draw a string to count each buton c
Here's the full error msg, and sorry if my code is a mess just a java newbee
java.lang.Error: Unresolved compilation problems:
void is an invalid type for the variable paint
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
void is an invalid type for the variable actionPerformed
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
Syntax error on token "int", @ expected
Syntax error, insert "enum Identifier" to complete EnumHeaderName
Syntax error, insert "EnumBody" to complete BlockStatement
Syntax error on token(s), misplaced construct(s)
Syntax error on token "void", @ expected
at CountClick.init(CountClick.java:20)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: component argument pData
at sun.awt.windows.Win32SurfaceData.initOps(Native Method)
at sun.awt.windows.Win32SurfaceData.<init>(Unknown Source)
at sun.awt.windows.Win32SurfaceData.createData(Unknow n Source)
at sun.awt.Win32GraphicsConfig.createSurfaceData(Unkn own Source)
at sun.awt.windows.WComponentPeer.replaceSurfaceData( Unknown Source)
at sun.awt.windows.WComponentPeer.replaceSurfaceData( Unknown Source)
at sun.awt.windows.WComponentPeer$2.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
and a better indented code
Java Code:import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.JButton; import javax.swing.JOptionPane; public class CountClick extends Applet implements ActionListener { private static final long serialVersionUID = 1L; private int count = 0; public void init() { Button[] b = new Button[10]; for(int i = 0; i < b.length; i++) { b[i] = new Button(); b[i].addActionListener(this); add(b[i]); public void paint(Graphics g) { g.drawString("Count: " + getCount(), 20, 20); } public void actionPerformed(ActionEvent e) { increaseCount(); repaint(); } public synchronized int getCount() { return count; } public synchronized void increaseCount() { count++; } } } public void paint(Graphics g) { g.drawString("Count: " + getCount(), 20, 20); } public void actionPerformed(ActionEvent e) { increaseCount(); repaint(); } public synchronized int getCount() { return count; } public synchronized void increaseCount() { count++; } }Last edited by Norm; 01-18-2012 at 02:48 PM. Reason: added code tags
- 01-18-2012, 02:51 PM #5
Re: How to call methods in a for loop-- trying to draw a string to count each buton c
You need to learn how to add code tags
Why do you have two paint and following methods?
Your code looks like you did some copy and pasting editing. It needs a serious reworking.
- 01-18-2012, 03:09 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: How to call methods in a for loop-- trying to draw a string to count each buton c
See what proper indenting does?Java Code:public void init() { Button[] b = new Button[10]; for(int i = 0; i < b.length; i++) { b[i] = new Button(); b[i].addActionListener(this); add(b[i]); public void paint(Graphics g) { g.drawString("Count: " + getCount(), 20, 20); } public void actionPerformed(ActionEvent e) { increaseCount(); repaint(); } public synchronized int getCount() { return count; } public synchronized void increaseCount() { count++; } } }
(Note, always use spaces rather than tabs).
That is your init() method.
You should now be able to see what the problem is.
- 01-20-2012, 10:29 AM #7
Member
- Join Date
- Jan 2012
- Location
- Kampala, Uganda
- Posts
- 3
- Rep Power
- 0
Re: How to call methods in a for loop-- trying to draw a string to count each buton c
Thanks very much for the help so far, I c the problem is the methods are within the init(), but when I put them outside then the are not in the loop and i only get one count string....I need a count string for each button, and the two paint and following methods just a mistake as I was playing around with the code
- 01-20-2012, 11:29 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Java Noob, trying to call methods from another methods
By gabrielpr12 in forum New To JavaReplies: 8Last Post: 11-17-2011, 09:07 PM -
Java Plugin 1..6.0.24 fails to draw stock chart with msg: "Error: Click for details
By tonygts in forum Java AppletsReplies: 2Last Post: 02-22-2011, 05:46 PM -
How to start a for loop back to top without starting the var count over?
By AcousticBruce in forum New To JavaReplies: 3Last Post: 01-07-2011, 12:23 AM -
Best way to draw to screen & call game update method?
By dwfait in forum Java AppletsReplies: 2Last Post: 04-03-2009, 05:04 AM -
Trying to use Graphics draw methods
By Lang in forum SWT / JFaceReplies: 5Last Post: 03-26-2008, 05:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks