Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Got a solution : But still a problem !!!

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Meghna ks
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this... replace the paintComponent method you have above with this -



Hopefully, this will work...

-Nate
 
Meghna ks
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Meghna,
use Image.getScaledInstance(width,height,hints) to get a scaled instance of the image.
Amit
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic