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

Thread: Need help figuring this out its driving me Nutz!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help figuring this out its driving me Nutz!

    Program 2 code.txt

    I have worked days on this and I can't figure it out. I just want to comprehend why it isn't working. If anyone have any tips please let me know. Just simply supposed to be a grid, the only problem i have is with the runoff.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    why it isn't working
    Please post the code and explain what is not working.
    Be sure to wrap the code in code tags: [code=java]<YOUR CODE HERE>[/code] to get highlighting
    Post the full text of any error messages you get

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    package program2;
     
    import javax.swing.JFrame;
    import java.awt.*;
    import javax.swing.JOptionPane;
     
    public class Program2 extends JFrame {
     
        private static final int FRAME_SIZE = 500;
        private static final int MAX_NUM = 40;
        private static final int MIN_NUM = 10;
        private static int lineNumber;
     
    public static void main(String[] args) {
     
        Program2 guiWindow = new Program2 ();
        //Set Frame Size
        guiWindow.setSize(FRAME_SIZE, FRAME_SIZE);
        guiWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
        String valueString;
     
        //Create input error trap to ask for Number of lines in the Grid
        do{
            valueString = JOptionPane.showInputDialog("Enter the Number of Lines in the Grid (10-40)");
            guiWindow.lineNumber = Integer.parseInt(valueString);
        } while(lineNumber < MIN_NUM || lineNumber > MAX_NUM);
     
        //Sets window to Visible
        guiWindow.setVisible(true);
     
        }
     
        @Override
        public void paint(Graphics g) {
        super.paint(g);
        Graphics canvas = getContentPane().getGraphics();
        //Gather width,height information
        int width = this.getContentPane().getWidth();
        int height = this.getContentPane().getHeight();
        //Declare my Variables
        double leftGrid = width * .1;
        int leftOfGrid = (int)leftGrid;
        double rightGrid = width * .9;
        int rightOfGrid = (int) rightGrid;
        double topGrid = height * .1;
        int topOfGrid = (int)topGrid;
        double bottomGrid = height * .9;
        int bottomOfGrid = (int) bottomGrid;
        int numberOfGaps = lineNumber - 1;
        double sVertical = (height * .8) / numberOfGaps;
        int spaceVertical = (int) sVertical;
        double sHorizontal = (width * .8) / numberOfGaps;
        int spaceHorizontal = (int) sHorizontal;
     
        //Draw the Horizontal Lines
         int topY = topOfGrid;
        for(int count = 1; count <= lineNumber; count++){
            canvas.drawLine(leftOfGrid, topY, rightOfGrid, topY);
            topY = topY + spaceVertical;
     
        }
     
        //Draw the Vertical Lines
         int leftX = leftOfGrid;
         for(int count = 1; count <= lineNumber; count++){
             canvas.drawLine(leftX, bottomOfGrid, leftX, topOfGrid);
             leftX = leftX + spaceHorizontal;
         }
     
     
        }
     
     
     
     
    }
    The program compiles fine, I have gotten this far. It's just the fact that the lines on my grid run past where they should.
    Last edited by Bamanen; February 24th, 2012 at 11:25 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    the lines on my grid run past where they should.
    Draw them shorter.

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    It has to be .1(10%) from each border. And the grid must scale when the window is enlarged. So I cant really just draw them shorter without messing one of these scenarios up.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    done, sorry

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    It looks like you have a simple arithmetic problem.
    Take a piece of grid paper and draw the grid and see how the number of grids should be used to compute the x,y values for the corners.
    How do you determine the x,y values for the corners?

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    Dunno guess I just don't see it. Been looking at this program way to much over past 48 hours. I just don't understand why its fine on the left and not the right.

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    If i put 10 into the pane, it is fine. Is it a rounding error because i parsed those doubles as ints? It seems the lines get more out of whack the higher the amount of lines the user specifies.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help figuring this out its driving me Nutz!

    Try computing the x,y values for the four corners of the gird.
    Consider that their positions must be an exact multiple of the number of squares times the size of each square.
    For example if there are 8 sqrs and each is 10 pixels wide then the length of the lines will be 80

Similar Threads

  1. [SOLVED] Help figuring out why my method isn't executing as expected.
    By Herah in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2011, 05:27 PM
  2. Need help figuring out the problem (GUI)
    By Prescott in forum What's Wrong With My Code?
    Replies: 10
    Last Post: August 8th, 2011, 12:01 AM
  3. Figuring Out Object Arrays
    By bengregg in forum Collections and Generics
    Replies: 2
    Last Post: April 5th, 2011, 05:55 AM
  4. I'm sure it is really simple but it is driving me crazy
    By Ademske in forum AWT / Java Swing
    Replies: 2
    Last Post: January 17th, 2011, 07:41 AM
  5. Replies: 0
    Last Post: March 28th, 2010, 01:27 PM