Regarding this short video of basic collision detection:
https://www.youtube.com/watch?v=ptqhnmP8FY0&list=PL6E90696571998DC2
Can someone tell me why the
Its the height of title bar of JFrame
that's why y
starts visible after 22.
Same for x
that starts visible from 2 due to JFrame
outer border width.
Here is the sample code along with snapshot
g.setColor(Color.RED);
g.drawRect(3, 22, 200, 200);
--EDIT--
I never suggest you to use it. Read below comments for it's drawbacks.
The main problem is, overriding paint
of JFrame
is allowing you to paint under the frames decorations.
This is one of, the many, reasons why you should avoid overriding paint
on a top level container.
As discussed:
In Swing it is generally recommended to perform custom painting within the paintComponent
method of a component that extends from JComponent
, like JPanel
.
This not only allows you to decouple of output from a specific container (JFrame
), it also ensures that when added to a top level container, it only occupies the viewable area of the window and does not render under the frames borders.
JComponent
based components also benefit from been double buffered, meaning that you don't need to implement your own double buffering strategy, but it won't flicker between repaints
Also, you MUST call the super method before you perform any of your own custom painting, failing to do so can produce paint artifacts
Take a look at Painting in AWT and Swing and Performing Custom Painting for more details
Side Notes:
You should also avoid the user of KeyListener
as they are notorious for having focus related issues. Instead you should make use of the Key Bindings API