Search in sources :

Example 51 with Dimensions

use of org.apache.pivot.wtk.Dimensions in project pivot by apache.

the class FillPaneSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    FillPane fillPane = (FillPane) getComponent();
    int baseline = -1;
    int contentHeight = 0;
    switch(fillPane.getOrientation()) {
        case HORIZONTAL:
            {
                int clientHeight = Math.max(height - padding.getHeight(), 0);
                for (Component component : fillPane) {
                    if (component.isVisible()) {
                        int componentWidth = component.getPreferredWidth(clientHeight);
                        baseline = Math.max(baseline, component.getBaseline(componentWidth, clientHeight));
                    }
                }
                break;
            }
        case VERTICAL:
            {
                int clientWidth = Math.max(width - padding.getWidth(), 0);
                for (Component component : fillPane) {
                    if (component.isVisible()) {
                        Dimensions size;
                        size = new Dimensions(clientWidth, component.getPreferredHeight(clientWidth));
                        if (baseline == -1) {
                            baseline = component.getBaseline(size.width, size.height);
                            if (baseline != -1) {
                                baseline += contentHeight;
                            }
                        }
                        contentHeight += size.height + spacing;
                    }
                }
                break;
            }
        default:
            {
                break;
            }
    }
    if (baseline != -1) {
        baseline += padding.top;
    }
    return baseline;
}
Also used : FillPane(org.apache.pivot.wtk.FillPane) Dimensions(org.apache.pivot.wtk.Dimensions) Component(org.apache.pivot.wtk.Component)

Example 52 with Dimensions

use of org.apache.pivot.wtk.Dimensions in project pivot by apache.

the class ImageViewSkin method getBaseline.

@Override
public int getBaseline(int width, int height) {
    ImageView imageView = (ImageView) getComponent();
    Image image = imageView.getImage();
    int baseline = -1;
    if (image != null) {
        baseline = image.getBaseline();
        if (baseline != -1) {
            Dimensions imageSize = image.getSize();
            if (fill) {
                // Scale to fit
                if (preserveAspectRatio) {
                    float aspectRatio = (float) width / (float) height;
                    float imageAspectRatio = (float) imageSize.width / (float) imageSize.height;
                    if (aspectRatio > imageAspectRatio) {
                        baseline *= (float) height / (float) imageSize.height;
                    } else {
                        float scaleYLocal = (float) width / (float) imageSize.width;
                        baseline *= scaleYLocal;
                        baseline += (int) (height - imageSize.height * scaleYLocal) / 2;
                    }
                } else {
                    baseline *= (float) height / (float) imageSize.height;
                }
            } else {
                if (verticalAlignment == VerticalAlignment.CENTER) {
                    baseline += (height - imageSize.height) / 2;
                } else if (verticalAlignment == VerticalAlignment.BOTTOM) {
                    baseline += height - imageSize.height;
                }
            }
        }
    }
    return baseline;
}
Also used : Dimensions(org.apache.pivot.wtk.Dimensions) ImageView(org.apache.pivot.wtk.ImageView) Image(org.apache.pivot.wtk.media.Image)

Example 53 with Dimensions

use of org.apache.pivot.wtk.Dimensions in project pivot by apache.

the class ImageViewSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    ImageView imageView = (ImageView) getComponent();
    Image image = imageView.getImage();
    return (image == null) ? Dimensions.ZERO : new Dimensions(image.getWidth(), image.getHeight());
}
Also used : Dimensions(org.apache.pivot.wtk.Dimensions) ImageView(org.apache.pivot.wtk.ImageView) Image(org.apache.pivot.wtk.media.Image)

Example 54 with Dimensions

use of org.apache.pivot.wtk.Dimensions in project pivot by apache.

the class ImageViewSkin method layout.

@Override
public void layout() {
    ImageView imageView = (ImageView) getComponent();
    Image image = imageView.getImage();
    if (image != null) {
        int width = getWidth();
        int height = getHeight();
        Dimensions imageSize = image.getSize();
        if (fill) {
            // Scale to fit
            if (preserveAspectRatio) {
                float aspectRatio = (float) width / (float) height;
                float imageAspectRatio = (float) imageSize.width / (float) imageSize.height;
                if (aspectRatio > imageAspectRatio) {
                    imageY = 0;
                    scaleY = (float) height / (float) imageSize.height;
                    imageX = (int) (width - imageSize.width * scaleY) / 2;
                    scaleX = scaleY;
                } else {
                    imageX = 0;
                    scaleX = (float) width / (float) imageSize.width;
                    imageY = (int) (height - imageSize.height * scaleX) / 2;
                    scaleY = scaleX;
                }
            } else {
                imageX = 0;
                scaleX = (float) width / (float) imageSize.width;
                imageY = 0;
                scaleY = (float) height / (float) imageSize.height;
            }
        } else {
            if (horizontalAlignment == HorizontalAlignment.CENTER) {
                imageX = (width - imageSize.width) / 2;
            } else if (horizontalAlignment == HorizontalAlignment.RIGHT) {
                imageX = width - imageSize.width;
            } else {
                imageX = 0;
            }
            scaleX = 1.0f;
            if (verticalAlignment == VerticalAlignment.CENTER) {
                imageY = (height - imageSize.height) / 2;
            } else if (verticalAlignment == VerticalAlignment.BOTTOM) {
                imageY = height - imageSize.height;
            } else {
                imageY = 0;
            }
            scaleY = 1.0f;
        }
    }
}
Also used : Dimensions(org.apache.pivot.wtk.Dimensions) ImageView(org.apache.pivot.wtk.ImageView) Image(org.apache.pivot.wtk.media.Image)

Example 55 with Dimensions

use of org.apache.pivot.wtk.Dimensions in project pivot by apache.

the class LabelSkin method getPreferredSize.

@Override
public Dimensions getPreferredSize() {
    Label label = (Label) getComponent();
    String text = label.getText();
    FontRenderContext fontRenderContext = Platform.getFontRenderContext();
    LineMetrics lm = font.getLineMetrics("", fontRenderContext);
    int lineHeight = (int) Math.ceil(lm.getHeight());
    int preferredHeight = 0;
    int preferredWidth = 0;
    if (text != null && text.length() > 0) {
        String[] str;
        if (wrapText) {
            str = text.split("\n");
        } else {
            str = new String[] { text };
        }
        for (String line : str) {
            Rectangle2D stringBounds = font.getStringBounds(line, fontRenderContext);
            int w = (int) Math.ceil(stringBounds.getWidth());
            if (w > preferredWidth) {
                preferredWidth = w;
            }
            preferredHeight += lineHeight;
        }
    } else {
        preferredHeight += lineHeight;
    }
    preferredHeight += (padding.top + padding.bottom);
    preferredWidth += (padding.left + padding.right);
    return new Dimensions(preferredWidth, preferredHeight);
}
Also used : Label(org.apache.pivot.wtk.Label) Rectangle2D(java.awt.geom.Rectangle2D) Dimensions(org.apache.pivot.wtk.Dimensions) FontRenderContext(java.awt.font.FontRenderContext) LineMetrics(java.awt.font.LineMetrics)

Aggregations

Dimensions (org.apache.pivot.wtk.Dimensions)76 Component (org.apache.pivot.wtk.Component)40 GradientPaint (java.awt.GradientPaint)21 Point (org.apache.pivot.wtk.Point)16 FontRenderContext (java.awt.font.FontRenderContext)9 Button (org.apache.pivot.wtk.Button)9 Paint (java.awt.Paint)7 Rectangle2D (java.awt.geom.Rectangle2D)6 LineMetrics (java.awt.font.LineMetrics)5 BoxPane (org.apache.pivot.wtk.BoxPane)5 FlowPane (org.apache.pivot.wtk.FlowPane)5 Label (org.apache.pivot.wtk.Label)5 ScrollPane (org.apache.pivot.wtk.ScrollPane)4 Separator (org.apache.pivot.wtk.Separator)4 Form (org.apache.pivot.wtk.Form)3 ImageView (org.apache.pivot.wtk.ImageView)3 Image (org.apache.pivot.wtk.media.Image)3 Color (java.awt.Color)2 Font (java.awt.Font)2 LineBreakMeasurer (java.awt.font.LineBreakMeasurer)2