Search in sources :

Example 26 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class EditableResources method setImage.

public void setImage(final String name, final com.codename1.ui.Image value) {
    if (overrideResource != null) {
        overrideResource.setImage(name, value);
        return;
    }
    // we need to replace the image in all the themes...
    final com.codename1.ui.Image oldValue = getImage(name);
    byte type;
    if (value instanceof Timeline) {
        type = MAGIC_TIMELINE;
    } else {
        type = MAGIC_IMAGE;
    }
    final byte finalType = type;
    pushUndoable(new UndoableEdit() {

        @Override
        protected String performAction() {
            replaceThemeValue(oldValue, value);
            setResource(name, finalType, value);
            return name;
        }

        @Override
        protected String performUndo() {
            replaceThemeValue(value, oldValue);
            setResource(name, finalType, oldValue);
            return name;
        }
    });
}
Also used : Timeline(com.codename1.ui.animations.Timeline) Image(com.codename1.ui.Image)

Example 27 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class EditorFont method getBitmapFont.

/**
 * @return the bitmapFont
 */
public Font getBitmapFont() {
    Font bitmapFont = Font.getBitmapFont(lookupFont);
    if (bitmapFont != null) {
        return bitmapFont;
    }
    BufferedImage image = new BufferedImage(5000, 50, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) image.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, bitmapAntialiasing);
    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
    g2d.setColor(new Color(0xff0000));
    g2d.setFont(java.awt.Font.decode(lookupFont.split(";")[0]));
    FontMetrics metrics = g2d.getFontMetrics();
    FontRenderContext context = g2d.getFontRenderContext();
    int height = (int) Math.ceil(metrics.getMaxDescent() + metrics.getMaxAscent());
    int baseline = (int) Math.ceil(metrics.getMaxAscent());
    String charsetStr = bitmapCharset;
    int[] offsets = new int[charsetStr.length()];
    int[] widths = new int[offsets.length];
    int currentOffset = 0;
    for (int iter = 0; iter < charsetStr.length(); iter++) {
        offsets[iter] = currentOffset;
        String currentChar = charsetStr.substring(iter, iter + 1);
        g2d.drawString(currentChar, currentOffset, baseline);
        Rectangle2D rect = g2d.getFont().getStringBounds(currentChar, context);
        widths[iter] = (int) Math.ceil(rect.getWidth());
        // occupies more ram
        if (g2d.getFont().isItalic()) {
            currentOffset += metrics.getMaxAdvance();
        } else {
            currentOffset += widths[iter] + 1;
        }
    }
    g2d.dispose();
    BufferedImage shrunk = new BufferedImage(currentOffset, height, BufferedImage.TYPE_INT_RGB);
    g2d = (Graphics2D) shrunk.getGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.dispose();
    int[] rgb = new int[shrunk.getWidth() * shrunk.getHeight()];
    shrunk.getRGB(0, 0, shrunk.getWidth(), shrunk.getHeight(), rgb, 0, shrunk.getWidth());
    com.codename1.ui.Image bitmap = com.codename1.ui.Image.createImage(rgb, shrunk.getWidth(), shrunk.getHeight());
    return com.codename1.ui.Font.createBitmapFont(lookupFont, bitmap, offsets, widths, charsetStr);
}
Also used : Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) Font(com.codename1.ui.Font) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) com.codename1.ui(com.codename1.ui) FontMetrics(java.awt.FontMetrics) FontRenderContext(java.awt.font.FontRenderContext)

Example 28 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class ResourceEditorView method launchOptiPngActionPerformed.

private void launchOptiPngActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_launchOptiPngActionPerformed
    if (loadedResources != null && configureOptiPNG()) {
        final ProgressMonitor pm = new ProgressMonitor(mainPanel, "Processing Images", "", 0, loadedResources.getImageResourceNames().length);
        new Thread() {

            public void run() {
                String node = Preferences.userNodeForPackage(ResourceEditorView.class).get("optiPng", null);
                int prog = 0;
                for (String imageName : loadedResources.getImageResourceNames()) {
                    if (pm.isCanceled()) {
                        pm.close();
                        return;
                    }
                    pm.setProgress(prog);
                    prog++;
                    pm.setNote(imageName);
                    Object image = loadedResources.getImage(imageName);
                    if (image instanceof com.codename1.ui.EncodedImage) {
                        if (loadedResources.getResourceObject(imageName) != image) {
                            // multi-image...
                            EditableResources.MultiImage multi = (EditableResources.MultiImage) loadedResources.getResourceObject(imageName);
                            EditableResources.MultiImage n = new EditableResources.MultiImage();
                            EncodedImage[] arr = new EncodedImage[multi.getInternalImages().length];
                            for (int iter = 0; iter < multi.getInternalImages().length; iter++) {
                                EncodedImage current = optimize(multi.getInternalImages()[iter], node);
                                if (current != null) {
                                    arr[iter] = current;
                                } else {
                                    arr[iter] = multi.getInternalImages()[iter];
                                }
                            }
                            n.setInternalImages(arr);
                            n.setDpi(multi.getDpi());
                            loadedResources.setMultiImage(imageName, n);
                        } else {
                            EncodedImage current = optimize((EncodedImage) image, node);
                            if (current != null) {
                                loadedResources.setImage(imageName, current);
                            }
                        }
                    }
                }
                pm.close();
            }
        }.start();
    }
}
Also used : ProgressMonitor(javax.swing.ProgressMonitor) AnimationObject(com.codename1.ui.animations.AnimationObject) EncodedImage(com.codename1.ui.EncodedImage) EditableResources(com.codename1.ui.util.EditableResources) EncodedImage(com.codename1.ui.EncodedImage)

Example 29 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class ResourceEditorView method isInUse.

/**
 * Returns true if the given image is used by a theme or timeline animation,
 * false otherwise.
 */
private boolean isInUse(String imageName) {
    Object multi = loadedResources.getResourceObject(imageName);
    if (multi instanceof EditableResources.MultiImage) {
        EditableResources.MultiImage m = (EditableResources.MultiImage) multi;
        for (com.codename1.ui.Image i : m.getInternalImages()) {
            if (isInUse(i)) {
                return true;
            }
        }
        return false;
    }
    com.codename1.ui.Image resourceValue = loadedResources.getImage(imageName);
    return isInUse(resourceValue);
}
Also used : AnimationObject(com.codename1.ui.animations.AnimationObject) EditableResources(com.codename1.ui.util.EditableResources)

Example 30 with Image

use of com.codename1.ui.Image in project CodenameOne by codenameone.

the class ResourceEditorView method initImagesComboBox.

/**
 * Creates a sorted image combo box that includes image previews. The combo box
 * can be searched by typing a letter even when images are used for the values...
 */
public static void initImagesComboBox(JComboBox cb, final EditableResources res, boolean asString, final boolean includeNull, boolean blockTimelines) {
    String[] imgs = res.getImageResourceNames();
    if (blockTimelines) {
        List<String> nonT = new ArrayList<String>();
        for (String c : imgs) {
            if (!(res.getImage(c) instanceof Timeline)) {
                nonT.add(c);
            }
        }
        imgs = new String[nonT.size()];
        nonT.toArray(imgs);
    }
    final String[] images = imgs;
    Arrays.sort(images, String.CASE_INSENSITIVE_ORDER);
    if (asString) {
        if (includeNull) {
            String[] n = new String[images.length + 1];
            System.arraycopy(images, 0, n, 1, images.length);
            cb.setModel(new DefaultComboBoxModel(n));
        } else {
            cb.setModel(new DefaultComboBoxModel(images));
        }
        cb.setRenderer(new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                boolean n = false;
                if (value == null) {
                    value = "[null]";
                    n = true;
                }
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (!n) {
                    setIcon(new CodenameOneImageIcon(res.getImage((String) value), 24, 24));
                } else {
                    setIcon(null);
                }
                return this;
            }
        });
    } else {
        int offset = 0;
        com.codename1.ui.Image[] arr;
        if (includeNull) {
            arr = new com.codename1.ui.Image[images.length + 1];
            offset++;
        } else {
            arr = new com.codename1.ui.Image[images.length];
        }
        for (String c : images) {
            arr[offset] = res.getImage(c);
            offset++;
        }
        cb.setModel(new DefaultComboBoxModel(arr));
        cb.setKeySelectionManager(new JComboBox.KeySelectionManager() {

            private String current;

            private long lastPress;

            public int selectionForKey(char aKey, ComboBoxModel aModel) {
                long t = System.currentTimeMillis();
                aKey = Character.toLowerCase(aKey);
                if (t - lastPress < 800) {
                    current += aKey;
                } else {
                    current = "" + aKey;
                }
                lastPress = t;
                for (int iter = 0; iter < images.length; iter++) {
                    if (images[iter].toLowerCase().startsWith(current)) {
                        if (includeNull) {
                            return iter + 1;
                        }
                        return iter;
                    }
                }
                return -1;
            }
        });
        cb.setRenderer(new DefaultListCellRenderer() {

            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                com.codename1.ui.Image i = (com.codename1.ui.Image) value;
                if (value == null) {
                    value = "[null]";
                } else {
                    value = res.findId(value);
                }
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (i != null) {
                    setIcon(new CodenameOneImageIcon(i, 24, 24));
                } else {
                    setIcon(null);
                }
                return this;
            }
        });
    }
}
Also used : JComboBox(javax.swing.JComboBox) ArrayList(java.util.ArrayList) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) EncodedImage(com.codename1.ui.EncodedImage) BufferedImage(java.awt.image.BufferedImage) Timeline(com.codename1.ui.animations.Timeline) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) AnimationObject(com.codename1.ui.animations.AnimationObject) Component(java.awt.Component) UIBuilderOverride(com.codename1.ui.util.UIBuilderOverride) JList(javax.swing.JList) ComboBoxModel(javax.swing.ComboBoxModel) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel)

Aggregations

Image (com.codename1.ui.Image)82 EncodedImage (com.codename1.ui.EncodedImage)46 IOException (java.io.IOException)29 Hashtable (java.util.Hashtable)19 AnimationObject (com.codename1.ui.animations.AnimationObject)18 BufferedImage (java.awt.image.BufferedImage)18 Style (com.codename1.ui.plaf.Style)16 Form (com.codename1.ui.Form)15 ActionEvent (com.codename1.ui.events.ActionEvent)14 Dimension (com.codename1.ui.geom.Dimension)14 FontImage (com.codename1.ui.FontImage)13 BorderLayout (com.codename1.ui.layouts.BorderLayout)13 Timeline (com.codename1.ui.animations.Timeline)12 ActionListener (com.codename1.ui.events.ActionListener)12 Border (com.codename1.ui.plaf.Border)12 Container (com.codename1.ui.Container)11 InputStream (java.io.InputStream)11 Graphics (com.codename1.ui.Graphics)10 Label (com.codename1.ui.Label)10 Button (com.codename1.ui.Button)9