Results 1 to 4 of 4
- 02-19-2012, 06:15 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
drawing a centered grid with JFrame
I am trying to figure out how to draw a grid that is prompted by the user on how many squares wide and high. With a 10% boarder that resizes with the window.
I have it working up to an xy axis in the window.
How do I get it to make a grid of even squares?
Thank you,Java Code:package gridWindow; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JOptionPane; import java.util.Scanner; public class GridWindow 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 numberOfLines; public static void main(String[] args) { GridWindow guiWindow = new GridWindow(); guiWindow.setSize(FRAME_SIZE, FRAME_SIZE); guiWindow.setDefaultCloseOperation(EXIT_ON_CLOSE); Scanner keyboard = new Scanner(System.in); String valueString; //Error check loop do { valueString = JOptionPane.showInputDialog("Enter the number of lines in the grid (10-40): "); guiWindow.numberOfLines = Integer.parseInt(valueString); } while (numberOfLines < MIN_NUM || numberOfLines > MAX_NUM); guiWindow.setVisible(true); } @ Override public void paint(Graphics g) { super.paint(g); Graphics canvas = getContentPane().getGraphics(); int width = this.getContentPane().getWidth(); int height = this.getContentPane().getHeight(); double boarder = width*0.1; int sideBoarder = (int)boarder; canvas.drawLine(width/2, sideBoarder , width/2, height-sideBoarder); canvas.drawLine(sideBoarder, height/2, width-sideBoarder, height/2); } }
Mandi
- 02-19-2012, 08:43 PM #2
Re: drawing a centered grid with JFrame
Take a piece of paper and a pencil, draw out what you want to see and then derive the arithmetic statements to compute the values you need to do the drawing.How do I get it to make a grid of even squares
- 02-19-2012, 09:00 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: drawing a centered grid with JFrame
You should NOT be overriding the paint() method of a JFrame.
Custom painting is done by overriding the paintComponent(...) method of a JPanel (or JComponent) and then you add the panel to the frame. See: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for more information and examples.
- 02-19-2012, 09:01 PM #4
Similar Threads
-
Problem Drawing Grid with BufferedImage.
By Shikatsu in forum Java 2DReplies: 10Last Post: 11-26-2011, 12:01 AM -
drawing a 3D hex-grid map
By samanyu in forum Java GamingReplies: 5Last Post: 07-14-2011, 01:54 AM -
JFrame: JButtons in a grid
By jackal in forum New To JavaReplies: 2Last Post: 06-09-2010, 07:56 PM -
Drawing a grid
By CrystalMoth in forum Java 2DReplies: 11Last Post: 01-10-2010, 05:07 AM -
How do you draw a grid in a JFrame
By Singing Boyo in forum New To JavaReplies: 2Last Post: 03-11-2009, 02:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks