aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Got a solution : But still a problem !!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Got a solution : But still a problem !!!" Watch "Got a solution : But still a problem !!!" New topic
Author

Got a solution : But still a problem !!!

Meghna ks
Ranch Hand

Joined: Mar 15, 2001
Posts: 122
Hi I did get a solution to put the background image in the buttons, but there's a little problem. I'm not sure as to how I should scale the image exactly to fit within the button ;
eg. SCALED_TO_FIT. Here's the code I've used to put the image & Text within the button.
String str = new String("Images/" + myClass.getIcon());
final ImageIcon imageIcon = new ImageIcon(getURL(str));
JButton button1 = new JButton()
{
Image image = imageIcon.getImage();
{
setOpaque(false);
}
}
public void paintComponent (Graphics g)
{
g.drawImage(image, 0, 0, this);
super.paintComponent(g);
}
};

I'd really appreciate it if somebody can suggest a solution to this problem .
Thanks in advance
Meghna
Swamy Vatti
Ranch Hand

Joined: May 02, 2001
Posts: 57
Try this code...

import java.awt.*;
import javax.swing.ImageIcon;
public class CustomButton extends javax.swing.JButton {
Dimension m_dim;
public Dimension getMaximumSize() {
return m_dim;
}
public Dimension getMinimumSize() {
return m_dim;
}
public Dimension getPrefferedSize() {
return m_dim;
}
public CustomButton(String text) {
super(text);
ImageIcon ii = new ImageIcon(getClass().getResource("/image.gif"));
setIcon(ii);
m_dim = new Dimension(ii.getIconWidth(), ii.getIconHeight());
setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
setVerticalTextPosition(javax.swing.SwingConstants.CENTER);
setOpaque(false);
setContentAreaFilled(false);
setBorder(null);
}
}


Swamy
Meghna ks
Ranch Hand

Joined: Mar 15, 2001
Posts: 122
Hi swami
Thanks for the immediate response. But still I have a problem in painting the text & yet again, the image is not getting scaled to the size of the button. The image is fitting to the button's width & height, no doubt, but it is losing all the remaining part of the gif file. By this, I mean, the whole image is not appearing in the button, only the image within the button's size is appearing. Could you please throw some light on this ?!!
Thanks again
Meghna
Swamy Vatti
Ranch Hand

Joined: May 02, 2001
Posts: 57
I didnt get what u mean by 'remaining part of the gif'.
the buttons size is equal to that of the gif. so there is no way u can lose some part of the gif
Meghna ks
Ranch Hand

Joined: Mar 15, 2001
Posts: 122
Hi swami
The button size & the image size is not the same. By saying "remaining part of the gif", I mean that the scaling is not happening properly i.e only that part of the gif appears which the button can accomodate, no the shrunk version of the gif. I hope I'm clear with this problem.
Let me know if there is a solution for this.
Thanks
Meghna
Swamy Vatti
Ranch Hand

Joined: May 02, 2001
Posts: 57
Yeah!! understood...
Scaling will not be done automatically.
Maybe you have to use some other techniques(I dont know exactly) to scale down the image size to whatever u need and then set it as an icon...
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Try this... replace the paintComponent method you have above with this -



Hopefully, this will work...

-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Meghna ks
Ranch Hand

Joined: Mar 15, 2001
Posts: 122
Hi
I've been trying to solve this problem since last week. I'm still not able to scale the image to the size of the button. I tried using a imageViewer, but in vain. Could anyone suggest me a link or anything that would furthur help me with this ?!!
Thanks to Swami & Nathan in advance for all their help.
Thanks
Meghna
Amit Agarwal
Ranch Hand

Joined: May 09, 2001
Posts: 92
Hi Meghna,
use Image.getScaledInstance(width,height,hints) to get a scaled instance of the image.
Amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Got a solution : But still a problem !!!