Search in sources :

Example 11 with Graphics

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

the class DefaultLookAndFeel method drawCheckBox.

/**
 * {@inheritDoc}
 */
public void drawCheckBox(Graphics g, Button cb) {
    if (chkBoxImages != null) {
        Image x;
        if (chkBoxImagesFocus != null && chkBoxImagesFocus[0] != null && cb.hasFocus() && Display.getInstance().shouldRenderSelection(cb)) {
            if (cb.isEnabled()) {
                x = chkBoxImagesFocus[cb.isSelected() ? 1 : 0];
            } else {
                x = chkBoxImagesFocus[cb.isSelected() ? 3 : 2];
            }
        } else {
            if (cb.isEnabled()) {
                x = chkBoxImages[cb.isSelected() ? 1 : 0];
            } else {
                x = chkBoxImages[cb.isSelected() ? 3 : 2];
            }
        }
        drawComponent(g, cb, cb.getIconFromState(), x, 0);
    } else {
        Style style = cb.getStyle();
        // checkbox square needs to be the height and width of the font height even
        // when no text is in the check box this is a good indication of phone DPI
        int height = cb.getStyle().getFont().getHeight();
        drawComponent(g, cb, cb.getIconFromState(), null, height);
        int gradientColor;
        g.setColor(style.getFgColor());
        gradientColor = style.getBgColor();
        int width = height;
        int rectWidth = scaleCoordinate(12f, 16, width);
        int tX = cb.getX();
        if (cb.isRTL()) {
            tX = tX + cb.getWidth() - style.getPaddingLeft(cb.isRTL()) - rectWidth;
        } else {
            tX += style.getPaddingLeft(cb.isRTL());
        }
        int tY = cb.getY() + style.getPaddingTop() + (cb.getHeight() - style.getPaddingTop() - style.getPaddingBottom()) / 2 - height / 2;
        g.translate(tX, tY);
        int x = scaleCoordinate(1.04f, 16, width);
        int y = scaleCoordinate(4.0f, 16, height);
        int rectHeight = scaleCoordinate(12f, 16, height);
        // brighten or darken the color slightly
        int destColor = findDestColor(gradientColor);
        g.fillLinearGradient(gradientColor, destColor, x + 1, y + 1, rectWidth - 2, rectHeight - 1, false);
        g.drawRoundRect(x, y, rectWidth, rectHeight, 5, 5);
        if (cb.isSelected()) {
            int color = g.getColor();
            g.setColor(0x111111);
            g.translate(0, 1);
            fillCheckbox(g, width, height);
            g.setColor(color);
            g.translate(0, -1);
            fillCheckbox(g, width, height);
        }
        g.translate(-tX, -tY);
    }
}
Also used : Image(com.codename1.ui.Image) FontImage(com.codename1.ui.FontImage)

Example 12 with Graphics

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

the class RoundBorder method paintBorderBackground.

@Override
public void paintBorderBackground(Graphics g, final Component c) {
    final int w = c.getWidth();
    final int h = c.getHeight();
    int x = c.getX();
    int y = c.getY();
    if (w > 0 && h > 0) {
        Object k = c.getClientProperty(CACHE_KEY + instanceVal);
        if (k instanceof CacheValue) {
            CacheValue val = (CacheValue) k;
            if (val.modificationTime == modificationTime && val.img.getWidth() == w && val.img.getHeight() == h) {
                g.drawImage(val.img, x, y);
                return;
            }
        }
    } else {
        return;
    }
    Image target = createTargetImage(c, w, h, true);
    g.drawImage(target, x, y);
    c.putClientProperty(CACHE_KEY + instanceVal, new CacheValue(target, modificationTime));
    // update the cache with a more refined version and repaint
    Display.getInstance().callSeriallyOnIdle(new Runnable() {

        public void run() {
            if (w == c.getWidth() && h == c.getHeight()) {
                Image target = createTargetImage(c, w, h, false);
                c.putClientProperty(CACHE_KEY + instanceVal, new CacheValue(target, modificationTime));
                c.repaint();
            }
        }
    });
}
Also used : Image(com.codename1.ui.Image)

Example 13 with Graphics

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

the class GlassTutorial method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g, Rectangle rect) {
    if (internal == null) {
        internal = new Label(" ");
        internal.setUIID("GlassTutorial");
    }
    internal.setSize(rect.getSize());
    internal.paintComponent(g);
    int componentCount = vec.size();
    for (int iter = 0; iter < componentCount; iter++) {
        Component current = (Component) vec.elementAt(iter);
        String pos = (String) current.getClientProperty(POS);
        Component dest = (Component) current.getClientProperty(DEST);
        int xpos = dest.getAbsoluteX();
        int ypos = dest.getAbsoluteY();
        int w = dest.getWidth();
        int h = dest.getHeight();
        if (pos.equals(BorderLayout.CENTER)) {
            current.setX(xpos);
            current.setY(ypos);
            current.setWidth(w);
            current.setHeight(h);
            current.paintComponent(g);
            continue;
        }
        Dimension d = current.getPreferredSize();
        current.setWidth(d.getWidth());
        current.setHeight(d.getHeight());
        if (pos.equals(BorderLayout.SOUTH)) {
            current.setX(xpos + w / 2 - d.getWidth() / 2);
            current.setY(ypos + h);
            current.paintComponent(g);
            continue;
        }
        if (pos.equals(BorderLayout.NORTH)) {
            current.setX(xpos + w / 2 - d.getWidth() / 2);
            current.setY(ypos - d.getHeight());
            current.paintComponent(g);
            continue;
        }
        if (pos.equals(BorderLayout.EAST)) {
            current.setX(xpos + w);
            current.setY(ypos + h / 2 - d.getHeight() / 2);
            current.paintComponent(g);
            continue;
        }
        if (pos.equals(BorderLayout.WEST)) {
            current.setX(xpos - d.getWidth());
            current.setY(ypos + h / 2 - d.getHeight() / 2);
            current.paintComponent(g);
            continue;
        }
    }
}
Also used : Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component)

Example 14 with Graphics

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

the class Spinner method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g) {
    super.paint(g);
    if (spinnerHandle != null) {
        Style s = getStyle();
        g.drawImage(spinnerHandle, getX() + getWidth() - spinnerHandle.getWidth() - s.getHorizontalPadding(), getY() + s.getPaddingTop());
    }
    if (System.currentTimeMillis() - inputSkipDelay < lastKeyInteraction || quickType.isPendingCommit()) {
        quickType.setWidth(Math.min(getWidth(), quickType.getPreferredW()));
        quickType.setHeight(Math.min(getHeight(), quickType.getPreferredH()));
        Style s = quickType.getStyle();
        quickType.setY(getScrollY() + getY());
        // positioning based on date/time
        if (getRenderer() instanceof DateTimeRenderer) {
            switch(currentInputAlign) {
                case LEFT:
                    quickType.setX(getX());
                    break;
                case RIGHT:
                    quickType.setX(getX() + quickType.getStyle().getFont().charWidth(TextArea.getWidestChar()) * 4 + s.getMarginRightNoRTL());
                    break;
                default:
                    quickType.setX(getX() + quickType.getStyle().getFont().charWidth(TextArea.getWidestChar()) * 2 + s.getMarginLeftNoRTL());
                    break;
            }
        } else {
            quickType.setX(getX());
        }
        quickType.paintComponent(g, true);
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 15 with Graphics

use of com.codename1.ui.Graphics 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)

Aggregations

Image (com.codename1.ui.Image)18 Component (com.codename1.ui.Component)17 Point (com.codename1.ui.geom.Point)16 Style (com.codename1.ui.plaf.Style)15 Graphics (com.codename1.ui.Graphics)14 Form (com.codename1.ui.Form)12 Font (com.codename1.ui.Font)11 GeneralPath (com.codename1.ui.geom.GeneralPath)9 Rectangle (com.codename1.ui.geom.Rectangle)9 Animation (com.codename1.ui.animations.Animation)8 Dialog (com.codename1.ui.Dialog)7 Dimension (com.codename1.ui.geom.Dimension)6 Painter (com.codename1.ui.Painter)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 FontImage (com.codename1.ui.FontImage)4 RGBImage (com.codename1.ui.RGBImage)4 Motion (com.codename1.ui.animations.Motion)4 ActionListener (com.codename1.ui.events.ActionListener)4 BorderLayout (com.codename1.ui.layouts.BorderLayout)4 IOException (java.io.IOException)4