Results 1 to 4 of 4

Thread: color properties

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99

    color properties

    I want to set color properties of panel objects such as buttons. Easy enough...however I want to pass the color in as a variable...such as...

    btnClose.setBackground(Color.VARIABLE);

    but that doesn't work. How would I pass a variable to this method?

    Steve

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    All you would have to do is create a new instance of a Color object with the rgb values that you choose then pass that object to a method that taks a Color object as an arguement.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Or you could pass the r, g, b values to the method and create the Color object within the method itself.
    Code:
    public void changeColor(int r,int g, int b){
      jb.setBackground(new Color(r,g,b)); 
    }

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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();
      }
     }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width