| 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 | |
| 22 | import org.jdesktop.animation.timing.Animator; |
| 23 | import org.jdesktop.animation.timing.Animator.RepeatBehavior; |
| 24 | import org.jdesktop.animation.timing.interpolation.KeyFrames; |
| 25 | import org.jdesktop.animation.timing.interpolation.KeyTimes; |
| 26 | import org.jdesktop.animation.timing.interpolation.KeyValues; |
| 27 | import org.jdesktop.animation.timing.interpolation.PropertySetter; |
| 28 | |
| 29 | public class FixedMattePainter extends AbstractMattePainter |
| 30 | { |
| 31 | @Override public Animator createAnimator(FullScreenPrefs prefs, String content) |
| 32 | { |
| 33 | _content = content; |
| 34 | |
| 35 | // Initialize some drawing properties |
| 36 | _font = prefs.getFont(); |
| 37 | |
| 38 | // Calculate limit coordinates of matte |
| 39 | _geometry = new MatteGeometry(getPane(), prefs, content); |
| 40 | _repaint = new Rectangle(_geometry.getMatteRectangle()); |
| 41 | _repaint.x = (getPane().getWidth() - _repaint.width) / 2; |
| 42 | |
| 43 | // Setup animator |
| 44 | Animator anim = new Animator(prefs.getDisplayTime() / 2); |
| 45 | anim.setRepeatBehavior(RepeatBehavior.REVERSE); |
| 46 | anim.setRepeatCount(2.0); |
| 47 | anim.setResolution(TIMER_RESOLUTION); |
| 48 | |
| 49 | PropertySetter timer; |
| 50 | KeyValues values; |
| 51 | KeyTimes times; |
| 52 | |
| 53 | values = KeyValues.create( makeTransparent(prefs.getForeground()), |
| 54 | prefs.getForeground(), |
| 55 | prefs.getForeground()); |
| 56 | times = new KeyTimes(0, ALPHA_RAMP, 1); |
| 57 | timer = new PropertySetter( this, |
| 58 | "foreground", |
| 59 | new KeyFrames(values, times)); |
| 60 | anim.addTarget(timer); |
| 61 | |
| 62 | values = KeyValues.create( makeTransparent(prefs.getBackground()), |
| 63 | prefs.getBackground(), |
| 64 | prefs.getBackground()); |
| 65 | timer = new PropertySetter( this, |
| 66 | "background", |
| 67 | new KeyFrames(values, times)); |
| 68 | anim.addTarget(timer); |
| 69 | |
| 70 | return anim; |
| 71 | } |
| 72 | |
| 73 | static private Color makeTransparent(Color source) |
| 74 | { |
| 75 | return new Color(source.getRed(), source.getGreen(), source.getBlue(), 0); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | public void setBackground(Color background) |
| 80 | { |
| 81 | _background = background; |
| 82 | getPane().repaint(_repaint); |
| 83 | } |
| 84 | |
| 85 | public void setForeground(Color foreground) |
| 86 | { |
| 87 | _foreground = foreground; |
| 88 | getPane().repaint(_repaint); |
| 89 | } |
| 90 | |
| 91 | @Override public void paint(Graphics2D g) |
| 92 | { |
| 93 | // Put the background first |
| 94 | g.setColor(_background); |
| 95 | g.fill(_repaint); |
| 96 | |
| 97 | // Then put the string |
| 98 | g.setColor(_foreground); |
| 99 | g.setFont(_font); |
| 100 | g.drawString( _content, |
| 101 | _repaint.x + _geometry.getMatteTextOrigin().x, |
| 102 | _geometry.getMatteTextOrigin().y); |
| 103 | } |
| 104 | |
| 105 | static private final int TIMER_RESOLUTION = 20; |
| 106 | static private final float ALPHA_RAMP = 0.33f; |
| 107 | |
| 108 | private MatteGeometry _geometry; |
| 109 | private Rectangle _repaint; |
| 110 | private String _content = null; |
| 111 | |
| 112 | private Color _foreground; |
| 113 | private Color _background; |
| 114 | private Font _font; |
| 115 | } |