Results 1 to 10 of 10
  1. #1
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default <identifier expected> error. please help!

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class gfxUser
    {
    JFrame f = new JFrame();
    GraphicsObj g = new GraphicsObj();
    f.add(g); // Error is here.
    I'm using BlueJ, and i'm a complete newby
    with it, but I have alot of background in c# and VB, so I'm not a total idiot! :D I just wondered where this error is coming from, can you help please guyys? Thankyou!

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

    Default Re: <identifier expected> error. please help!

    You're trying to call methods outside of a method or constructor. Perhaps you want to do this in the class's constructor?

  3. #3
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: <identifier expected> error. please help!

    I guess I should have been more explicit with the code. The GraphicsObj extends a JPanel, so in theory it should just be like
    adding a JPanel too a Frame? The problem is that isn't working for me!

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

    Default Re: <identifier expected> error. please help!

    Quote Originally Posted by christopherx View Post
    I guess I should have been more explicit with the code. The GraphicsObj extends a JPanel, so in theory it should just be like adding a JPanel too a Frame? The problem is that isn't working for me!
    That has no bearing on the problem. Again, you're trying to make method calls outside of a constructor or method. Again, you must make these calls inside of a constructor or method. Please let me know if you don't understand this as it is a fundamental concept in Java.

  5. #5
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: <identifier expected> error. please help!

    Ah. In that case please tell me what you mean by Methods and constructors. Thankyou! :)

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

    Default Re: <identifier expected> error. please help!

    Quote Originally Posted by christopherx View Post
    Ah. In that case please tell me what you mean by Methods and constructors. Thankyou! :)
    What text or tutorial are you using to learn Java as surely it will describe these things for you. If not, I'm very surprised, but you could also look at the Oracle Java tutorials here: Methods, Constructors

  7. #7
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: <identifier expected> error. please help!

    Sorry! Just dawned on me!! False alarm :)

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

    Default Re: <identifier expected> error. please help!

    Quote Originally Posted by christopherx View Post
    Sorry! Just dawned on me!! False alarm :)
    So you understand? If not:

    Java Code:
    public class Foo {
       // variables can be declared or declared and initialized here:
       int a; // declared
       int b = 3; // declared and initialized, 
       
       // but that's it. You can't do:
       b = b + 3;  // *** illegal ***
       
       // but must do it in a method or constructor:
       
       public void myMethod() {
          b = b + 3;  // this is OK
       }
    }

  9. #9
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: <identifier expected> error. please help!

    While we're here, I've just realised, obviously, that everything needs to be in a method. Completely rookie mistake on my part, sorry! One more thing though, and that's actually going about calling it.

    Java Code:
    public class GraphicsObj extends JPanel
    {
    public void paint(Graphics g)
    {
    // Set up Graphics object.
        super.paintComponent(g);
        this.setBackground(Color.WHITE);
        g.setColor(Color.blue);
    
        // Set Constants.
        final int XCOORD = 50;
        final int YCOORD = 50;
        final int RAD = 30;
        
        double xCurCoord = 0;
        double yCurCoord = 0;
        
        
        // Draw Circle using Circle Theorem.
    for(int t=0;t<=360;t++)
    {
        xCurCoord = XCOORD + (RAD * Math.cos(Math.toRadians(t)));
        yCurCoord = YCOORD + (RAD * Math.sin(Math.toRadians(t)));
        g.drawLine(XCOORD,YCOORD,(int)xCurCoord,(int)yCurCoord);
    }
    }
    }
    Basically, I dont know how to call this. It asks for a parameter of type Graphics, but whenever I try to pass it one when it's called it says that Graphics is an abstract and can not be instantiated. Any help?

  10. #10
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: <identifier expected> error. please help!

    oh my lord im such an idiot..

    Sorry, I just realised i don't need to treat it like that. Thankyou so much for your help, and I'm sorry i've asked two questions which i've answered myself.

Similar Threads

  1. identifier expected error
    By Java Learner in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 08:13 PM
  2. Error:identifier expected(Help!)
    By chhoton in forum New To Java
    Replies: 8
    Last Post: 09-22-2009, 04:42 PM
  3. getting identifier expected error . help me !
    By victorkeath in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 05:49 PM
  4. Identifier expected error
    By vasu18 in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 05:49 PM
  5. Error: <identifier> expected
    By barney in forum AWT / Swing
    Replies: 2
    Last Post: 07-31-2007, 07:38 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
  •