Results 1 to 20 of 38
Thread: Help needed with methods.
- 07-25-2010, 05:23 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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 05:35 PM.
- 07-25-2010, 05:26 PM #2
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
it replaced : : D (without spaces) with smileys so just know what it is.
- 07-25-2010, 05:29 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
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
- 07-25-2010, 05:35 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
Thanks that worked.
-
This is not Java code:
Java Code:void Ball::Draw() {
- 07-25-2010, 06:36 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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.
- 07-25-2010, 07:36 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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.
- 07-25-2010, 10:29 PM #9
O_o ::
Java uses : in boolean statements the way like
String var=(b<1)?"Mo":"Po";
- 07-25-2010, 10:33 PM #10
That's a ternary operator, not a "boolean statement"
db
- 07-25-2010, 10:51 PM #11
In the code you posted you have the following method definitions:What I didn't understand about methods is how to set them up.
public void init()
public void update(Graphics g)
I guess I don't understand your problem. The posted code has several method defined.
- 07-26-2010, 02:36 AM #12
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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.
- 07-26-2010, 02:46 AM #13
Please post the FULL text of the error message.I have an error that says
Also post the code around where the error is occurring.
- 07-26-2010, 03:33 AM #14
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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(); ^
-
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.
- 07-26-2010, 03:36 AM #16
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)
{
...
}
- 07-26-2010, 04:16 AM #17
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
I have "public void Draw(Graphics g)" in my code just the compiler thinks its just "public void Draw()"
-
- 07-26-2010, 04:26 AM #19
Member
- Join Date
- Jul 2010
- Posts
- 74
- Rep Power
- 0
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); } }
-
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.
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Methods Please help
By bdario1 in forum New To JavaReplies: 33Last Post: 03-25-2010, 03:26 AM -
Sr. Web Methods Developer Needed
By pjhong in forum Jobs OfferedReplies: 1Last Post: 08-11-2009, 03:08 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
help needed with methods in subclasses
By uncopywritable in forum New To JavaReplies: 4Last Post: 08-01-2007, 01:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks