import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class Alpha {
JFrame f = new JFrame(); //JFrame, in welchem die Zeichnung ist
static int W=300, H=300;
public Alpha() {
f.setSize(W, H);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Alpha Value");
JPanel pane = new JPanel(new BorderLayout());
VarAlpha var = new VarAlpha();
pane.add("Center", var.makeLabel());
//Thread, der die Alpha Werte variert
Thread thr = new Thread(var);
thr.start();
f.setContentPane(pane);
f.setVisible(true);
}
public static void main(String[] args) {
new Alpha();
}
}
class Draw extends JLabel {
int W, H, tw = 0, t=0;
Color col1 = Color.ORANGE, col2 = Color.BLUE, col3 = Color.RED;
public Draw () {
this.W = Alpha.W;
this.H = Alpha.H;
}
//hier werden die Bestandteile gezeichnet
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(col1);
Rectangle2D.Double rect = new Rectangle2D.Double( 0, 0, W, H );
g2.fill(rect);
g2.draw(rect);
g2.setColor(col2);
Ellipse2D.Double circle = new Ellipse2D.Double( 0, 0, W, H );
g2.fill(circle);
g2.setColor(col3);
Ellipse2D.Double ell = new Ellipse2D.Double( W/4, 0, W/2, H );
g2.fill(ell);
}
//hier werden die Farbe mit den entsprechenden Alpha-Werten erstellt, dann wird geupdatet
public void setAlphas(int alpha1, int alpha2, int alpha3) {
col1 = new Color( col1.getRed(), col1.getGreen(), col1.getBlue(), alpha2 );
col2 = new Color( col2.getRed(), col2.getGreen(), col2.getBlue(), alpha2 );
col3 = new Color( col3.getRed(), col3.getGreen(), col3.getBlue(), alpha3 );
update();
}
public void update() {
this.repaint();
}
}
class VarAlpha implements Runnable {
int alpha = 0;
Draw draw = new Draw();
public JLabel makeLabel() {
return draw;
}
//hier wird das alpha variert, dann der Draw übergeben, und kurz schlafen gelegt
public void run() {
while(true) {
while( alpha <=255 ) {
draw.setAlphas(alpha, 255-alpha, alpha);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
alpha ++;
}
alpha--;
while( alpha > 0) {
draw.setAlphas(alpha, 255-alpha, alpha);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
alpha--;
}
}
}
}