Search in sources :

Example 16 with Image

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

the class Resources method readTimeline.

Timeline readTimeline(DataInputStream input) throws IOException {
    int duration = input.readInt();
    int width = input.readInt();
    int height = input.readInt();
    AnimationObject[] animations = new AnimationObject[input.readShort()];
    int alen = animations.length;
    for (int iter = 0; iter < alen; iter++) {
        String name = input.readUTF();
        int startTime = input.readInt();
        int animDuration = input.readInt();
        int x = input.readInt();
        int y = input.readInt();
        Image i = getImage(name);
        if (i == null) {
            animations[iter] = AnimationObject.createAnimationImage(name, this, x, y);
        } else {
            animations[iter] = AnimationObject.createAnimationImage(i, x, y);
        }
        animations[iter].setStartTime(startTime);
        animations[iter].setEndTime(startTime + animDuration);
        int frameDelay = input.readInt();
        if (frameDelay > -1) {
            int frameWidth = input.readInt();
            int frameHeight = input.readInt();
            animations[iter].defineFrames(frameWidth, frameHeight, frameDelay);
        }
        if (input.readBoolean()) {
            animations[iter].defineMotionX(input.readInt(), startTime, animDuration, x, input.readInt());
        }
        if (input.readBoolean()) {
            animations[iter].defineMotionY(input.readInt(), startTime, animDuration, y, input.readInt());
        }
        if (input.readBoolean()) {
            animations[iter].defineWidth(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
        }
        if (input.readBoolean()) {
            animations[iter].defineHeight(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
        }
        if (input.readBoolean()) {
            animations[iter].defineOpacity(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
        }
        if (input.readBoolean()) {
            animations[iter].defineOrientation(input.readInt(), startTime, animDuration, input.readInt(), input.readInt());
        }
    }
    Timeline tl = Timeline.createTimeline(duration, animations, new Dimension(width, height));
    return tl;
}
Also used : Timeline(com.codename1.ui.animations.Timeline) AnimationObject(com.codename1.ui.animations.AnimationObject) Dimension(com.codename1.ui.geom.Dimension) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage)

Example 17 with Image

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

the class Effects method squareShadow.

/**
 * Generates a square shadow and returns it
 *
 * @param width the width of the shadow image
 * @param height the height of the shadow image
 * @param blurRadius a shadow is blurred using a gaussian blur when available, a value of 10 is often satisfactory
 * @param opacity the opacity of the shadow between 0 - 1 where 1 is completely opaque
 * @return an image containing the shadow for source
 */
public static Image squareShadow(int width, int height, int blurRadius, float opacity) {
    Image img = Image.createImage(width + blurRadius * 2, height + blurRadius * 2, 0);
    Graphics g = img.getGraphics();
    int destAlpha = (int) (opacity * 255.0);
    g.setColor(0);
    Motion lin = Motion.createLinearMotion(2, destAlpha, blurRadius);
    // draw a gradient of sort for the shadow
    for (int iter = blurRadius - 1; iter >= 0; iter--) {
        lin.setCurrentMotionTime(iter);
        g.setAlpha(lin.getValue());
        g.drawRect(blurRadius + iter, blurRadius + iter, width - iter * 2, height - iter * 2);
    }
    if (Display.getInstance().isGaussianBlurSupported()) {
        img = Display.getInstance().gaussianBlurImage(img, blurRadius);
    }
    return img;
}
Also used : Graphics(com.codename1.ui.Graphics) Motion(com.codename1.ui.animations.Motion) RGBImage(com.codename1.ui.RGBImage) Image(com.codename1.ui.Image)

Example 18 with Image

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

the class ImageIO method saveAndKeepAspect.

/**
 * Scales an image on disk while maintaining an aspect ratio, the appropriate aspect size will be
 * picked based on the status of scaleToFill
 * @param imageFilePath the path to the image
 * @param preferredOutputPath the url where the image will be saved
 * @param format the format for the image either FORMAT_JPEG or FORMAT_PNG
 * @param width the desired width, either width or height will be respected based on aspect dimensions
 * @param height the desired height, either width or height will be respected based on aspect dimensions
 * @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that
 * this isn't implemented in all platforms.
 * @param onlyDownscale will not scale if the resolution to scale will be higher in this case will return the imageFilePath
 * @param scaleToFill when set to true will pick the larger value so the resulting image will be at least as big as width x height, when set to false
 * will create an image that is no bigger than width x height
 * @return the url for the scaled image or the url of the unscaled image
 * @throws IOException if the operation fails
 */
public String saveAndKeepAspect(String imageFilePath, String preferredOutputPath, String format, int width, int height, float quality, boolean onlyDownscale, boolean scaleToFill) throws IOException {
    Dimension d = getImageSize(imageFilePath);
    if (onlyDownscale) {
        if (scaleToFill) {
            if (d.getHeight() <= height || d.getWidth() <= width) {
                return imageFilePath;
            }
        } else {
            if (d.getHeight() <= height && d.getWidth() <= width) {
                return imageFilePath;
            }
        }
    }
    float ratio = ((float) d.getWidth()) / ((float) d.getHeight());
    int heightBasedOnWidth = (int) (((float) width) / ratio);
    int widthBasedOnHeight = (int) (((float) height) * ratio);
    if (scaleToFill) {
        if (heightBasedOnWidth >= width) {
            height = heightBasedOnWidth;
        } else {
            width = widthBasedOnHeight;
        }
    } else {
        if (heightBasedOnWidth > width) {
            width = widthBasedOnHeight;
        } else {
            height = heightBasedOnWidth;
        }
    }
    OutputStream im = FileSystemStorage.getInstance().openOutputStream(preferredOutputPath);
    save(imageFilePath, im, format, width, height, quality);
    return preferredOutputPath;
}
Also used : OutputStream(java.io.OutputStream) Dimension(com.codename1.ui.geom.Dimension)

Example 19 with Image

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

the class ImageIO method save.

/**
 * Saves an image object to the given format
 *
 * @param img the image object
 * @param response resulting image output will be written to this stream
 * @param format the format for the image either FORMAT_PNG or FORMAT_JPEG
 * @param quality the quality for the resulting image output (applicable mostly for JPEG), a value between 0 and 1 notice that
 * this isn't implemented in all platforms.
 */
public void save(Image img, OutputStream response, String format, float quality) throws IOException {
    if (img instanceof EncodedImage) {
        EncodedImage i = (EncodedImage) img;
        save(new ByteArrayInputStream(i.getImageData()), response, format, i.getWidth(), i.getHeight(), quality);
    } else {
        if (img.getImage() == null) {
            Image img2 = Image.createImage(img.getWidth(), img.getHeight(), 0);
            Graphics g = img2.getGraphics();
            g.drawImage(img, 0, 0);
            saveImage(img2, response, format, quality);
        } else {
            saveImage(img, response, format, quality);
        }
    }
}
Also used : Graphics(com.codename1.ui.Graphics) ByteArrayInputStream(java.io.ByteArrayInputStream) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) EncodedImage(com.codename1.ui.EncodedImage)

Example 20 with Image

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

the class PointsLayer method addPoint.

/**
 * Adds a point to the PointsLayer
 *
 * @param point a point to add
 */
public void addPoint(PointLayer point) {
    Image pointIcon = point.getIcon();
    if (pointIcon == null) {
        point.setIcon(icon);
    }
    if (!point.isProjected()) {
        Coord c = getProjection().fromWGS84(point);
        point.setLatitude(c.getLatitude());
        point.setLongitude(c.getLongitude());
        point.setProjected(true);
    }
    points.addElement(point);
}
Also used : Coord(com.codename1.maps.Coord) Image(com.codename1.ui.Image)

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