How to scale an image displayed on JButton

Hi all,
I has a panel contains 42 buttons of the same size arranged with GridLayout manager. I try to display some images on the buttons. But the problem is that the image bigger than buttons. Is it possible to scale the image to fit the size of button and which method is used?
Thank you very much in advance.

Hi,
I try your tips. But I still see no changes.
here is the code. I wonder where I go wrong.
                Image i=Toolkit.getDefaultToolkit().getImage("red.JPEG");                    i.getScaledInstance(-5,5,Image.SCALE_DEFAULT);
                //or i.getScaledInstance(10,1,Image.SCALE_DEFAULT);                          
     ImageIcon ic=new ImageIcon(i);
               button1.setIcon(ic);//add image to the button
    

Similar Messages

  • Can anyone explain me how to scale the image in canvas in flash builder 4.6

    Can anyone explain me how to scale the image in canvas in flash builder 4.6, Scaling of image in component , can i get some examples ..  it should set almost all size screen

  • UREGENT: How to get a Image displayed on Canvas?

    Hi, my problem is: I have an image drew on Canvas, but I cannot access this image through Java, the image is there on Canvas, but I simply can't get that image.
    Anyone know how can I get an Image displayed on Canvas?

    Pt 1. > but I cannot access this image through Java, the
    Q1. > Anyone know how can I get an Image displayed on Canvas?
    From the axioms given, I would say that it is not possible. :)
    Why is it so UREGENT?

  • How to scale linked images back to 1:1 {normal proportions}?

    Hi there,
    I am workind on document, containing 470 linked images. I accidentaly did some operation with frame fitting probably, so all my images got non-proportional scaling now. Amount of Undos was not long enough...
    Do you have some clever recipe how to scale them back to normal aspect ratio?

    Well,
    thank you for your reply, but I do not uderstand your advice clearly: If the chain link is not broken, I just resize the frame containing photo and keep that bad proportions which I want to get rid of.
    OK, I can just click the Content Grabber circle button and resize the image manualy to be about fine. But in case of 470 links I hoped in some one-click-magic... {"Equalize the proportions of linked images" button to be exact }
    Thank you...

  • How to solve the image display problem in the iTunes Store

    Hi guys!
    I just want to give a work-around for all of you having the image display problem in the iTunes Store. In my case only the square images in the slider sections won't load:
    I had this in my last W7-installation and got it again in W8 after the first few weeks.
    There are a lot of reports about this problem out there and adhoc solutions like 'clear the cache' or 'reinstall and drink ice tea' that do not work. It is obviously a file request error. It is all about finding out which server addresses get blocked by your computer.
    Solution: Go to your firewall and your Anti-virus-program. There must be a whitelist option. In Kaspersky, check the web-options, the banner-options and the firewall-options, because the all have whitelists. The following addresses have to be whitelisted:
    *.apple
    *.mzstatic.apple.com.akadns.net
    *.mzstatic.com.edgesuite.net
    *.da1.akamai.net
    *.itunes.apple.com
    se.itunes.apple.com
    upp.itunes.apple.com
    ax.init.itunes.apple.com
    xp.apple.com
    *.mzstatic.com
    client-api.itunes.apple.com
    www.apple.com.edgekey.net
    www.isg-apple.com.akadns.net
    Press Ctrl + R in the iTunes Shop then and browse throught the music sections. All images should be displayed correctly now.
    If not, there might be more Apple servers involved in your case. You can find them by running a DNS sniffing tool like the DNS query sniffer by Nirsoft or Wireshark.

    Phone restarting randomly could be loose battery. Tighten by putting a small strip of electrical tape on the back edge.
    THE BITTERNESS OF POOR QUALITY, LINGERS LONG AFTER THE CHEAPNESS OF PRICE, IS SOON FORGOTTEN.

  • How to get minimal Image Display VI

    I'm building a GUI for a multi camera ROV (Underwater Robot), and I want to include several live image feeds in the display.
    I can get the feeds to work, but I seem to be stuck using one of the two "Display Image" VI's on the Vision Pallet.
    These have a range of additional features that I don't want, and cant seem to turn completely off.
    eg: 
    -There is a "raised" area with border around the image that is just taking up real-estate.  I can't seem to reduce this to zero. (I've tried the new and classic vi)
    -There is a tool pallet which I don't want.  I have various "preview" windows that just need to be dumb displays.  The tools are not only unnecessary, but a problem is someone clicks on the image by accident.
    So.. Bottom line I'd like just a dumb minimal Image display window VI that I can feed with an IMAQ Image.ctl wire, and that accepts no user input.
    Is such a thing available?
    Phil.
    Get a life? This IS my life!
    Solved!
    Go to Solution.

    You can turn off scrollbars, tool palette, as well as Image Info string. Then just resize visible area over boders:
    See attachment
    Andrey.
    Attachments:
    Mini Vision Display.vi ‏34 KB

  • How to scale an image(URGENT)

    I am having a problem in scaling an image.
    The code I've written is as follows.
    Toolkit tk=Toolkit.getDefaultToolkit();
    Image img=tk.getImage("abc.png");
    img=getScaledInstance(100,100,Image.SCALE_FAST);
    Then when I draw the image, it doesn't scale it. It draws nothing. But if i remove the line, getScaled...,
    then it is loaded properly.
    Wat can be the problem?
    Also, if any1 can tell other ways to scale an image to a predefined size, please tell me.
    Its very URGENT, please respond fast.

    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    public class X {
        public static BufferedImage readScaled(URL url, int w, int h) throws IOException {
            BufferedImage orig = ImageIO.read(url);
            int transparent = orig.getColorModel().getTransparency();
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            BufferedImage image = gc.createCompatibleImage(w, h, transparent);
            Graphics2D g = image.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            double scaleX = w / (double) orig.getWidth();
            double scaleY = h / (double) orig.getHeight();
            AffineTransform xform = AffineTransform.getScaleInstance(scaleX, scaleY);
            g.drawRenderedImage(orig, xform);
            g.dispose();
            orig.flush();
            return image;
        public static void main(String[] args) throws IOException {
            String url = "http://i.a.cnn.net/cnn/interactive/world/0312/gallery.saddam.captured/1.saddam.beard.jpg";
            BufferedImage image = readScaled(new URL(url), 400, 400);
            JLabel label = new JLabel(new ImageIcon(image));
            JFrame f = new JFrame("X");
            f.getContentPane().add(label);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }

  • How to scale an Image?

    I found lots of threads about scaling images but none worked for me. The closest one's are these:
    image.getScaledInstance(width, height, Image.SCALE_DEFAULT);seems to scale the image but the resulting image returns "-1" for both getWidth()/getHeight().
    The other "solution" sets the correct dimension nut the resulting image is just black (= background color):
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D biContext = bi.createGraphics();
    biContext.drawImage(image, 0, 0, width, height, null);           
    result = bi;

    It looks like you need to make sure that your original image
    is fully loaded prior to rendering.
    You should use java.awt.MediaTracker for that.
    Or load your image with javax.imageio.ImageIO.read() method.
    Dmitri

  • How to have posted images display as sRGB on wide-gamut monitors.

    I understand an sRGB profile is necessary for  posting images but I need help on how to do that with Photoshop CS2 and  my new wide-gamut monitor (HP  LP2475w with Spider3Express  calibration). Before doing a "save for web" and posting, I "convert to  profile" to either "sRGB IEEE61966-2.1" or "sRGB with hardware  configuration derived from calibration" (it makes no difference which), but I  see then a significant color shift in the posted images when I view them  through Firefox 3.6 (with operating color management software -verified  on other posted images) and the same color shift with IE6 (which has NO color management software).  So,  Firefox is NOT RECOGNIZING my posted images with the sRGB profiles that  I thought I was embedding in them in Photoshop.  So my question boils  down to: WHAT EXACTLY DO I HAVE TO DO IN PHOTOSHOP SO FIREFOX WILL RECOGNIZE THE POSTED IMAGES AS sRGB and display them 'correctly' on wide-gamut monitors??
    Thank you very much.
    -Jeff

    ISSUE RESOLVED.  Photoshop CS2 was not embedding an sRGB profile in  the images I was posting.  I needed to check the "ICC Profile" checkbox  in "save for web" to make that actually happen (after I did "convert to  profile" sRGB).
    I also have switched to "Full Color Management" Value 1 (hidden) in Firefox 3.6 so that Firefox now assumes any untagged image is sRGB standard and then converts those, and all other  images which ARE tagged, to my calibrated monitor profile. This definitely looks to me like the way to operate with a wide-gamut  monitor although I understand it is still recommended to always tag images for  posting as sRGB and not embed any other profile for color consistency  over color accuracy.  At least if there are some out there with wider-gamut  tags, I should get some benefit.
    Have I got it right ?
    Thanks,
    Jeff

  • How to scale an image? iOS 7.1........

    Updated to iOS 7.1 but still can't set a wallpaper to the dimension I want.
    Am I doing something wrong?

    Why your wallpapers look messed up on iOS 7, and how to fix them!
    http://www.imore.com/having-issues-wallpapers-ios-7-heres-why-and-how-fix-it
    How to Change iPad Wallpaper and Grab or Save Images from the Web
    http://portables.about.com/od/newsandviews/ss/iPad-tutorials_5.htm
    You could use the free Instafit app https://itunes.apple.com/us/app/instafit-post-photos-to-instagram/id591706840?mt =8
     Cheers, Tom

  • How to scale image to A3 size in adobe reader XI

    Hi there,
    Im having trouble trying to find out how to scale an image to fit onto an A3 sixe paper. I tried the fit command but it doesnt 'fit' the image to A3 size. Any help would be appreciated.
    Tamika

    As far as I know, this cannot be done with the free Reader.

  • IMAGE NOT DISPLAYED OVER JButton ON JDialog

    Hi,
    I am trying to display an image over a JButton inside a JDialog(pops up on button click from an applet). But it is invisible. But I am sure that it is drawn on the ContentPane since when i click partially/half at the position
    of the JButton I could see the JButton with the image. Actualy I am resizing the JDialog since I have two buttons, one to minimise and the other two maximise the size of the JDailog.
    All the buttons are in place but not visible neither at the first place nor when the JDialog is resized.
    The code is as follows:
    package com.dataworld.gis;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.print.*;
    import javax.swing.*;
    public class PrintThisPolygon extends JDialog {
         protected Image image;
         protected JButton print;
         protected JButton resize;
         protected JButton desize;
         private int pX=0, pY=0, pWidth=0, pHeight=0;
         public PrintThisPolygon(JFrame parent, Viewer viewer) {
              super(parent, "Print Polygon", true);
              this.setModal(false);
              getContentPane().setLayout(null);
              image = this.viewer.getScreenBuffer();
              pane = new Panel();
              print = new JButton("print", new ImageIcon("images/print.gif"));
              print.setBounds(0,5,50,20);
              print.setEnabled(true);
              print.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent ae){
                        print();
              resize = new JButton("+", new ImageIcon("images/print.gif"));
              resize.setBounds(55,5,50, 20);
              resize.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent ae){
                        resizePlus();
              desize = new JButton("-", new ImageIcon("images/print.gif"));
              desize.setBounds(105,5,50, 20);
              desize.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent ae){
                        resizeMinus();
              getContentPane().add(print);
              getContentPane().add(resize);
              getContentPane().add(desize);
    public String getAppletInfo() {
         return "Identify Polygon\n"
    public void paint(Graphics g){
         System.out.println("Paint : pX " + pX + " pY :" + pY);
         g.drawImage(image, pX, pY, pWidth, pHeight, getBackground(), this);
    protected void print() {
         ImagePrint sp = new ImagePrint(this.viewer);
    public void resizeMinus(){
         repaint();
    public void resizePlus(){
         repaint();
    Can anybody has any clue. Can anybody help me out.
    Thanx

    I added resize code for those buttons and ended up with repaint problems too. When you manually resized the window everything was fine again. After a bit of digging around I found you need to call validate on the dialog and then things work.
    IL,
    There is the new scaling version of the code I modified above:package forum.com.dataworld.gis;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class PanelPaintFrame extends JFrame {
         public static void main(String[] args) {
              PanelPaintFrame frame = new PanelPaintFrame ();
              frame.setVisible (true);
         public PanelPaintFrame ()  {
              setBounds (100, 100, 600, 600);
              setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
              JButton showPrintDialogButton = new JButton ("Show Print Dialog...");
              JPanel buttonPanel = new JPanel ();
              buttonPanel.add (showPrintDialogButton);
              getContentPane ().add (buttonPanel);
              showPrintDialogButton.addActionListener (new ActionListener ()  {
                   public void actionPerformed (ActionEvent e)  {
                        PrintThisPolygon printDialog = new PrintThisPolygon (PanelPaintFrame.this);
                        printDialog.show();
              pack ();
         public class PrintThisPolygon extends JDialog {
              protected ImageIcon myOrigonalImage = null;
              protected Image image;
              protected JButton print;
              protected JButton resize;
              protected JButton desize;
              private int pX=0, pY=0, pWidth=0, pHeight=0;
              JPanel pane = null;
              public PrintThisPolygon(JFrame parent/*, Viewer viewer*/) {
                   super(parent, "Print Polygon", true);
    //               this.setModal(false);
                   getContentPane().setLayout(null);
                   //  Substitute my own image
                   myOrigonalImage = new ImageIcon (PrintThisPolygon.class.getResource ("images/MyCoolImage.jpg"));
                   //  Start off full size
                   pWidth = myOrigonalImage.getIconWidth();
                   pHeight = myOrigonalImage.getIconHeight();
                   image = myOrigonalImage.getImage ().getScaledInstance(pWidth, pHeight, Image.SCALE_DEFAULT);
    //               image = this.viewer.getScreenBuffer();
    //               pane = new Panel();
                   //  Create a JPanel to draw the image
                   pane = new JPanel(){
                        protected void paintComponent(Graphics g)  {
                             System.out.println("Paint : pX " + pX + " pY :" + pY);
                             g.drawImage(image, pX, pY, pWidth, pHeight, getBackground(), this);
                   pane.setBounds (0, 0, pWidth, pHeight);
                   //  Load the button image using a URL
                   print = new JButton("print", new ImageIcon(PrintThisPolygon.class.getResource ("images/print.gif")));
    //               print = new JButton("print", new ImageIcon("images/print.gif"));
                   print.setBounds(0,5,55,20);
                   print.setEnabled(true);
                   print.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent ae){
                             print();
                   resize = new JButton("+", new ImageIcon("images/print.gif"));
                   resize.setBounds(55,5,50, 20);
                   resize.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent ae){
                             resizePlus();
                   desize = new JButton("-", new ImageIcon("images/print.gif"));
                   desize.setBounds(105,5,50, 20);
                   desize.addActionListener(new ActionListener(){
                        public void actionPerformed(ActionEvent ae){
                             resizeMinus();
                   //  Setup a transparent glass panel with no layout manager
                   JPanel glassPanel = new JPanel ();
                   glassPanel.setLayout (null);
                   glassPanel.setOpaque (false);
                   //  Add the image panel to the dialog
                   getContentPane().add(pane);
                   //  Add the buttons to the glass panel
                   glassPanel.add(print);
                   glassPanel.add(resize);
                   glassPanel.add(desize);
                   //  Set our created panel as the glass panel and turn it on
                   setGlassPane (glassPanel);
                   glassPanel.setVisible (true);
                   setBounds (100, 100, pWidth, pHeight);
    //               setBounds (100, 100, 500, 300);
              public String getAppletInfo() {
                   return "Identify Polygon\n";
    //          public void paint(Graphics g){
    //          protected void paintComponent(Graphics g)  {
    //               System.out.println("Paint : pX " + pX + " pY :" + pY);
    //               g.drawImage(image, pX, pY, pWidth, pHeight, getBackground(), this);
              protected void print() {
                   //  Pretend to print
    //               ImagePrint sp = new ImagePrint(this.viewer);
                   System.out.println ("Print view...");
              public void resizeMinus(){
                   //  Scale the image down 10%
                   int scaledWidth = pWidth - (int)(pWidth * 0.1);
                   int scaledHeight = pHeight - (int)(pHeight * 0.1);
                   scaleImage (scaledWidth, scaledHeight);
              public void resizePlus(){
                   //  Scale the image up 10%
                   int scaledWidth = pWidth + (int)(pWidth * 0.1);
                   int scaledHeight = pHeight + (int)(pHeight * 0.1);
                   scaleImage (scaledWidth, scaledHeight);
              private void scaleImage (int scaledWidth, int scaledHeight)  {
                   //  Create a new icon for drawing and retrieve its actuall size
                   image = myOrigonalImage.getImage ().getScaledInstance (scaledWidth, scaledHeight, Image.SCALE_DEFAULT);
                   pWidth = scaledWidth;
                   pHeight = scaledHeight;
                   //  Resize
                   setSize (pWidth, pHeight);
                   pane.setSize (pWidth, pHeight);
                   validate();
    }

  • How to capture an image from my usb camera and display on my front panel

    How to capture an image from my usb camera and display on my front panel

    Install NI Vision Acquisition Software and NI IMAQ for USB and open an example.
    Christian

  • How to place an image in database and how to retrieve and display it in the front end

    how to place an image in database and how to retrieve and display it in the front end
    and to place an image in database and retrieve the image from database using xml
    please,help me out.

    Create a table with a Long RAW Datatype column for storing the Image Column Data.
    Create the form based on the table , which by defaults the column with LONG RAW atatype to a Image Item.
    You can use Forms Built in function READ_IMAGE_FILE to read a Image file stored on the file system in to the image item.
    A save on the form saves the image in the Image item in the long raw column.

  • How to fit to size to show the whole image in image display?

    HI, I am using a control palette to display my video images using image display. However, the size is alway out of proportion. How do i show the whole image in the small box screen size. Basically how to fit to size for the images? 
    Solved!
    Go to Solution.

    This should do the trick:
    Norbert
    CEO: What exactly is stopping us from doing this?
    Expert: Geometry
    Marketing Manager: Just ignore it.

Maybe you are looking for