If you’re drawing behind the title bar, all you have to do is add the size of the title bar to the coordinates you’re using. Java has a way to do this that covers the menu bar and any other borders as well. Do the following:
Insets insets = myJFrame.insets();
int xLeft = insets.left;
int xRight = myJFrame.getWidth() - insets.left - insets.right;
int yTop = insets.top;
int yBottom = myJFrame.getHeight() - insets.top - insets.bottom;
That determines the boundaries of the coordinates you can use. Then you just have to add xLeft to all your x-coordinates and add yTop to all your y-coordinates.
You can translate your Graphics object like CommanderKeith did to avoid having to add the coordinates each time, but you may have to translate it back at the end of the paintComponent method to avoid screwing up other paint methods (I’m not entirely sure about this).
Using paintComponent(Graphics g) is much better though. You won’t have to translate coordinates if you do what I describe in the rest of this post.
You don’t call paintComponent(Graphics g). Swing calls it. You just write the method. You should be doing the drawing within the paintComponent(Graphics g) method of your JPanel, not by getting the Graphics from the BufferStrategy. Here is all my BufferStrategy code from my current program:
//create the BufferStrategy - the EventQueue avoids potential deadlock in some
//versions of Java (such as 1.4.1_02)
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {mainFrame.createBufferStrategy(2);}
});
} catch(InterruptedException exception) {
//ignore
} catch(InvocationTargetException exception) {
//ignore
}
I should note that if you disabled the RepaintManager, Swing won’t draw anything for you. If you called “myJFrame.setIgnoreRepaint(true)”, all you have to do is call “myJFrame.repaint()” within your main loop wherever you want to draw the screen. You don’t have to mess around with getting the Graphics object out of the BufferStrategy.
You should do the following when setting up your JPanel:
myJFrame.getContentPane().setLayout(null);
myJFrame.add(myJPanel);
myJPanel.setLocation(0, 0);
Insets insets = myJFrame.insets();
//set the size of the JPanel to the part of the JFrame that doesn't include the title bar
myJPanel.setSize(myJFrame.getWidth() - insets.left - insets.right, myJFrame.getHeight() - insets.top - insets.bottom);
You still need to add the JPanel to the JFrame and do all the other stuff you’re doing now.
This puts the JPanel right under the title bar. You would think that it would have to be “myJPanel.setLocation(insets.left, insets.top)”, but that is not the case. When setting the location, the insets are taken into to account automatically.
If you do that, you shouldn’t have to adjust the coordinates within the JPanel.
Setting the layout to null makes it so that you can just set the coordinates of your Components instead of having a LayoutManager do it for you. You do not necessarily have to set the layout to null - you can just set up your Components and choose an appropriate LayoutManager so that the Components get put at the right spot.
In the program I’m working on now, my main frame has a layout of CardLayout so that I can switch between screens easily.