Results 1 to 5 of 5
Thread: Add an image to JFrame
- 03-14-2008, 04:04 AM #1
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Add an image to JFrame
Hi all,
I want to add an image to a JFrame as the background. On top of that I want to working on more controls.
How can I do that, I work on Netbeans for this.
Eranga.
- 03-14-2008, 04:38 AM #2
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImagePanel extends JPanel { BufferedImage image; public ImagePanel(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); // Draw image centered. int x = (getWidth() - image.getWidth())/2; int y = (getHeight() - image.getHeight())/2; g.drawImage(image, x, y, this); } public static void main(String[] args) throws IOException { String path = "images/reindeer.jpg"; BufferedImage image = ImageIO.read(new File(path)); ImagePanel contentPane = new ImagePanel(image); // You'll want to be sure this component is opaque // since it is required for contentPanes. Some // LAFs may use non-opaque components. contentPane.setOpaque(true); contentPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weightx = 1.0; gbc.weighty = 1.0; // Add components. for(int j = 0; j < 8; j++) { gbc.gridwidth = ((j+1)%2 == 0) ? GridBagConstraints.REMAINDER : GridBagConstraints.RELATIVE; contentPane.add(new JButton("button " + (j+1)), gbc); } JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(contentPane); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
- 03-14-2008, 08:24 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Thanks a lot pal. It's really nice work.
I've done interesting thing here pal. Just add a JPanel on top of the JFrame. Then drag all the components onto it. On JPanel add a image icon in form load event.
Seems it's fine.
Eranga
- 02-01-2010, 09:58 AM #4
Member
- Join Date
- Feb 2010
- Posts
- 1
- Rep Power
- 0
hi ı have a problem
ı must do a project..
I want to add all images in a folder to a JFrame as the background in slide with threads ..
how can ı do ???
plaese help me !!!
emergency
- 02-01-2010, 03:09 PM #5
Similar Threads
-
can display image in JFrame?
By xCLARAx in forum AWT / SwingReplies: 14Last Post: 04-03-2009, 07:02 PM -
GUI... setting my background to an image, im using a JFrame
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-24-2008, 05:29 AM -
JFrame problem
By vassil_zorev in forum AWT / SwingReplies: 1Last Post: 01-25-2008, 02:53 AM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
Help with JFrame
By Albert in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 04:44 AM


LinkBack URL
About LinkBacks

Bookmarks