I am creating a GUI, albeit a simple one, and I want to have a background image (2048 X 2048) fill up the whole window and a square to the left top corner where the occasional 64 X 64 image can be loaded. Thanks in advance to anyone who helps out :) Edit: I already know how to make the JFrame a set size, its the image loading I need help with.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This is a simple example for adding the background image in a JFrame:
The best way to load an image is through the
ImageIO
APIThere are a number of ways you can render an image to the screen.
You could use a
JLabel
. This is the simplest method if you don't want to modify the image in anyway...Then simply add it to your window as you see fit. If you need to add components to it, then you can simply set the label's layout manager to whatever you need and add your components.
If, however, you need something more sophisticated, need to change the image somehow or want to apply additional effects, you may need to use custom painting.
First cavert: Don't ever paint directly to a top level container (like
JFrame
). Top level containers aren't double buffered, so you may end up with some flashing between repaints, other objects live on the window, so changing it's paint process is troublesome and can cause other issues and frames have borders which are rendered inside the viewable area of the window...Instead, create a custom component, extending from something like
JPanel
. Override it'spaintComponent
method and render your output to it, for example...Take a look at Performing Custom Painting and 2D Graphics for more details
I used a very similar method to @bott, but I modified it a little bit to make there be no need to resize the image:
Works every time. You can also get the width and height of the jFrame and use that in place of the 800 and 508 respectively.
You can do:
At first line of the Jframe class constructor, that works fine for me