Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: issue with drawing shapes bottom right to top left

  1. #1
    Carm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default issue with drawing shapes bottom right to top left

    Hello everyone, I am having issues with drawing shapes from bottom right to top left.

    Issue:
    - g.drawRect() will show like I am calling g.fillRect()
    - other shapes will not even show the shape in that area
    - my code (not my program) works on my classmates laptop (I gave her the code help, so her code is the same as mine)
    - my program does the same thing on my friends pc as it does to mine

    Needs:
    - g.drawSHAPE needs to show and not be filled unless I have my fill checkbox selected

    The Program:
    - Create a JFrame with a draw panel and a component panel
    - have a combobox with shapes that, when selected, will draw that shape in the draw panel
    - have a button that, when clicked, will launch JColorChooser to change the color of the drawn shape (draw panel is set to black)
    - have a checkbox that, when checked, fills the shape
    - have mouse listeners to adjust X and Y and will instantly update the shapes size to where you drag/click/press/release

    Code for my drawRect():

    Java Code:
    // if statement to check if mouse drag X is less than starting X
    if(x2 <= x){
        if(emptyORfill.isSelected())  // emptyORfill is my JCheckBox
            g.fillRect(x2, y, x-x2, y2-y);  // x-x2 is the same as Math.abs(x2-x)
        else                                       
            g.drawRect(x2, y, x-x2, y2-y);
    
    // if statement to check if mouse drag Y is less than starting Y
    }else if(y2 <= y){
        if(emptyORfill.isSelected())
            g.fillRect(x, y2, x2-x, y-y2);
        else
            g.drawRect(x, y2, x2-x, y-y2);
    
    // if statement to check if mouse drag X and Y is less than starting X and Y (where the issue is)
    }else if(x2 <= x && y2 <= y){
        if(emptyORfill.isSelected())  
            g.fillRect(x2, y2, x-x2, y-y2);  
         else
            g.drawRect(x2, y2, x-x2, y-y2);  
    
    // if mouse drag X and Y are not less then either starting X or Y, draw normally
    }else{
         if(emptyORfill.isSelected())
             g.fillRect(x, y, x2-x, y2-y);
          else
             g.drawRect(x, y, x2-x, y2-y);
     } // end if
    This is just for my Rectangle. This will show a filled rectangle when both mouse drag X and Y are less then the starting X and Y. If I take this fully functional code and adapt it to drawRoundRect(), the round rectangle wont even show the shape when mouse drag X and Y are less than the starting X and Y but will be fine if one or the other is less than the starting X or Y. NOTE: This same exact code worked on my classmates laptop in her program that I helped her with, but in my program on her laptop it did not. She took out the "else" in the else if's and just made them if statements all the way down and it worked on her laptop in my program, but the same "fix" did not work on my pc. I gave my program to my buddy to test, and he also gets my issue.

    My mouse listener just sets X and Y values in my Shape class that updates my shape methods. I have an item listener for my comboBox that sets default values when a new selection is made and enables/disables editable on my fill checkbox for certain shapes. My action listener looks for the button click and the checkBox click.

    If you need any other code, let me know.

    Thanks,

    Carm

  2. #2
    Carm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: issue with drawing shapes bottom right to top left

    Solved... The order of the if statements needed to be changed. I guess this matters on some PC's and not on others. I put (x2 <= x && y2 <= y) check first and it works perfectly. Funny how a whole day of trying things got me nowhere, but getting a thought 5 minutes after posting my issue solves it :(

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: issue with drawing shapes bottom right to top left

    IMO you only made the problem go away, you didn't solve it. You would have solved it if you could properly explain WHY the changes made an impact.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,388
    Rep Power
    5

    Default Re: issue with drawing shapes bottom right to top left

    Quote Originally Posted by Carm View Post
    I guess this matters on some PC's and not on others.(
    Unlikely. More likely is that you have a design flaw. Best to take Gimbal's advice and ensure you understand what is going on.

    Regards,
    Jim
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Carm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: issue with drawing shapes bottom right to top left

    See, I thought I did know what was going on until you said that...

    I am still in school and this is my first draw/GUI program, so it is true I may be missing something. Where should I look next if all I did was hide my issue? What code would you like to see?

    Thanks,

    Carm

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,703
    Rep Power
    5

    Default Re: issue with drawing shapes bottom right to top left

    Quote Originally Posted by Carm View Post
    See, I thought I did know what was going on until you said that...
    Let me quote you earlier:

    I guess this matters on some PC's and not on others
    Guessing is not knowing. So stop guessing and start explaining: why did changing the order of things make a difference? YOU have to explain it, not us so showing us more code isn't going to make a difference. Or you can stick to what you have now and be content with it. If you're still in school then I wouldn't spend too much time trying to figure out the inner workings of Swing and focus more on what is important; learning the language and trying to figure out generic programming theory. That's already difficult enough for now.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    Carm is offline Member
    Join Date
    Mar 2014
    Posts
    7
    Rep Power
    0

    Default Re: issue with drawing shapes bottom right to top left

    I honestly didn't know why I was getting the issue. The only reason I tried what I did with putting the X2 and Y2 check before the just X2 and just Y2 check was I thought that, for some reason, the order of the if statements mattered. I thought that, by putting the check for just X2 and just Y2 before the check for both, when I would go "negative" with an X and cross over "negative" with a Y, the (X2 <= x) was still being called and not displaying the Y correctly. Now, after you have made me think about it, putting the X and Y check inside the just X and just Y check might be a better solution.

    Thanks,

    Carm

Similar Threads

  1. Swing Timer issue for Drawing Shapes
    By angelusofnex in forum New To Java
    Replies: 7
    Last Post: 10-10-2012, 05:24 AM
  2. Drawing Shapes on to a Jpanel
    By pepsi in forum AWT / Swing
    Replies: 7
    Last Post: 03-09-2012, 05:39 PM
  3. Drawing different shapes with stars.
    By Amazineous in forum New To Java
    Replies: 23
    Last Post: 11-14-2011, 03:55 PM
  4. Replies: 3
    Last Post: 07-04-2011, 12:50 PM
  5. drawing shapes in java help
    By alphajoseph in forum Java 2D
    Replies: 2
    Last Post: 09-29-2009, 06:35 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
  •