Example usage for javax.swing ImageIcon getIconWidth

List of usage examples for javax.swing ImageIcon getIconWidth

Introduction

In this page you can find the example usage for javax.swing ImageIcon getIconWidth.

Prototype

public int getIconWidth() 

Source Link

Document

Gets the width of the icon.

Usage

From source file:edu.ku.brc.specify.tasks.StartUpTask.java

@Override
public JPanel createSplashPanel() {
    AppPreferences ap = AppPreferences.getLocalPrefs();
    int width = ap.getInt("Startup.Image.width", 400);
    int height = ap.getInt("Startup.Image.height", 700);

    Image img = null;//from w  w  w .  j a v  a2s.co m
    ImageIcon bgImg = IconManager.getIcon(SPECIFY_SPLASH);
    if (bgImg.getIconWidth() > width || bgImg.getIconHeight() > height) {
        img = GraphicsUtils.getScaledImage(bgImg, width, height, true);
    } else {
        img = bgImg.getImage();
    }
    JPanel splashPanel = new JPanel(new BorderLayout());
    //splashPanel.setBackground(Color.WHITE);
    splashPanel.setOpaque(false);
    splashPanel.add(new JLabel(new ImageIcon(img)), BorderLayout.CENTER);
    return splashPanel;
}

From source file:edu.ku.brc.specify.tasks.subpane.wb.ImageFrame.java

/**
 * Makes sure the image can be read and it is not corrupted.
 * @param srcFile the full path to the image
 * @return true if ok/*  w w w  . j a va 2  s.  co m*/
 */
public static boolean testImageFile(final String srcFile) {
    try {
        byte[] bytes = GraphicsUtils.readImage(srcFile);
        if (bytes != null) {
            ImageIcon testIcon = new ImageIcon(bytes);

            //System.err.println(testIcon.getIconHeight()+"  "+testIcon.getIconWidth());

            // this image file is corrupted or a format that we cannot display
            return testIcon.getIconHeight() > 0 && testIcon.getIconWidth() > 0;
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:components.ScrollDemo.java

public ScrollDemo() {
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

    //Get the image to use.
    ImageIcon bee = createImageIcon("images/flyingBee.jpg");

    //Create the row and column headers.
    columnView = new Rule(Rule.HORIZONTAL, true);
    rowView = new Rule(Rule.VERTICAL, true);

    if (bee != null) {
        columnView.setPreferredWidth(bee.getIconWidth());
        rowView.setPreferredHeight(bee.getIconHeight());
    } else {//  ww w  .j  a  v a  2 s  . c  om
        columnView.setPreferredWidth(320);
        rowView.setPreferredHeight(480);
    }

    //Create the corners.
    JPanel buttonCorner = new JPanel(); //use FlowLayout
    isMetric = new JToggleButton("cm", true);
    isMetric.setFont(new Font("SansSerif", Font.PLAIN, 11));
    isMetric.setMargin(new Insets(2, 2, 2, 2));
    isMetric.addItemListener(this);
    buttonCorner.add(isMetric);

    //Set up the scroll pane.
    picture = new ScrollablePicture(bee, columnView.getIncrement());
    JScrollPane pictureScrollPane = new JScrollPane(picture);
    pictureScrollPane.setPreferredSize(new Dimension(300, 250));
    pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));

    pictureScrollPane.setColumnHeaderView(columnView);
    pictureScrollPane.setRowHeaderView(rowView);

    //Set the corners.
    //In theory, to support internationalization you would change
    //UPPER_LEFT_CORNER to UPPER_LEADING_CORNER,
    //LOWER_LEFT_CORNER to LOWER_LEADING_CORNER, and
    //UPPER_RIGHT_CORNER to UPPER_TRAILING_CORNER.  In practice,
    //bug #4467063 makes that impossible (in 1.4, at least).
    pictureScrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, buttonCorner);
    pictureScrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, new Corner());
    pictureScrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new Corner());

    //Put it in this panel.
    add(pictureScrollPane);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:misc.AccessibleScrollDemo.java

public AccessibleScrollDemo() {
    // Get the image to use.
    ImageIcon bee = createImageIcon("images/flyingBee.jpg", "Photograph of a flying bee.");

    // Create the row and column headers.
    columnView = new Rule(Rule.HORIZONTAL, true);
    if (bee != null) {
        columnView.setPreferredWidth(bee.getIconWidth());
    } else {/*  w  w  w.ja v a 2  s  .  c  o m*/
        columnView.setPreferredWidth(320);
    }
    columnView.getAccessibleContext().setAccessibleName("Column Header");
    columnView.getAccessibleContext()
            .setAccessibleDescription("Displays horizontal ruler for " + "measuring scroll pane client.");
    rowView = new Rule(Rule.VERTICAL, true);
    if (bee != null) {
        rowView.setPreferredHeight(bee.getIconHeight());
    } else {
        rowView.setPreferredHeight(480);
    }
    rowView.getAccessibleContext().setAccessibleName("Row Header");
    rowView.getAccessibleContext()
            .setAccessibleDescription("Displays vertical ruler for " + "measuring scroll pane client.");

    // Create the corners.
    JPanel buttonCorner = new JPanel();
    isMetric = new JToggleButton("cm", true);
    isMetric.setFont(new Font("SansSerif", Font.PLAIN, 11));
    isMetric.setMargin(new Insets(2, 2, 2, 2));
    isMetric.addItemListener(this);
    isMetric.setToolTipText("Toggles rulers' unit of measure " + "between inches and centimeters.");
    buttonCorner.add(isMetric); //Use the default FlowLayout
    buttonCorner.getAccessibleContext().setAccessibleName("Upper Left Corner");

    String desc = "Fills the corner of a scroll pane " + "with color for aesthetic reasons.";
    Corner lowerLeft = new Corner();
    lowerLeft.getAccessibleContext().setAccessibleName("Lower Left Corner");
    lowerLeft.getAccessibleContext().setAccessibleDescription(desc);

    Corner upperRight = new Corner();
    upperRight.getAccessibleContext().setAccessibleName("Upper Right Corner");
    upperRight.getAccessibleContext().setAccessibleDescription(desc);

    // Set up the scroll pane.
    picture = new ScrollablePicture(bee, columnView.getIncrement());
    picture.setToolTipText(bee.getDescription());
    picture.getAccessibleContext().setAccessibleName("Scroll pane client");

    JScrollPane pictureScrollPane = new JScrollPane(picture);
    pictureScrollPane.setPreferredSize(new Dimension(300, 250));
    pictureScrollPane.setViewportBorder(BorderFactory.createLineBorder(Color.black));

    pictureScrollPane.setColumnHeaderView(columnView);
    pictureScrollPane.setRowHeaderView(rowView);

    // In theory, to support internationalization you would change
    // UPPER_LEFT_CORNER to UPPER_LEADING_CORNER,
    // LOWER_LEFT_CORNER to LOWER_LEADING_CORNER, and
    // UPPER_RIGHT_CORNER to UPPER_TRAILING_CORNER.  In practice,
    // bug #4467063 makes that impossible (at least in 1.4.0).
    pictureScrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, buttonCorner);
    pictureScrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, lowerLeft);
    pictureScrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, upperRight);

    // Put it in this panel.
    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
    add(pictureScrollPane);
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}

From source file:FileChooserDemo2.java

public void loadImage() {
    if (file == null) {
        thumbnail = null;//  w ww  . ja va 2 s  . co  m
        return;
    }

    // Don't use createImageIcon (which is a wrapper for getResource)
    // because the image we're trying to load is probably not one
    // of this program's own resources.
    ImageIcon tmpIcon = new ImageIcon(file.getPath());
    if (tmpIcon != null) {
        if (tmpIcon.getIconWidth() > 90) {
            thumbnail = new ImageIcon(tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
        } else { // no need to miniaturize
            thumbnail = tmpIcon;
        }
    }
}

From source file:examples.monalisa.gui.GeneticDrawingView.java

/**
 * Scale an image to fit the size of the targetImageLabel.
 *
 * @param a_image the image to scale/*from www. j  av  a 2s .c o m*/
 * @return scaled image
 */
public ImageIcon scaleToImageLabel(Image a_image) {
    ImageIcon scaled = new ImageIcon(a_image);
    if (scaled.getIconHeight() > targetImageLabel.getHeight()) {
        scaled = new ImageIcon(a_image.getScaledInstance(-1, targetImageLabel.getHeight(), Image.SCALE_FAST));
    }
    if (scaled.getIconWidth() > targetImageLabel.getWidth()) {
        scaled = new ImageIcon(a_image.getScaledInstance(targetImageLabel.getWidth(), -1, Image.SCALE_FAST));
    }
    return scaled;
}

From source file:examples.gp.monalisa.gui.GeneticDrawingView.java

/**
 * Scale an image to fit the size of the targetImageLabel.
 *///from   ww w  . jav  a 2  s  .c om
public ImageIcon scaleToImageLabel(Image image) {
    ImageIcon scaled = new ImageIcon(image);
    if (scaled.getIconHeight() > targetImageLabel.getHeight()) {
        scaled = new ImageIcon(image.getScaledInstance(-1, targetImageLabel.getHeight(), Image.SCALE_FAST));
    }
    if (scaled.getIconWidth() > targetImageLabel.getWidth()) {
        scaled = new ImageIcon(image.getScaledInstance(targetImageLabel.getWidth(), -1, Image.SCALE_FAST));
    }
    return scaled;
}

From source file:gd.gui.GeneticDrawingView.java

/**
 * Scale an image to fit the size of the targetImageLabel.
 *//*from w  ww  . j  a  v a 2  s  . c  o m*/
public ImageIcon scaleToImageLabel(Image image) {
    ImageIcon scaled = new ImageIcon(image);

    if (scaled.getIconHeight() > targetImageLabel.getHeight()) {
        scaled = new ImageIcon(image.getScaledInstance(-1, targetImageLabel.getHeight(), Image.SCALE_FAST));
    }
    if (scaled.getIconWidth() > targetImageLabel.getWidth()) {
        scaled = new ImageIcon(image.getScaledInstance(targetImageLabel.getWidth(), -1, Image.SCALE_FAST));
    }

    return scaled;
}

From source file:edu.ku.brc.util.services.MapGrabber.java

/**
 * Retrieves a map from the web service.
 * //  www.ja v a  2  s.co  m
 * @return a map image
  * @throws HttpException if network errors occur while grabbing map from service
  * @throws IOException if network errors occur while grabbing map from service
 */
public Image getMap() throws HttpException, IOException {
    log.debug("Entering MapGrabber.getMap()");
    log.debug("width=" + width + "\theight=" + height);
    double longSpread = maxLong - minLong;
    double latSpread = maxLat - minLat;
    log.debug("lat spread:  min=" + minLat + "\tmax=" + maxLat + "\trange=" + latSpread);
    log.debug("long spread: min=" + minLong + "\tmax=" + maxLong + "\trange=" + longSpread);
    if (width == null && height == null) {
        width = defaultWidth;
        height = defaultHeight;
    }

    StringBuilder url = new StringBuilder("http://");
    url.append(host);
    url.append(defaultPathAndParams);

    // set layers
    url.append("&layers=");
    url.append(layers);

    // set bounding box
    url.append("&bbox=");
    url.append(minLong);
    url.append(",");
    url.append(minLat);
    url.append(",");
    url.append(maxLong);
    url.append(",");
    url.append(maxLat);

    // set size
    url.append("&height=");
    url.append(height);
    url.append("&width=");
    url.append(width);

    Image image;
    String urlStr = url.toString();
    if (imageCache != null) {
        // check the image cache
        log.info("Asking cache to grab map image: " + urlStr);
        File imageFile = imageCache.getCacheFile(urlStr);
        if (imageFile == null) {
            // the image wasn't in the cache
            // grab it again
            urlStr = imageCache.cacheWebResource(urlStr);
            imageFile = imageCache.getCacheFile(urlStr);
        }
        //image = Toolkit.getDefaultToolkit().getImage(imageFile.getAbsolutePath());
        image = ImageIO.read(imageFile);
        if (image == null) {
            imageCache.refreshCachedWebResource(urlStr);
        }
        ImageIcon mapIcon = new ImageIcon(image);
        if (mapIcon.getIconHeight() < 0 || mapIcon.getIconWidth() < 0) {
            // since it was invalid, throw it out of the cache
            imageCache.clearItem(urlStr);
            throw new IOException(
                    "Mapping service failed to return a valid image.  Map request URL: " + urlStr);
        }
    } else {
        log.info("No image cache available.  Grabbing map internally.");
        GetMethod get = new GetMethod(urlStr);
        get.setFollowRedirects(true);
        int resultCode = httpClient.executeMethod(get);
        log.info("GET " + urlStr + " returned " + resultCode);
        log.info("Exiting MapGrabber.getMap()");
        byte[] data = get.getResponseBody();
        image = Toolkit.getDefaultToolkit().createImage(data);
        ImageIcon mapIcon = new ImageIcon(image);
        if (mapIcon.getIconHeight() < 0 || mapIcon.getIconWidth() < 0) {
            throw new IOException(
                    "Mapping service failed to return a valid image.  Map request URL: " + urlStr);
        }
    }

    return image;
}

From source file:de.quadrillenschule.azocamsyncd.gui.ExploreWifiSDPanel.java

private void updateSingleView() {
    try {/*from w  w  w  . j a v  a 2s.  c  o  m*/
        if (remotejTree.getSelectionPaths().length > 0) {
            TreePath tp = remotejTree.getSelectionPaths()[0];
            String mynode = tp.getLastPathComponent().toString();
            AZoFTPFile myaffilea = null;
            for (AZoFTPFile af : afs) {
                if (new String(af.dir + af.ftpFile.getName()).equals(mynode)) {
                    myaffilea = af;
                    //     GlobalProperties gp=new GlobalProperties();
                    //     gp.setProperty(GlobalProperties.CamSyncProperties.LATESTIMAGEPATH, af.dir+af.ftpFile.getName());
                    break;
                }
            }
            final AZoFTPFile myaffile = myaffilea;
            Thread imageUpdater = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {
                        File localFile;

                        localFile = localStorage.getLocalFile(myaffile);

                        localFileNamejTextField1.setText(localFile.getAbsolutePath());
                        ImageIcon ii = new ImageIcon(localFile.toURI().toURL());

                        int mywidth = imagejLabel.getWidth();
                        int width = ii.getIconWidth();
                        int height = ii.getIconHeight();
                        if (width <= 0) {
                            imagejLabel.setText("No image to view.");
                        } else {
                            imagejLabel.setText("");
                        }
                        double factor = (double) height / (double) width;
                        Image image = ii.getImage().getScaledInstance(mywidth,
                                (int) ((double) mywidth * factor), Image.SCALE_FAST);
                        imagejLabel.setIcon(new ImageIcon(image));
                    } catch (Exception ex) {
                        imagejLabel.setText("No image to view.");
                    }
                }
            });
            imageUpdater.start();

        }
    } catch (Exception e) {

    }
}