Example usage for javax.swing Icon paintIcon

List of usage examples for javax.swing Icon paintIcon

Introduction

In this page you can find the example usage for javax.swing Icon paintIcon.

Prototype

void paintIcon(Component c, Graphics g, int x, int y);

Source Link

Document

Draw the icon at the specified location.

Usage

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Paint <code>v</code>'s icon on <code>g</code> at <code>(x,y)</code>.
 *//*from   ww  w  .  j a  v  a  2s . co m*/
public void paintIconForVertex(Graphics g, Vertex v, int x, int y) {
    Icon icon = vertexIconFunction.getIcon(v);
    if (icon == null) {
        Shape s = AffineTransform.getTranslateInstance(x, y)
                .createTransformedShape(getVertexShapeFunction().getShape(v));
        paintShapeForVertex((Graphics2D) g, v, s);
    } else {
        int xLoc = x - icon.getIconWidth() / 2;
        int yLoc = y - icon.getIconHeight() / 2;
        icon.paintIcon(screenDevice, g, xLoc, yLoc);
    }
}

From source file:org.executequery.gui.browser.TableDataTab.java

private Object setTableResultsPanel(DatabaseObject databaseObject) {

    tableDataChanges.clear();/*from   w  w w  . j  a  va  2  s.co  m*/
    primaryKeyColumns.clear();
    foreignKeyColumns.clear();

    this.databaseObject = databaseObject;
    try {

        initialiseModel();
        tableModel.setCellsEditable(false);
        tableModel.removeTableModelListener(this);

        if (isDatabaseTable()) {

            DatabaseTable databaseTable = asDatabaseTable();
            if (databaseTable.hasPrimaryKey()) {

                primaryKeyColumns = databaseTable.getPrimaryKeyColumnNames();
                canEditTableLabel.setText("This table has a primary key(s) and data may be edited here");
            }

            if (databaseTable.hasForeignKey()) {

                foreignKeyColumns = databaseTable.getForeignKeyColumnNames();
            }

            if (primaryKeyColumns.isEmpty()) {

                canEditTableLabel.setText("This table has no primary keys defined and is not editable here");
            }

            canEditTableNotePanel.setVisible(alwaysShowCanEditNotePanel);
        }

        if (!isDatabaseTable()) {

            canEditTableNotePanel.setVisible(false);
        }

        Log.debug("Retrieving data for table - " + databaseObject.getName());

        ResultSet resultSet = databaseObject.getData(true);
        tableModel.createTable(resultSet);
        if (table == null) {

            createResultSetTable();
        }
        tableModel.setNonEditableColumns(primaryKeyColumns);

        TableSorter sorter = new TableSorter(tableModel);
        table.setModel(sorter);
        sorter.setTableHeader(table.getTableHeader());

        if (isDatabaseTable()) {

            SortableHeaderRenderer renderer = new SortableHeaderRenderer(sorter) {

                private ImageIcon primaryKeyIcon = GUIUtilities
                        .loadIcon(BrowserConstants.PRIMARY_COLUMNS_IMAGE);
                private ImageIcon foreignKeyIcon = GUIUtilities
                        .loadIcon(BrowserConstants.FOREIGN_COLUMNS_IMAGE);

                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                        boolean hasFocus, int row, int column) {

                    DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) super.getTableCellRendererComponent(
                            table, value, isSelected, hasFocus, row, column);

                    Icon keyIcon = iconForValue(value);
                    if (keyIcon != null) {

                        Icon icon = renderer.getIcon();
                        if (icon != null) {

                            BufferedImage image = new BufferedImage(
                                    icon.getIconWidth() + keyIcon.getIconWidth() + 2,
                                    Math.max(keyIcon.getIconHeight(), icon.getIconHeight()),
                                    BufferedImage.TYPE_INT_ARGB);

                            Graphics graphics = image.getGraphics();
                            keyIcon.paintIcon(null, graphics, 0, 0);
                            icon.paintIcon(null, graphics, keyIcon.getIconWidth() + 2, 5);

                            setIcon(new ImageIcon(image));

                        } else {

                            setIcon(keyIcon);
                        }

                    }

                    return renderer;
                }

                private ImageIcon iconForValue(Object value) {

                    if (value != null) {

                        String name = value.toString();
                        if (primaryKeyColumns.contains(name)) {

                            return primaryKeyIcon;

                        } else if (foreignKeyColumns.contains(name)) {

                            return foreignKeyIcon;
                        }

                    }

                    return null;
                }

            };
            sorter.setTableHeaderRenderer(renderer);

        }

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        scroller.getViewport().add(table);
        removeAll();

        add(canEditTableNotePanel, canEditTableNoteConstraints);
        add(scroller, scrollerConstraints);

        if (displayRowCount && SystemProperties.getBooleanProperty("user", "browser.query.row.count")) {

            add(rowCountPanel, rowCountPanelConstraints);
            rowCountField.setText(String.valueOf(sorter.getRowCount()));
        }

    } catch (DataSourceException e) {

        if (!cancelled) {

            addErrorLabel(e);

        } else {

            addCancelledLabel();
        }

    } finally {

        tableModel.addTableModelListener(this);
    }

    setTableProperties();
    validate();
    repaint();

    return "done";
}

From source file:org.openstreetmap.josm.tools.ImageProvider.java

/**
 * Decorate one icon with an overlay icon.
 *
 * @param ground the base image//from w  ww  .  j ava  2  s.c om
 * @param overlay the overlay image (can be smaller than the base image)
 * @param pos position of the overlay image inside the base image (positioned
 * in one of the corners)
 * @return an icon that represent the overlay of the two given icons. The second icon is layed
 * on the first relative to the given position.
 */
public static ImageIcon overlay(Icon ground, Icon overlay, OverlayPosition pos) {
    GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration();
    int w = ground.getIconWidth();
    int h = ground.getIconHeight();
    int wo = overlay.getIconWidth();
    int ho = overlay.getIconHeight();
    BufferedImage img = conf.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    Graphics g = img.createGraphics();
    ground.paintIcon(null, g, 0, 0);
    int x = 0, y = 0;
    switch (pos) {
    case NORTHWEST:
        x = 0;
        y = 0;
        break;
    case NORTHEAST:
        x = w - wo;
        y = 0;
        break;
    case SOUTHWEST:
        x = 0;
        y = h - ho;
        break;
    case SOUTHEAST:
        x = w - wo;
        y = h - ho;
        break;
    }
    overlay.paintIcon(null, g, x, y);
    return new ImageIcon(img);
}

From source file:org.pmedv.core.components.RelativeImageView.java

/**
 * Paints the image.//from   w w w . j  a  va 2s  .  co  m
 * 
 * @param g
 *            the rendering surface to use
 * @param a
 *            the allocated region to render into
 * @see View#paint
 */
public void paint(Graphics g, Shape a) {

    Color oldColor = g.getColor();
    fBounds = a.getBounds();
    int border = getBorder();
    int x = fBounds.x + border + getSpace(X_AXIS);
    int y = fBounds.y + border + getSpace(Y_AXIS);
    int width = fWidth;
    int height = fHeight;
    int sel = getSelectionState();

    // If no pixels yet, draw gray outline and icon
    if (!hasPixels(this)) {
        g.setColor(Color.lightGray);
        g.drawRect(x, y, width - 1, height - 1);
        g.setColor(oldColor);
        loadImageStatusIcons();
        Icon icon = ((fImage == null) ? sMissingImageIcon : sPendingImageIcon);
        if (icon != null) {
            icon.paintIcon(getContainer(), g, x, y);
        }
    }

    // Draw image
    if (fImage != null) {
        g.drawImage(fImage, x, y, width, height, this);
    }

    // If selected exactly, we need a black border & grow-box
    Color bc = getBorderColor();
    if (sel == 2) {
        // Make sure there's room for a border
        int delta = 2 - border;
        if (delta > 0) {
            x += delta;
            y += delta;
            width -= delta << 1;
            height -= delta << 1;
            border = 2;
        }
        bc = null;
        g.setColor(Color.black);
        // Draw grow box
        g.fillRect(x + width - 5, y + height - 5, 5, 5);
    }

    // Draw border
    if (border > 0) {
        if (bc != null) {
            g.setColor(bc);
        }
        // Draw a thick rectangle:
        for (int i = 1; i <= border; i++) {
            g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i);
        }
        g.setColor(oldColor);
    }
}

From source file:util.ui.UiUtilities.java

/**
 * Scales Icons to a specific size//w w  w  . j  a v  a  2 s  .  c o m
 *
 * @param icon
 *          Icon that should be scaled
 * @param width
 *          scaled width
 * @param height
 *          scaled height
 * @return Scaled Icon
 */
public static Icon scaleIcon(Icon icon, int width, int height) {
    if (icon == null) {
        return null;
    }
    int currentWidth = icon.getIconWidth();
    int currentHeight = icon.getIconHeight();
    if ((currentWidth == width) && (currentHeight == height)) {
        return icon;
    }
    try {
        // Create Image with Icon
        BufferedImage iconImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = iconImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        AffineTransform z = g2.getTransform();
        g2.setTransform(z);
        icon.paintIcon(null, g2, 0, 0);
        g2.dispose();
        BufferedImage scaled = scaleDown(iconImage, width, height);
        // Return new Icon
        return new ImageIcon(scaled);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return icon;
}

From source file:util.ui.UiUtilities.java

/**
 * Creates a scaled Version of the Icon.
 *
 * The scaled Version will have a Background and a Border.
 *
 * @param ic//from www  .  ja  v  a 2s  .  c o m
 * @return ImageIcon
 * @since 2.1
 */
public static ImageIcon createChannelIcon(Icon ic) {
    BufferedImage img = new BufferedImage(42, 22, BufferedImage.TYPE_INT_RGB);

    if (ic == null) {
        ic = new ImageIcon("./imgs/tvbrowser16.png");
    }

    int height = 20;
    int width = 40;

    if ((ic.getIconWidth() != 0) && (ic.getIconHeight() != 0)) {
        double iWidth = ic.getIconWidth();
        double iHeight = ic.getIconHeight();
        if (iWidth / iHeight < 2.0) {
            width = (int) (iWidth * (20.0 / iHeight));
        } else {
            height = (int) (iHeight * (40.0 / iWidth));
        }
    }
    ic = scaleIcon(ic, width, height);

    Graphics2D g = img.createGraphics();

    g.setColor(Color.WHITE);
    g.fillRect(1, 1, 40, 20);

    int x = 1 + 20 - ic.getIconWidth() / 2;
    int y = 1 + 10 - ic.getIconHeight() / 2;

    ic.paintIcon(null, g, x, y);

    g.setColor(Color.BLACK);
    g.drawRect(0, 0, 42, 22);

    return new ImageIcon(img);
}