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: what's wrong with my code

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default what's wrong with my code

    hi guys,
    im new to the forum and to Java in general. I'm in the middle of a project at the moment and it just dosen't work. The project is a GUI that draws shapes. my problem is that i can only draw lines .what i need to do is to declare shapetype before i start drawing. how do i do that??.I have included the various classes that I have completed.
    I apologize in advance if I have put this question in the wrong section. Thanks in advance for any help.
    Attached Files Attached Files
    • File Type: txt 1.txt (4.0 KB, 6 views)
    • File Type: txt 2.txt (5.1 KB, 2 views)
    • File Type: txt 3.txt (1.5 KB, 1 views)
    • File Type: txt 4.txt (645 Bytes, 1 views)
    • File Type: txt 5.txt (846 Bytes, 1 views)
    • File Type: txt 6.txt (853 Bytes, 1 views)
    • File Type: txt 7.txt (2.3 KB, 1 views)
    • File Type: txt 8.txt (812 Bytes, 1 views)


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: what's wrong with my code

    Welcome to the forum. Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers.

    Some highlights: Posting code in code tags is preferred over adding code as attachments.

    If you can draw lines, please explain why you can't draw ovals, squares, or any of the other available ready-to-draw shapes.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my code

    hi Greg, Thanks for the info. What I think is that ShapeType is always set to draw lines. if i manually change the variable shapetype i can draw ovals rectangles etc. However when i select rectangles or ovals in the comboBox it will only draw lines. any help would be appreciated.
    thanks

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: what's wrong with my code

    This isn't your problem, but it's an interesting logic error. Think about it:
       // sets the type of shape to draw
       public void setShapeType( int shapeType )
       {
          if ( shapeType < 0 || shapeType > 2 )
             shapeType = 0;
     
          this.shapeType = shapeType;
       } // end method setShapeType

    The root cause of the error you've asked about is that you have added two DrawPanels to the DrawFrame. Get rid of the second DrawPanel and your shapes will appear as chosen by the combobox. If you have trouble finding the second one and eliminating it, let me know.

    Overall, a well designed and programmed project. The TestDraw class looks like it was written in haste or by someone other than the person who wrote the rest of the code, so you might clean that up a bit.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my code

    Hi Greg, fair play on the quick reply. I had spotted the logical error and had rectified it. But that's an old copy. Two drawPanel to the DrawFrame. If you could show me it would be great. Thanks again

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: what's wrong with my code

    Dang. I want you to think about this and figure it out, because it's a common mistake made by those new to working with objects.

    Two instances of DrawPanel are created, one each in two different classes. Both DrawPanel instances are added to the application's single instance of DrawFrame. Think. Where are the TWO places the SINGLE DrawFrame is created and built?

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my code

    hi greg, deleted the instance of DrawPanel in drawFrame and still just draws lines. I get what your saying there. but now what??

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: what's wrong with my code

    So you found the two instances of DrawPanel (good job), but to make the lesson even more useful, you deleted the wrong one. Why is the instance of DrawPanel created in the DrawFrame() constructor the wrong one to delete? Because the instance of DrawPanel created in DrawFrame's constructor is the one to which changes are made when the user makes changes in the interface. Specifically, when the user changes the selected shape in the combo box, the instance of DrawPanel created in DrawFrame is updated with the chosen shape. The shape in the instance of DrawPanel created in the TestDraw class (or whatever it's called) is never changed. I'm wondering how you changed the code so that the shape changes made by DrawFrame aren't being made to a null DrawPanel, but you must have done something to fix that error.

    So, you need to revert to your earlier code modified so that only one instance of DrawPanel is created in DrawFrame's constructor. You could start with the versions of those classes you attached to your first post and delete the instance of DrawPanel created in the test class. I think I commented out two lines in the Test class, and the program worked as you needed it to.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: what's wrong with my code

    hi Greg,
    just tried that this morning and all is well. thanks a milllon for your help. as we say in Ireland your a gentleman.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: what's wrong with my code

    Glad to help, and being of Irish descent, I appreciate the compliment. Good luck!

Similar Threads

  1. what is wrong with my code? please help
    By black.angel in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2012, 02:34 PM
  2. What is wrong with this code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 24th, 2012, 12:31 AM
  3. What is wrong with my code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 23rd, 2012, 02:40 PM
  4. What is wrong with this code?
    By A19 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 11th, 2011, 09:45 PM
  5. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM

Tags for this Thread