This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image problem

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any of you give more clear and explanatory soltuion ??

Thanks.
 
samir Sadiki
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are very welcome my friend.
Good luck!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic