Avatar of Sreejith22
Sreejith22Flag for India

asked on 

Components not moving in JFrame while dragging the frame

I have a JFrame. I have added certain components to this JFrame.
I have added an image panel and into this image panel, I have added the components like textbox,textarea,buttons etc.
When I drag the frame, the components(text boxes, buttons etc.) added to image panel are not moving relative to the image. What should be done to rectify this?
Regards,
Anees
Editors IDEs

Avatar of undefined
Last Comment
Mick Barry
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sreejith22
Sreejith22
Flag of India image

ASKER

Its about repositioning components when a frame is resized.
The jist of the question is that  a background image should be on a panel with components on top and these components should reposition when the frame is resized.
Anees
Avatar of Sreejith22
Sreejith22
Flag of India image

ASKER

I have attached the screenshot(normal frame && resized frame) of my problem and my optimized code.
Anees
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.border.LineBorder;
public class ImagePanel extends JPanel
{
public static final int TILED = 0;
public static final int SCALED = 1;
public static final int ACTUAL = 2;
 
private BufferedImage image;
private int style;
private float alignmentX = 0.5f;
private float alignmentY = 0.5f;
 
public ImagePanel(BufferedImage image)
{
this(image, TILED);
}
 
public ImagePanel(BufferedImage image, int style)
{
this.image = image;
this.style = style;
setLayout( new BorderLayout() );
}
 
public void setImageAlignmentX(float alignmentX)
{
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
}
 
public void setImageAlignmentY(float alignmentY)
{
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;
 
}
 
public void add(JComponent component)
{
add(component, null);
}
 
public void add(JComponent component, Object constraints)
{
component.setOpaque( false );
 
if (component instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane)component;
JViewport viewport = scrollPane.getViewport();
viewport.setOpaque( false );
Component c = viewport.getView();
 
if (c instanceof JComponent)
{
((JComponent)c).setOpaque( false );
}
}
 
super.add(component, constraints);
}
 
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
 
if (image == null ) return;
 
switch (style)
{
case TILED :
drawTiled(g);
break;
 
case SCALED :
Dimension d = getSize();
g.drawImage(image, 0, 0, d.width, d.height, null);
break;
 
case ACTUAL :
drawActual(g);
break;
}
}
 
private void drawTiled(Graphics g)
{
Dimension d = getSize();
int width = image.getWidth( null );
int height = image.getHeight( null );
 
for (int x = 0; x < d.width; x += width)
{
for (int y = 0; y < d.height; y += height)
{
g.drawImage( image, x, y, null, null );
}
}
}
 
private void drawActual(Graphics g)
{
Dimension d = getSize();
float x = (d.width - image.getWidth()) * alignmentX;
float y = (d.height - image.getHeight()) * alignmentY;
g.drawImage(image, (int)x, (int)y, this);
}
 
public static void main(String [] args)throws Exception
{
BufferedImage image = javax.imageio.ImageIO.read(new java.io.File("c:\\final.jpg"));
ImagePanel south = new ImagePanel(image, ImagePanel.SCALED);
south.setPreferredSize(new Dimension(490, 340));
south.setLayout(null);
JPanel panEmail = new JPanel();
panEmail.setBounds(5,105,350,50);
JTextField toAddress = new JTextField(28);
toAddress.setBorder( new LineBorder(Color.gray, 1) );
JPanel panTextArea = new JPanel();
panTextArea.setBounds(25,135,420,180);
JTextArea msgArea = new JTextArea(10,38);
msgArea.setBorder( new LineBorder(Color.gray, 1) );
panTextArea.add(msgArea);
panEmail.add(toAddress);
south.add(panEmail);
south.add(panTextArea);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add( south);
frame.pack();
frame.setVisible(true);
}
public static void setSize(ImagePanel ip, int x, int y){
}
}

Open in new window

normal.JPG
resized-frame.JPG
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you shouldn't be overriding add(), see the examples I posted above for examples of how it should be implemented.

Editors IDEs
Editors IDEs

Development in most programming languages is frequently done with an editor or integrated development environment (IDE). An IDE is a software application that provide comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. XCode, Visual Studio, Adobe Dreamweaver and Eclipse are some of the more popular development tools, but an editor or IDE can be anything from simple text editors to sophisticated programs. Related topics: All programming and development language, database and web based content management systems topics

25K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo