1

I am new with programming. I am not sure how to put an object in the center of a frame. This is how far i got:

public class LetSee extends JPanel {

     public void paintComponent(Graphics g) {

          int row;   // Row number, from 0 to 7
          int col;   // Column number, from 0 to 7
          int x,y;   // Top-left corner of square
          for ( row = 0;  row < 5;  row++ ) {

             for ( col = 0;  col < 5;  col++) {
                x = col * 60;
                y = row * 60;
                if ( (row % 2) == (col % 2) )
                   g.drawRect(x, y, 60, 60);

                else
                   g.drawRect(x, y, 60, 60);

             } 

          } // end for row

      }
 }



public class LetSeeFrame extends JFrame {

    public LetSeeFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1900, 1000);
        setVisible(true);
        LetSee let = new LetSee();
        let.setLayout(new BorderLayout());
        add(let,BorderLayout.CENTER);
        setLocationRelativeTo(null);  
    }

    public static void main(String[] args) {
        LetSeeFrame l = new LetSeeFrame();  
    }
}
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
getXgetY
  • 61
  • 1
  • 9
  • Do you mean 'centre' as in border layout, or 'centre' as in the geometric actual centre? – Steve Smith Jan 23 '17 at 09:43
  • Don't forget to call `super.paintComponent` before you doing any custom painting, strange things will start doing wrong if you don't. Also `paintComponent` doesn't need to `public`, no one should ever call it directly – MadProgrammer Jan 23 '17 at 10:40
  • See also [this answer](http://stackoverflow.com/a/7181197/418556) - but that depends on the custom component returning a logical preferred size. – Andrew Thompson Jan 23 '17 at 10:46
  • @MadProgrammer : You're right, I quoted your comment in the answer . – Arnaud Jan 23 '17 at 11:04
  • @SteveSmith actually as border layout...sry but Barger had good solution to,but is hard to manage.. :/ I am learning this for now :) – getXgetY Jan 23 '17 at 13:53

1 Answers1

2

Actually your panel is centered in the frame, but what it draws isn't.

You should make use of the width and the height of the JPanel to center the drawing .

Also put your sizes and numbers into variables, it is less error-prone when you use them several times in your code .

Finally as @MadProgrammer said in the comments :

Don't forget to call super.paintComponent before you doing any custom painting, strange things will start doing wrong if you don't. Also paintComponent doesn't need to be public, no one should ever call it directly

import java.awt.Graphics;

import javax.swing.JPanel;

public class LetSee extends JPanel {

    public void paintComponent(final Graphics g) {

        super.paintComponent(g);

        int row; // Row number, from 0 to 7
        int col; // Column number, from 0 to 7
        int x, y; // Top-left corner of square

        int maxRows = 5;
        int maxCols = 5;

        int rectWidth = 60;
        int rectHeight = 60;

        int maxDrawWidth = rectWidth * maxCols;
        int maxDrawHeight = rectHeight * maxRows;

        // this is for centering :
        int baseX = (getWidth() - maxDrawWidth) / 2;
        int baseY = (getHeight() - maxDrawHeight) / 2;

        for (row = 0; row < maxRows; row++) {

            for (col = 0; col < maxCols; col++) {
                x = col * rectWidth;
                y = row * rectHeight;
                if ((row % 2) == (col % 2)) {
                    g.drawRect(baseX + x, baseY + y, rectWidth, rectHeight);
                } else {
                    g.drawRect(baseX + x, baseY + y, rectWidth, rectHeight);
                }

            }

        } // end for row

    }
}
Arnaud
  • 17,229
  • 3
  • 31
  • 44