Here is an example that i was working on for a little while. All it does is change the r,g,b values of a JButton when clicked. If any of the r,g,b values is > 255 the background color of the JButton is reset to the orgional color.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class X{
private JFrame jf;
private JButton jb;
private Container c;
private int r;
private int g;
private int b;
public X(){
jf = new JFrame();
jb = new JButton("Button");
c = jf.getContentPane();
c.add(jb, BorderLayout.SOUTH);
jf.setSize(300,200);
jf.setVisible(true);
}
public void changeColor(int r,int g, int b){
if((r < 255) && (g < 255) && (g < 255)){
jb.setBackground(new Color(r,g,b));
}else{
jb.setBackground(new Color(0,0,0));
setValues(true);
}
}
public void addButtonListener(){
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent avt){
changeColor(r,g,b);
setValues(false);
}
});
}
public void setValues(boolean reset){
if(!reset){
r += 14;
g += 7;
b += 1;
}else{
r = 0;
g = 0;
b = 0;
}
}
public void addWindowMonitor(){
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
}
}
public class E{
public static void main(String[] args){
X x = new X();
x.addButtonListener();
x.addWindowMonitor();
}
}