Search in sources :

Example 11 with AnimationObject

use of com.codename1.ui.animations.AnimationObject 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 12 with AnimationObject

use of com.codename1.ui.animations.AnimationObject 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 13 with AnimationObject

use of com.codename1.ui.animations.AnimationObject 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 14 with AnimationObject

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

the class CodenameOneImageRenderer method paintComponent.

@Override
public void paintComponent(Graphics g) {
    g.setColor(new Color(CheckerBoardColorCalibration.getColorA()));
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(new Color(CheckerBoardColorCalibration.getColorB()));
    int width = getWidth();
    int height = getHeight();
    for (int x = 0; x < width; x += 8) {
        if (x % 16 == 0) {
            for (int y = 0; y < height; y += 16) {
                g.fillRect(x, y, 8, 8);
            }
        } else {
            for (int y = 8; y < height; y += 16) {
                g.fillRect(x, y, 8, 8);
            }
        }
    }
    if (drawBorder) {
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, image.getWidth() + 1, image.getHeight() + 1);
    }
    if (zoom != 1) {
        ((Graphics2D) g).scale(zoom, zoom);
    }
    if (animationObjectList != null) {
        if (((Timeline) image).isPause()) {
            int selectedRow = animationObjectList.getSelectedRow();
            AnimationObject sel = null;
            if (selectedRow > -1) {
                sel = ((TimelineEditor.AnimationObjectTableModel) animationObjectList.getModel()).getElementAt(selectedRow);
            }
            if (dragging != null) {
                if (draggingImage == null) {
                    draggingImage = new BufferedImage(AnimationAccessor.getWidthInt(dragging), AnimationAccessor.getHeightInt(dragging), BufferedImage.TYPE_INT_ARGB);
                    draggingImage.setRGB(0, 0, draggingImage.getWidth(), draggingImage.getHeight(), AnimationAccessor.getImageMethod(dragging).modifyAlpha((byte) 110).getRGB(), 0, draggingImage.getWidth());
                }
            } else {
                draggingImage = null;
            }
            if (sel != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(buffer[currentFrame], 0, 0, this);
                g2d.setColor(Color.BLUE);
                g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                g2d.drawRect(AnimationAccessor.getX(sel), AnimationAccessor.getY(sel), AnimationAccessor.getWidthInt(sel), AnimationAccessor.getHeightInt(sel));
                if (draggingImage != null) {
                    g2d.drawImage(draggingImage, dragX, dragY, null);
                }
                g2d.dispose();
                return;
            }
        }
    }
    g.drawImage(buffer[currentFrame], 0, 0, this);
    if (draggingImage != null) {
        g.drawImage(draggingImage, dragX, dragY, null);
    }
}
Also used : BasicStroke(java.awt.BasicStroke) Timeline(com.codename1.ui.animations.Timeline) Color(java.awt.Color) AnimationObject(com.codename1.ui.animations.AnimationObject) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

AnimationObject (com.codename1.ui.animations.AnimationObject)14 Timeline (com.codename1.ui.animations.Timeline)11 Point (java.awt.Point)8 EncodedImage (com.codename1.ui.EncodedImage)2 Motion (com.codename1.ui.animations.Motion)2 BufferedImage (java.awt.image.BufferedImage)2 Image (com.codename1.ui.Image)1 Dimension (com.codename1.ui.geom.Dimension)1 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 Graphics2D (java.awt.Graphics2D)1 MouseListener (java.awt.event.MouseListener)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 ImageReader (javax.imageio.ImageReader)1