Search in sources :

Example 26 with Timeline

use of com.codename1.ui.animations.Timeline in project CodenameOne by codenameone.

the class TimelineEditor method selectFile.

/**
 * Selects a gif file using a file chooser and converts it to a timeline
 */
public static void selectFile(ResourceEditorView view, EditableResources res, String timelineName) {
    File[] files = ResourceEditorView.showOpenFileChooser("Images", ".gif");
    if (files != null) {
        File sel = files[0];
        if (timelineName == null) {
            timelineName = sel.getName();
        }
        Preferences.userNodeForPackage(view.getClass()).put("lastDir", sel.getParentFile().getAbsolutePath());
        ImageReader iReader = ImageIO.getImageReadersBySuffix("gif").next();
        try {
            iReader.setInput(ImageIO.createImageInputStream(new FileInputStream(sel)));
            int frames = iReader.getNumImages(true);
            AnimationObject[] anims = new AnimationObject[frames];
            int currentTime = 0;
            for (int frameIter = 0; frameIter < frames; frameIter++) {
                BufferedImage currentImage = iReader.read(frameIter);
                ByteArrayOutputStream bo = new ByteArrayOutputStream();
                ImageIO.write(currentImage, "png", bo);
                bo.close();
                // create a PNG image in the resource file
                String label = sel.getName() + " frame:" + frameIter;
                EncodedImage i = EncodedImage.create(bo.toByteArray());
                res.setImage(label, i);
                int duration = Math.max(40, AnimationImpl.getFrameTime(iReader, frameIter));
                Point pos = AnimationImpl.getPixelOffsets(iReader, frameIter);
                anims[frameIter] = AnimationObject.createAnimationImage(i, pos.x, pos.y);
                anims[frameIter].setStartTime(currentTime);
                anims[frameIter].setEndTime(100000000);
                String disposeMethod = getDisposalMethod(iReader, frameIter);
                if (disposeMethod != null) {
                    if ("restoreToBackgroundColor".equals(disposeMethod)) {
                        if (frameIter + 1 < frames) {
                            int t = Math.max(40, AnimationImpl.getFrameTime(iReader, frameIter + 1));
                            anims[frameIter].setEndTime(currentTime + t);
                        } else {
                            anims[frameIter].setEndTime(currentTime + duration);
                        }
                    // for(int iter = frameIter ; iter >= 0 ; iter--) {
                    // anims[iter].setEndTime(currentTime);
                    // }
                    }
                // "none" |
                // "doNotDispose" | "restoreToBackgroundColor" |
                // "restoreToPrevious"
                }
                currentTime += duration;
            }
            Timeline t = Timeline.createTimeline(currentTime, anims, new com.codename1.ui.geom.Dimension(iReader.getWidth(0), iReader.getHeight(0)));
            res.setImage(timelineName, t);
            view.setSelectedResource(timelineName);
        } catch (IOException err) {
            err.printStackTrace();
            JOptionPane.showMessageDialog(JFrame.getFrames()[0], "Error reading file " + err, "IO Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Point(java.awt.Point) IOException(java.io.IOException) EncodedImage(com.codename1.ui.EncodedImage) FileInputStream(java.io.FileInputStream) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Timeline(com.codename1.ui.animations.Timeline) AnimationObject(com.codename1.ui.animations.AnimationObject) ImageReader(javax.imageio.ImageReader) File(java.io.File)

Example 27 with Timeline

use of com.codename1.ui.animations.Timeline in project CodenameOne by codenameone.

the class TimelineEditor method cloneCurrentTimeline.

// </editor-fold>//GEN-END:initComponents
private Timeline cloneCurrentTimeline() {
    Timeline t = (Timeline) res.getImage(name);
    AnimationObject[] arr = new AnimationObject[t.getAnimationCount()];
    for (int iter = 0; iter < arr.length; iter++) {
        arr[iter] = t.getAnimation(iter);
    }
    Timeline nt = Timeline.createTimeline(getValue(duration), arr, new com.codename1.ui.geom.Dimension(getValue(width), getValue(height)));
    nt.setPause(t.isPause());
    nt.setTime(t.getTime());
    return nt;
}
Also used : Timeline(com.codename1.ui.animations.Timeline) AnimationObject(com.codename1.ui.animations.AnimationObject) Point(java.awt.Point)

Example 28 with Timeline

use of com.codename1.ui.animations.Timeline in project CodenameOne by codenameone.

the class TimelineEditor method moveUpActionPerformed.

// GEN-LAST:event_duplicateObjectActionPerformed
private void moveUpActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_moveUpActionPerformed
    int i = animationObjectList.getSelectedRow();
    Timeline t = cloneCurrentTimeline();
    AnimationObject[] animations = new AnimationObject[t.getAnimationCount()];
    for (int iter = 0; iter < animations.length; iter++) {
        animations[iter] = t.getAnimation(iter);
    }
    AnimationObject o = animations[i - 1];
    animations[i - 1] = animations[i];
    animations[i] = o;
    Timeline nt = Timeline.createTimeline(getValue(duration), animations, new com.codename1.ui.geom.Dimension(getValue(width), getValue(height)));
    nt.setPause(t.isPause());
    setImage(nt);
    moveUp.setEnabled(i > 1);
    moveDown.setEnabled(true);
    animationObjectList.getSelectionModel().setSelectionInterval(i - 1, i - 1);
}
Also used : Timeline(com.codename1.ui.animations.Timeline) AnimationObject(com.codename1.ui.animations.AnimationObject) Point(java.awt.Point)

Example 29 with Timeline

use of com.codename1.ui.animations.Timeline in project CodenameOne by codenameone.

the class TimelineEditor method playActionPerformed.

private void playActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_playActionPerformed
    final Timeline t = (Timeline) renderer.getImage();
    t.setPause(false);
    play.setEnabled(false);
    pause.setEnabled(true);
    refreshTimeline = new javax.swing.Timer(500, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (t.isPause()) {
                pause.setEnabled(false);
                play.setEnabled(true);
                refreshTimeline.stop();
                refreshTimeline = null;
                return;
            }
            refreshingTimelineLock = true;
            timeline.setValue(t.getTime());
            refreshingTimelineLock = false;
        }
    });
    refreshTimeline.setRepeats(true);
    refreshTimeline.start();
}
Also used : Timeline(com.codename1.ui.animations.Timeline) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent)

Example 30 with Timeline

use of com.codename1.ui.animations.Timeline in project CodenameOne by codenameone.

the class Resources method createImage.

Image createImage(DataInputStream input) throws IOException {
    if (majorVersion == 0 && minorVersion == 0) {
        byte[] data = new byte[input.readInt()];
        input.readFully(data, 0, data.length);
        return EncodedImage.create(data);
    } else {
        int type = input.readByte() & 0xff;
        switch(type) {
            // PNG file
            case 0xf1:
            // JPEG File
            case 0xf2:
                byte[] data = new byte[input.readInt()];
                input.readFully(data, 0, data.length);
                if (minorVersion > 3) {
                    int width = input.readInt();
                    int height = input.readInt();
                    boolean opaque = input.readBoolean();
                    return EncodedImage.create(data, width, height, opaque);
                }
                return EncodedImage.create(data);
            // Indexed image
            case 0xF3:
                return createPackedImage8();
            // SVG
            case 0xF5:
                {
                    int svgSize = input.readInt();
                    if (Image.isSVGSupported()) {
                        byte[] s = new byte[svgSize];
                        input.readFully(s);
                        String baseURL = input.readUTF();
                        boolean animated = input.readBoolean();
                        loadSVGRatios(input);
                        byte[] fallback = new byte[input.readInt()];
                        if (fallback.length > 0) {
                            input.readFully(fallback, 0, fallback.length);
                        }
                        return Image.createSVG(baseURL, animated, s);
                    } else {
                        svgSize -= input.skip(svgSize);
                        while (svgSize > 0) {
                            svgSize -= input.skip(svgSize);
                        }
                        // read the base url, the animated property and screen ratios to skip them as well...
                        input.readUTF();
                        input.readBoolean();
                        input.readFloat();
                        input.readFloat();
                        byte[] fallback = new byte[input.readInt()];
                        input.readFully(fallback, 0, fallback.length);
                        return EncodedImage.create(fallback);
                    }
                }
            // SVG with multi-image
            case 0xf7:
                {
                    int svgSize = input.readInt();
                    if (Image.isSVGSupported()) {
                        byte[] s = new byte[svgSize];
                        input.readFully(s);
                        String baseURL = input.readUTF();
                        boolean animated = input.readBoolean();
                        Image img = readMultiImage(input, true);
                        Image svg = createSVG(animated, s);
                        if (svg.getSVGDocument() == null) {
                            return img;
                        }
                        return svg;
                    } else {
                        svgSize -= input.skip(svgSize);
                        while (svgSize > 0) {
                            svgSize -= input.skip(svgSize);
                        }
                        String baseURL = input.readUTF();
                        // read the animated property to skip it as well...
                        input.readBoolean();
                        return readMultiImage(input);
                    }
                }
            // mutli image
            case 0xF6:
                return readMultiImage(input);
            case 0xEF:
                Timeline tl = readTimeline(input);
                return tl;
            // Fail this is the wrong data type
            default:
                throw new IOException("Illegal type while creating image: " + Integer.toHexString(type));
        }
    }
}
Also used : Timeline(com.codename1.ui.animations.Timeline) IOException(java.io.IOException) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage)

Aggregations

Timeline (com.codename1.ui.animations.Timeline)23 AnimationObject (com.codename1.ui.animations.AnimationObject)18 EncodedImage (com.codename1.ui.EncodedImage)10 Point (java.awt.Point)9 BufferedImage (java.awt.image.BufferedImage)7 Image (com.codename1.ui.Image)6 Hashtable (java.util.Hashtable)6 Border (com.codename1.ui.plaf.Border)4 RoundBorder (com.codename1.ui.plaf.RoundBorder)4 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)4 ArrayList (java.util.ArrayList)3 Component (com.codename1.ui.Component)2 Container (com.codename1.ui.Container)2 EditorFont (com.codename1.ui.EditorFont)2 EditorTTFFont (com.codename1.ui.EditorTTFFont)2 Dimension (com.codename1.ui.geom.Dimension)2 UIBuilderOverride (com.codename1.ui.util.UIBuilderOverride)2 LegacyFont (com.codename1.ui.util.xml.LegacyFont)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2