001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test.check;
031:
032: import java.awt.*;
033: import java.text.SimpleDateFormat;
034: import java.util.Date;
035:
036: import javax.swing.JPanel;
037:
038: import org.jvnet.lafwidget.layout.TransitionLayout;
039: import org.jvnet.substance.SubstanceLookAndFeel;
040: import org.jvnet.substance.utils.SubstanceCoreUtilities;
041:
042: /**
043: * A simple panel that paints the current time on itself.
044: *
045: * @author Kirill Grouchnikov
046: */
047: public class NumberedPanel extends JPanel {
048: /**
049: * Panel that paints the current time and has a custom colored background.
050: *
051: * @author Kirill Grouchnikov
052: */
053: protected static class ColoredNumberedPanel extends JPanel {
054: /**
055: * Panel ID number.
056: */
057: private int number;
058:
059: /**
060: * Creates a new panel.
061: *
062: * @param number
063: * Panel ID number.
064: */
065: public ColoredNumberedPanel(int number) {
066: this .number = number;
067: this .setBackground(new Color(255 - (int) (50.0 * Math
068: .random()), 255 - (int) (50.0 * Math.random()),
069: 255 - (int) (50.0 * Math.random())));
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
076: */
077: @Override
078: protected void paintComponent(Graphics g) {
079: int w = this .getWidth();
080: int h = this .getHeight();
081: Graphics2D graphics = (Graphics2D) g.create();
082: graphics.setComposite(TransitionLayout
083: .getAlphaComposite(this ));
084:
085: if (SubstanceCoreUtilities.isThemeDark(SubstanceLookAndFeel
086: .getTheme()))
087: graphics.setColor(Color.black);
088: else
089: graphics.setColor(Color.white);
090: graphics.fillRect(0, 0, w, h);
091: graphics.setComposite(TransitionLayout.getAlphaComposite(
092: this , 0.6f));
093: graphics.setColor(this .getBackground());
094: graphics.fillRect(0, 0, w, h);
095: graphics.setComposite(TransitionLayout
096: .getAlphaComposite(this ));
097: if (SubstanceCoreUtilities.isThemeDark(SubstanceLookAndFeel
098: .getTheme()))
099: graphics.setColor(Color.white);
100: else
101: graphics.setColor(Color.black);
102: int size = Math.min(60, Math.min(w, h) / 2);
103: graphics.setFont(new Font("Arial", Font.BOLD, size));
104: graphics.drawString("" + number, (w - size) / 2,
105: (h / 2 + size) / 2);
106:
107: SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
108: String currDate = sdf.format(new Date());
109:
110: int size2 = Math.min(50, Math.min(w, h) / 3);
111: graphics.setFont(new Font("Arial", Font.BOLD, size2));
112: int timeLen = graphics.getFontMetrics().stringWidth(
113: currDate);
114: graphics.drawString(currDate, (w - timeLen) / 2,
115: (h + size2) / 2);
116:
117: graphics.dispose();
118: }
119: }
120:
121: /**
122: * Creates a new panel.
123: *
124: * @param number
125: * Panel ID number.
126: */
127: public NumberedPanel(int number) {
128: this .setLayout(new BorderLayout());
129: this .add(new ColoredNumberedPanel(number));
130: }
131: }
|