3

I have a JFrame that I've set to a certain size, using setBounds. However, that makes the window, including the borders, that size (which in hindsight makes complete sense). But what I want is for the size of the window it be, say, 800x600 plus the borders. This is important because I'm drawing to a Graphics object from the BufferStategy from the JFrame, but I've been drawing underneath the title bar when using a y value of less than about 20. I imagine different OSs (or even different OS settings) can have different thickness borders, too. I thought the borders were just tacked on a window afterwards, but this doesn't seem to be the case.

So, how do I make the space inside the borders a certain size, regardless of the thickness of the borders? Also, to make my life easier, how do I make point 0, 0 be the top left corner of the viewable contents of the frame?

By the way, using setUndecorated presents it's own problems, so I'm not trying that at the moment.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
AlbeyAmakiir
  • 2,217
  • 6
  • 25
  • 48

6 Answers6

19
  1. Set a preferred size for the content pane.
  2. Pack the frame.
  3. Job done.

E.G.

Fixed Size Content

import java.awt.*;
import javax.swing.*;

class FixedSizeContent {
    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JFrame f = new JFrame("Fixed size content");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                Container c = f.getContentPane();
                c.setBackground(Color.YELLOW);
                // adjust to need.
                Dimension d = new Dimension(400,40);
                c.setPreferredSize(d);
                f.setResizable(false);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Sorry, this does not solve my problem. Let's say I draw a line from (0, 0) to (100, 100). That line starts where the top left of the window is. Not the yellow area in your picture, but the window itself. The line does not appear where the title bar is. This means that anything I draw between y = 0 and y = ~25 is hidden by the bar. Your solution does not change that. It does, however, make it so that, when I go to fullscreen-exclusive mode, nothing gets drawn at all, which is the same problem I had with setUndecorated. – AlbeyAmakiir Oct 15 '11 at 10:25
  • 3
    *"Let's say I.."* ..post an [SSCCE](http://pscode.org/sscce.html) of your best attempt? – Andrew Thompson Oct 15 '11 at 10:35
  • 1
    @kleopatra JaHuhh? Every time I see that comment I figure it has to be a joke, but I just don't get it ( and that makes me sad :( ). What do you mean? – Andrew Thompson Oct 21 '11 at 13:06
  • 1
    sorry, don't want you sad - it's German, the sound a dog makes when it suffers – kleopatra Oct 21 '11 at 13:34
  • 2
    I had a dog once. He never made a sound like that. But then, he never suffered. ;) – Andrew Thompson Oct 21 '11 at 13:38
7

let the drawing component report the size of the graphic as its pref

 @Override
 public Dimension getPreferredSize() {
      return new Dimension(myDrawing.getWidth(), myDrawing.getHeight());  
 } 

then follow @Andrew's bullets 2 and 3

kleopatra
  • 51,061
  • 28
  • 99
  • 211
6

This is important because I'm drawing to a Graphics object from the BufferStategy from the JFrame,

Why are you using a BufferStrategy. That is old AWT code. Swing is double buffered by default.

When doing custom painting in Swing you should extend JPanel (or JComponent) and then override the paintComponent() method. You add this component to the content pane of the frame. Then if you follow kleopatra's advice you won't have a problem.

See the section from the Swing tutorial on Custom Painting for more information and examples.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Oh, so this is a case of me trying to find a solution to the wrong problem. The symptom, not the cause. I'll check that out, and mark you as accepted if all goes well. – AlbeyAmakiir Oct 16 '11 at 00:41
2

You can use this to avoid using Container.

JFrame f=new JFrame("Your title");

Dimension d=new Dimension();

d.setSize(x,y);

f.setSize(d); 
Mike Laren
  • 8,028
  • 17
  • 51
  • 70
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
0

Create Canvas canvas = new Canvas();

and add(canvas); to the JFrame,

create canvas.createBufferStrategy(2);

get BufferStrategy bs = canvas.getBufferStrategy();

create Graphics2D graphics2D = (Graphics2D) bufferStrategy.getDrawGraphics();

draw with graphics2D into a BufferedImage.

show it on screen bufferStrategy.show();

Add controls to canvas of all sorts...

try graphics2D.translate(x,y) to move the canvas to certain position measured against the window edges Insets insets...

Pyrrhic
  • 65
  • 10
GianniTee
  • 147
  • 1
  • 9
0

i got a solution for my project. Its quite an old thread, maybe someone wants the code. Try this

public gfx_CFrame(String _Title, int _Height, int _Width)
{
    super(_Title);

    Dimension Dim = new Dimension(_Width, _Height);

    setMaximumSize(Dim);
    setMinimumSize(Dim);
    setPreferredSize(Dim);
    pack();

    Dim.width  = _Width  + (getWidth()  - getContentPane().getWidth()); 
    Dim.height = _Height + (getHeight() - getContentPane().getHeight());

    setMaximumSize(Dim);
    setMinimumSize(Dim);
    setPreferredSize(Dim);
    pack();
    // ....
}
cPunkt
  • 1