| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.view; |
| 16 | |
| 17 | import java.awt.Color; |
| 18 | import java.awt.Font; |
| 19 | import java.awt.Graphics2D; |
| 20 | import java.awt.Rectangle; |
| 21 | import java.awt.image.BufferedImage; |
| 22 | |
| 23 | import org.jdesktop.animation.timing.Animator; |
| 24 | import org.jdesktop.animation.timing.Animator.RepeatBehavior; |
| 25 | import org.jdesktop.animation.timing.interpolation.PropertySetter; |
| 26 | import org.jdesktop.animation.timing.interpolation.SplineInterpolator; |
| 27 | |
| 28 | public class ScrollingMattePainter extends AbstractMattePainter |
| 29 | { |
| 30 | @Override public Animator createAnimator(FullScreenPrefs prefs, String content) |
| 31 | { |
| 32 | // Initialize some drawing properties |
| 33 | _foreground = prefs.getForeground(); |
| 34 | _background = prefs.getBackground(); |
| 35 | _font = prefs.getFont(); |
| 36 | |
| 37 | // Calculate limit coordinates of matte |
| 38 | int width = getPane().getWidth(); |
| 39 | _geometry = new MatteGeometry(getPane(), prefs, content); |
| 40 | Rectangle matteRect = _geometry.getMatteRectangle(); |
| 41 | _repaint = new Rectangle(matteRect); |
| 42 | _repaint.x = 0; |
| 43 | _repaint.width = width; |
| 44 | |
| 45 | // Create buffered image with just the text |
| 46 | _content = new BufferedImage( |
| 47 | matteRect.width, matteRect.height, BufferedImage.TYPE_INT_ARGB); |
| 48 | Graphics2D g = _content.createGraphics(); |
| 49 | g.setColor(_foreground); |
| 50 | g.setFont(_font); |
| 51 | g.drawString( |
| 52 | content, _geometry.getMatteTextOrigin().x, _geometry.getMatteTextOrigin().y); |
| 53 | g.dispose(); |
| 54 | |
| 55 | // Setup animator |
| 56 | Animator anim = PropertySetter.createAnimator( prefs.getScrollTime(), |
| 57 | this, |
| 58 | "matteX", |
| 59 | width, |
| 60 | -matteRect.width); |
| 61 | anim.setRepeatBehavior(RepeatBehavior.LOOP); |
| 62 | double repeat = prefs.getDisplayTime() / (double) prefs.getScrollTime(); |
| 63 | anim.setRepeatCount(repeat); |
| 64 | anim.setResolution(TIMER_RESOLUTION); |
| 65 | anim.setInterpolator(new SplineInterpolator(CONTROL_POINT_1_X, |
| 66 | CONTROL_POINT_1_Y, |
| 67 | CONTROL_POINT_2_X, |
| 68 | CONTROL_POINT_2_Y)); |
| 69 | |
| 70 | return anim; |
| 71 | } |
| 72 | |
| 73 | public void setMatteX(int x) |
| 74 | { |
| 75 | _matteX = x; |
| 76 | getPane().repaint(_repaint); |
| 77 | } |
| 78 | |
| 79 | @Override public void paint(Graphics2D g) |
| 80 | { |
| 81 | g.setColor(_background); |
| 82 | // Paint the backgrounf first |
| 83 | g.fill(_repaint); |
| 84 | // Paint the picture with the text content |
| 85 | g.drawImage(_content, _matteX, _geometry.getMattePaintOrigin(), null); |
| 86 | } |
| 87 | |
| 88 | static private final int TIMER_RESOLUTION = 80; |
| 89 | static private final float CONTROL_POINT_1_X = 0.2f; |
| 90 | static private final float CONTROL_POINT_1_Y = 0; |
| 91 | static private final float CONTROL_POINT_2_X = 0.8f; |
| 92 | static private final float CONTROL_POINT_2_Y = 1; |
| 93 | |
| 94 | private MatteGeometry _geometry; |
| 95 | private BufferedImage _content = null; |
| 96 | private Rectangle _repaint; |
| 97 | private int _matteX; |
| 98 | |
| 99 | private Color _foreground; |
| 100 | private Color _background; |
| 101 | private Font _font; |
| 102 | } |