rendered paste body//Author: Seth D. Adam
//Instructor: Zhang && Slonneger
//Program: Project 2 RotateButtons.java
//Due: Tuesday, Sept. 20, 2005
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
class RotateWords extends JFrame
{
RotateWords()
{
setTitle("Rotate Words");
Font font = new Font("SansSerif", Font.PLAIN, 18);
Font font2 = new Font("SansSerif", Font.PLAIN, 130);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{System.exit(0);}
});
JPanel wordPanel = new JPanel();
JPanel bottomPanel = new JPanel();
checkHandler checkh = new checkHandler();
buttonHandler buttonh = new buttonHandler();
comboHandler comboh = new comboHandler();
bottomPanel.setBackground(Color.lightGray);
rotate = new JButton("Rotate");
rotate.addActionListener(buttonh);
bottomPanel.add(rotate);
comboBox = new JComboBox();
comboBox.addItem("WORD");
comboBox.addItem("JAVA");
comboBox.addItem("R2D2");
comboBox.addItem("JEDI");
comboBox.addItem("IOWA");
comboBox.addActionListener(comboh);
bottomPanel.add(comboBox);
left = new JCheckBox("Left");
left.setFont(font);
left.setBackground(Color.lightGray);
left.addActionListener(checkh);
bottomPanel.add(left);
right = new JCheckBox("Right");
right.setFont(font);
right.setBackground(Color.lightGray);
right.addActionListener(checkh);
bottomPanel.add(right);
first = new JLabel("W", JLabel.CENTER);
second = new JLabel("O", JLabel.CENTER);
third = new JLabel("R", JLabel.CENTER);
fourth = new JLabel("D", JLabel.CENTER);
first.setFont(font2); second.setFont(font2);
third.setFont(font2); fourth.setFont(font2);
firstP = new JPanel(); secondP = new JPanel();
thirdP = new JPanel(); fourthP = new JPanel();
firstP.setLayout(new GridLayout()); secondP.setLayout(new GridLayout());
thirdP.setLayout(new GridLayout()); fourthP.setLayout(new GridLayout());
firstP.add(first, "Center"); firstP.setBackground(new Color(0,100,0));
secondP.add(second, "Center");secondP.setBackground(new Color(0,150,0));
thirdP.add(third, "Center"); thirdP.setBackground(new Color(0,200,0));
fourthP.add(fourth,"Center"); fourthP.setBackground(new Color(0,250,0));
wordPanel.add(firstP);
wordPanel.add(secondP);
wordPanel.add(thirdP);
wordPanel.add(fourthP);
wordPanel.setLayout(new GridLayout(1,4));
getContentPane().add(bottomPanel, "South");
getContentPane().add(wordPanel, "Center");
}
private JLabel first, second, third, fourth, temp;
private JComboBox comboBox;
private JPanel firstP, secondP, thirdP, fourthP;
private JCheckBox left, right;
private JButton rotate;
private int direction = 0;
class buttonHandler implements ActionListener
{ //Manages clicks to the "Rotate" button
public void actionPerformed(ActionEvent evt)
{
String firsttext = first.getText();
String secondtext = second.getText();
String thirdtext = third.getText();
String fourthtext = fourth.getText();
if (direction == 0)
return;
else if (direction == 1)
{
fourth.setText(thirdtext);
third.setText(secondtext);
second.setText(firsttext);
first.setText(fourthtext);}
else{
fourth.setText(firsttext);
third.setText(fourthtext);
second.setText(thirdtext);
first.setText(secondtext);
}
}
}
class checkHandler implements ActionListener
{ //Manages the check boxes
boolean marker = false;
//marker denotes whether an "OOPS" has just occurred
public void actionPerformed(ActionEvent evt)
{
if (left.isSelected()&&right.isSelected())
// an occurance of "OOPS"
{
marker = true;
first.setText("O"); second.setText("O");third.setText("P");fourth.setText("S");
direction = 0;
System.out.println(direction); //test point
return;
}
else if(left.isSelected())
direction = -1;
else if(right.isSelected())
direction = 1;
else
direction = 0;
System.out.println(direction);//another test point, shows directional variable
if(marker == true)
{
String value = (String)comboBox.getSelectedItem();
// This sequence restores the value from JComboBox to the wordpane
// after an "OOPS" occurance (both boxes checked) is corrected (one or both unchecked)
if(value == "IOWA")
{
first.setText("I"); second.setText("O");third.setText("W");fourth.setText("A");
}
else if(value == "JEDI")
{
first.setText("J"); second.setText("E");third.setText("D");fourth.setText("I");
}
else if(value == "R2D2")
{
first.setText("R"); second.setText("2");third.setText("D");fourth.setText("2");
}
else if(value == "JAVA")
{
first.setText("J"); second.setText("A");third.setText("V");fourth.setText("A");
}
else if(value == "WORD")
{
first.setText("W"); second.setText("O");third.setText("R");fourth.setText("D");
}
}
marker = false;
}
}
class comboHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt) //Manages changes in the JComboBox
{
if(left.isSelected()&&right.isSelected())
return;
JComboBox jcb = (JComboBox)evt.getSource();
String value = (String)jcb.getSelectedItem();
if(value == "IOWA")
{
first.setText("I"); second.setText("O");third.setText("W");fourth.setText("A");
}
else if(value == "JEDI")
{
first.setText("J"); second.setText("E");third.setText("D");fourth.setText("I");
}
else if(value == "R2D2")
{
first.setText("R"); second.setText("2");third.setText("D");fourth.setText("2");
}
else if(value == "JAVA")
{
first.setText("J"); second.setText("A");third.setText("V");fourth.setText("A");
}
else if(value == "WORD")
{
first.setText("W"); second.setText("O");third.setText("R");fourth.setText("D");
}
}
}
public static void main(String[] args)
{ JFrame jf = new RotateWords();
jf.setSize(600, 400);
jf.setVisible(true);
}
}