1

I am writing a program that uses a JFrame for rendering. This is what my frame setup looks like:

    //Creating the frame.
    frame = new JFrame(this.title);

    frame.setSize(this.width, this.height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    //Creating the canvas.
    canvas = new Canvas();

    canvas.setSize(this.width, this.height);
    canvas.setBackground(Color.BLACK);
    canvas.setVisible(true);
    canvas.setFocusable(false);


    //Putting it all together.
    frame.add(canvas);

    canvas.createBufferStrategy(3);
    bufferStrategy = canvas.getBufferStrategy();
    graphics = bufferStrategy.getDrawGraphics();

When I want to draw something I simply use the object graphics. There is only one issue; I find that the JFrame border is overlapping the draw area. Is there a way to find the native (I assume it changes per OS but I have not tested it) border thickness of the JFrame in order to compensate for that?

EDIT: This question is not a dupe of Drawing in Java using Canvas. The difference is that in that question the 'asker' could not draw anything at all. My problem is that the decoration/border of the JFrame is being placed over the top of some of the draw area. For example if I were to draw something at 0, 0 with the graphics object some of it would be hidden behind the JFrame border.

Community
  • 1
  • 1
Llewv
  • 123
  • 14
  • 1
    Use a layout manager to take care of this for you. Take a look at https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html `BorderLayout` center will probably do. – pvg Jun 06 '16 at 04:35
  • Oh and just as importantly, `Canvas` is not what you want to use if you want to be painting in a swing. This question may be a dupe of http://stackoverflow.com/questions/9612684/drawing-in-java-using-canvas – pvg Jun 06 '16 at 04:47
  • @pvg It is if you want to take control of the paint process and get closer to the hardware rendering level – MadProgrammer Jun 06 '16 at 09:56
  • Override the `Canvas`s `getPreferredSize` method, supply a desired size value. Call `pack` of the frame AFTER you've added the `Canvas` to the frame and BEFORE you call `setVisible` – MadProgrammer Jun 06 '16 at 09:57

0 Answers0