permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Image problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image problem" Watch "Image problem" New topic
Author

Image problem

memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Hello,
I add an image to the Panel via the Label object. But, my image does not cover all the Panel. So how can I make my image cover all the panel ??


Thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Here's one way to scale an image to fill a component. This approach is easy enough to adapt to AWT components. If you use a Label or Panel override "public void paint(Graphics g)". You may also have to override "public void update(Graphics g) { paint(g); }" to get it to work properly.
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Can any of you give more clear and explanatory soltuion ??

Thanks.
samir Sadiki
Ranch Hand

Joined: Apr 22, 2006
Posts: 31
Hi,
I don't know if there is a way to do what you wanna do using a Label.
You could draw the image directly on the Panel if you want.
Here is a sample code:

import javax.swing.*;
import java.awt.*;

public class TestImage extends JPanel {


public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new TestImage());
frame.setTitle("test image in panel");
frame.setSize(200, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon imageIcon = new ImageIcon("USA.gif");
Image image = imageIcon.getImage();
g.drawImage(image,0,0,getWidth(),getHeight(),this);
}
}
memati bas
Ranch Hand

Joined: Jan 29, 2006
Posts: 85
Thanks very much for this...
It is indeed what I am searching for one days.

Again thanks very much,

Best wishes.
samir Sadiki
Ranch Hand

Joined: Apr 22, 2006
Posts: 31
You are very welcome my friend.
Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Image problem
 
Similar Threads
ScrollPane in netbeans GUI
invisible canvas
panel include another panel
adding images to components
Problem with custom painting and multiple JPanel