Nihilish
Nihilish

Reputation: 105

Working with local image or audio files in Swing applications?

I'm working on a small Swing game, for which I'm designing custom JButton icons and sound effects myself. I'm using IntelliJ and I want to know what is the simplest, most efficient way of referencing those files. As in, where should I put my source files in my project structure and how should I "import" them in my code as say, an ImageIcon. Alternatively, is there an IntelliJ feature making the process faster? I looked up other threads and it was either very specific to the OP's project structure, or very convoluted for something as simple as using a custom image. I already know how to use a file from the Internet, but not local files.

Upvotes: 0

Views: 84

Answers (1)

Adrian Sowiński
Adrian Sowiński

Reputation: 93

Maybe I don't understand your question but if you look for Swing project management you can look at How To Structure a Swing Application.

If you are using JButton(String text, Icon icon) constructor (Icon is interface that is implemented by ImageIcon), and ImageIcon(String filename) constructor for ImageIcon I will suggest creating something like at Using Swing Components: Examples or just study it.

And next you can create:

Icon a = new ImageIcon("images/a.jpg");
JButton button = new JButton("Something in button", a);

Upvotes: 2

Related Questions