Results 1 to 5 of 5
- 07-23-2008, 09:45 PM #1
Member
- Join Date
- Jul 2008
- Posts
- 26
- Rep Power
- 0
Adding JPanels to JFrames based on x-y co-ordinates
I have written a code for adding background image to a JFrame as given below.
My requirement is that, I need to add other JComponents like JTextfields, JTextArea,JButton etc. on to this frame and it should be on top of this image.Java Code:JFrame frame = new JFrame(); ImageIcon icon = new ImageIcon("c:\\final.jpg"); JLabel background = new JLabel(icon); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
When I maximize or drag and enlarge the JFrame, the JComponents should automatically get aligned with the frame and background. Please help.
Regards,
Anees
- 07-23-2008, 10:50 PM #2
One way could be to write your own Layout manager for the container that you are adding the components to.
What happens with your current code?
- 07-24-2008, 07:46 AM #3
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import javax.swing.*; public class ImageComponents extends JPanel { BufferedImage image; Rectangle r; JPanel parent; public ImageComponents(BufferedImage image) { this.image = image; r = new Rectangle(image.getWidth(), image.getHeight()); parent = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5,5,5,5); gbc.weighty = 1.0; gbc.weightx = 1.0; gbc.gridwidth = 3; parent.add(new JTextArea(4, 16), gbc); gbc.gridy = 1; gbc.gridwidth = 1; parent.add(new JTextField(12), gbc); parent.add(new JButton("button"), gbc); parent.add(new JButton("button"), gbc); // See through to image below. parent.setOpaque(false); // Position parent over image in sizer. setLayout(null); add(parent); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(image, r.x, r.y, this); } public static void main(String[] args) throws IOException { File file = new File("images/bison.jpg"); BufferedImage image = javax.imageio.ImageIO.read(file); ImageComponents component = new ImageComponents(image); component.addComponentListener(component.sizer); JFrame f = new JFrame("drag frame edges"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(component); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private ComponentListener sizer = new ComponentAdapter() { public void componentResized(ComponentEvent e) { r.x = (getWidth() - r.width)/2; r.y = (getHeight() - r.height)/2; //System.out.printf("r.x = %d r.y = %d%n", r.x, r.y); parent.setBounds(r); } }; }
- 07-24-2008, 08:36 AM #4
Member
- Join Date
- Jul 2008
- Posts
- 26
- Rep Power
- 0
Dear hardwired,
The image is not getting fit to the frame, when the frame is resized.
Also, how should I move the components to a desired position(say, a little bit to the left/right or to top/bottom, I mean by small increments).
I am much close to the most sought solution with your thread hardwired. Thanks a lot.
- 07-24-2008, 10:41 PM #5
The image is not getting fit to the frame, when the frame is resized.
You didn't mention this before. Here's one way to dynamically fit/fill a background image into the contentPane.
Also, how should I move the components to a desired position(say, a little bit to the left/right or to top/bottom, I mean by small increments).Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.swing.*; public class ImageComponents2 extends JPanel { BufferedImage image; public ImageComponents2(BufferedImage image) { this.image = image; // You can add your child components to this // component using the layout manager of your // choice. } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); int w = getWidth(); int h = getHeight(); int iw = image.getWidth(); int ih = image.getHeight(); double xScale = (double)w/iw; double yScale = (double)h/ih; // Scale-to-fit. //double scale = Math.min(xScale, yScale); // Scale-to-fill. double scale = Math.max(xScale, yScale); int width = (int)(scale*iw); int height = (int)(scale*ih); int x = (int)(w - scale*iw)/2; int y = (int)(h - scale*ih)/2; g2.drawImage(image, x, y, width, height, this); } public static void main(String[] args) throws IOException { File file = new File("images/bison.jpg"); BufferedImage image = javax.imageio.ImageIO.read(file); JFrame f = new JFrame("drag frame edges"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new ImageComponents2(image)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } }
This really depends on what you want the layout to look like and what kind of resizing behavior you want. There are a lot of layout managers you can use/combine. I recommend either
1 — show/describe what you want the layout to look like with ascii art or image(s), or
2 — have a look here: Lesson: Laying Out Components Within a Container.
Similar Threads
-
i need an example of JSR179 ((Location based Ser)implementation for CDC based device
By talk_to_vivekmishra in forum CDC and Personal ProfileReplies: 3Last Post: 12-30-2010, 10:07 AM -
JList and JPanels
By JetsYanks in forum New To JavaReplies: 8Last Post: 12-25-2009, 02:11 PM -
Adding Plugin Dependency for Feature-based Product
By abiieez in forum SWT / JFaceReplies: 2Last Post: 05-27-2008, 08:02 PM -
problem with getting new JFrames
By geork in forum New To JavaReplies: 0Last Post: 02-09-2008, 12:46 PM -
Handling Two JFrames
By hiranya in forum AWT / SwingReplies: 2Last Post: 11-05-2007, 07:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks