Keselme
Keselme

Reputation: 4249

How to center JPanel on the screen

I am trying to do something similar to "game of life" using JPanel, the problem is that it appears in left upper side of the screen. My question is how can I make it screen centered. P.S I also tried creating JFrame and adding JPanel to it, but it just created JFrame and JPanel separately. Here is the code:

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JPanel;
public class Animation extends JPanel implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Cell[][] cellMatrix;
private Options op;

Animation(Options received) {

this.op = received;
this.cellMatrix = new Cell[op.getNumberOfCells()][op.getNumberOfCells()];
this.setBackground(op.getBackGroundColour());

}

Timer time = new Timer(5,this);

public void paintComponent(Graphics g) {

    super.paintComponent(g);
    for (int i = 0; i < op.getNumberOfCells() ; i++) {
        for(int j = 0; j < op.getNumberOfCells(); j++) {    
        Cell c1 = new Cell();

        if(c1.getVal() % 2 == 0) {

            g.setColor(op.getAliveCellColour());
            g.fillRect(i * 10, j * 10, 10, 10);
            c1.changeStat(0);
            cellMatrix[i][j]= c1;

        }
        else {

            g.setColor(op.getDeadCellColour());
            g.fillRect(i * 10,j * 10, 10, 10);
            c1.changeStat(1);
            cellMatrix[i][j]= c1;

        }
        }
    }
    time.start();

    for (int i = 0; i < op.getNumberOfCells() ; i=i+2) {
        for (int j = 0; j < op.getNumberOfCells()-1; j++){
            int sum = cellMatrix[i][j].getVal() + cellMatrix[i+1][j].getVal()+ cellMatrix[i][j+1].getVal() + cellMatrix[i+1][j+1].getVal();
            switch(sum) {
            case(2) : continue;
            case(0) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(1) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(4) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(3) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();blockShift(i,j);break;
            }
        }
    }
    for (int i = 1; i < op.getNumberOfCells()-1 ; i=i+2) {
        for (int j = 1; j < op.getNumberOfCells()-1; j++){
            int sum = cellMatrix[i][j].getVal() + cellMatrix[i+1][j].getVal()+ cellMatrix[i][j+1].getVal() + cellMatrix[i+1][j+1].getVal();
            switch(sum) {
            case(2) : continue;
            case(0) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(1) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(4) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();break;
            case(3) : cellMatrix[i][j].reverseState(); cellMatrix[i+1][j].reverseState(); cellMatrix[i][j+1].reverseState(); cellMatrix[i+1][j+1].reverseState();blockShift(i,j);break;
            }
        }
    }
}

public void blockShift(int i, int j) {

    Cell temp1 = new Cell(this.cellMatrix[i][j]);
    this.cellMatrix[i][j]=this.cellMatrix[i+1][j+1];
    this.cellMatrix[i+1][j+1] = temp1;
    Cell temp2 = new Cell(this.cellMatrix[i+1][j]);
    this.cellMatrix[i+1][j] = this.cellMatrix[i][j+1];
    this.cellMatrix[i][j+1] = temp2;

}

public void actionPerformed(ActionEvent e) {
    time.setDelay(1000);
    repaint();
}

}

Here is the part where I try to use JFrame:

public class Animation extends JPanel implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Cell[][] cellMatrix;
private Options op;
private JFrame jf;

Animation(Options received) {

jf = new JFrame("Example");
jf.setSize(860, 640);
jf.setLocationByPlatform(false);

this.op = received;
this.cellMatrix = new Cell[op.getNumberOfCells()][op.getNumberOfCells()];
this.setBackground(op.getBackGroundColour());
jf.add(this);
jf.setVisible(true);

}[![enter image description here][1]][1]

enter image description here

Upvotes: 1

Views: 5327

Answers (2)

gpasch
gpasch

Reputation: 2682

You have a JPanel: you add it to a JFrame:

Then to center the JFrame on the screen you do

  Toolkit it=Toolkit.getDefaultToolkit();
  Dimension d=it.getScreenSize();
  int w=jf.getWidth(), h=jf.getHeight();
  jf.setLocation(d.width/2-w/2, d.height/2-h/2);

Upvotes: 1

user3437460
user3437460

Reputation: 17454

If you want to let the frame appear at the center of your screen, you can set the location by:

frame.setLocationRelativeTo(null);

Note that you would want to invoke the above after you set the size (or pack()) your frame.

Upvotes: 5

Related Questions