|
|
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.
|
|

10-18-2007, 07:33 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
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:
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
|
|

10-18-2007, 10:10 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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);
}
}
|
|

10-18-2007, 03:11 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Hardwired,
Thanks for the help.
|
|

10-19-2007, 05:42 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
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
|
|

10-19-2007, 11:38 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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.
CreateGridRx test = new CreateGridRx(gridSquares);
test.setPreferredSize(new Dimension(calculatedWidth, calculatedHeight));
JFrame f = new JFrame();
f.pack(); // instead of setSize
|
|

10-19-2007, 03:24 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Thanks again
|
|

10-19-2007, 04:34 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
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.
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));
}
}
}
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); //
}
}
|
|

10-19-2007, 10:19 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
Check the calculation for the size of the grid:
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.
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);
}
}
}
|
|

10-19-2007, 11:31 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
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-23-2007, 06:10 AM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
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:
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
|
|

10-23-2007, 10:15 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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);
|
|

10-24-2007, 02:41 PM
|
|
Member
|
|
Join Date: Jul 2007
Posts: 46
|
|
|
Thanks again fo the help
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|