Friday, March 20, 2009

Exercise 2 --- Color Cycle

/* Programmer: Maribelle Lacno
  * Program name: colorCycle
  * Subject: IT134_Computer Programming 3 
  * Instructor: Mr. Dony Dongiapon 
  * Date Started: 03/16/09 
  * Date Finished: 03/18/09 
  * Purpose: To create a program that has only one button 
  *     in the frame but in clicking this button will change the
  *     background color. 
*/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class colorCycle extends JFrame{
 int x=0;
 boolean click = false;
 JPanel buttonPanel;
 JButton buttons;
 Container container = getContentPane();
 String colors[] = {"red","blue","green","yellow"};
 Color buttonColors[] = {Color.red,Color.blue,Color.green,Color.yellow};
 
 public colorCycle(){
  super("Color Cycle");
  changeColor action = new changeColor();
  container.setLayout(new FlowLayout());
  buttons = new JButton(colors[x]);
  if(click){
  buttons.setBackground(buttonColors[x]);
  }else{
  buttons.setForeground(buttonColors[x]);
  }
  buttonPanel = new JPanel();
  buttonPanel.setLayout(new GridLayout());
  buttons.addActionListener(action);
  buttonPanel.add(buttons);
  container.add(buttonPanel,BorderLayout.CENTER);
  setSize(400,150);
  setVisible(true);
 }
 
 private class changeColor implements ActionListener{
  public void actionPerformed(ActionEvent event){
  x=++x  buttons.setText(colors[x]);
  container.setBackground(buttonColors[x]);
  buttons.setBackground(buttonColors[x]);
  }
 }

 public static void main(String[] args){
  colorCycle color = new colorCycle();
  color.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}

No comments:

Post a Comment