/** * Sets new point shape. Bound property. * @param pointShape new Shape. */ public final void setPointShape(Shape pointShape) { Shape old = getPointShape(); this.pointShape = pointShape; firePropertyChange("pointShape", old, getPointShape()); }
/** * Sets new trajectory. Expected trajectory have to be closed shape. Bound property. * @param trajectory New trajectory. */ public final void setTrajectory(Shape trajectory) { Shape old = getTrajectory(); this.trajectory = trajectory; firePropertyChange("trajectory", old, getTrajectory()); }
/** * Sets new spinning direction. * @param dir Spinning direction. */ public void setDirection(Direction dir) { Direction old = getDirection(); this.direction = dir; firePropertyChange("direction", old, getDirection()); } }
/** * Creates new painter initialized to the shape of circle and bounds of square of specified height. * @param height Painter height. */ public BusyPainter(int height) { this(getScaledDefaultPoint(height), getScaledDefaultTrajectory(height)); }
/**Sets current frame. * @param frame Current frame. */ public void setFrame(int frame) { int old = getFrame(); this.frame = frame; firePropertyChange("frame", old, getFrame()); }
@Test @Override public void testDefaults() { super.testDefaults(); BusyPainter bp = (BusyPainter) p; assertThat(bp.getBaseColor(), is(Color.LIGHT_GRAY)); assertThat(bp.getDirection(), is(BusyPainter.Direction.RIGHT)); assertThat(bp.getFrame(), is(-1)); assertThat(bp.getHighlightColor(), is(Color.BLACK)); assertThat(bp.getPoints(), is(8)); assertThat(bp.getPointShape(), is(BusyPainter.getScaledDefaultPoint(26))); assertThat(bp.getTrailLength(), is(4)); assertThat(bp.getTrajectory(), is(BusyPainter.getScaledDefaultTrajectory(26))); assertThat(bp.isPaintCentered(), is(false)); } }
Rectangle r = getTrajectory().getBounds(); int tw = width - r.width - 2*r.x; int th = height - r.height - 2*r.y; if (isPaintCentered()) { g.translate(tw/2, th/2); float c = calcLine(coords, cp); totalDist += c; float c = calcCube(coords, cp); totalDist += c; segStack.add(new float[] { c, coords[0], coords[1], coords[2], float c = calcLengthOfQuad(coords, cp); totalDist += c; segStack.add(new float[] { c, coords[0], coords[1], 0 ,0 , coords[2], float nxtP = totalDist / getPoints(); List<Point2D.Float> pList = new ArrayList<Point2D.Float>(); pList.add(new Float(sp.x, sp.y)); float travDist = nxtP; Float center = new Float(sp.x, sp.y); for (int i = 1; i < getPoints(); i++) { while (len < nxtP) { sgIdx++; Float p = calcPoint(travDist, sp, sgmt, width, height); pList.add(p);
/** * Selects appropriate color for given frame based on the frame position and gradient difference. * @param i Frame. * @return Frame color. */ private Color calcFrameColor(final int i) { if (frame == -1) { return getBaseColor(); } for (int t = 0; t < getTrailLength(); t++) { if (direction == Direction.RIGHT && i == (frame - t + getPoints()) % getPoints()) { float terp = 1 - ((float) (getTrailLength() - t)) / (float) getTrailLength(); return PaintUtils.interpolate(getBaseColor(), getHighlightColor(), terp); } else if (direction == Direction.LEFT && i == (frame + t) % getPoints()) { float terp = ((float) (t)) / (float) getTrailLength(); return PaintUtils.interpolate(getBaseColor(), getHighlightColor(), terp); } } return getBaseColor(); }
/** * Returns the current BusyPainter. If no BusyPainter is currently * set on this BusyLabel, the {@link #createBusyPainter(Dimension)} * method is called to create one. Afterwards, * {@link #initPainter(Dimension)} is called to update the BusyLabel * with the created BusyPainter. * * @return the busyPainter * * @see #createBusyPainter(Dimension) * @see #initPainter(Dimension) */ public final BusyPainter getBusyPainter() { if (null == busyPainter) { Dimension prefSize = getPreferredSize(); busyPainter = createBusyPainter((prefSize.width == 0 && prefSize.height == 0 && !isPreferredSizeSet()) ? null : prefSize); if (null != busyPainter) { if (!isPreferredSizeSet() && (null == prefSize || prefSize.width == 0 || prefSize.height == 0)) { Rectangle rt = busyPainter.getTrajectory().getBounds(); Rectangle rp = busyPainter.getPointShape().getBounds(); int max = Math.max(rp.width, rp.height); prefSize = new Dimension(rt.width + max, rt.height + max); } initPainter(prefSize); } } return busyPainter; }
/** * Sets total amount of points in spinner. Bound property. * @param points Total amount of points. */ public void setPoints(int points) { int old = getPoints(); this.points = points; firePropertyChange("points", old, getPoints()); }
/** * Initializes painter to the specified trajectory and and point shape. Bounds are dynamically calculated to so the specified trajectory fits in. * @param point Point shape. * @param trajectory Trajectory shape. */ public BusyPainter(Shape point, Shape trajectory) { init(point, trajectory, Color.LIGHT_GRAY, Color.BLACK); }
private void startAnimation() { if(busy != null) { stopAnimation(); } busy = new Timer(delay, new ActionListener() { BusyPainter busyPainter = getBusyPainter(); int frame = busyPainter.getPoints(); public void actionPerformed(ActionEvent e) { frame = (frame+1)%busyPainter.getPoints(); busyPainter.setFrame(direction == Direction.LEFT ? busyPainter.getPoints() - frame : frame); frameChanged(); } }); busy.start(); }
/** * Sets new base color. Bound property. * @param baseColor Base color. */ public void setBaseColor(Color baseColor) { Color old = getBaseColor(); this.baseColor = baseColor; firePropertyChange("baseColor", old, getBaseColor()); }
/** * Sets new highlight color. Bound property. * @param highlightColor New highlight color. */ public void setHighlightColor(Color highlightColor) { Color old = getHighlightColor(); this.highlightColor = highlightColor; firePropertyChange("highlightColor", old, getHighlightColor()); }
@Override public void tick() { currentFrame = (currentFrame + 1) % POINTS; busyPainter.setFrame(currentFrame); } }
private void paintRotatedCenteredShapeAtPoint(Float p, Float c, Graphics2D g) { Shape s = getPointShape(); double hh = s.getBounds().getHeight() / 2; double wh = s.getBounds().getWidth() / 2; double t, x, y; double a = c.y - p.y; double b = p.x - c.x; double sa = Math.signum(a); double sb = Math.signum(b); sa = sa == 0 ? 1 : sa; sb = sb == 0 ? 1 : sb; a = Math.abs(a); b = Math.abs(b); t = Math.atan(a / b); t = sa > 0 ? sb > 0 ? -t : -Math.PI + t : sb > 0 ? t : Math.PI - t; x = Math.sqrt(a * a + b * b) - wh; y = -hh; g.rotate(t); g.translate(x, y); g.fill(s); g.translate(-x, -y); g.rotate(-t); }
Rectangle r = getTrajectory().getBounds(); int tw = width - r.width - 2*r.x; int th = height - r.height - 2*r.y; if (isPaintCentered()) { g.translate(tw/2, th/2); float c = calcLine(coords, cp); totalDist += c; float c = calcCube(coords, cp); totalDist += c; segStack.add(new float[] { c, coords[0], coords[1], coords[2], float c = calcLengthOfQuad(coords, cp); totalDist += c; segStack.add(new float[] { c, coords[0], coords[1], 0 ,0 , coords[2], float nxtP = totalDist / getPoints(); List<Point2D.Float> pList = new ArrayList<Point2D.Float>(); pList.add(new Float(sp.x, sp.y)); float travDist = nxtP; Float center = new Float(sp.x, sp.y); for (int i = 1; i < getPoints(); i++) { while (len < nxtP) { sgIdx++; Float p = calcPoint(travDist, sp, sgmt, width, height); pList.add(p);
/** * Selects appropriate color for given frame based on the frame position and gradient difference. * @param i Frame. * @return Frame color. */ private Color calcFrameColor(final int i) { if (frame == -1) { return getBaseColor(); } for (int t = 0; t < getTrailLength(); t++) { if (direction == Direction.RIGHT && i == (frame - t + getPoints()) % getPoints()) { float terp = 1 - ((float) (getTrailLength() - t)) / (float) getTrailLength(); return PaintUtils.interpolate(getBaseColor(), getHighlightColor(), terp); } else if (direction == Direction.LEFT && i == (frame + t) % getPoints()) { float terp = ((float) (t)) / (float) getTrailLength(); return PaintUtils.interpolate(getBaseColor(), getHighlightColor(), terp); } } return getBaseColor(); }
/** * Returns the current BusyPainter. If no BusyPainter is currently * set on this BusyLabel, the {@link #createBusyPainter(Dimension)} * method is called to create one. Afterwards, * {@link #initPainter(Dimension)} is called to update the BusyLabel * with the created BusyPainter. * * @return the busyPainter * * @see #createBusyPainter(Dimension) * @see #initPainter(Dimension) */ public final BusyPainter getBusyPainter() { if (null == busyPainter) { Dimension prefSize = getPreferredSize(); busyPainter = createBusyPainter((prefSize.width == 0 && prefSize.height == 0 && !isPreferredSizeSet()) ? null : prefSize); if (null != busyPainter) { if (!isPreferredSizeSet() && (null == prefSize || prefSize.width == 0 || prefSize.height == 0)) { Rectangle rt = busyPainter.getTrajectory().getBounds(); Rectangle rp = busyPainter.getPointShape().getBounds(); int max = Math.max(rp.width, rp.height); prefSize = new Dimension(rt.width + max, rt.height + max); } initPainter(prefSize); } } return busyPainter; }