List of usage examples for java.awt Component getBounds
public Rectangle getBounds()
From source file:self.philbrown.javaQuery.$.java
/** * For `ImageView`s, this will set the image to the given asset or url. Otherwise, it will set the * background image for the selected views. * @param source asset path, file path (starting with "file://") or URL to image * @param width specifies the output bitmap width * @param height specifies the output bitmap height * @param error if the given source is a file or asset, this receives a javaQuery wrapping the * current context and the {@code Throwable} error. Otherwise, this will receive an * Ajax error.//from www .j av a 2 s . co m * @return this * @see AjaxOptions#error(Function) */ public $ image(String source, int width, int height, Function error) { if (source.startsWith("file://")) { try { Image image = ImageIO.read(new File(source.substring(7))); for (Component v : views) { if (v instanceof JLabel) { ((JLabel) v).setIcon(new ImageIcon(image)); } else if (v instanceof Window) { ((Window) v).setIconImage(image); } else { if (v instanceof Container) { JLabel l = new JLabel(new ImageIcon(image)); l.setBounds(v.getBounds()); ((Container) v).add(l); } else if (v.getParent() != null && v.getParent() instanceof Container) { JLabel l = new JLabel(new ImageIcon(image)); l.setBounds(v.getBounds()); ((Container) v.getParent()).add(l); } } } } catch (Throwable t) { if (error != null) { error.invoke($.with(view(0)), t); } } } else { boolean fallthrough = false; try { new URL(source); } catch (Throwable t) { fallthrough = true; } if (fallthrough) { AjaxOptions options = new AjaxOptions().url(source).type("GET").dataType("image").context(view(0)) .global(false).success(new Function() { @Override public void invoke($ javaQuery, Object... params) { Image image = (Image) params[0]; for (Component v : views) { if (v instanceof JLabel) { ((JLabel) v).setIcon(new ImageIcon(image)); } else if (v instanceof Window) { ((Window) v).setIconImage(image); } else { if (v instanceof Container) { JLabel l = new JLabel(new ImageIcon(image)); l.setBounds(v.getBounds()); ((Container) v).add(l); } else if (v.getParent() != null && v.getParent() instanceof Container) { JLabel l = new JLabel(new ImageIcon(image)); l.setBounds(v.getBounds()); ((Container) v.getParent()).add(l); } } } } }); if (error != null) { options.error(error); } if (width >= 0) { options.imageWidth(width); } if (height >= 0) { options.imageHeight(height); } $.ajax(options); } } return this; }
From source file:self.philbrown.javaQuery.$.java
/** * Animate multiple view properties at the same time. Example: * <pre>/*from w ww . j a v a 2 s.co m*/ * $.with(myView).animate(new QuickMap(QuickEntry.qe("alpha", .8f), QuickEntry.qe("width", 50%)), 400, Easing.LINEAR, null); * </pre> * @param properties mapping of property names and final values to animate * @param options the options for setting the duration, easing, etc of the animation * @return this */ public $ animate(Map<String, Object> properties, final AnimationOptions options) { List<Animator> animations = new ArrayList<Animator>(); for (Entry<String, Object> entry : properties.entrySet()) { final String key = entry.getKey(); //Java sometimes will interpret these Strings as Numbers, so some trickery is needed below Object value = entry.getValue(); for (final Component view : this.views) { Animator anim = null; if (value instanceof String) value = getAnimationValue(view, key, (String) value); //final Object fValue = value; if (value != null) { //special color cases if (key.equals("alpha") || key.equals("red") || key.equals("green") || key.equals("blue")) { if (key.equals("alpha") && view instanceof JComponent) { ((JComponent) view).setOpaque(false); } try { final Method getComponent = Color.class .getMethod(Log.buildString("get", capitalize(key))); final int colorComponent = (Integer) getComponent.invoke(view.getBackground()); final ColorHelper color = new ColorHelper(view.getBackground()); final Method setComponent = ColorHelper.class.getMethod( Log.buildString("set", capitalize(key)), new Class<?>[] { int.class }); anim = new Animator(); //if integer - assume 0-255 if (value instanceof Integer || is(value, int.class)) { anim.addTarget(PropertySetter.getTarget(color, key, colorComponent, Integer.parseInt(value.toString()))); } //if float - assume 0.0-1.0 else if (value instanceof Float || is(value, float.class)) { anim.addTarget(PropertySetter.getTarget(color, key, colorComponent, (int) (255 * Float.parseFloat(value.toString())))); } anim.addTarget(new TimingTargetAdapter() { @Override public void timingEvent(Animator source, double fraction) { double d = source.getInterpolator().interpolate(fraction); try { setComponent.invoke(color, (int) d); view.setBackground(color.getColor()); // if (view instanceof JComponent) // ((JComponent) view).revalidate(); view.repaint(); } catch (Throwable t) { if (options.debug()) t.printStackTrace(); } } }); } catch (Throwable t) { if (options.debug()) t.printStackTrace(); } } else { final Rectangle params = view.getBounds(); try { final Field field = params.getClass().getField(key); if (field != null) { anim = new Animator(); anim.addTarget(PropertySetter.getTarget(params, key, field.get(params), value)); anim.addTarget(new TimingTargetAdapter() { @Override public void timingEvent(Animator source, double fraction) { Rectangle bounds = view.getBounds(); double d = source.getInterpolator().interpolate(fraction); try { field.set(bounds, d); } catch (Throwable t) { if (options.debug()) t.printStackTrace(); } view.setBounds(bounds); view.repaint(); if (options.progress() != null) { options.progress().invoke($.with(view), key, d, source.getDuration() - source.getTotalElapsedTime()); } } }); } } catch (Throwable t) { if (options.debug()) Log.w("$", String.format(Locale.US, "%s is not a LayoutParams attribute.", key)); } if (anim == null) { anim = new Animator(); Object first; try { final Method getter = view.getClass() .getMethod(Log.buildString("get", capitalize(key))); first = getter.invoke(view); } catch (Throwable t) { first = 0; } anim.addTarget(PropertySetter.getTarget(view, key, first, value)); if (options.progress() != null) { anim.addTarget(new TimingTargetAdapter() { @Override public void timingEvent(Animator source, double fraction) { double d = source.getInterpolator().interpolate(fraction); if (options.progress() != null) { options.progress().invoke($.with(view), key, d, source.getDuration() - source.getTotalElapsedTime()); } } }); } } } if (options.repeatCount() >= 1) anim.setRepeatCount(options.repeatCount()); if (options.reverse()) anim.setRepeatBehavior(Animator.RepeatBehavior.REVERSE); animations.add(anim); } } } AnimatorSet animation = animationWithOptions(options, animations); animation.start(); return this; }