Advertisement
OoryaK

Horse race / Oorya

Apr 16th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. //*****************************MAIN ACTIVITY
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.SeekBar;
  6. import android.widget.TextView;
  7. import java.util.ArrayList;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.  
  11.     private SeekBar horseBar1, horseBar2,horseBar3,horseBar4,horseBar5;
  12.     private TextView txtRes;
  13.     private String res="";
  14.     private int place;
  15.     public static ArrayList<String> listResult=new ArrayList<>();
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.         setPointer();
  22.     }
  23.  
  24.     public void setPointer()
  25.     {
  26.         horseBar1=(SeekBar)findViewById(R.id.horse1);
  27.         horseBar2=(SeekBar)findViewById(R.id.horse2);
  28.         horseBar3=(SeekBar)findViewById(R.id.horse3);
  29.         horseBar4=(SeekBar)findViewById(R.id.horse4);
  30.         horseBar5=(SeekBar)findViewById(R.id.horse5);
  31.         txtRes=(TextView)findViewById(R.id.txtRes);
  32.     }
  33.  
  34.  
  35.     public void startRace(View view)
  36.     {
  37.         final ThreadRace horse1=new ThreadRace(horseBar1,this);
  38.         ThreadRace horse2=new ThreadRace(horseBar2,this);
  39.         ThreadRace horse3=new ThreadRace(horseBar3,this);
  40.         ThreadRace horse4=new ThreadRace(horseBar4,this);
  41.         ThreadRace horse5=new ThreadRace(horseBar5,this);
  42.  
  43.         horse1.setName("Horse 1");
  44.         horse2.setName("Horse 2");
  45.         horse3.setName("Horse 3");
  46.         horse4.setName("Horse 4");
  47.         horse5.setName("Horse 5");
  48.  
  49.         horse1.start();
  50.         horse2.start();
  51.         horse3.start();
  52.         horse4.start();
  53.         horse5.start();
  54.     }
  55.  
  56.     public void getResult(View view) {
  57.         for (int counter=0;counter<listResult.size();counter++)
  58.         {
  59.             place=counter+1;
  60.             res+="\n Place "+place+":  "+listResult.get(counter);
  61.         }
  62.         txtRes.setText(res);
  63.         listResult.clear();
  64.     }
  65. }
  66.  
  67. //***********************HORSE RACE CLASS
  68. import android.content.Context;
  69. import android.widget.SeekBar;
  70. import android.widget.Toast;
  71. import java.util.Random;
  72.  
  73. public class ThreadRace extends Thread
  74. {
  75.     private SeekBar horse;
  76.     private Context context;
  77.     private Random rnd=new Random();
  78.  
  79.     public ThreadRace(SeekBar horse, Context context)
  80.     {
  81.         this.horse=horse;
  82.         this.context=context;
  83.     }
  84.  
  85.  
  86.  
  87.     @Override
  88.     public void run()
  89.     {
  90.         horse.setProgress(0);
  91.  
  92.         for (int counter=0;counter<=100;counter++)
  93.         {
  94.             horse.setProgress(counter);
  95.             try
  96.             {
  97.                 Thread.sleep(rnd.nextInt(100)+100);
  98.             }
  99.             catch (InterruptedException e)
  100.             {
  101.                 e.printStackTrace();
  102.             }
  103.         }
  104.  
  105.         MainActivity.listResult.add(Thread.currentThread().getName());
  106.  
  107.         horse.post(new Runnable() {
  108.             @Override
  109.             public void run() {
  110.                 Toast.makeText(context, MainActivity.listResult.get(MainActivity.listResult.size()-1)+" finished", Toast.LENGTH_SHORT).show();
  111.             }
  112.         });
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement