Search in sources :

Example 51 with java.awt.image

use of java.awt.image in project processdash by dtuma.

the class ScalableImageIcon method getScaledIcon.

private ImageIcon getScaledIcon(int desiredWidth, int desiredHeight) {
    // Look for the best icon from our list of delegates
    ImageIcon best = delegates[0];
    int bestHeight = best.getIconHeight();
    for (ImageIcon i : delegates) {
        int oneHeight = i.getIconHeight();
        if (i.getIconWidth() == desiredWidth && oneHeight == desiredHeight) {
            // we found an icon that is exactly the right size!
            if (filters == null)
                // if no filter is in place, return the icon unchanged.
                return i;
            else
                // create a filtered version of the unscaled icon.
                return new ImageIcon(runFilters(i));
        } else if (oneHeight >= desiredHeight && oneHeight < bestHeight) {
            // keep track of the smallest icon we find that is larger
            // than the desired size.
            best = i;
            bestHeight = oneHeight;
        }
    }
    // use the best icon we found. apply any filter if needed, then resize.
    Image img = runFilters(best);
    return new ImageIcon(img.getScaledInstance(desiredWidth, desiredHeight, Image.SCALE_SMOOTH));
}
Also used : ImageIcon(javax.swing.ImageIcon) Image(java.awt.Image)

Example 52 with java.awt.image

use of java.awt.image in project processdash by dtuma.

the class ReportsAndToolsIcon method paintLargeIconImpl.

private void paintLargeIconImpl(Graphics2D g2, int pw, int ph) {
    // draw a white page with a black outline.
    int pageHeight = ph - 1;
    int pageWidth = ph * 9 / 11;
    g2.setColor(Color.white);
    g2.fillRect(0, 0, pageWidth, pageHeight);
    g2.setColor(Color.gray);
    g2.drawRect(0, 0, pageWidth, pageHeight);
    // draw text onto the page
    int pad = 1 + ph / 20;
    float lineSpacing = Math.max(2, ph / 14);
    float fontSize = 0.85f * lineSpacing;
    float y = pad + lineSpacing;
    g2.setClip(pad + 1, pad, pageWidth - 2 * pad, pageHeight - 2 * pad);
    g2.setFont(new Font("Dialog", Font.PLAIN, 10).deriveFont(fontSize));
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2.setColor(Color.darkGray);
    String s = TEXT;
    while (y <= ph) {
        g2.drawString(s, pad + 1, y);
        s = s.substring(20);
        if (s.charAt(0) == ' ')
            s = s.substring(1);
        y += lineSpacing;
    }
    // calculate the geometry for the chart in the lower-left corner
    int barWidth = pageWidth / 5;
    int chartHeight = (int) (pageHeight * 0.4);
    int chartTop = pageHeight - pad - chartHeight;
    Rectangle2D[] bars = new Rectangle2D[BAR_DELTA.length];
    for (int i = bars.length; i-- > 0; ) {
        float barGap = chartHeight * BAR_DELTA[i];
        bars[i] = new Rectangle2D.Float(pad + 1 + barWidth * i, chartTop + barGap, barWidth, chartHeight);
    }
    // draw white areas to ensure the text doesn't run into the bars
    g2.setColor(Color.white);
    g2.setStroke(new BasicStroke(Math.max(3, 1 + pad * 1.2f)));
    for (int i = bars.length; i-- > 0; ) g2.draw(bars[i]);
    // draw the bars themselves
    for (int i = bars.length; i-- > 0; ) {
        Color light = PaintUtils.mixColors(BAR_COLORS[i], Color.white, 0.7);
        g2.setPaint(new GradientPaint((int) bars[i].getX(), 0, BAR_COLORS[i], (int) bars[i].getMaxX(), 0, light));
        g2.fill(bars[i]);
    }
    // draw the calculator
    ImageIcon calc = loadImage("calc.png");
    int calcHeight = ph * 2 / 3;
    float calcScale = calcHeight / (float) calc.getIconHeight();
    int calcWidth = (int) (0.5 + calc.getIconWidth() * calcScale);
    int calcLeft = (int) (pageWidth - pad * 2 / 3);
    int calcTop = ph / 4;
    Image scaledCalc = new ImageIcon(//
    calc.getImage().getScaledInstance(calcWidth, calcHeight, Image.SCALE_SMOOTH)).getImage();
    g2.setClip(null);
    g2.drawImage(scaledCalc, calcLeft, calcTop, null);
}
Also used : BasicStroke(java.awt.BasicStroke) ImageIcon(javax.swing.ImageIcon) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) GradientPaint(java.awt.GradientPaint) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) GradientPaint(java.awt.GradientPaint) Font(java.awt.Font)

Example 53 with java.awt.image

use of java.awt.image in project processdash by dtuma.

the class DashboardHelpURLConnection method maybeResizeImage.

private InputStream maybeResizeImage(byte[] rawData, int headerLen) throws IOException {
    BufferedImage image = ImageIO.read(new ByteArrayInputStream(rawData, headerLen, rawData.length - headerLen));
    // if we couldn't understand the image, resend the raw data.
    if (image == null)
        return null;
    // if the image is already small enough. Just resend the raw data.
    if (image.getWidth() <= MAX_WIDTH)
        return null;
    // resize the image to fit within the desired width.
    Image resizedImage = image.getScaledInstance(MAX_WIDTH, -1, Image.SCALE_SMOOTH);
    // force the image resize operation to finish before continuing.
    ImageIcon icon = new ImageIcon(resizedImage);
    int rWidth = icon.getIconWidth();
    int rHeight = icon.getIconHeight();
    // paint the image into a buffer
    BufferedImage resizedBuf = new BufferedImage(rWidth, rHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = resizedBuf.createGraphics();
    g2.setColor(Color.white);
    g2.fillRect(0, 0, rWidth, rHeight);
    g2.drawImage(icon.getImage(), 0, 0, null);
    g2.dispose();
    // Compute the binary PNG representation of the image
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    ImageIO.write(resizedBuf, "PNG", outStream);
    // return an input stream containing the generated bytes.
    return new ByteArrayInputStream(outStream.toByteArray());
}
Also used : ImageIcon(javax.swing.ImageIcon) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 54 with java.awt.image

use of java.awt.image in project processdash by dtuma.

the class IconImageHandler method drawTimingImage.

private Image drawTimingImage(Image baseImage) {
    ImageIcon baseIcon = new ImageIcon(baseImage);
    Image result = new BufferedImage(baseIcon.getIconWidth(), baseIcon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
    Component c = new Component() {
    };
    Graphics2D g = (Graphics2D) result.getGraphics();
    baseIcon.paintIcon(c, g, 0, 0);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(Color.black);
    int x = baseIcon.getIconWidth() / 4;
    int dx = baseIcon.getIconWidth() / 2 - 2;
    int y = baseIcon.getIconHeight() / 2;
    int dy = baseIcon.getIconHeight() / 4 - 1;
    int[] xx = new int[] { x, x + dx, x };
    int[] yy = new int[] { y, y + dy, y + 2 * dy };
    g.fillPolygon(xx, yy, 3);
    return result;
}
Also used : ImageIcon(javax.swing.ImageIcon) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Component(java.awt.Component) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 55 with java.awt.image

use of java.awt.image in project JMRI by JMRI.

the class LightTableAction method createModel.

/**
     * Create the JTable DataModel, along with the changes for the specific case
     * of Lights.
     */
@Override
protected void createModel() {
    // load graphic state column display preference
    _graphicState = InstanceManager.getDefault(GuiLafPreferencesManager.class).isGraphicTableState();
    m = new BeanTableDataModel() {

        public static final int ENABLECOL = NUMCOLUMN;

        public static final int INTENSITYCOL = ENABLECOL + 1;

        public static final int EDITCOL = INTENSITYCOL + 1;

        protected String enabledString = Bundle.getMessage("ColumnHeadEnabled");

        protected String intensityString = Bundle.getMessage("ColumnHeadIntensity");

        @Override
        public int getColumnCount() {
            return NUMCOLUMN + 3;
        }

        @Override
        public String getColumnName(int col) {
            if (col == EDITCOL) {
                // no heading on "Edit"
                return "";
            }
            if (col == INTENSITYCOL) {
                return intensityString;
            }
            if (col == ENABLECOL) {
                return enabledString;
            } else {
                return super.getColumnName(col);
            }
        }

        @Override
        public Class<?> getColumnClass(int col) {
            if (col == EDITCOL) {
                return JButton.class;
            }
            if (col == INTENSITYCOL) {
                return Double.class;
            }
            if (col == ENABLECOL) {
                return Boolean.class;
            } else if (col == VALUECOL && _graphicState) {
                // use an image to show light state
                return JLabel.class;
            } else {
                return super.getColumnClass(col);
            }
        }

        @Override
        public int getPreferredWidth(int col) {
            // override default value for UserName column
            if (col == USERNAMECOL) {
                return new JTextField(16).getPreferredSize().width;
            }
            if (col == EDITCOL) {
                return new JTextField(6).getPreferredSize().width;
            }
            if (col == INTENSITYCOL) {
                return new JTextField(6).getPreferredSize().width;
            }
            if (col == ENABLECOL) {
                return new JTextField(6).getPreferredSize().width;
            } else {
                return super.getPreferredWidth(col);
            }
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == EDITCOL) {
                return true;
            }
            if (col == INTENSITYCOL) {
                return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).isIntensityVariable();
            }
            if (col == ENABLECOL) {
                return true;
            } else {
                return super.isCellEditable(row, col);
            }
        }

        @Override
        public String getValue(String name) {
            Light l = lightManager.getBySystemName(name);
            if (l == null) {
                return ("Failed to find " + name);
            }
            int val = l.getState();
            switch(val) {
                case Light.ON:
                    return Bundle.getMessage("LightStateOn");
                case Light.INTERMEDIATE:
                    return Bundle.getMessage("LightStateIntermediate");
                case Light.OFF:
                    return Bundle.getMessage("LightStateOff");
                case Light.TRANSITIONINGTOFULLON:
                    return Bundle.getMessage("LightStateTransitioningToFullOn");
                case Light.TRANSITIONINGHIGHER:
                    return Bundle.getMessage("LightStateTransitioningHigher");
                case Light.TRANSITIONINGLOWER:
                    return Bundle.getMessage("LightStateTransitioningLower");
                case Light.TRANSITIONINGTOFULLOFF:
                    return Bundle.getMessage("LightStateTransitioningToFullOff");
                default:
                    return "Unexpected value: " + val;
            }
        }

        @Override
        public Object getValueAt(int row, int col) {
            switch(col) {
                case EDITCOL:
                    return Bundle.getMessage("ButtonEdit");
                case INTENSITYCOL:
                    return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).getTargetIntensity();
                case ENABLECOL:
                    return ((Light) getBySystemName((String) getValueAt(row, SYSNAMECOL))).getEnabled();
                default:
                    return super.getValueAt(row, col);
            }
        }

        @Override
        public void setValueAt(Object value, int row, int col) {
            switch(col) {
                case EDITCOL:
                    // Use separate Runnable so window is created on top
                    class WindowMaker implements Runnable {

                        int row;

                        WindowMaker(int r) {
                            row = r;
                        }

                        @Override
                        public void run() {
                            // set up to edit
                            addPressed(null);
                            fixedSystemName.setText((String) getValueAt(row, SYSNAMECOL));
                            // don't really want to stop Light w/o user action
                            editPressed();
                        }
                    }
                    WindowMaker t = new WindowMaker(row);
                    javax.swing.SwingUtilities.invokeLater(t);
                    break;
                case INTENSITYCOL:
                    // alternate
                    try {
                        Light l = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
                        double intensity = ((Double) value);
                        if (intensity < 0) {
                            intensity = 0;
                        }
                        if (intensity > 1.0) {
                            intensity = 1.0;
                        }
                        l.setTargetIntensity(intensity);
                    } catch (IllegalArgumentException e1) {
                        status1.setText(Bundle.getMessage("LightError16"));
                    }
                    break;
                case ENABLECOL:
                    // alternate
                    Light l = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
                    boolean v = l.getEnabled();
                    l.setEnabled(!v);
                    break;
                case VALUECOL:
                    if (_graphicState) {
                        // respond to clicking on ImageIconRenderer CellEditor
                        Light ll = (Light) getBySystemName((String) getValueAt(row, SYSNAMECOL));
                        clickOn(ll);
                        fireTableRowsUpdated(row, row);
                        break;
                    }
                //$FALL-THROUGH$
                default:
                    super.setValueAt(value, row, col);
                    break;
            }
        }

        /**
             * Delete the bean after all the checking has been done.
             * <P>
             * Deactivate the light, then use the superclass to delete it.
             */
        @Override
        void doDelete(NamedBean bean) {
            ((Light) bean).deactivateLight();
            super.doDelete(bean);
        }

        // all properties update for now
        @Override
        protected boolean matchPropertyName(java.beans.PropertyChangeEvent e) {
            return true;
        }

        @Override
        public Manager getManager() {
            return lightManager;
        }

        @Override
        public NamedBean getBySystemName(String name) {
            return lightManager.getBySystemName(name);
        }

        @Override
        public NamedBean getByUserName(String name) {
            return lightManager.getByUserName(name);
        }

        @Override
        protected String getMasterClassName() {
            return getClassName();
        }

        @Override
        public void clickOn(NamedBean t) {
            int oldState = ((Light) t).getState();
            int newState;
            switch(oldState) {
                case Light.ON:
                    newState = Light.OFF;
                    break;
                case Light.OFF:
                    newState = Light.ON;
                    break;
                default:
                    newState = Light.OFF;
                    log.warn("Unexpected Light state " + oldState + " becomes OFF");
                    break;
            }
            ((Light) t).setState(newState);
        }

        @Override
        public JButton configureButton() {
            return new JButton(" " + Bundle.getMessage("LightStateOff") + " ");
        }

        @Override
        protected String getBeanType() {
            return Bundle.getMessage("BeanNameLight");
        }

        /**
             * Customize the light table Value (State) column to show an appropriate graphic for the light state
             * if _graphicState = true, or (default) just show the localized state text
             * when the TableDataModel is being called from ListedTableAction.
             *
             * @param table a JTable of Lights
             */
        @Override
        protected void configValueColumn(JTable table) {
            // have the value column hold a JPanel (icon)
            //setColumnToHoldButton(table, VALUECOL, new JLabel("123456")); // for small round icon, but cannot be converted to JButton
            // add extras, override BeanTableDataModel
            log.debug("Light configValueColumn (I am {})", super.toString());
            if (_graphicState) {
                // load icons, only once
                // editor
                table.setDefaultEditor(JLabel.class, new ImageIconRenderer());
                // item class copied from SwitchboardEditor panel
                table.setDefaultRenderer(JLabel.class, new ImageIconRenderer());
            } else {
                // classic text style state indication
                super.configValueColumn(table);
            }
        }

        /**
             * Visualize state in table as a graphic, customized for Lights (2 states + ... for transitioning).
             * Renderer and Editor are identical, as the cell contents are not actually edited,
             * only used to toggle state using {@link #clickOn(NamedBean)}.
             * @see jmri.jmrit.beantable.sensor.SensorTableDataModel.ImageIconRenderer
             * @see jmri.jmrit.beantable.BlockTableAction#createModel()
             * @see jmri.jmrit.beantable.TurnoutTableAction#createModel()
             */
        class ImageIconRenderer extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {

            protected JLabel label;

            // also used in display.switchboardEditor
            protected String rootPath = "resources/icons/misc/switchboard/";

            // for Light
            protected char beanTypeChar = 'L';

            protected String onIconPath = rootPath + beanTypeChar + "-on-s.png";

            protected String offIconPath = rootPath + beanTypeChar + "-off-s.png";

            protected BufferedImage onImage;

            protected BufferedImage offImage;

            protected ImageIcon onIcon;

            protected ImageIcon offIcon;

            protected int iconHeight = -1;

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                log.debug("Renderer Item = {}, State = {}", row, value);
                if (iconHeight < 0) {
                    // load resources only first time, either for renderer or editor
                    loadIcons();
                    log.debug("icons loaded");
                }
                return updateLabel((String) value, row);
            }

            @Override
            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                log.debug("Renderer Item = {}, State = {}", row, value);
                if (iconHeight < 0) {
                    // load resources only first time, either for renderer or editor
                    loadIcons();
                    log.debug("icons loaded");
                }
                return updateLabel((String) value, row);
            }

            public JLabel updateLabel(String value, int row) {
                if (iconHeight > 0) {
                // if necessary, increase row height;
                //table.setRowHeight(row, Math.max(table.getRowHeight(), iconHeight - 5)); // TODO adjust table row height for Lights
                }
                if (value.equals(Bundle.getMessage("LightStateOff")) && offIcon != null) {
                    label = new JLabel(offIcon);
                    label.setVerticalAlignment(JLabel.BOTTOM);
                    log.debug("offIcon set");
                } else if (value.equals(Bundle.getMessage("LightStateOn")) && onIcon != null) {
                    label = new JLabel(onIcon);
                    label.setVerticalAlignment(JLabel.BOTTOM);
                    log.debug("onIcon set");
                } else if (value.equals(Bundle.getMessage("BeanStateInconsistent"))) {
                    // centered text alignment
                    label = new JLabel("X", JLabel.CENTER);
                    label.setForeground(Color.red);
                    log.debug("Light state inconsistent");
                    iconHeight = 0;
                } else if (value.equals(Bundle.getMessage("LightStateIntermediate"))) {
                    // centered text alignment
                    label = new JLabel("...", JLabel.CENTER);
                    log.debug("Light state in transition");
                    iconHeight = 0;
                } else {
                    // failed to load icon
                    // centered text alignment
                    label = new JLabel(value, JLabel.CENTER);
                    log.warn("Error reading icons for LightTable");
                    iconHeight = 0;
                }
                label.setToolTipText(value);
                label.addMouseListener(new MouseAdapter() {

                    @Override
                    public final void mousePressed(MouseEvent evt) {
                        log.debug("Clicked on icon in row {}", row);
                        stopCellEditing();
                    }
                });
                return label;
            }

            @Override
            public Object getCellEditorValue() {
                log.debug("getCellEditorValue, me = {})", this.toString());
                return this.toString();
            }

            /**
                 * Read and buffer graphics. Only called once for this table.
                 * @see #getTableCellEditorComponent(JTable, Object, boolean, int, int)
                 */
            protected void loadIcons() {
                try {
                    onImage = ImageIO.read(new File(onIconPath));
                    offImage = ImageIO.read(new File(offIconPath));
                } catch (IOException ex) {
                    log.error("error reading image from {} or {}", onIconPath, offIconPath, ex);
                }
                log.debug("Success reading images");
                int imageWidth = onImage.getWidth();
                int imageHeight = onImage.getHeight();
                // scale icons 50% to fit in table rows
                Image smallOnImage = onImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_DEFAULT);
                Image smallOffImage = offImage.getScaledInstance(imageWidth / 2, imageHeight / 2, Image.SCALE_DEFAULT);
                onIcon = new ImageIcon(smallOnImage);
                offIcon = new ImageIcon(smallOffImage);
                iconHeight = onIcon.getIconHeight();
            }
        }
    };
// end of custom data model
}
Also used : ImageIcon(javax.swing.ImageIcon) NamedBean(jmri.NamedBean) JButton(javax.swing.JButton) JTextField(javax.swing.JTextField) LightManager(jmri.LightManager) InstanceManager(jmri.InstanceManager) GuiLafPreferencesManager(apps.gui.GuiLafPreferencesManager) Manager(jmri.Manager) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) Light(jmri.Light) AbstractCellEditor(javax.swing.AbstractCellEditor) TableCellEditor(javax.swing.table.TableCellEditor) TableCellRenderer(javax.swing.table.TableCellRenderer) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) IOException(java.io.IOException) JTable(javax.swing.JTable) File(java.io.File)

Aggregations

Image (java.awt.Image)515 BufferedImage (java.awt.image.BufferedImage)263 ImageIcon (javax.swing.ImageIcon)127 Graphics2D (java.awt.Graphics2D)97 IOException (java.io.IOException)73 File (java.io.File)64 Point (java.awt.Point)53 URL (java.net.URL)51 Graphics (java.awt.Graphics)46 JLabel (javax.swing.JLabel)31 Dimension (java.awt.Dimension)28 Rectangle (java.awt.Rectangle)27 AffineTransform (java.awt.geom.AffineTransform)25 Color (java.awt.Color)24 Test (org.junit.Test)24 RenderedImage (java.awt.image.RenderedImage)21 ArrayList (java.util.ArrayList)21 Toolkit (java.awt.Toolkit)20 IIOImage (javax.imageio.IIOImage)18 MediaTracker (java.awt.MediaTracker)17