Results 1 to 4 of 4
  1. #1
    adonaldson is offline Member
    Join Date
    Feb 2012
    Rep Power

    Default 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?

    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);
            Scanner keyboard = new Scanner(;
            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);
        @ Override
        public void paint(Graphics 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);
    Thank you,

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: drawing a centered grid with JFrame

    How do I get it to make a grid of even squares
    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.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

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

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: drawing a centered grid with JFrame

    Moved from 'New to Java'

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Problem Drawing Grid with BufferedImage.
    By Shikatsu in forum Java 2D
    Replies: 10
    Last Post: 11-26-2011, 12:01 AM
  2. drawing a 3D hex-grid map
    By samanyu in forum Java Gaming
    Replies: 5
    Last Post: 07-14-2011, 01:54 AM
  3. JFrame: JButtons in a grid
    By jackal in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 07:56 PM
  4. Drawing a grid
    By CrystalMoth in forum Java 2D
    Replies: 11
    Last Post: 01-10-2010, 05:07 AM
  5. How do you draw a grid in a JFrame
    By Singing Boyo in forum New To Java
    Replies: 2
    Last Post: 03-11-2009, 02:31 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