Scale an image

Hi all,
I used the following code to zoom in & zoom out an image. By this code image is aligned to the left side. I need the image to appear in the centre of the panel when it is scaled down. please can anyone help me to do that?
private void setImageToLabel(BufferedImage src,JLabel label,double scale){
     label.setIcon(null);
         try{     
            int width = (int)(src.getWidth()*scale);
            int height = (int)(src.getHeight()*scale); 
        AffineTransform trans = AffineTransform.getScaleInstance(scale,scale);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        GraphicsConfiguration cg = gd.getDefaultConfiguration();
        int transparency = src.getColorModel().getTransparency();
        BufferedImage dest = cg.createCompatibleImage(width, height, transparency);
        Graphics2D g = dest.createGraphics();
        g.drawRenderedImage(src, trans);
        g.dispose();  
        label.setIcon(new ImageIcon(dest));
     }catch(Exception e){
          System.out.println("setImageToLable");
    }    }the 'label' is in a scroll pane, which is in a JPanel
thanks in advance

Could you just put the label into the JPanel using GridBagLayout and the default GridBagConstraints? That should place the JLabel smack center of the JPanel. You would likely need to revalidate the JPanel whenever the Image changed size.

Similar Messages

  • 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
        

  • 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

  • How do I re-scale cropped images in Fianal Cut Express 4?

    How do I re-scale cropped images to the correct aspect ratio in the sequence? I am in Final Cut Express 4. The manual says: 'Modify>Scale to sequence' but I do not see this command in the Modify drop-down menu. Does anyone know where this is located or how to to re-scale? Many thanks for any help.

    I'm not quite sure exactly what the problem is and I am no expert.
    However, if the photos are too small you can simply enlarge them to fit  the frame.
    To do this, select the clip in the timeline and open the Motion tab in the Viewer.
    Drag the Scale slider along until the photo enlarges the correct amount. (If the picture does not enlarge, double click the clip)
    If you have lots of photos which need exactly the same enlargement you can copy (Cmd C) the treated picture and then select all the others.
    Go up to the Edit menu and select "Paste Attributes".
    A window will open. Tick the "Basic Motion" box and all the selected pictures will be enlarged.
    They will then need rendering.

  • CS5: Content Aware Scale vs Image Size

    Can someone explain the difference between the outcome of using the new content aware scale versus just going ahead and resizing the image? Or is this a matter of preference? Thanks!

    Hi,
    Content aware scale is not so much about resizing an image as much as it is about extending an image. It allows you to stretch the existing pixels along a single axis without distorting (or with less distorting) of the subject or subjects of the image.
    In this first photo is the original and the area that needs to be filled with image data to the right.
    In this 2nd image I just scaled the image up to fill. As you can see, I've lost a lot of the image as I'm basically cropping it as I scale it up along both the x axis and the y axis.
    If I try to just scale the image along the x-axis the image will distort. The ship and the bathers get wider.
    In this 4th image I've used content aware scale to "pull" the pixels over only along the x axis. Notice that the ship and the swimmers retain their original appearance.
    Hope that helps
    Michael
    [signature deleted by host]

  • When trying to use photo as wallpaper, cannot scale the image to size I want.  This feature worked fine with ios6

    When trying to use a photo for wallpaper, cannot scale the image to size I want to use.  Feature worked fine in iOS6

    James, thank you. I'm experiencing the same issue and submitted feelback at that link. I'm trying to use the same pumpkin image I used last year for Halloween on my lock screen and attempting to follow instructions (Move and Scale) and would like to make the image smaller so that it appears BELOW the time and date, but although I'm able to move the image, I'm unable to scale it - make it smaller. I've since tried this with several other photos with the same outcome. It seems that in iOS 7 one can make images bigger, but not smaller. I'm not sure what the logic behind that could be.

  • How can I scale an image without it becoming blurry?

    I'm following this tutorial on youtube and I'm doing everything he says, but when I scale the image it becomes blurry on mine and his was still perfect.

    Rick is correct, but there are some features that can give better results than the default scaling:
    http://helpx.adobe.com/after-effects/using/detail-preserving-upscale-effect.html
    http://blogs.adobe.com/aftereffects/2013/06/bicubic-resampling-for-improved-scaling-and-ot her-transformations-in-after-effects.html
    By the way, it helps if you give a lot more information when asking a question. We have no idea what you've tried so far, and we don't even know what version of After Effects you're using.

  • How to create an gray scale png image from intensitie​s values

    Hi,
      I would like create a gray scale png image from set of intensities values which is saved in excel sheet . Is IMAQ tool necessary for this vi?
    thanks & regards
    Manoj

    You posted in the feedback forum, which is not correct for your question. I am guessing you are talking about LabVIEW since you mention "vi".
    manojkp89 wrote:
    Is IMAQ tool necessary for this vi?
    The answer is NO. Plain LabVIEW is sufficient.
    LabVIEW Champion . Do more with less code and in less time .

  • Rotate and scale an image witohout deforming it

    Hello you all!
    I have to rotate and scale an image, but i don't wont to deform it.
    I'm thinking to something like 16/9 images on 4/3 screens (with two horizontal or vertical black lines around the scaled image)...
    I thinked to transform the image in bitmap format, then create a bigger image and fill the empty spaces with zero-pixels...
    Is there a simplest and more efficient way to do it with 2D java classes?
    Thank you!

    See reply 8 in Help to rotate image for an idea.

  • 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

  • Is it possible to rotate and scale an image?

    Is it possible to rotate and scale an image with Grapchis2D at the same time?
    One method call to do it all?
    lets say the original image size is 200x200
    I can scale the image with
    Graphics.drawImage(image, 0,0, 500,500,this);
    But now i need to rotate it as well and keep the new size which is 500x500
    how do i do that ?

    Have you already tried the scale(double sx, double
    sy) and rotate(double theta) methods of Graphics2D?no.

  • Scale grouped image frames while maintaining internal margins

    say i have a grid of images separated by 5 pts of white space between each image, is there a way to set up transformations so that as i scale the image group the internal spacing while remain consistant at 5 pts?
    also question number 2: how to globally disable scaling of strokes, so that all my strokes are conistant.
    Thanks!

    Q1
    As far as know - no, but you can scale your images all together and after that set distribution with Allign panel´s Distribute Spacing option, you may have to show that option from Allign panel´s menu...
    Q2
    Open Window>Object&Layout>Transformations.... open panel menu and uncheck Adjust Stroke Weight when Scaling

  • Gray scale photos images are different but should be the same; help?

    I send a gray scale image from Lightroom (Edit with ...) and it opened CS4 photoshop.
    I made a blank document , 11x14".
    I used Windows/arrange/ tile to view the blank and the gray scale image. I tired various methods of bringing the gray scale image to the blank one (move tool, select all and move, copy and paste). Each time, I did this within seconds of each other (I made no changes to fill, opacity, and I used Normal). I brought the image in twice so I could print out the SAME image twice on the larger 11x14 at Sams club in order to use the Epson 7800 printer and not the old wet process on the Fugi machine. One image is darker(lighter) than the other every time. I even tried another image and I got two different exposures.
    I wonder if it it only happens with the grayscale images? Perhaps it is a problem coming from Lightroom 2.5?
    I did a three image 16x20 and I didn't notice the difference - partly because they were three differenct images in color. I will try two color images from the same file tomorrow.
    Any thoughts? I haven't found a possible setting problem that would have been applied between the operations.
    Rich

    Paulo,
    Thanks. I was ready to believe the monitor idea but I still have some reservations.
    I don't doubt what you said about the pixels (sounds correct to me).
    Why?
    I tried another monitor and the images I attached also look like the top one is darker (vertically).
    Also, the same is true if I scrolled the images. It seems to me that I should see a difference as I slowly scroll the image vertically. I didn't see a difference.
    Read on please.
    At any rate (I long for a CRT - my 21" crapped out last year), I am using  two SAMSUNG screens, the
    one attached to the main computer is the Syncmaster T220 - a well rated
    monitor; as far as I knew , read and had friends who recommended them.
    The other I just tried is attached to the second computer a couple of feet away. It is the syncMaster T240HD. 
    So, if it is an LCD monitor problem, it is with both and the same
    company. I would think I would have read some bad reports about these
    LCDs?
    Also, wouldn't the color images also be different when presented the same way? (they aren't).
    However, I went back to the original T220 LCD display and moved an image in to a blank one as before, then did the control J to copy the layer, and the images we different again. But, I zoomed in more than usual and scrolled the images and they were lighter at the bottom of the screen and darker at the top of the screen!!!!!!
    That is good news and bad news! $$$$$$
    If it is the monitor problem, then both images should print as the same.????
    Secondly, the display (LCD) attached to the machine at Sams showed the same difference between the two images. AND, I recall that the display is also a Samsung (I will check that tomorrow).
    What a bunch of coincidences! I guess I have to search the web for some Samsung answers? and contact the company?
    I want to explore as much on my end as possible.
    Hunt and another suggested the Control-J to avoid the problem by copying the layer. That didn't work either.
    I suppose I could tweak one of the images after bringing them in but I don't know how (I clicked the layer and I wasn't able to do anything - like lighten that image).
    Again, thanks for the help. I think the question maybe answered.
    I will send the screen capture shortly.
    Rich

  • Scale an image to actual size?

    I often use the Crop Tool to get part of an image to print at actual size.
    For example, Crop: Width 7cm @ Resolution 600 pixels/inch (same as my
    printer), then select a known width of 7cm then crop and print.
    I figure there must be a way without actually cropping the image, perhaps by selecting the known dimension.
    I could do the math myself, but I figure Photoshop CS4 can do it for me.
    Any suggestions?

    Nigel: Here is a method that may work for you.
    (1) Since you have PSCS4, you are able to convert your image to a
        smart object.
    (2) Create a layer mask that corresponds to the physical size you
        would want the known object to print at.  E.g., your known object
        (the coin) is 20.3mm wide, so you create a rectangular marquee
        that is 20.3mm wide:
        (a) Create an arbitrary sized rectangular marquee, then use
            Select->Transform Selection and enter the desired height and
            width into the object bar (e.g., 20.3mm <tab> 20.3mm) and
            commit. It doesn't matter where in the image you make this;
            all that matters is that you have a 20.3x20.3mm marquee.
        (b) Turn this marquee into a mask by clicking on the masks panel
            and then the icon second from the right.  Unlock the mask
            from the smart object by clicking on the chain icon.
    (3) Use this layer mask to calibrate the image against the known
        object:
        (a) Click on the smart object icon in the layer, then use
            Edit->Transform (with the aspect ratio locked) to move the
            known object (coin) into the mask and scale it so it just
            barely fits into its width.
        (b) Your smart object is now scaled properly in Photoshop.  You
            should now delete (or disable) the layer mask, which is no
            longer needed.
    (4) Use the Move tool, if necessary, and the Rectangular Marquee tool
        to select the area of the image to be printed.
    (5) In the Print dialog, set scale to 100%.  Don't use borderless
        printing, which typically reduces the printing scale slightly.

  • 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...

Maybe you are looking for