Java Upper Boundary Graphics

后端 未结 2 1719
南旧
南旧 2020-12-22 07:10

Regarding this short video of basic collision detection:

https://www.youtube.com/watch?v=ptqhnmP8FY0&list=PL6E90696571998DC2

Can someone tell me why the

相关标签:
2条回答
  • 2020-12-22 07:30

    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);
    

    enter image description here

    --EDIT--

    I never suggest you to use it. Read below comments for it's drawbacks.

    0 讨论(0)
  • 2020-12-22 07:40

    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:

    • How to get the EXACT middle of a screen, even when re-sized
    • Java AWT drawString() does not display on window
    • How can I set in the midst?
    • Java graphic image
    • Graphics rendering in title bar

    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

    0 讨论(0)
提交回复
热议问题