Center an image in a component

I am painting different size images and need to know how to center them in a component. I'm painting to a canvas object that I add to a panal Object. Not sure if this is the correct structure.
Thanks for the help

Does JLabel+ImageIcon already do that for you?
import java.awt.*;
import java.net.*;
import javax.swing.*;
public class Example {
    public static void main(String[] args) throws MalformedURLException {
        JPanel cp = new JPanel(new GridLayout(2,2));
        addImage(cp, "http://today.java.net/jag/Image68-small.jpeg");
        addImage(cp, "http://today.java.net/jag/Image63-small.jpeg");
        addImage(cp, "http://today.java.net/jag/Image65-small.jpeg");
        addImage(cp, "http://today.java.net/jag/Image64-small.jpeg");
        JFrame f = new JFrame("Example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(cp);
        f.pack();
        //add a bit of space to show off centring
        f.setSize(f.getWidth()*6/5, f.getHeight()*6/5);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    static void addImage(JPanel cp, String url) throws MalformedURLException {
        JLabel label = new JLabel(new ImageIcon(new URL(url)));
        label.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        cp.add(label);
}

Similar Messages

  • Text on top of an image in a component

    How do I get text on top of an image in a component?
    ImageIcon icon = new ImageIcon("foo.jpg");
    JLabel label = new JLabel();
    label.setIcon(icon);
    // this prints next to, not on top of, the image
    label.setText("Hello"); if I add this:
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setVerticalAlignment(SwingConstants.CENTER);The text doesn't appear at all. I assume it is "underneath" the image.
    null

    if I add this:
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.setVerticalAlignment(SwingConstants.CENTER);The alignment methods determine the position of the component in the parent container, if the layout manager respects those values.
    I believe you want:
    JLabel label = new JLabel("Some Centered Text");
    label.setIcon( new ImageIcon("???.jpg") );
    label.setHorizontalTextPosition(JLabel.CENTER);
    label.setVerticalTextPosition(JLabel.CENTER);

  • How can I dynamically center an image in Crystal Reports?

    Hi! I'm using Visual Studio 2010, SQL Server 2008 R2 and Crystal Reports 13_0_11.
    I have a database full of images type image in SQL Server. Every image has diferent width. 
    I create a report with Crystal Reports and a Dataset and does everything that I want. But I can't horinzotal align the image, in the property window, the horizontal align option is disable, I don't know why... and I don't know what to do... 
    Please help me with how can I center the images or how can I enable the option...

    Hi Fafa0239,
    Since it is related to the Crystal Reports, please post questions related to third-party products in their forums (http://scn.sap.com/thread/3440443).
    Best Regards,
    Jack
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • How do I center the image created in my pdf files?

    When I create a pdf file from a windows application and the image has a border around it. Then open the file to view it, the border appears to be shoved to the lower left corner of the opened pdf file image so instead of having a 1/4" border around the page, the border is 0 on the left side and bottom, and 1/2" on the right side and top. It prints this way also. How can I make Acrobat 8 Standard center my image so the border is the same all the way around? The border is all the way around in the application the document is created in, and when printed from the application it is correct all the way around, but the pdf file is not.

    Terry
    In your windows application, if you can click "center horizontally" and "center vertically" do that. I'm not sure what windows application you're using, but this happens to me when I use Microsoft Office programs.

  • Images in the full screen lightbox are not centered horizontally on the screen. Is there a way to center the images?

    I have tried it in different screen sizes but teh result is all same. Horizontally images are slightly on the right. How can I center the images?

    Hi,
    Thanks for your answer. It is ok when using the lightbox without full screen option. When I use the fullscreen option, the container of the image is centered but the image in the container is not. I do center everything but when it comes to full scren there is nothing like centering.
    Here is what happens when I view in the browser:

  • Background Image on a Component

    How do I add a background image to a component, in this case, a scrollpane containing a drawing area. This is for a level editor for my game. I want a level's background image to be drawn and tiled so the user can get a better preview of what the level will really look like. Is there an easy way to do this?

    Do you have a little code-snippet on how to use this
    class? The only things I've ever needed to do in
    graphics is loading, transforming, and drawing images,
    so I'm a bit in the dark on anything outside that.I think something like this will do it. I don't know for sure though since I've never used it:
    private BufferedImage bImage;
    // ... constructors, code blahdy blah, construct the bufferedimage here etc.
    public void paintComponent(Graphics g) {
      // the Rectangle2D in user space used to anchor and replicate the texture:
      Rectangle2D anchor = new Rectangle2D.Double(0,0,20,20);
      // the actual paint object:
      TexturePaint tpaint = new TexturePaint(bImage, anchor);
      Graphics2D g2d = (Graphics2D) g;
      g2d.setPaint(tpaint);
      // g2d.fillRect(0,0,getWidth(), getHeight());
    }

  • Creating an image of a component

    hi
    i'm working on a small applet. i need to create an image of any component (like JLabel) before it is shown on the screen.
    can i do it?

    Try:
    Image getComponentImage(Component comp) {
        BufferedImage imgBuf = new BufferedImage(comp.getWidth(),comp.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
        Graphics gr = imgBuf.createGraphics();
        comp.paint(gr);
        return imgBuf;
    }Graeme

  • Jdeveloper visual editor do not show images and other component shapes

    I used jdeveloper(10.1.3.3.0) to create an ADF BC application, I create a jsf jsp file and drag and drop components from ADF Faces Core to this file visual editor, but jdeveloper visual editor do not show images and other component shapes.
    Could you tell me how I can fix this problem?
    Thanks,
    Mike

    Actually, I need to make a dummy change to the jsp file and then it will display boxes representing the objects in the visual editor whereas I get an exception in the log:
    javax.servlet.jsp.JspException: Cannot find FacesContext
    This does not talk much to me !
    Thanks

  • Center an image

    I am new to Dreamweaver. I just bought CS6 and I'm trying to center an image in a div on the left side of my page. My page is set up with multiple background layers and a left navigation column and then the right side is the main context of the page. Instead of using something like a horizontal line in my navigation area, I have a small border type design that I want centered in this column. I understand how I can place the image to the right or left but how do you center it?
    I tried creating a class called .center but there's no options for centering something except for text. I did try using align center for text but that didn't work.
    I'm basically a novice with all of this so any help would be greatly appreciated.

    Do something like this:
    1) CSS like this:
    img.center {   display: block;   margin-left: auto;   margin-right: auto; }
    2)  HTML like this:
    <img src="my_big_one.jpg" alt="big_one" class="center" />
    This should do the job.

  • Displaying Images In Form Component

    Hi
    How do I display an employee image in a form Componet, if it is located in file server??
    and how do display an employee image in form component
    if it is saved in the Databse ??
    Many Thanks
    null

    Walid,
    If you want to display an image in a form it should be "physically" uploaded to the table on which your form is based. The only supported datatypes now are BLOB and ORDIMAGE (interMedia image).
    Thanks,
    Dmitry

  • How to create a image as a component

    Hi
    Thanks for all the help.
    Hope you can help me on this one also.
    I have a few dynamic pages .In each of this page i have a logo(a .jpg file) to be displayed at the top of each page. how do i make an image as a component in the portal & use it in my dynamic pages.
    Kindly reply
    Regards
    Sushant

    Hi,
    You can create an image as a shared component. But you cannot use this image directly on the dynamic page. The image should be used in a template and this template can be used in the dynamic page or any other component. But the problem with dynamic pages is that it does not support templates in release 1 and 2. It would be available only in the next release. The best way would be is to pick up the image from the mid tier.
    Thanks,
    Sharmila

  • How do I center an image inside a div tag using fluid grid layout?

    where do I find and what do i use the image properties position when using the fluid grid layout. I'd like to center the image in the mobile page versus th left position in the desktop.

    This is what my css codes looks like where the image is in the div tag:
    #logo_links {
        clear: both;
        float: left;
        margin-left: 0;
        width: 32.2033%;
        display: block;
    This is the html code where the image is:
      </div>
        <div id="logo_links">
          <div align="center">
            <p><img src="../fluidgrid/images/3_7x2logo.jpg" alt="" width="236" height="110" align="left"></p>
          </div>
        </div>
    I'm going to need more detail where to insert the suggested code as I am a newby!
    Thanks

  • How do I center an image inside a CSS div tag using dreamweaver?

    I know this may seem like a very silly queston to ask because it should seem so simple but how do I do that? I am having trouble being able to center an image inside of a div tag. Here is the code I have come up with so far. Thanks in advance for your help.
    </style>
    </head>
    <body>
    <div id="PageContainer">
      <div id="PageHeader"> img.centered{display:block; margin-left:auto; margin-right:auto; }<img src="Untitled-5.jpg" alt="rowland" class="centered" /></div>
    <div id="PageMenu">Content for  id "PageHeader" Goes Here</div>
    <div id="PageBody">Content for  id "PageHeader" Goes Here</div>
    <div id="PageFooter">Content for  id "PageHeader" Goes Here</div>
    </body>
    </html>
    Also you can ignore the other div tags after the first div id. The first div id that has my image link is the one I am trying to get to center my image. The image is in there just not centering.

    Centering Pages, Images and other elements with CSS:
    http://cookbooks.adobe.com/post_Centering_web_pages_and_other_elements_with_CSS-16640.html
    Nancy O.

  • HOW TO: Action to resize & center crop images of various sizes

    Hello,
    I've gotten a lot of help here over the years so I wanted to share an action I created. I needed to resize and automatically center crop images of various sizes for my company's website. For example: All images needed to be 900px x 460px 72dpi - and because input images were from many different sources, the dimensions were different for each image. It seemed straight forward at first (sort of) but I couldn't find a way of doing this without a script or some third party program - so I came up with the following action. Hope someone gets some use out of it.
    Note: Change action as needed to suit your needs.
    Resize and Center Crop (Auto)
    Fit Image
         height: 900px
         width: 900px
         With Don't Enlarge
    Canvas Size
         height: 460px
         vertical: center
         extension color: white
    Image Size
         width: 900px
         resolution: 72 per inch
         with scale styles
         with constrain proportions
    Save (optional)
         As: JPEG
         Quality: 12
         Scans: 5 (for web images)
    This assumes that the images are at least horizontal, but if you have photoshop CC you can create a conditional action that will work with vertical and horizontal images. If you are square cropping, this action makes it a breeze. I also have an action that uses the same basic logic, but lets me manually move the crop box. The only real issue I have encountered is when the original image ratio is very narrow, which leads to some white space on the top and bottom (I'm working on updating the action to address this). But it has worked on about 95% of my images. Again, you will probably need to adjust all of the variables depending on what you are trying to do, but it should be possible with the same steps. And of course this can also be used to batch process.
    Feel free to let me know if anyone comes up with improvements! Thanks

    Hmm thanks Mylenium, I got that response a lot when I was searching before. But if you re-read above, I am explaining a method of doing it without a script. So it wasn't really a question, just a tip in case anyone else wanted to do the same.

  • Center the image when click the movieclip

    Need a big help guys...
    i am new with flash, and i had swf which contains big image it will resize to best fit.
    when i double click the image the clicked position come to center of the stage.
    i achived little bit, it zooming but image not come to center position.
    i have attached the fla file in it please give me a solution for it
    and i have used this script to zoom on double click and bolded 2 lines are to position the image.
    imagePanel.onRelease = function()
        if(lastclick - (lastclick=getTimer()) + 500 > 0){
            var sf = Math.abs(imagePanel.loadTemp._xscale);
            if (sf < 100) {
            //trace("zoomIn");      
         var xss = imagePanel.loadTemp._xscale > 0 ? (1) : (-1);
        var yss = imagePanel.loadTemp._yscale > 0 ? (1) : (-1);
        var sf = Math.abs(imagePanel.loadTemp._xscale);
        sf = 100;
        imagePanel.loadTemp._xscale = sf * xss;
        imagePanel.loadTemp._yscale = sf * yss;
        imagePanel.refreshPane();
      imagePanel.loadTemp._x = - (_xmouse) -35;
        imagePanel.loadTemp._y = - (_ymouse) - 150;
        else {
        //trace("zoomOut");
        var xss = imagePanel.loadTemp._xscale > 0 ? (1) : (-1);
        var yss = imagePanel.loadTemp._yscale > 0 ? (1) : (-1);
        var sf = Math.abs(imagePanel.loadTemp._xscale);
        sf = best_fit;
        imagePanel.loadTemp._xscale = sf * xss;
        imagePanel.loadTemp._yscale = sf * yss;
        imagePanel.refreshPane();

    Hi friend, just change with following code in the zoom in portion
    //////////////Zoom In/////////////////////////////////////           
                    //trace("zoomIn");       
       var perX = (_xmouse - 10)/imagePanel.loadTemp._width * 100;
        var perY = (_ymouse - 43)/imagePanel.loadTemp._height * 100;
        var xss = imagePanel.loadTemp._xscale > 0 ? (1) : (-1);
        var yss = imagePanel.loadTemp._yscale > 0 ? (1) : (-1);
        var sf = Math.abs(content._xscale);
        sf = 100;
        content._xscale = sf * xss;
        content._yscale = sf * yss;
           imagePanel.refreshPane();
    ///////////////Zoom In ends////////////////////////////////////
    /////////Position to center the image /////////////////////////
    /////////This is part i needed, when i click the image it cannot move center postion, please solve my problem///////////
        /*if(_xmouse<Stage.width/2 && _ymouse<Stage.height){
            imagePanel.loadTemp._x = - (_xmouse) -35;
               imagePanel.loadTemp._y = - (_ymouse) - 150;
        var newX = imagePanel.loadTemp._width * (perX/100);
        var newY = imagePanel.loadTemp._height * (perY/100);
        imagePanel.loadTemp._x = imagePanel.loadTemp._x - newX;
        imagePanel.loadTemp._y = imagePanel.loadTemp._y - newY;
        //imagePanel.loadTemp._x = - (_xmouse) -35;
        //imagePanel.loadTemp._y = - (_ymouse) - 150;
    The code that i had newly included is marked with red color.
    Please reply...

Maybe you are looking for

  • Free Goods for multiple line items of the same material

    Hi Friends, We have configured Free Goods to a particular material in such a way that if the customer orders 100 qty of Material A then he gets 10 qty of Material B free. This is working fine if there is only one line item of Material A with qty more

  • Is there a way to force tablet mode?

    Firefox defaults to the interface instead the tablet interface on my 9" tablet. (Not to be confused with the desktop vs. phone agent for web pages.). Is there a way to force the tablet interface without messing with custom display resolutions?

  • How do I find and launch iTunes radio on my mac pro?  I do not see the radio icon on the top menu in my iTunes.

    How do I find and launch itunes radio on my macbook pro?  I do not see the radio icon in the top banner in itunes.  I am new to this... help!

  • Creative Cloud App Screen Download error

    I continue to get the exclamation point in a triangle and it asks me to uninstall and then download it again. Needless to say I've don't his about five times and I keep getting the same error. PLEASE HELP!!

  • DWF Template refresh

    Hi I keep on having issues in Dreamweaver (CS4) where I update the DWF template and then navigate to one of the pages to review the global update and find this has not happened, so I then have to make the changes manually. Can anyone provide an answe