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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.io.*;
import java.util.*;
public class Transparence3 extends JFrame implements ChangeListener {
private JSlider slider;
private MonContainer container;
public Transparence3 () {
super("Transparence");
slider = new JSlider(JSlider.HORIZONTAL,0,100,100);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.addChangeListener(this);
container = new MonContainer(this.getLayeredPane());
this.setContentPane(container);
this.pack();
this.setResizable(false);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.show();
}
public void stateChanged(ChangeEvent e)
{
}
public static void main (String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {}
new Transparence3();
}
}
class Dessin extends JPanel {
private float transparency = 1.0f;
private int x1 = 0;
private int x2 = 0;
private int y1 = 0;
private int y2 = 0;
public Dessin (int X1, int X2, int Y1, int Y2)
{
super();
this.setBounds(0,0,400,400);
this.setOpaque(false);
x1 = X1;
x2 = X2;
y1 = Y1;
y2 = Y2;
}
public void setTransparency(float aTransparency)
{
transparency = aTransparency;
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,Math.min(transparency,1.0f)));
g2.draw(new Rectangle(x1,y1,x2,y2));
}
}
class MonContainer extends JPanel {
private JLayeredPane layeredPane;
private Dessin panel1;
private Dessin panel2;
public MonContainer (JLayeredPane aLayeredPane)
{
super();
this.setPreferredSize(new Dimension(400,400));
this.panel1 = new Dessin(50,50,30,30);
this.panel2 = new Dessin(60,60,20,20);
this.layeredPane = aLayeredPane;
this.layeredPane.add(panel1,new Integer(-3000));
this.layeredPane.add(panel2,new Integer(0));
}
} |
Partager