Results 1 to 9 of 9

Thread: Any help?

  1. #1
    matiasfretes1 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Post Any help?

    Hi guys! first of all, awesome forum, ive read amazing thing that you did with java, just amazing.
    Well, my name is Matt, im from Argentina and i just got "Java how to program 9th edition", great book, but im really stuck in the chapter 4, control statements part 1.
    My question is if any of you could help me through a case study in which i have to make, some kind of fan using only while statemenst and the drawline method.
    This is the image of what i have to make:Any help?-image.jpg

    This is what i have so far:

    Java Code:
    package drawpaneltest;
    
    import java.awt.Graphics; 
    import javax.swing.JPanel;
    
    class DrawPanel extends JPanel{
       
       public void paintComponent( Graphics g )
       {
            
           int n=1;
           int widht=getWidth();
           int height=getHeight();
           
           while(n<15){
               contador++;
               g.drawLine(0, 0, height/15, widht/15);
               
           }
           g.drawLine(0,height,widht,0);
          
       }
    }
    I would really appreciate any help.

    Regards.

    Matt.
    Last edited by Fubarable; 08-08-2012 at 07:05 PM. Reason: code tags added

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

    Default Re: Any help?

    Moderator Edit: code tags ([code] [/code]) added to your original post so that your posted code will retain its formatting and be easier to read.

    One problem: Your while loop will go on forever since it is checking the value of n, but you never change n from within the while loop and instead change a completely different variable, contador. Another problem, is that you don't use the loops index variable, n or contador, when figuring out which line to draw.

    I suggest that you do the drawing on paper and figure out the logic of how the lines go where, and then use this knowledge in constructing your code algorithm.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Any help?

    Also, welcome to the java-forums.org.

    Some suggestions that may help us help you:
    • Use [code] [/code] around your code when posting code to this forum. This allows your code to retain its formatting, making it much easier to read.
    • Please use an informative title for your question. "any help" doesn't tell us much about your underlying problem other than you need help. But of course you need help, otherwise you wouldn't be here. Instead use the question title kind of like the headline of a newspaper article -- a very brief statement that summarizes your problem in an informative way. Perhaps a better title for your question would be something like: "Using a while loop to draw lines on a JPanel", or something similar. This would help attract the forum volunteers who best understand Swing graphics to look at your question.


    Again welcome to our forum, and please post below if you have any questions about the recommendations above, or in my previous post to this thread.

  4. #4
    matiasfretes1 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Any help?

    Thank you very much!!!!! ill keep that in mind for next postings, well...
    I still stuck, maybe is a lack of practice, but i cant figure out yet the solution in paper...
    I do know how to use while statements, but my issue comes when i try to, decrement the lines...
    Ive been trying to solve this for a whole week and still cant figure out :(.

    Again, sorry for the post error, wont happen again.

  5. #5
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Any help?

    first you need to draw an arc, which is a quarter segment of a circle

    from that arc at regular intervals (6 degrees) you need to calculate a point on that arc.
    the end point of the lines from those points would be a point on another intangible arc which has a radius 200 pixels larger than the visible arc, rotated + or - 60 degrees.

    to do this i'd use 4 arrays:

    int[] degrees = new int[60];
    float[] radians = new float[60];
    Point[] points1 = new Point[60];
    Point[] points2 = new Point[60];

    use a while loop with a counter that starts at 1 to populate those arrays:

    degrees = counter * 6
    radians = PI * degrees / 180

    point1.x = width + Math.sin(radians) * width
    point1.y = 0 - Math.cos(radians) * height

    point2.x = width + Math.sin(radians) * (width + 200)
    point2.y = 0 - Math.cos(radians) * (height + 200)

  6. #6
    matiasfretes1 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: Any help?

    Quote Originally Posted by .paul. View Post
    first you need to draw an arc, which is a quarter segment of a circle

    from that arc at regular intervals (6 degrees) you need to calculate a point on that arc.
    the end point of the lines from those points would be a point on another intangible arc which has a radius 200 pixels larger than the visible arc, rotated + or - 60 degrees.

    to do this i'd use 4 arrays:

    int[] degrees = new int[60];
    float[] radians = new float[60];
    Point[] points1 = new Point[60];
    Point[] points2 = new Point[60];

    use a while loop with a counter that starts at 1 to populate those arrays:

    degrees = counter * 6
    radians = PI * degrees / 180

    point1.x = width + Math.sin(radians) * width
    point1.y = 0 - Math.cos(radians) * height

    point2.x = width + Math.sin(radians) * (width + 200)
    point2.y = 0 - Math.cos(radians) * (height + 200)


    thanks for your help paul :) , but according to the chapter im reading, i have to write this code using only while statements, i havent seen arrays and methods from math library yet.
    Again, thx for you help.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Any help?

    Quote Originally Posted by .paul. View Post
    first you need to draw an arc
    Nope. There's no arc in the illustration.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    .paul. is offline Member
    Join Date
    Jun 2012
    Posts
    73
    Blog Entries
    1
    Rep Power
    0

    Default Re: Any help?

    Quote Originally Posted by DarrylBurke View Post
    Nope. There's no arc in the illustration.
    ok i see it now. there is no arc. it's easier than that. it's an illusion

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Any help?

    Do your math: asssuming a square JComponent with sizes SxS and a coordinate system with the origin (0,0) in the upper left corner you have straight lines between the points (x, S) and (0, x); iterate of the values of x.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •