Results 1 to 12 of 12
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default Help creating a changing grid

    I'm a student who is new to Java. I have a project that requires that I prompt the user for a number ranging from 1 to 5. The number they provide is than to be used to create a grid within a frame of that size. For example if you input 3 a frame with a 3x3 grid inside it will be created. I have been able to create the code for the initial prompt but I'm having trouble getting the input to create the grid. Any tips or help would be greatly appreciated.

    Here is the code that I have so far:

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTable;
    
    public class CreateGrid {
        
        public static void main(String[] args) {
            
            String input = JOptionPane.showInputDialog(
                    "Enter number of rows and columns you want grid to contain\n" +
                    "(For example entering 2 will create a 2x2 grid)\n");
             
              int gridSquares = Integer.parseInt( input ); 
    
              if( gridSquares >= 1 && gridSquares <= 5){
                
           
        }
                 }
            }
    Thanks

    Bill

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;
    
    public class CreateGridRx extends JPanel {
        int gridSize;
        final int PAD = 20;
    
        public CreateGridRx(int size) {
            this.gridSize = size;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            double xInc = (double)(w - 2*PAD)/gridSize;
            double yInc = (double)(h - 2*PAD)/gridSize;
            for(int j = 0; j <= gridSize; j++) {
                double x = PAD + j*xInc;
                g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
            }
            for(int j = 0; j <= gridSize; j++) {
                double y = PAD + j*yInc;
                g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
           }
        }
    
        public static void main(String[] args) {
            String input = JOptionPane.showInputDialog(
                    "Enter number of rows and columns you want grid to contain\n" +
                    "(For example entering 2 will create a 2x2 grid)\n");
            int gridSquares = Integer.parseInt( input ); 
            CreateGridRx test = new CreateGridRx(gridSquares);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }

  3. #3
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Hardwired,

    Thanks for the help.

  4. #4
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Hardwired,

    Part of the project is to also include a 10 pixel bordor between the grid and the edge of the frame. I think I was able to accomplish this by changing the int PAD value in the code to a 10. However, I also need to make each square 100 pixels by 100 pixels in size. I have tried some things but I cannot seem to figure it out. Any hints you could give me?

    Thanks again

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    In CreateGridRx xInc and yInc are the size of each grid cell. So each one would become 100. After you get the user input you can figure the size of the grid: number of cols/rows times 100 plus twice the pad for both width and height. You can call setPreferredSize on the panel and call pack on the JFrame or make sure that the size of the frame is big enough to allow for proper display if you want to use setSize. Or you could put the child panel in a JScrollPane - setting the panels prefSize will allow the scrollPane to properly set its scrollBars.
    Java Code:
    CreateGridRx test = new CreateGridRx(gridSquares);
    test.setPreferredSize(new Dimension(calculatedWidth, calculatedHeight));
    JFrame f = new JFrame();
    f.pack();  // instead of setSize

  6. #6
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Thanks again

  7. #7
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Hardwired sorry to bother you but I was able to get the JFrame to size itself based on the number of grid squares. I also have each square as being 100x100. However at the bottom and right side of the grid I get lines that just keep going. I get the correct number of squares in each direction but than the extra lines appear. Below is the code I have but I cannot seem to pinpoint where the issue lies.

    Java Code:
    import java.awt.*;
    import java.awt.geom.Line2D;
    import javax.swing.*;
    
    public class CreateGridRx extends JPanel {
        int gridSize;
        final int PAD = 10;
        
        public CreateGridRx(int size) {
            this.gridSize = size;
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
           int w = getWidth();
           int h = getHeight();
           //double xInc = (double)(w - 2*PAD)/gridSize;
           //double yInc = (double)(h - 2*PAD)/gridSize;
            double xInc = 100;
            double yInc = 100;
            for(int j = 0; j <= gridSize; j++) {
                double x = PAD + j*xInc;
                g2.draw(new Line2D.Double(x, PAD, x, h-PAD));
            }
            for(int j = 0; j <= gridSize; j++) {
                double y = PAD + j*yInc;
                g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
           }
        }
    }
    Java Code:
    
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTable;
    import java.awt.*;
    
    
    public class CreateGrid {
        
           /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
                  
           // Prompts user for size of grid.  For example if user inputs a 4 a 4x4 
           // grid of squares is created.
            String input = JOptionPane.showInputDialog(
                    "Enter number of rows and columns you want grid to contain\n" +
                    "(For example entering 2 will create a 2x2 grid)\n");
            
             // Takes input and parses it from a string type to an integer type that
            // can be used to generate the grid squares
              int gridSquares = Integer.parseInt( input ); 
         
              
           // Creates frame that holds the grid which is prouced by the CreateGridRx
           // class
            CreateGridRx test = new CreateGridRx(gridSquares);
            int PAD = 10;
            int calculatedWidth = ( 100 + 2*(PAD) )*gridSquares;
            int calculatedHeight = ( 100 + 2*(PAD) )*gridSquares;
            test.setPreferredSize(new Dimension(calculatedWidth, calculatedHeight));
            JFrame f = new JFrame(); // creates new JFrame object
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.pack(); // sets size of frame
            f.setLocation(200,200); // sets location of frame
            f.setVisible(true); // 
                 }
            }

  8. #8
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Check the calculation for the size of the grid:
    Java Code:
    int calculatedSize = 100*gridSquares + 2*PAD;
    int calculatedWidth = ( 100 + 2*(PAD) )*gridSquares;
    System.out.printf("calculatedSize = %d  calculatedWidth = %d%n",
                       calculatedSize, calculatedWidth);
    test.setPreferredSize(new Dimension(calculatedSize, calculatedSize));
    A more elegant way to size the grid/graphic component is to let the graphic class establish its own preferred size. You can compute the size in the class constructor and call setPreferredSize with it or you can override the getPreferredSize method in the class to return the desired size. Here's one way you could do the second option.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class CreateGrid {
        public static void main(String[] args) {
            // Prompts user for size of grid.  For example if user inputs a 4 a 4x4 
            // grid of squares is created.
            String input = JOptionPane.showInputDialog(
                    "Enter number of rows and columns you want grid to contain\n" +
                    "(For example entering 2 will create a 2x2 grid)\n");
    
            // Takes input and parses it from a string type to an integer type that
            // can be used to generate the grid squares
            int gridSquares = Integer.parseInt( input ); 
    
            // Creates frame that holds the grid which is prouced by the CreateGridRx
            // class
            CreateGridRx2 test = new CreateGridRx2(gridSquares);
            JFrame f = new JFrame();  // creates new JFrame object
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.pack();                 // sets size of frame
            f.setLocation(200,200);   // sets location of frame
            f.setVisible(true);
        }
    }
    
    class CreateGridRx2 extends JPanel {
        int gridSize;
        final int cellSize = 100;
        final int PAD = 10;
        
        public CreateGridRx2(int size) {
            this.gridSize = size;
        }
    
        public Dimension getPreferredSize() {
            int n = gridSize*cellSize + 2*PAD;
            return new Dimension(n, n);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            for(int j = 0; j <= gridSize; j++) {
                int x = PAD + j*cellSize;
                g2.drawLine(x, PAD, x, h-PAD);
            }
            for(int j = 0; j <= gridSize; j++) {
                int y = PAD + j*cellSize;
                g2.drawLine(PAD, y, w-PAD, y);
            }
        }
    }

  9. #9
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    Thanks I will give it a try. I really appreciate the help. I'm getting some it but the GUI stuff has been a hard one to grasp.

  10. #10
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default

    I have added an if...else statement to handle situations when the inputted number is outside of the specified range of 1 to 5. If the number is outside of the range a JOptionPane opens containing an error message. I would like to make it so that clicking the ok button on this pane will bring up the input dialog box again. Any ideas on how I would do this?

    Here is the code I have right now for the section:

    Java Code:
    if((gridSquares >= 1) && (gridSquares <= 5)){
                  
           // Creates frame that holds the grid which is produced by the CreateGridRx
           // class
            CreateGridRx test = new CreateGridRx(gridSquares);  // create new grid
            JFrame f = new JFrame(); // creates new JFrame object
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);  // add grid to JFrame 
            f.pack(); // sets size of frame
            f.setLocation(200,200); // sets location of frame
            f.setVisible(true); // show the frame
            } 
            else {
                  // Error message that is shown if inputted number is outside of range.
                  JOptionPane.showMessageDialog(null, "Number must be between 1 and 5", "Input Error", JOptionPane.ERROR_MESSAGE);
            } // end else
    Thanks

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    int gridSquares;
    do {
        String input = JOptionPane.showInputDialog(
            "Enter number of rows and columns you want grid to contain\n" +
            "(For example entering 2 will create a 2x2 grid)\n");
    
        gridSquares = Integer.parseInt( input );
        if(gridSquares < 1 || gridSquares > 5)
            JOptionPane.showMessageDialog(null, "Number must be between 1 and 5",
                                          "Input Error", JOptionPane.ERROR_MESSAGE);
    } while(gridSquares < 1 || gridSquares > 5);

  12. #12
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

Similar Threads

  1. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-23-2008, 12:19 AM
  2. Object locations via grid coordinates HELP.
    By deadman_uk in forum New To Java
    Replies: 4
    Last Post: 11-18-2007, 09:32 PM
  3. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 10:51 AM
  4. Help with Grid Layout
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:03 PM
  5. Grid Resources for Industrial Applications 5.1
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 10:57 AM

Posting Permissions

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