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 2 of 2

Thread: Java applet for Fibonacci grid

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java applet for Fibonacci grid

    I have to create a 7x7 grid that remains proportionally the same even when resized. Each fibonacci number will have to go in each square in order from top to bottom then resuming back to the top. The numbers must be centered horizontally in each grid square.

    I already have the class that allows to create each fibonacci number and I have to use that class into my new class.


    public class Fibs {
    long n=0;
    long myfib;
    long last1;
    long last2;

    public long nextFib(){

    if (n == 0){
    myfib = n;
    last1 = n;
    n++;
    }
    else if (n == 1){
    myfib = n;
    last2 = n;
    n++;
    }
    else{
    myfib = last1 + last2;
    last1=last2;
    last2 = myfib;

    }
    return myfib;

    }


    }

    And this is what I have for the applet.. This was supposed to be an introductory programming class and everyone has told me this is far too advanced for intro so any help is appreciated.



    import javax.swing.JApplet;
    import java.awt.*;

    public class DrawFibs extends JApplet {

    //create new object
    Fibs f = new Fibs ();

    //numbers of squares to draw
    static final int numBoxes = 7;

    public void paint (Graphics page){
    //get the dimensions of the page
    Rectangle r = page.getClipBounds();

    //figure out the number of pixels to skip to make 7x7 squares
    int stepX = r.width / numBoxes;
    int stepY = r.height / numBoxes;

    //converting nextFib method to a string
    for (int i=0; i<=48; i++){
    String s = String.valueOf(f.nextFib());
    //get info about the size of the font in pixels
    FontMetrics m = page.getFontMetrics (page.getFont());

    //w is the width of the string s in pixels
    int w = m.stringWidth(s);

    //h is the height of the font in pixels
    int h = m.getHeight();


    //draw vertical lines
    for(int x=0;x<r.width;x+=stepX){
    page.drawLine(x, 0, x, r.height);
    }
    //draw horizontal lines
    for(int y=0;y<r.width;y+=stepY){
    page.drawLine(0, y, r.width, y);
    }

    //distribute string
    for(int x=0;x<r.width;x+=stepX){
    page.drawString(s, r.width/numBoxes, r.height/numBoxes);

    }
    }
    }
    }


    here is the actual URL to the assignment if I happen to not be explaining it correctly
    http://www.cs.utsa.edu/~dj/cs1713-1/hw2/index.html


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: Java applet for Fibonacci grid

    //distribute string
    for(int x=0;x<r.width;x+=stepX){
    page.drawString(s, r.width/numBoxes, r.height/numBoxes);

    }

    your this for loop should be execute only 49 times to place 49 fibs , but it is inside your outer loop
    so it is executing more and giving unexpected output.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. java grid and colors/graphics
    By ajmukon in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 25th, 2011, 03:38 PM
  2. problem to get Fibonacci series- please help.
    By zoala001 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 3rd, 2011, 01:10 AM
  3. Fibonacci Spiral Help?
    By cmh0114 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 12th, 2010, 09:21 PM
  4. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM
  5. [SOLVED] Problem in generating Fibonacci sequence in java
    By big_c in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 24th, 2009, 08:52 AM