VasilM

AWTHomework9_threads

May 28th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. package awtHomework9;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class AWTHomework9 extends Frame implements WindowListener, ActionListener, Runnable {
  7.    
  8.     Panel left, right;
  9.     Label thread1, thread2, stepLabelLeft, stepLabelRight;
  10.     TextField leftText, rightText;
  11.     Choice stepChoiceLeft, stepChoiceRight;
  12.     Button startLeft, startRight, stopLeft, stopRight;
  13.    
  14.     private Thread tLeft, tRight;
  15.     private int sumLeft, sumRight = 0;
  16.     private boolean leftIsRunning = false, rightIsRunning = false; 
  17.    
  18.     public AWTHomework9() {
  19.         // left part
  20.         thread1 = new Label("Thread 1");
  21.         leftText = new TextField();
  22.         stepLabelLeft = new Label("Step: ");
  23.         stepChoiceLeft = new Choice();
  24.         stepChoiceLeft.addItem("1");
  25.         stepChoiceLeft.addItem("5");
  26.         stepChoiceLeft.addItem("10");
  27.         startLeft = new Button("Start");
  28.         startLeft.addActionListener(this);
  29.         stopLeft = new Button("Stop");
  30.         stopLeft.addActionListener(this);
  31.         Panel [] panels = new Panel[4];
  32.         for(int i=0; i<4; i++) {
  33.             panels[i] = new Panel( new GridLayout(1,0));
  34.         }
  35.         panels[0].add(thread1);
  36.         panels[1].add(leftText);
  37.         panels[2].add(stepLabelLeft);
  38.         panels[2].add(stepChoiceLeft);
  39.         panels[3].add(startLeft);
  40.         panels[3].add(stopLeft);
  41.         left = new Panel(new GridLayout(0,1));
  42.         for(Panel x: panels){
  43.             left.add(x);
  44.         }
  45.        
  46.         // right part      
  47.         thread2 = new Label("Thread 2");
  48.         rightText = new TextField();
  49.         stepLabelRight = new Label("Step: ");
  50.         stepChoiceRight = new Choice();
  51.         stepChoiceRight.addItem("1");
  52.         stepChoiceRight.addItem("5");
  53.         stepChoiceRight.addItem("10");
  54.         startRight = new Button("Start");
  55.         startRight.addActionListener(this);
  56.         stopRight = new Button("Stop");
  57.         stopRight.addActionListener(this);
  58.         Panel [] panels2 = new Panel[4];
  59.         for(int i=0; i<4; i++) {
  60.             panels2[i] = new Panel( new GridLayout(1,0));
  61.         }
  62.         panels2[0].add(thread2);
  63.         panels2[1].add(rightText);
  64.         panels2[2].add(stepLabelRight);
  65.         panels2[2].add(stepChoiceRight);
  66.         panels2[3].add(startRight);
  67.         panels2[3].add(stopRight);
  68.         right = new Panel(new GridLayout(0,1));
  69.         for(Panel x: panels2){
  70.             right.add(x);
  71.         }
  72.        
  73.         //the hole thing
  74.         setLayout( new GridLayout(1,0));
  75.         addWindowListener(this);
  76.         add(left);
  77.         add(right);
  78.         setTitle("Thread Demo");
  79.         setSize(500, 350);
  80.         setVisible(true);      
  81.     }
  82.    
  83.     public static void main(String[] args) {
  84.         new AWTHomework9();
  85.     }
  86.  
  87.     public void actionPerformed(ActionEvent e) {
  88.         if (e.getSource().equals(startLeft)) {
  89.             if(!leftIsRunning) { //за да не се стартира повече от 1 нишка
  90.                 tLeft = new Thread( this );
  91.                 leftIsRunning = true;
  92.                 tLeft.start();
  93.             }
  94.         }
  95.         if (e.getSource().equals(stopLeft)) {
  96.             stopLeft();
  97.         }
  98.         if (e.getSource().equals(startRight)) {
  99.             if(!rightIsRunning) {
  100.                 tRight = new Thread( this );
  101.                 rightIsRunning = true;
  102.                 tRight.start();
  103.             }
  104.         }
  105.         if (e.getSource().equals(stopRight)) {
  106.             stopRight();
  107.         }
  108.     }
  109.    
  110.     public void run() {
  111.         if (Thread.currentThread().equals(tLeft)) {
  112.             while(leftIsRunning) {
  113.                 try {
  114.                     Thread.sleep(1000);
  115.                     switch(stepChoiceLeft.getSelectedIndex()) {
  116.                         case 0: sumLeft += 1;
  117.                         break;
  118.                         case 1: sumLeft += 5;
  119.                         break;
  120.                         case 2: sumLeft += 10; 
  121.                         break;
  122.                     }
  123.                     leftText.setText(""+sumLeft);
  124.                 }
  125.                 catch (InterruptedException e) {
  126.                     e.printStackTrace();
  127.                 }
  128.             }
  129.         }
  130.         if (Thread.currentThread().equals(tRight)) {
  131.             while(rightIsRunning) {
  132.                 try {
  133.                     Thread.sleep(1000);
  134.                     switch(stepChoiceRight.getSelectedIndex()) {
  135.                         case 0: sumRight += 1;
  136.                         break;
  137.                         case 1: sumRight += 5;
  138.                         break;
  139.                         case 2: sumRight += 10;
  140.                         break;
  141.                     }
  142.                     rightText.setText(""+sumRight);
  143.                 }
  144.                 catch (InterruptedException e) {
  145.                     e.printStackTrace();
  146.                 }
  147.             }
  148.         }      
  149.     }
  150.    
  151.     public void stopLeft() {
  152.         leftIsRunning = false;
  153.     }
  154.    
  155.     public void stopRight() {
  156.         rightIsRunning = false;
  157.     }
  158.  
  159.     @Override
  160.     public void windowActivated(WindowEvent arg0) {
  161.         // TODO Auto-generated method stub
  162.        
  163.     }
  164.  
  165.     @Override
  166.     public void windowClosed(WindowEvent arg0) {
  167.         // TODO Auto-generated method stub
  168.        
  169.     }
  170.  
  171.     @Override
  172.     public void windowClosing(WindowEvent e) {
  173.         System.exit(0);
  174.        
  175.     }
  176.  
  177.     @Override
  178.     public void windowDeactivated(WindowEvent arg0) {
  179.         // TODO Auto-generated method stub
  180.        
  181.     }
  182.  
  183.     @Override
  184.     public void windowDeiconified(WindowEvent arg0) {
  185.         // TODO Auto-generated method stub
  186.        
  187.     }
  188.  
  189.     @Override
  190.     public void windowIconified(WindowEvent arg0) {
  191.         // TODO Auto-generated method stub
  192.        
  193.     }
  194.  
  195.     @Override
  196.     public void windowOpened(WindowEvent arg0) {
  197.         // TODO Auto-generated method stub
  198.        
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment