import java.beans.*; |
import java.awt.*; |
import java.awt.event.*; |
import java.awt.image.*; |
import javax.swing.*; |
import javax.swing.plaf.LayerUI; |
import java.awt.geom.Point2D; |
class FirstLayerUI extends LayerUI<JComponent> |
{ |
public void paint(Graphics g, JComponent c) |
{ |
super .paint(g, c); |
Graphics2D g2 = (Graphics2D) g.create(); |
g2.setComposite(AlphaComposite.getInstance( |
AlphaComposite.SRC_OVER, .5f)); |
g2.setPaint( new GradientPaint( 0 , 0 , Color.RED |
, 0 , c.getHeight() , Color.BLUE)); |
g2.fillRect( 0 , 0 , c.getWidth(), c.getHeight()); |
g2.dispose(); |
} |
} |
class BlurLayerUI extends LayerUI<JComponent> |
{ |
private BufferedImage screenBlurImage; |
private BufferedImageOp operation; |
public BlurLayerUI() |
{ |
float ninth = 1 .0f / 9 .0f; |
float [] blurKernel = { |
ninth, ninth, ninth, |
ninth, ninth, ninth, |
ninth, ninth, ninth |
}; |
operation = new ConvolveOp( |
new Kernel( 3 , 3 , blurKernel), |
ConvolveOp.EDGE_NO_OP, null ); |
} |
public void paint(Graphics g, JComponent c) |
{ |
int w = c.getWidth(); |
int h = c.getHeight(); |
if (w == 0 || h == 0 ) |
return ; |
if (screenBlurImage == null |
|| screenBlurImage.getWidth() != w |
|| screenBlurImage.getHeight() != h) |
{ |
screenBlurImage = new BufferedImage(w |
, h , BufferedImage.TYPE_INT_RGB); |
} |
Graphics2D ig2 = screenBlurImage.createGraphics(); |
ig2.setClip(g.getClip()); |
super .paint(ig2, c); |
ig2.dispose(); |
Graphics2D g2 = (Graphics2D)g; |
g2.drawImage(screenBlurImage, operation, 0 , 0 ); |
} |
} |
class SpotlightLayerUI extends LayerUI<JComponent> |
{ |
private boolean active; |
private int cx, cy; |
public void installUI(JComponent c) |
{ |
super .installUI(c); |
JLayer layer = (JLayer)c; |
layer.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK |
| AWTEvent.MOUSE_MOTION_EVENT_MASK); |
} |
public void uninstallUI(JComponent c) |
{ |
JLayer layer = (JLayer)c; |
layer.setLayerEventMask( 0 ); |
super .uninstallUI(c); |
} |
public void paint(Graphics g, JComponent c) |
{ |
Graphics2D g2 = (Graphics2D)g.create(); |
super .paint (g2, c); |
if (active) |
{ |
Point2D center = new Point2D.Float(cx, cy); |
float radius = 72 ; |
float [] dist = { 0 .0f, 1 .0f}; |
Color[] colors = {Color.YELLOW , Color.BLACK}; |
RadialGradientPaint p = new RadialGradientPaint(center |
, radius , dist , colors); |
g2.setPaint(p); |
g2.setComposite(AlphaComposite.getInstance( |
AlphaComposite.SRC_OVER, .6f)); |
g2.fillRect( 0 , 0 , c.getWidth(), c.getHeight()); |
} |
g2.dispose(); |
} |
public void processMouseEvent(MouseEvent e, JLayer layer) |
{ |
if (e.getID() == MouseEvent.MOUSE_ENTERED) |
active = true ; |
if (e.getID() == MouseEvent.MOUSE_EXITED) |
active = false ; |
layer.repaint(); |
} |
public void processMouseMotionEvent(MouseEvent e, JLayer layer) |
{ |
Point p = SwingUtilities.convertPoint( |
e.getComponent(), e.getPoint(), layer); |
cx = p.x; |
cy = p.y; |
layer.repaint(); |
} |
} |
public class JLayerTest |
{ |
public void init() |
{ |
JFrame f = new JFrame( "JLayer测试" ); |
JPanel p = new JPanel(); |
ButtonGroup group = new ButtonGroup(); |
JRadioButton radioButton; |
p.add(radioButton = new JRadioButton( "网购购买" , true )); |
group.add(radioButton); |
p.add(radioButton = new JRadioButton( "书店购买" )); |
group.add(radioButton); |
p.add(radioButton = new JRadioButton( "图书馆借阅" )); |
group.add(radioButton); |
p.add( new JCheckBox( "疯狂Java讲义" )); |
p.add( new JCheckBox( "疯狂Android讲义" )); |
p.add( new JCheckBox( "疯狂Ajax讲义" )); |
p.add( new JCheckBox( "轻量级Java EE企业应用" )); |
JButton orderButton = new JButton( "投票" ); |
p.add(orderButton); |
LayerUI<JComponent> layerUI = new SpotlightLayerUI(); |
JLayer<JComponent> layer = new JLayer<JComponent>(p, layerUI); |
f.add(layer); |
f.setSize( 300 , 170 ); |
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); |
f.setVisible ( true ); |
} |
public static void main(String[] args) |
{ |
new JLayerTest().init(); |
} |
} |
by: 发表于:2018-01-04 11:23:21 顶(0) | 踩(0) 回复
??
回复评论