001 /**
002 * Copyright (c) 2009, Piet Blok
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions
007 * are met:
008 *
009 * * Redistributions of source code must retain the above copyright
010 * notice, this list of conditions and the following disclaimer.
011 * * Redistributions in binary form must reproduce the above
012 * copyright notice, this list of conditions and the following
013 * disclaimer in the documentation and/or other materials provided
014 * with the distribution.
015 * * Neither the name of the copyright holder nor the names of the
016 * contributors may be used to endorse or promote products derived
017 * from this software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
020 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
021 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
022 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
023 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
024 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
025 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
026 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
027 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
029 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030 */
031
032 package org.pbjar.jxlayer.demo;
033
034 import java.awt.BorderLayout;
035 import java.awt.Color;
036 import java.awt.Font;
037 import java.awt.Graphics;
038 import java.awt.Graphics2D;
039 import java.awt.GridLayout;
040 import java.awt.Rectangle;
041 import java.awt.Shape;
042 import java.awt.event.MouseAdapter;
043 import java.awt.event.MouseEvent;
044 import java.awt.font.GlyphVector;
045 import java.awt.geom.AffineTransform;
046 import java.awt.geom.Area;
047 import java.awt.geom.NoninvertibleTransformException;
048 import java.awt.geom.Rectangle2D;
049
050 import javax.swing.BorderFactory;
051 import javax.swing.JComponent;
052 import javax.swing.JDialog;
053 import javax.swing.JFrame;
054 import javax.swing.JPanel;
055 import javax.swing.JScrollPane;
056 import javax.swing.SwingUtilities;
057 import javax.swing.border.BevelBorder;
058
059 import org.jdesktop.jxlayer.JXLayer;
060 import org.jdesktop.swingx.JXBusyLabel;
061 import org.jdesktop.swingx.JXFrame;
062 import org.jdesktop.swingx.JXPanel;
063 import org.jdesktop.swingx.painter.CheckerboardPainter;
064
065 /**
066 * A test application specifically designed to test the cooperation of
067 * RepaintManagers.
068 * <p>
069 * Run a web start demo: <a
070 * href="http://www.pbjar.org/blogs/jxlayer/version_2/SwingXDemo.jnlp"> <IMG
071 * style="CLEAR: right" alt="Web Start Shared JXLayer"
072 * src="http://javadesktop.org/javanet_images/webstart.small2.gif"
073 * align="middle" border="1" /> </a>
074 * </p>
075 *
076 * @author Piet Blok
077 */
078 public class TestSwingX implements TestGUI {
079
080 /**
081 * Run the demo.
082 *
083 * @param args
084 * not used
085 */
086 public static void main(String[] args) {
087 SwingUtilities.invokeLater(new Runnable() {
088
089 @Override
090 public void run() {
091 new TestSwingX().createGUI();
092 }
093 });
094 }
095
096 private final JXFrame frame = new JXFrame(
097 "SwingX and SimpleBufferedLayerUI");
098
099 private static final float alphaHigh = 0.99f, alphaLow = 0.4f;
100
101 private final JScrollPane scroller = new JScrollPane();
102
103 private JXLayer<?> layer;
104
105 @Override
106 public void repaintGUI(boolean force) {
107 if (force) {
108 frame.pack();
109 frame.setLocationRelativeTo(null);
110 frame.setVisible(true);
111 }
112 }
113
114 @Override
115 public void setScroller(boolean scrollPane) {
116 if (scrollPane) {
117 frame.getContentPane().remove(layer);
118 this.scroller.setViewportView(layer);
119 frame.getContentPane().add(this.scroller, BorderLayout.CENTER);
120 } else {
121 frame.getContentPane().remove(scroller);
122 frame.getContentPane().add(layer, BorderLayout.CENTER);
123 }
124 layer.revalidate();
125 layer.repaint();
126 repaintGUI(false);
127
128 }
129
130 private JComponent createContent() {
131 final JPanel content = new JPanel(new BorderLayout());
132 JXPanel xPanel = new JXPanel(new GridLayout(3, 0)) {
133
134 private static final long serialVersionUID = 1L;
135
136 private Shape shape = null;
137
138 private Font myFont = null;
139
140 private void createShape(Graphics2D g2, String message) {
141 Rectangle inner = new Rectangle();
142 SwingUtilities.calculateInnerArea(this, inner);
143
144 myFont = g2.getFont().deriveFont(Font.BOLD);
145
146 GlyphVector myGlyph = myFont.createGlyphVector(g2
147 .getFontRenderContext(), message);
148 Rectangle2D glyphRect = myGlyph.getVisualBounds();
149
150 Area area = new Area(myGlyph.getOutline((float) (inner
151 .getCenterX() - glyphRect.getCenterX()), (float) (inner
152 .getCenterY() - glyphRect.getCenterY())));
153
154 Rectangle2D areaBounds = area.getBounds2D();
155
156 AffineTransform rotator = new AffineTransform();
157 rotator.rotate(Math.PI / -4.0, areaBounds.getCenterX(),
158 areaBounds.getCenterY());
159 area.transform(rotator);
160 Rectangle2D newBounds = area.getBounds2D();
161 double scale = Math.min(
162 inner.getWidth() / newBounds.getWidth(), inner
163 .getHeight()
164 / newBounds.getHeight());
165 try {
166 area.transform(rotator.createInverse());
167 } catch (NoninvertibleTransformException e) {
168 e.printStackTrace();
169 }
170 AffineTransform scaler = new AffineTransform();
171 scaler.translate(areaBounds.getCenterX(), areaBounds
172 .getCenterY());
173 scaler.scale(scale, scale);
174 scaler.translate(-areaBounds.getCenterX(), -areaBounds
175 .getCenterY());
176 area.transform(scaler);
177 area.transform(rotator);
178 shape = area;
179 }
180
181 @Override
182 protected void paintComponent(Graphics g) {
183 super.paintComponent(g);
184 Graphics2D g2 = (Graphics2D) g.create();
185 if (shape == null) {
186 createShape(g2, "JXPanel");
187 }
188 g2.setFont(myFont);
189 g2.setColor(Color.CYAN);
190 g2.fill(shape);
191 g2.setColor(Color.YELLOW);
192 g2.draw(shape);
193 }
194
195 };
196 // xPanel.setOpaque(true);
197 content.add(xPanel, BorderLayout.CENTER);
198 for (int index = 0; index < 3 * 3; index++) {
199 final JXPanel item = new JXPanel(new BorderLayout());
200 item.setBorder(BorderFactory.createCompoundBorder(BorderFactory
201 .createBevelBorder(BevelBorder.RAISED), BorderFactory
202 .createBevelBorder(BevelBorder.LOWERED)));
203 xPanel.add(item, BorderLayout.CENTER);
204 JXBusyLabel busyLabel = new JXBusyLabel();
205 item.add(busyLabel);
206 busyLabel.setBusy(true);
207 busyLabel.getBusyPainter().setHighlightColor(Color.MAGENTA);
208 item.setAlpha(alphaLow);
209 MouseAdapter adapter = new MouseAdapter() {
210
211 @Override
212 public void mouseEntered(MouseEvent e) {
213 item.setAlpha(alphaHigh);
214 }
215
216 @Override
217 public void mouseExited(MouseEvent e) {
218 item.setAlpha(alphaLow);
219 }
220
221 };
222 item.addMouseListener(adapter);
223 }
224 xPanel.setBackgroundPainter(new CheckerboardPainter(Color.CYAN,
225 Color.YELLOW, xPanel.getPreferredSize().width / 2));
226 return content;
227 }
228
229 private void createGUI() {
230 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
231 frame.add(scroller);
232 layer = TransformUtils
233 .createTransformJXLayer(createContent(), 4.0,
234 new QualityHints());
235 scroller.setViewportView(layer);
236 JDialog controlDialog = new ControlDialog(layer, this);
237 controlDialog.pack();
238 controlDialog.setVisible(true);
239 repaintGUI(true);
240 }
241 }