0

Why if i set a width and height of a frame to 400 for example the usable space is smaller and how i can work around it and how i can place someting in the center without the content being cut? Example

   public class Main extends JPanel{

    static int width = 400;
    static int height = 400;
    static int arcWidth = 400;
    static int arcHeight = 400;

    public static void main(String args[]){

        JFrame frame = new JFrame();
        JFrame mainContent = new JFrame();
        Main panel = new Main();
        frame.setSize(width, height);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Clock");
        frame.add(panel);
        frame.setVisible(true);


    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g.drawArc(width/2 -arcWidth/2, height/2 - arcHeight/2, arcWidth, arcHeight, 0, 360);
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Put `frame.setLocationRelativeTo(null);` after `frame.setVisible(true);`; see also [this question](https://stackoverflow.com/questions/3480102/java-jframe-setlocationrelativetonull-not-centering-the-window-on-ubuntu-10-0) – pzaenger Feb 19 '18 at 00:11
  • 3
    Possible duplicate [How can I set in the midst?](https://stackoverflow.com/questions/13734069/how-can-i-set-in-the-midst/13734319#13734319); [How to get the EXACT middle of a screen, even when re-sized](https://stackoverflow.com/questions/13457237/how-to-get-the-exact-middle-of-a-screen-even-when-re-sized/13460914#13460914) – MadProgrammer Feb 19 '18 at 00:13
  • @pzaenger I'm left scratching my head over that comment. Why would you need to call `setLocationRelativeTo` AFTER `setVisible`, this would make the frame appear (by default at `0x0`), but which would then make the frame "jump" to the centre of the screen. I "think" the OP is confused over the fact that the window border is occupying space that they expected to be to be given to the content area (ie why is the content space less then size set via `setSize`) – MadProgrammer Feb 19 '18 at 00:15
  • @MadProgrammer Guess, I should delete my comment. I am not that pretty sure, but it should work with `frame.pack();`, which the OP does not use. Though, my comment is wrong. – pzaenger Feb 19 '18 at 00:17
  • 1
    @pzaenger The OP is making some fundamental mistakes based on a misunderstanding/lack of knowledge over how the API works - story of our lives :P – MadProgrammer Feb 19 '18 at 00:18

1 Answers1

1

Start by using "actual" values and not "magic" values

getWidth and getHeight will tell you what the "actual" size of the component is

@Override 
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(Color.BLACK);
    g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    g.drawArc(getWidth()/2 -arcWidth/2, getHeight()/2 - arcHeight/2, arcWidth, arcHeight, 0, 360);
}

As to why you're having issues, see:

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366