Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-18-2007, 07:33 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-18-2007, 10:10 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-18-2007, 03:11 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Hardwired,

Thanks for the help.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-19-2007, 05:42 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-19-2007, 11:38 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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.
Code:
CreateGridRx test = new CreateGridRx(gridSquares); test.setPreferredSize(new Dimension(calculatedWidth, calculatedHeight)); JFrame f = new JFrame(); f.pack(); // instead of setSize
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-19-2007, 03:24 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Thanks again
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-19-2007, 04:34 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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.

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)); } } }
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); // } }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-19-2007, 10:19 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Check the calculation for the size of the grid:
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.
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); } } }
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-19-2007, 11:31 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 10-23-2007, 06:10 AM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 10-23-2007, 10:15 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 10-24-2007, 02:41 PM
Member
 
Join Date: Jul 2007
Posts: 46
adlb1300 is on a distinguished road
Thanks again fo the help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 06:02 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org