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.
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.
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.
Please post the code and explain what is not working.why it isn't 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
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.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; } } }
Last edited by Bamanen; February 24th, 2012 at 11:25 AM.
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Draw them shorter.the lines on my grid run past where they should.
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.
Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
done, sorry
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?
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.
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.
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