Results 1 to 8 of 8
  1. #1
    jeff30 is offline Member
    Join Date
    Jan 2012
    Location
    Kampala, Uganda
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    Check the {}s to be sure the method definitions are not inside of other method definitions.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default 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".

  4. #4
    jeff30 is offline Member
    Join Date
    Jan 2012
    Location
    Kampala, Uganda
    Posts
    3
    Rep Power
    0

    Default 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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default 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.

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: How to call methods in a for loop-- trying to draw a string to count each buton c

    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++;
            }
        }
    }
    See what proper indenting does?
    (Note, always use spaces rather than tabs).
    That is your init() method.
    You should now be able to see what the problem is.

  7. #7
    jeff30 is offline Member
    Join Date
    Jan 2012
    Location
    Kampala, Uganda
    Posts
    3
    Rep Power
    0

    Default 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

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: How to call methods in a for loop-- trying to draw a string to count each buton c

    SO what does your code look like now, and what exactly is the problem?

Similar Threads

  1. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  2. Replies: 2
    Last Post: 02-22-2011, 05:46 PM
  3. Replies: 3
    Last Post: 01-07-2011, 12:23 AM
  4. Replies: 2
    Last Post: 04-03-2009, 05:04 AM
  5. Trying to use Graphics draw methods
    By Lang in forum SWT / JFace
    Replies: 5
    Last Post: 03-26-2008, 05:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •