Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Help please.

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Help please.

    I'm in an intro to Java course, and I'm trying to compile some code from my book - however I can't get it to compile.

    I'm using JBuilder Person 9.0, and I've never had trouble before getting source to compile with it.

    The error I'm getting is:
    java.lang.Error: Do not use DrawArcs.add() use DrawArcs.getContentPane().add() instead
     
    	at javax.swing.JFrame.createRootPaneException(JFrame.java:458)
     
    	at javax.swing.JFrame.addImpl(JFrame.java:484)
     
    	at java.awt.Container.add(Container.java:307)
     
    	at DrawArcs.<init>(DrawArcs.java:8)
     
    	at DrawArcs.main(DrawArcs.java:13)
     
    Exception in thread "main"

    And here is the source code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Graphics;
     
    public class DrawArcs extends JFrame {
      public DrawArcs() {
        setTitle("DrawArcs");
        add(new ArcsPanel());
      }
     
      /** Main method */
      public static void main(String[] args) {
        DrawArcs frame = new DrawArcs();
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(250, 300);
        frame.setVisible(true);
      }
    }
     
    // The class for drawing arcs on a panel
    class ArcsPanel extends JPanel {
      // Draw four blazes of a fan
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
     
        int xCenter = getWidth() / 2;
        int yCenter = getHeight() / 2;
        int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);
     
        int x = xCenter - radius;
        int y = yCenter - radius;
     
        g.fillArc(x, y, 2 * radius, 2 * radius, 0, 30);
        g.fillArc(x, y, 2 * radius, 2 * radius, 90, 30);
        g.fillArc(x, y, 2 * radius, 2 * radius, 180, 30);
        g.fillArc(x, y, 2 * radius, 2 * radius, 270, 30);
      }
    }

    Any help would be greatly appreciated, thanks.

  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Help please.

    Hi Bruzurk,
    can u tell me what do u jdk's version use for compiling?
    I'm using jdk1.6.0.18 and I don't have your problem.

    Bye

  3. The Following User Says Thank You to lucasantos For This Useful Post:

    Bruzurk (November 7th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help please.

    Quote Originally Posted by lucasantos View Post
    Hi Bruzurk,
    can u tell me what do u jdk's version use for compiling?
    I'm using jdk1.6.0.18 and I don't have your problem.

    Bye
    Hey, thanks for the reply. I got it to compile using the command prompt (javac DrawArcs.java), but when I try to run the class file it gives me an error, does it give you an error aswell?

  5. #4
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Help please.

    I see a draw like a ventilator.
    I don't have runtime error.

    If you're using the command prompt for compiling, you can type
    java -version
    and you know your jdk's version.

  6. The Following User Says Thank You to lucasantos For This Useful Post:

    Bruzurk (November 7th, 2010)

  7. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help please.

    Quote Originally Posted by lucasantos View Post
    I see a draw like a ventilator.
    I don't have runtime error.

    If you're using the command prompt for compiling, you can type
    java -version
    and you know your jdk's version.
    I believe I'm using the same version as you. My JDK folder name is: jdk1.6.0_18

  8. #6
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Help please.

    I'm using Eclipse. Can u try with it?

  9. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help please.

    This looks like a runtime and not compile time error. Did you change the add to getContentPane().add, which is what the error is telling you to use?
    (see Using Top Level Components)
    Last edited by copeg; November 5th, 2010 at 09:03 AM.

  10. The Following User Says Thank You to copeg For This Useful Post:

    Bruzurk (November 7th, 2010)

  11. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 6 Times in 3 Posts

    Default Re: Help please.

    Bruzurk,

    I also ran your code using Eclipse and it compiled fine for me. The code itself looks fine.

    I would strongly recommend downloading Eclipse for your Java development as it has great advantages including a very useful debugger and great error detection before runtime. This is a great tool to have especially if your going to take more Java classes. Its available at "www.eclipse.org/downloads" and its completely free.

    Dejan

  12. The Following 3 Users Say Thank You to Dejan For This Useful Post:

    Bruzurk (November 7th, 2010), javapenguin (November 5th, 2010), JavaPF (November 5th, 2010)

  13. #9
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Help please.

    As tell you copeg, if DrawArcs constructor is

    public DrawArcs() {
    setTitle("DrawArcs");
    getContentPane().add(new ArcsPanel());
    }

    u don't have problems

  14. #10
    Junior Member
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Help please.

    Re-installed the JDK, and started using Netbeans - it made everything work fine. Thanks for the help everyone.