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);
}
}
}
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;
}
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);
}
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();
}
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));
}
}
}
Aggregations