Search in sources :

Example 26 with Point

use of com.codename1.ui.geom.Point in project CodenameOne by codenameone.

the class Effects method reflectionImage.

/**
 * Takes the given image and appends an effect of reflection below it that
 * is similar to the way elements appear in water beneath them. This method
 * shouldn't be used when numAlpha is very low.
 *
 * @param source image to add the reflection effect to
 * @param mirrorRatio generally less than 1, a mirror ration of 0.5f will create a mirror image half the
 * height of the image, 0.75f will create a 3 quarter height mirror etc.
 * @param alphaRatio starting point for the alpha value in the mirror, this should be a number between 0 - 255
 * (recommended larger than 0) indicating the opacity of the closest pixel. For a mirror thats completely
 * opaque use 255. A recommended value would be between 128 to 90.
 * @param spacing the distance in pixels between the image and its reflection
 * @return new image with a reflection effect for the source image
 */
public static Image reflectionImage(Image source, float mirrorRatio, int alphaRatio, int spacing) {
    int w = source.getWidth();
    int h = source.getHeight();
    int mirrorHeight = ((int) (h * mirrorRatio)) * w;
    // create an array big enough to hold the mirror data
    RGBImage rgbImg = new RGBImage(new int[w * (h + spacing) + mirrorHeight], w, h + ((int) (h * mirrorRatio) + spacing));
    source.toRGB(rgbImg, 0, 0, 0, 0, w, h);
    int[] imageData = rgbImg.getRGB();
    int ilen = imageData.length;
    for (int iter = 0; iter < mirrorHeight; iter++) {
        int sourcePos = w * h - iter - 1;
        int off = iter % w;
        off = w - off + iter - off;
        int mirrorPos = ilen - (mirrorHeight - off) + (spacing * w);
        int color = imageData[sourcePos];
        // if the color is not transparent
        if ((color & 0xff000000) != 0 && mirrorPos < ilen) {
            int alpha = (int) (alphaRatio * ((float) mirrorHeight - iter) / ((float) mirrorHeight));
            imageData[mirrorPos] = (imageData[sourcePos] & 0xffffff) | ((alpha << 24) & 0xff000000);
        }
    }
    return rgbImg;
}
Also used : RGBImage(com.codename1.ui.RGBImage)

Example 27 with Point

use of com.codename1.ui.geom.Point 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 28 with Point

use of com.codename1.ui.geom.Point in project CodenameOne by codenameone.

the class JavaSEPort method loadSkinFile.

private void loadSkinFile(InputStream skin, final JFrame frm) {
    try {
        ZipInputStream z = new ZipInputStream(skin);
        ZipEntry e = z.getNextEntry();
        final Properties props = new Properties();
        BufferedImage map = null;
        BufferedImage landscapeMap = null;
        // if we load the native theme imediately the multi-image's will be loaded with the size of the old skin
        byte[] nativeThemeData = null;
        nativeThemeRes = null;
        nativeTheme = null;
        while (e != null) {
            String name = e.getName();
            if (name.equals("skin.png")) {
                portraitSkin = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("header.png")) {
                header = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("header_l.png")) {
                headerLandscape = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin.properties")) {
                props.load(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin_l.png")) {
                landscapeSkin = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin_map.png")) {
                map = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.equals("skin_map_l.png")) {
                landscapeMap = ImageIO.read(z);
                e = z.getNextEntry();
                continue;
            }
            if (name.endsWith(".res")) {
                long esize = e.getSize();
                if (esize > 0) {
                    nativeThemeData = new byte[(int) esize];
                    readFully(z, nativeThemeData);
                } else {
                    ByteArrayOutputStream b = new ByteArrayOutputStream();
                    Util.copyNoClose(z, b, 8192);
                    nativeThemeData = b.toByteArray();
                }
                e = z.getNextEntry();
                continue;
            }
            if (name.endsWith(".ttf")) {
                try {
                    java.awt.Font result = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, z);
                    GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(result);
                } catch (FontFormatException ex) {
                    ex.printStackTrace();
                }
                e = z.getNextEntry();
                continue;
            }
            e = z.getNextEntry();
        }
        z.close();
        String ppi = props.getProperty("ppi");
        if (ppi != null) {
            double ppiD = Double.valueOf(ppi);
            pixelMilliRatio = ppiD / 25.4;
        } else {
            String pix = props.getProperty("pixelRatio");
            if (pix != null && pix.length() > 0) {
                try {
                    pixelMilliRatio = Double.valueOf(pix);
                } catch (NumberFormatException err) {
                    err.printStackTrace();
                    pixelMilliRatio = null;
                }
            } else {
                pixelMilliRatio = null;
            }
        }
        portraitSkinHotspots = new HashMap<Point, Integer>();
        portraitScreenCoordinates = new Rectangle();
        landscapeSkinHotspots = new HashMap<Point, Integer>();
        landscapeScreenCoordinates = new Rectangle();
        if (props.getProperty("roundScreen", "false").equalsIgnoreCase("true")) {
            portraitScreenCoordinates.x = Integer.parseInt(props.getProperty("displayX"));
            portraitScreenCoordinates.y = Integer.parseInt(props.getProperty("displayY"));
            portraitScreenCoordinates.width = Integer.parseInt(props.getProperty("displayWidth"));
            portraitScreenCoordinates.height = Integer.parseInt(props.getProperty("displayHeight"));
            landscapeScreenCoordinates.x = portraitScreenCoordinates.y;
            landscapeScreenCoordinates.y = portraitScreenCoordinates.x;
            landscapeScreenCoordinates.width = portraitScreenCoordinates.height;
            landscapeScreenCoordinates.height = portraitScreenCoordinates.width;
            roundedSkin = true;
        } else {
            initializeCoordinates(map, props, portraitSkinHotspots, portraitScreenCoordinates);
            initializeCoordinates(landscapeMap, props, landscapeSkinHotspots, landscapeScreenCoordinates);
        }
        platformName = props.getProperty("platformName", "se");
        platformOverrides = props.getProperty("overrideNames", "").split(",");
        String ua = null;
        if (platformName.equals("and")) {
            ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
        } else if (platformName.equals("rim")) {
            ua = "Mozilla/5.0 (BlackBerry; U; BlackBerry 9860; en-GB) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.296 Mobile Safari/534.11+";
        } else if (platformName.equals("ios")) {
            if (isTablet()) {
                ua = "Mozilla/5.0 (iPad; U; CPU OS 4_3_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5";
            } else {
                ua = "Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5";
            }
        } else if (platformName.equals("me")) {
            ua = "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124";
        } else {
            if (platformName.equals("win")) {
                ua = "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 800)";
            } else {
                ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
            }
        }
        Display.getInstance().setProperty("User-Agent", ua);
        isIOS = props.getProperty("systemFontFamily", "Arial").toLowerCase().contains("helvetica");
        setFontFaces(props.getProperty("systemFontFamily", "Arial"), props.getProperty("proportionalFontFamily", "SansSerif"), props.getProperty("monospaceFontFamily", "Monospaced"));
        int med;
        int sm;
        int la;
        if (pixelMilliRatio == null) {
            float factor = ((float) getDisplayHeightImpl()) / 480.0f;
            med = (int) (15.0f * factor);
            sm = (int) (11.0f * factor);
            la = (int) (19.0f * factor);
        } else {
            med = (int) Math.round(2.6 * pixelMilliRatio.doubleValue());
            sm = (int) Math.round(2 * pixelMilliRatio.doubleValue());
            la = (int) Math.round(3.3 * pixelMilliRatio.doubleValue());
        }
        setFontSize(Integer.parseInt(props.getProperty("mediumFontSize", "" + med)), Integer.parseInt(props.getProperty("smallFontSize", "" + sm)), Integer.parseInt(props.getProperty("largeFontSize", "" + la)));
        tablet = props.getProperty("tablet", "false").equalsIgnoreCase("true");
        rotateTouchKeysOnLandscape = props.getProperty("rotateKeys", "false").equalsIgnoreCase("true");
        touchDevice = props.getProperty("touch", "true").equalsIgnoreCase("true");
        keyboardType = Integer.parseInt(props.getProperty("keyboardType", "0"));
        softkeyCount = Integer.parseInt(props.getProperty("softbuttonCount", "1"));
        if (softkeyCount < 2) {
            // the values of the static variables to be correct!
            try {
                Field f = com.codename1.ui.MenuBar.class.getDeclaredField("leftSK");
                f.setAccessible(true);
                f.setInt(null, KeyEvent.VK_F1);
                f = com.codename1.ui.MenuBar.class.getDeclaredField("rightSK");
                f.setAccessible(true);
                f.setInt(null, KeyEvent.VK_F2);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        final byte[] nativeThemeFinalData = nativeThemeData;
        Display.getInstance().callSerially(new Runnable() {

            public void run() {
                if (nativeThemeFinalData != null) {
                    try {
                        nativeThemeRes = Resources.open(new ByteArrayInputStream(nativeThemeFinalData));
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                } else {
                    try {
                        boolean isJ2me = props.getProperty("platformName", "").equals("me");
                        String t = props.getProperty("nativeThemeAttribute", null);
                        if (t != null) {
                            Properties cnop = new Properties();
                            File cnopFile = new File("codenameone_settings.properties");
                            if (cnopFile.exists()) {
                                cnop.load(new FileInputStream(cnopFile));
                                int themeConst = Integer.parseInt(cnop.getProperty("codename1.j2me.nativeThemeConst", "3"));
                                t = cnop.getProperty(t, null);
                                if (isJ2me && themeConst == 3 && t != null && new File(t).exists()) {
                                    nativeThemeRes = Resources.open(new FileInputStream(t));
                                }
                            }
                        }
                    } catch (IOException ioErr) {
                        ioErr.printStackTrace();
                    }
                }
            }
        });
        installMenu(frm, false);
    } catch (IOException err) {
        err.printStackTrace();
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) Rectangle(java.awt.Rectangle) Properties(com.codename1.io.Properties) BufferedImage(java.awt.image.BufferedImage) Field(java.lang.reflect.Field) Point(java.awt.Point) FontFormatException(java.awt.FontFormatException) Point(java.awt.Point) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) java.awt(java.awt)

Example 29 with Point

use of com.codename1.ui.geom.Point in project CodenameOne by codenameone.

the class PointLayer method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g, Tile tile) {
    Point pos = tile.pointPosition(this);
    int width = 6;
    int height = 6;
    if (icon != null) {
        width = icon.getWidth();
        height = icon.getHeight();
    }
    int x = pos.getX() - width / 2;
    int y = pos.getY() - height / 2;
    if (icon == null) {
        g.fillRect(x, y, width, height);
    } else {
        g.drawImage(icon, x, y);
    }
    if (name != null && displayName) {
        g.drawString(getName(), x + width + 1, pos.getY() - g.getFont().getHeight() / 2 - 1);
    }
}
Also used : Point(com.codename1.ui.geom.Point) Point(com.codename1.ui.geom.Point)

Example 30 with Point

use of com.codename1.ui.geom.Point in project CodenameOne by codenameone.

the class LayerWithZoomLevels method getTiles.

private void getTiles() throws RuntimeException {
    _tiles = new Vector();
    Dimension tileSize = _map.tileSize();
    int posY = 0;
    _delta = null;
    while (posY - tileSize.getHeight() < getHeight()) {
        int posX = 0;
        while (posX - tileSize.getWidth() < getWidth()) {
            Tile tile;
            Coord cur = _map.translate(_center, _zoom, posX - getWidth() / 2, getHeight() / 2 - posY);
            if (_map.projection().extent().contains(cur)) {
                tile = _map.tileFor(_map.bboxFor(cur, _zoom));
                if (_delta == null) {
                    _delta = tile.pointPosition(cur);
                }
                tile.setsTileReadyListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        refreshLayers = true;
                        repaint();
                    }
                });
                _tiles.addElement(new PositionedTile(new Point(posX, posY), tile));
            }
            posX += tileSize.getWidth();
        }
        posY += tileSize.getHeight();
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) ActionEvent(com.codename1.ui.events.ActionEvent) Dimension(com.codename1.ui.geom.Dimension) Point(com.codename1.ui.geom.Point) Vector(java.util.Vector) Point(com.codename1.ui.geom.Point)

Aggregations

Point (com.codename1.ui.geom.Point)11 Point (com.codename1.charts.models.Point)10 Coord (com.codename1.maps.Coord)9 Paint (com.codename1.charts.compat.Paint)7 Component (com.codename1.ui.Component)5 TextArea (com.codename1.ui.TextArea)3 Dimension (com.codename1.ui.geom.Dimension)3 GeneralPath (com.codename1.ui.geom.GeneralPath)3 Rectangle (com.codename1.ui.geom.Rectangle)3 Border (com.codename1.ui.plaf.Border)3 ArrayList (java.util.ArrayList)3 SeriesSelection (com.codename1.charts.models.SeriesSelection)2 Container (com.codename1.ui.Container)2 EncodedImage (com.codename1.ui.EncodedImage)2 Form (com.codename1.ui.Form)2 Image (com.codename1.ui.Image)2 Label (com.codename1.ui.Label)2 ActionEvent (com.codename1.ui.events.ActionEvent)2 ActionListener (com.codename1.ui.events.ActionListener)2 Style (com.codename1.ui.plaf.Style)2