1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| public class FadePanel extends JComponent {
private float trans = 0.0f;
private int h = 0;
private int w = 0;
public FadePanel() {
setBackground(new Color(238, 238, 238));
}
public FadePanel(int w, int h) {
this.h = h;
this.w = w;
setBackground(new Color(238, 238, 238));
}
public void fadeIn() {
try {
float f = 1.0f;
while (f > 0.1f) {
trans = f;
f -= 0.2f;
repaint();
Thread.sleep(20);
}
trans = 0.0f;
repaint();
} catch (InterruptedException ex) {
Logger.getLogger(FadePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void fadeOut() {
try {
float f = 0.0f;
while (f < 0.9f) {
trans = f;
f += 0.2f;
repaint();
Thread.sleep(20);
}
trans = 1.0f;
repaint();
} catch (InterruptedException ex) {
Logger.getLogger(FadePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle clip = g.getClipBounds();
// sets a 65% translucent composite
AlphaComposite alpha = AlphaComposite.SrcOver.derive(trans);
g2.setComposite(alpha);
// fills the background
g2.setColor(getBackground());
g2.fillRect(clip.x + w, clip.y + h, clip.width - w, clip.height - h);
}
} |
Partager