Page 1 of 2 12 LastLast
Results 1 to 20 of 38
  1. #1
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default Help needed with methods.

    I was told that methods and classes in java are alot like c++. I got my classes to work just I have errors calling a method. My errors are:

    Java Code:
                                     
    '(' expected at   void Ball::Draw()
                                     ^
    invalid method declaration; return type required at   void Ball::Draw()
                                                                                       ^
    
    
    Here is my whole code:
    
    import java.applet.*;
    import java.awt.*;
    
    public class BallDraw extends Applet implements Runnable
    {
    
    	private Image dbImage;
    	private Graphics dbg;
    
    	public void init()
    	{
    		setBackground (Color.blue);
    	}
    
    	public void start ()
    	{
    		Thread th = new Thread (this);
    		// Starten des Threads
    		th.start ();
    	}
    
    	public void stop()
    	{
    
    	}
    
    	public void destroy()
    	{
    
    	}
    
    	class Ball
    	{
    		public int size;
    		public int x_pos;
    		public int y_pos;
    		public void Draw();
    	}
    
    	void Ball::Draw()
    	{
    		g.setColor  (Color.red);
    
    		g.fillOval (x_pos - size, y_pos - size, 2 * size, 2 * size);
    	}
    
    	public void run ()
    	{
    		Ball One;
    		One.size = 10;
    		One.x_pos = 50;
    		One.y_pos = 50;
    		One.Draw();
    
    	}
    
    
    	public void update (Graphics g)
    	{
    		if (dbImage == null)
    		{
    			dbImage = createImage (this.getSize().width, this.getSize().height);
    			dbg = dbImage.getGraphics ();
    		}
    
    		dbg.setColor (getBackground ());
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    		dbg.setColor (getForeground());
    
    		g.drawImage (dbImage, 0, 0, this);
    	}
    
    }
    Last edited by pizzadude223; 07-25-2010 at 06:35 PM.

  2. #2
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    it replaced : : D (without spaces) with smileys so just know what it is.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Your code is unreadable because the forum software translated certain combination of characters to silly smilyes. Next time put a [code] tag before you start your code and put a [/code] tag after your code so the indentation will be preserved and it greatly enhances its readability.

    If I'm not mistaken you can edit your original post.

    kind regards,

    Jos

  4. #4
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Thanks that worked.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I know that, did you not read all of my first post or something. How would I make the java equivilant of a method that could be called from the Ball class.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pizzadude223 View Post
    I know that, did you not read all of my first post or something. How would I make the java equivilant of a method that could be called from the Ball class.
    I read your first post but you are not clear on the issue. To make a method that can be called from Ball, you simply declare a valid Java public method as you do elsewhere in your code. You don't state what you don't understand about creating methods that comply with Java syntax requirements. Also it is likely that you'll want to pass a Graphics parameter to your draw method and quite possibly call it from within the applet's paint(Graphics g) method.

  8. #8
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Yes I wish to pass the graphics parameter to my draw method. So to create a valid public method would it be like public void draw()? What I didn't understand about methods is how to set them up.

  9. #9
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    O_o ::

    Java uses : in boolean statements the way like

    String var=(b<1)?"Mo":"Po";

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    That's a ternary operator, not a "boolean statement"

    db

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    What I didn't understand about methods is how to set them up.
    In the code you posted you have the following method definitions:
    public void init()
    public void update(Graphics g)

    I guess I don't understand your problem. The posted code has several method defined.

  12. #12
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Okay I figured some stuff out now, but i still need to have something fixed. I have an error that says "missing method body, or declare abstract" at public void Draw();
    ^

    I don't really understand the error. How would I fix it? In the () at public void Draw I have Graphics g. Yes I did save it.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    I have an error that says
    Please post the FULL text of the error message.

    Also post the code around where the error is occurring.

  14. #14
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    The error is around here:

    Java Code:
    	public void Draw(Graphics g)
    	{
    		g.setColor  (Color.red);
    
    		g.fillOval (x_pos - size, y_pos - size, 2 * size, 2 * size);
    	}
    
    
    all it says for the error is:
    missing method body, or declare abstract
            public void Draw();
                        ^

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't see why that code would result in the error shown. You may need to post the entire class and in the class indicate with a comment the line causing the error. Also post the actual error message itself.

    Luck.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default

    public void Draw(); // the compiler says it is missing method body
    What is the above statement? Looks like one of the dumb things you have to do with a c compiler.

    Try deleting that line. It's not valid java code.
    This is valid java code:
    public void Draw(Graphics g)
    {
    ...
    }

  17. #17
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    I have "public void Draw(Graphics g)" in my code just the compiler thinks its just "public void Draw()"

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by pizzadude223 View Post
    I have "public void Draw(Graphics g)" in my code just the compiler thinks its just "public void Draw()"
    The error is around here:
    You should know exactly where the error is as the compiler will tell you so. Please give us more information so that we can answer this question. We need more code and we need to see exactly where the error occurs.

  19. #19
    pizzadude223 is offline Member
    Join Date
    Jul 2010
    Posts
    74
    Rep Power
    0

    Default

    Java Code:
    	public void Draw(Graphics g) //error is here
    	{
    		g.setColor  (Color.red);
    
    		g.fillOval (x_pos - size, y_pos - size, 2 * size, 2 * size);
    
    		if (dbImage == null)
    		{
    			dbImage = createImage (this.getSize().width, this.getSize().height);
    			dbg = dbImage.getGraphics ();
    		}
    
    		dbg.setColor (getBackground ());
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    		dbg.setColor (getForeground());
    
    		g.drawImage (dbImage, 0, 0, this);
    	}
    
    }

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Something is not quite right and I don't see why the code posted should cause the error mentioned. You may wish to repost the updated code for your class, the current code that is causing the most recent compiler error. Also you may wish to repost the exact error message and indicate by way of a comment which line in the class is causing the error.

    Luck.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Methods Please help
    By bdario1 in forum New To Java
    Replies: 33
    Last Post: 03-25-2010, 04:26 AM
  3. Sr. Web Methods Developer Needed
    By pjhong in forum Jobs Offered
    Replies: 1
    Last Post: 08-11-2009, 04:08 PM
  4. Replies: 4
    Last Post: 04-03-2009, 07:36 AM
  5. help needed with methods in subclasses
    By uncopywritable in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 02:47 PM

Posting Permissions

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