Skip to content

Instantly share code, notes, and snippets.

@shamun
Created July 16, 2011 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamun/1086865 to your computer and use it in GitHub Desktop.
Save shamun/1086865 to your computer and use it in GitHub Desktop.
import sdk.video.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;
public class ButtonTest extends JWindow {
private static String frameperSecond = "20/1";
private static String x = "800";
private static String y = "600";
public static P p = null;
private static JFrame frame;
private JButton opaqueButton1;
private JButton opaqueButton2;
private SoftJButton softButton1;
private SoftJButton softButton2;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ButtonTest j = new ButtonTest();
j.createAndShowGUI();
t.init();
p = P.l("web camera here");
X.setWindowID(j);
p.play();
}
});
}
public void createAndShowGUI() {
this.setLayout (new BorderLayout ());
this.setBounds(100,30,600, 600);
opaqueButton1 = new JButton("Opaque Button");
opaqueButton2 = new JButton("Opaque Button");
softButton1 = new SoftJButton("Transparent Button");
softButton2 = new SoftJButton("Transparent Button");
opaqueButton1.setBackground(Color.GREEN);
softButton1.setBackground(Color.GREEN);
//frame = new JFrame();
//frame.getContentPane().setLayout(new java.awt.BorderLayout ());
this.setBackground( new Color(0, 0, 0, 255) );
add(opaqueButton1);
add(softButton1);
add(opaqueButton2);
add(softButton2);
//setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setVisible(true);
Timer alphaChanger = new Timer(30, new ActionListener() {
private float incrementer = -.03f;
@Override
public void actionPerformed(ActionEvent e) {
float newAlpha = softButton1.getAlpha() + incrementer;
if (newAlpha < 0) {
newAlpha = 0;
incrementer = -incrementer;
} else if (newAlpha > 1f) {
newAlpha = 1f;
incrementer = -incrementer;
}
softButton1.setAlpha(newAlpha);
softButton2.setAlpha(newAlpha);
}
});
alphaChanger.start();
Timer uiChanger = new Timer(3500, new ActionListener() {
private LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
private int index = 1;
@Override
public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(laf[index].getClassName());
SwingUtilities.updateComponentTreeUI(frame);
} catch (Exception exc) {
exc.printStackTrace();
}
index = (index + 1) % laf.length;
}
});
uiChanger.start();
}
public static class SoftJButton extends JButton {
private static final JButton lafDeterminer = new JButton();
private static final long serialVersionUID = 1L;
private boolean rectangularLAF;
private float alpha = 1f;
public SoftJButton() {
this(null, null);
}
public SoftJButton(String text) {
this(text, null);
}
public SoftJButton(String text, Icon icon) {
super(text, icon);
setOpaque(false);
setFocusPainted(false);
}
public float getAlpha() {
return alpha;
}
public void setAlpha(float alpha) {
this.alpha = alpha;
repaint();
}
@Override
public void paintComponent(java.awt.Graphics g) {
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
if (rectangularLAF && isBackgroundSet()) {
Color c = getBackground();
g2.setColor(c);
g.fillRect(0, 0, getWidth(), getHeight());
}
super.paintComponent(g2);
}
@Override
public void updateUI() {
super.updateUI();
lafDeterminer.updateUI();
rectangularLAF = lafDeterminer.isOpaque();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment