Center Align a rectangle in Java Frame/Panel [closed]

Basically, you draw what ever you want around the center of the component, based on it’s width and height

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int width = getWidth() - 1;
    int height = getHeight() - 2;
    g.drawLine(width / 2, 0, width / 2, height);
    g.drawLine(0, height / 2, width, height / 2);
}

Check out Performing Custom Painting and 2D Graphics for details

Browse More Popular Posts

Leave a Comment