Share Pastebin
Guest
Public paste!

FaceCode

By: a guest | Mar 18th, 2010 | Syntax: Java | Size: 7.64 KB | Hits: 69 | Expires: Never
Copy text to clipboard
  1.  
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.Timer;
  7.  
  8. /*
  9.  * To change this template, choose Tools | Templates
  10.  * and open the template in the editor.
  11.  */
  12.  
  13. /*
  14.  * face.java
  15.  *
  16.  * Created on 18-Mar-2010, 14:15:14
  17.  */
  18.  
  19.  
  20.     // Variables declaration - do not modify
  21. /**
  22.  *
  23.  * @author k00112628
  24.  */
  25. public class face extends javax.swing.JFrame {
  26.     private int figureID;
  27.  
  28.  private int loop;
  29.     /** Creates new form face */
  30.     public face() {
  31.         initComponents();
  32.         myTimer.start();
  33.  
  34.     }
  35.  
  36.     /** This method is called from within the constructor to
  37.      * initialize the form.
  38.      * WARNING: Do NOT modify this code. The content of this method is
  39.      * always regenerated by the Form Editor.
  40.      */
  41.     @SuppressWarnings("unchecked")
  42.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  43.     private void initComponents() {
  44.  
  45.         HappySadButtonGroup = new javax.swing.ButtonGroup();
  46.         facePanel = new javax.swing.JPanel();
  47.         labelPanel = new javax.swing.JPanel();
  48.         happyRadioButton = new javax.swing.JRadioButton();
  49.         sadRadioButton = new javax.swing.JRadioButton();
  50.  
  51.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  52.  
  53.         javax.swing.GroupLayout facePanelLayout = new javax.swing.GroupLayout(facePanel);
  54.         facePanel.setLayout(facePanelLayout);
  55.         facePanelLayout.setHorizontalGroup(
  56.             facePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  57.             .addGap(0, 315, Short.MAX_VALUE)
  58.         );
  59.         facePanelLayout.setVerticalGroup(
  60.             facePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  61.             .addGap(0, 212, Short.MAX_VALUE)
  62.         );
  63.  
  64.         HappySadButtonGroup.add(happyRadioButton);
  65.         happyRadioButton.setText("Happy");
  66.         happyRadioButton.addActionListener(new java.awt.event.ActionListener() {
  67.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  68.                 happyRadioButtonActionPerformed(evt);
  69.             }
  70.         });
  71.  
  72.         HappySadButtonGroup.add(sadRadioButton);
  73.         sadRadioButton.setText("Sad");
  74.         sadRadioButton.addActionListener(new java.awt.event.ActionListener() {
  75.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  76.                 sadRadioButtonActionPerformed(evt);
  77.             }
  78.         });
  79.  
  80.         javax.swing.GroupLayout labelPanelLayout = new javax.swing.GroupLayout(labelPanel);
  81.         labelPanel.setLayout(labelPanelLayout);
  82.         labelPanelLayout.setHorizontalGroup(
  83.             labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  84.             .addGroup(labelPanelLayout.createSequentialGroup()
  85.                 .addGap(35, 35, 35)
  86.                 .addComponent(happyRadioButton)
  87.                 .addGap(36, 36, 36)
  88.                 .addComponent(sadRadioButton)
  89.                 .addContainerGap(37, Short.MAX_VALUE))
  90.         );
  91.         labelPanelLayout.setVerticalGroup(
  92.             labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  93.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, labelPanelLayout.createSequentialGroup()
  94.                 .addContainerGap(16, Short.MAX_VALUE)
  95.                 .addGroup(labelPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  96.                     .addComponent(happyRadioButton)
  97.                     .addComponent(sadRadioButton))
  98.                 .addContainerGap())
  99.         );
  100.  
  101.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  102.         getContentPane().setLayout(layout);
  103.         layout.setHorizontalGroup(
  104.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  105.             .addGroup(layout.createSequentialGroup()
  106.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  107.                     .addGroup(layout.createSequentialGroup()
  108.                         .addGap(41, 41, 41)
  109.                         .addComponent(facePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  110.                     .addGroup(layout.createSequentialGroup()
  111.                         .addGap(74, 74, 74)
  112.                         .addComponent(labelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
  113.                 .addContainerGap(44, Short.MAX_VALUE))
  114.         );
  115.         layout.setVerticalGroup(
  116.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  117.             .addGroup(layout.createSequentialGroup()
  118.                 .addGap(19, 19, 19)
  119.                 .addComponent(facePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  120.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
  121.                 .addComponent(labelPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  122.                 .addContainerGap())
  123.         );
  124.  
  125.         pack();
  126.     }// </editor-fold>                        
  127.  
  128.     private void happyRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
  129.        figureID = 1;
  130.        repaint();
  131.     }                                                
  132.  
  133.     private void sadRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
  134.         figureID = 2;
  135.        repaint();
  136.     }                                              
  137.  
  138.     /**
  139.     * @param args the command line arguments
  140.     */
  141.     public static void main(String args[]) {
  142.         java.awt.EventQueue.invokeLater(new Runnable() {
  143.             public void run() {
  144.                 new face().setVisible(true);
  145.             }
  146.         });
  147.     }
  148.  
  149.  
  150.  
  151.     @Override
  152.      public void paint(Graphics g){
  153.  
  154.         if (figureID == 1){
  155.             g.setColor(Color.PINK);
  156.             g.fillOval(20,20, 250, 250);
  157.             g.setColor(Color.CYAN);
  158.             g.fillOval(100, 50, 30, 30);
  159.             g.fillOval(200, 50, 30, 30);
  160.             g.setColor(Color.BLUE);
  161.             g.fillOval(150, 100, 20, 20);
  162.            
  163.             g.setColor(Color.RED);
  164.  
  165.             g.drawArc(90, 150, 150, 90,220, 120);
  166.  
  167.         }
  168.         else if (figureID ==2){
  169.              g.setColor(Color.PINK);
  170.             g.fillOval(20,20, 250, 250);
  171.             g.setColor(Color.CYAN);
  172.             g.fillOval(100, 50, 30, 30);
  173.             g.fillOval(200, 50, 30, 30);
  174.             g.setColor(Color.BLUE);
  175.             g.fillOval(150, 100, 20, 20);
  176.        
  177.             g.setColor(Color.RED);
  178.  
  179.             g.drawArc(90, 150, 150,90, 20, 140);
  180.          
  181.         }
  182.  
  183.  
  184.     }
  185.  
  186.     Timer myTimer = new Timer(250,new ActionListener(){
  187.     public void  actionPerformed (ActionEvent e){
  188.       loop++;
  189.  
  190.       if(loop%2==0){
  191.           figureID = 1;
  192.       }
  193.       else{
  194.           figureID = 2;
  195.       }
  196.       repaint();
  197.  
  198.     }
  199.  
  200.     });
  201.  
  202.     // Variables declaration - do not modify                    
  203.     private javax.swing.ButtonGroup HappySadButtonGroup;
  204.     private javax.swing.JPanel facePanel;
  205.     private javax.swing.JRadioButton happyRadioButton;
  206.     private javax.swing.JPanel labelPanel;
  207.     private javax.swing.JRadioButton sadRadioButton;
  208.     // End of variables declaration                  
  209.  
  210.      
  211. }