Results 1 to 5 of 5
Thread: add image on panel
- 07-26-2008, 10:26 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
- 07-26-2008, 06:50 PM #2
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.swing.*; public class ImageOnBackground { public static void main(String[] args) throws IOException { BufferedImage image = javax.imageio.ImageIO.read(new File("images/hawk.jpg")); ImageBackgroundPanel imp = new ImageBackgroundPanel(image); // Set layout and add components to imp as desired. JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(imp); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class ImageBackgroundPanel extends JPanel { BufferedImage image; ImageBackgroundPanel(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, this); } }
- 07-26-2008, 08:43 PM #3
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
thanx for the reply...it really helps me a lot...
but i wont be able to add more panels or textboxes n buttons on that...can u pls guide me for that also
- 07-26-2008, 09:33 PM #4
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.swing.*; public class ImageOnBackground { public static void main(String[] args) throws IOException { BufferedImage image = javax.imageio.ImageIO.read(new File("images/hawk.jpg")); ImageBackgroundPanel imp = new ImageBackgroundPanel(image); // Set layout and add components to imp as desired. addComponents(imp); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new JScrollPane(imp)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private static void addComponents(JPanel panel) { panel.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(20,5,20,5); gbc.weightx = 1.0; gbc.weighty = 1.0; for(int i = 0; i < 10; i++) { gbc.gridwidth = ((i+1) % 2 == 0) ? GridBagConstraints.REMAINDER : GridBagConstraints.RELATIVE; panel.add(new JButton("button " + (i+1)), gbc); } } } class ImageBackgroundPanel extends JPanel { BufferedImage image; ImageBackgroundPanel(BufferedImage image) { this.image = image; } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, 0, 0, this); } }
- 08-02-2008, 07:38 PM #5
Member
- Join Date
- Jul 2008
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Problem in getting table on panel
By adeeb in forum AWT / SwingReplies: 0Last Post: 06-09-2008, 08:23 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
Why the panel text changed?
By ottawalyli in forum SWT / JFaceReplies: 0Last Post: 12-16-2007, 04:16 PM -
How to place panel into frame
By vivek_9912 in forum AWT / SwingReplies: 2Last Post: 11-19-2007, 11:21 PM -
Help with drag from panel
By fernando in forum AWT / SwingReplies: 2Last Post: 08-07-2007, 10:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks