Source code file content
onyx / src / org / pushingpixels / onyx / content / Stage0Base.java
Size: 5255 bytes, 1 line
/*
* Copyright (c) 2009 Onyx Kirill Grouchnikov. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of Onyx Kirill Grouchnikov nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.pushingpixels.onyx.content;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import org.pushingpixels.trident.Timeline;
import org.pushingpixels.trident.callback.Repaint;
/**
* The basis of the album scroller container. Provides the functionality of:
*
* <ul>
* <li>Fading in once it becomes a part of the host window hierarchy</li>
* <li>Dragging the host window with the mouse</li>
* <li>Painting a rounded translucent background</li>
* </ul>
*
* @author Kirill Grouchnikov
*/
public class Stage0Base extends JComponent {
/**
* The alpha value for this container. Is updated in the fade-in timeline
* which starts when this container becomes a part of the host window
* hierarchy.
*/
float alpha;
/**
* Creates the basic container.
*/
public Stage0Base() {
this.setOpaque(false);
this.alpha = 0.0f;
// mouse listener for dragging the host window
MouseAdapter adapter = new MouseAdapter() {
int lastX;
int lastY;
@Override
public void mousePressed(MouseEvent e) {
Component source = (Component) e.getSource();
Point eventLocationOnScreen = e.getLocationOnScreen();
if (eventLocationOnScreen == null) {
eventLocationOnScreen = new Point(e.getX()
+ source.getLocationOnScreen().x, e.getY()
+ source.getLocationOnScreen().y);
}
lastX = eventLocationOnScreen.x;
lastY = eventLocationOnScreen.y;
}
@Override
public void mouseDragged(MouseEvent e) {
Component source = (Component) e.getSource();
Point eventLocationOnScreen = e.getLocationOnScreen();
if (eventLocationOnScreen == null) {
eventLocationOnScreen = new Point(e.getX()
+ source.getLocationOnScreen().x, e.getY()
+ source.getLocationOnScreen().y);
}
int dx = eventLocationOnScreen.x - lastX;
int dy = eventLocationOnScreen.y - lastY;
Window win = SwingUtilities.getWindowAncestor(Stage0Base.this);
Point loc = win.getLocation();
win.setLocation(loc.x + dx, loc.y + dy);
lastX = eventLocationOnScreen.x;
lastY = eventLocationOnScreen.y;
}
};
this.addMouseListener(adapter);
this.addMouseMotionListener(adapter);
// fade in the container once it's part of the window
// hierarchy
this.addHierarchyListener(new HierarchyListener() {
@Override
public void hierarchyChanged(HierarchyEvent e) {
Timeline shownTimeline = new Timeline(Stage0Base.this);
shownTimeline.addPropertyToInterpolate("alpha", 0.0f, 0.75f);
shownTimeline.addCallback(new Repaint(Stage0Base.this));
shownTimeline.setDuration(500);
shownTimeline.play();
}
});
}
/**
* Sets the alpha value. Used by the fade-in timeline.
*
* @param alpha
* Alpha value for this container.
*/
public void setAlpha(float alpha) {
this.alpha = alpha;
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setStroke(new BasicStroke(1.0f));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int radius = 32;
Shape contour = new RoundRectangle2D.Double(0, 0, getWidth() - 1,
getHeight() - 1, radius, radius);
g2d.setComposite(AlphaComposite.SrcOver.derive(this.alpha));
g2d.setColor(new Color(0, 0, 0));
g2d.fill(contour);
g2d.setColor(Color.gray);
g2d.draw(contour);
g2d.dispose();
}
}






