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

Thread: can someone tell me what's wrong with my code

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

    Default can someone tell me what's wrong with my code

    import java.awt.Color;
    import javax.swing.JFrame;
    import java.awt.Graphics;
    import javax.swing.JOptionPane;
     
    public class Program2 extends JFrame {
     
        private static final int FRAME_SIZE = 500;
        private int numberOfLines;
     
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            Graphics canvas = getContentPane().getGraphics();
            int width = this.getContentPane().getWidth();
            int height = this.getContentPane().getHeight();
     
     
     
        public static void main(String[] args) {
            Program2 guiWindow = new Program2();
            guiWindow.setSize(FRAME_SIZE, FRAME_SIZE);
            guiWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
            String valueString;
            valueString = JOptionPane.showInputDialog("Enter the number of lines in the grid (10-40):");
            guiWindow.numberOfLines = Integer.parseInt(valueString);
            guiWindow.setVisible(true);
     
        }
    }


    It's highlighting the "public static void main" line in red, but when I post the paint method in that section, it highlights all of that in red too.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: can someone tell me what's wrong with my code

    Check your braces and parenthesis. Every open { or ( should match with a closing ) or }.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: can someone tell me what's wrong with my code

    That was my problem, thank you. But now I have another question, I need to have an input trap error so that the user can only enter numbers from 10 to 40. How would I do that, would I use an "if" statement or what?

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

    Default Re: can someone tell me what's wrong with my code

    if (i > 10) && (i <40)

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

    Default Re: can someone tell me what's wrong with my code

    Make a function to get the user input. The use an if to see if the value is in the range. Then if the value is not in the wanted range say "Value must be: blah blah" and then run the function again

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

    Default Re: can someone tell me what's wrong with my code

    Or you could just as easily use a while:

    while( i<10 && i>40)
    {
    //Get input method
    }

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

    Default Re: can someone tell me what's wrong with my code

    To finish the program I have to draw the lines using the following rules:

    -Whatever number the user entered, that’s how many horizontal and vertical lines should be in the grid.
    -The lines should be evenly spaced.
    -The border of the content pane, that is, the part that is between the border of the frame and the grid drawing, must be 10% of the content frame height and width.
    -The grid must stretch and shrink based on the size of the content pane.

    The height of the grid should always be 80% (0.8) of the height of the content pane.
    The top and bottom borders should be 10% (0.1) of the content pane’s height.
    The width of the grid should always be 80% (0.8) of the width of the content pane.
    The left and right borders should be 10% (0.1) of the content pane’s width.

    Resizing the window should cause the grid to distort to stay in place.
    This can be done by using the height and width of the content pane to calculate where to draw your lines.


    I'm totally lost on what the code should be for calculating all of this.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: can someone tell me what's wrong with my code

    Recommended reading: http://www.javaprogrammingforums.com...e-posting.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What's Wrong With My Code?
    By Nuggets in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 31st, 2012, 10:11 PM
  2. What's wrong with my code?
    By mjballa in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 19th, 2011, 03:57 PM
  3. What is wrong in the code
    By Rajiv in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: July 29th, 2011, 12:09 PM
  4. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM
  5. What's wrong with my code
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 10th, 2010, 03:24 PM