简体   繁体   中英

How to get the inner frame in java swing?

I was building a simplistic "game" as sort of tutorial into developing apps or such in java. My Main class extends JFrame . It was just moving a ball around the screen. After getting to the point where I can move the ball can move I started implementing collision with the boundaries of the window and spent several minutes trying to figure out why the only went some beyond the border on 3 sides and then far beyond the top before I realized that the boundaries were being obeyed but that they were the edges of the actual window, beyond the display area.

How would I set up the Main class so that the boundaries are the visible area? Would it extend a different class? And then what it be inside of something else?

  • It's hard to say what you may be doing wrong without code,
  • but for one thing, you should never be drawing directly in the JFrame and should avoid extending JFrame.
  • Instead extend JPanel, get it's boundaries, and draw in this JPanel.
  • Also, make sure to override the JPanel's paintComponent(...) method,
  • to also call the super.paintComponent(g) inside the override.
  • Put the JPanel into a JFrame for display.
  • Make sure to check out the Swing drawing tutorials before doing this coding because often you have to change basic assumptions when doing graphics program and especially animation programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM