import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* face.java
*
* Created on 18-Mar-2010, 14:15:14
*/
// Variables declaration - do not modify
/**
*
* @author k00112628
*/
public class face extends javax.swing.JFrame {
private int figureID;
private int loop;
/** Creates new form face */
public face() {
initComponents();
myTimer.start();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
HappySadButtonGroup = new javax.swing.ButtonGroup();
facePanel = new javax.swing.JPanel();
labelPanel = new javax.swing.JPanel();
happyRadioButton = new javax.swing.JRadioButton();
sadRadioButton = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout facePanelLayout = new javax.swing.GroupLayout(facePanel);
facePanel.setLayout(facePanelLayout);
facePanelLayout.setHorizontalGroup(
facePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 315, Short.MAX_VALUE)
);
facePanelLayout.setVerticalGroup(
facePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 212, Short.MAX_VALUE)
);
HappySadButtonGroup.add(happyRadioButton);
happyRadioButton.setText("Happy");
happyRadioButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
happyRadioButtonActionPerformed(evt);
}
});
HappySadButtonGroup.add(sadRadioButton);
sadRadioButton.setText("Sad");
sadRadioButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sadRadioButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout labelPanelLayout = new javax.swing.GroupLayout(labelPanel);
labelPanel.setLayout(labelPanelLayout);
labelPanelLayout.setHorizontalGroup(
labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(labelPanelLayout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(happyRadioButton)
.addGap(36, 36, 36)
.addComponent(sadRadioButton)
.addContainerGap(37, Short.MAX_VALUE))
);
labelPanelLayout.setVerticalGroup(
labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, labelPanelLayout.createSequentialGroup()
.addContainerGap(16, Short.MAX_VALUE)
.addGroup(labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(happyRadioButton)
.addComponent(sadRadioButton))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(facePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(74, 74, 74)
.addComponent(labelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(44, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(facePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
.addComponent(labelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void happyRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
figureID = 1;
repaint();
}
private void sadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {
figureID = 2;
repaint();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new face().setVisible(true);
}
});
}
@Override
public void paint(Graphics g){
if (figureID == 1){
g.setColor(Color.PINK);
g.fillOval(20,20, 250, 250);
g.setColor(Color.CYAN);
g.fillOval(100, 50, 30, 30);
g.fillOval(200, 50, 30, 30);
g.setColor(Color.BLUE);
g.fillOval(150, 100, 20, 20);
g.setColor(Color.RED);
g.drawArc(90, 150, 150, 90,220, 120);
}
else if (figureID ==2){
g.setColor(Color.PINK);
g.fillOval(20,20, 250, 250);
g.setColor(Color.CYAN);
g.fillOval(100, 50, 30, 30);
g.fillOval(200, 50, 30, 30);
g.setColor(Color.BLUE);
g.fillOval(150, 100, 20, 20);
g.setColor(Color.RED);
g.drawArc(90, 150, 150,90, 20, 140);
}
}
Timer myTimer = new Timer(250,new ActionListener(){
public void actionPerformed (ActionEvent e){
loop++;
if(loop%2==0){
figureID = 1;
}
else{
figureID = 2;
}
repaint();
}
});
// Variables declaration - do not modify
private javax.swing.ButtonGroup HappySadButtonGroup;
private javax.swing.JPanel facePanel;
private javax.swing.JRadioButton happyRadioButton;
private javax.swing.JPanel labelPanel;
private javax.swing.JRadioButton sadRadioButton;
// End of variables declaration
}