Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import java.awt.image.BufferedImage;
  6. import javax.swing.JOptionPane;
  7. import java.awt.Image;
  8.  
  9. public class Bubble extends Applet implements Runnable {
  10.  
  11. int width, height;
  12. int i = 0,j=0;
  13. int first;
  14. int second;
  15.  
  16. Thread t = null;
  17. boolean threadSuspended;
  18. int[] myIntArray = {9,8,7,6,5,4,3,2,1,0};
  19. String str;
  20. String val;
  21. // Executed when the applet is first created.
  22. public void init() {
  23. /* for(int m=0;m<10;m++)
  24. {
  25. val = JOptionPane.showInputDialog("Enter value>> ");
  26. int x = Integer.parseInt(val);
  27. myIntArray[m]=x;
  28. }*/
  29.  
  30. System.out.println("init(): begin");
  31. width = getSize().width;
  32. height = getSize().height;
  33.  
  34. setBackground( Color.black );
  35. System.out.println("init(): end");
  36.  
  37. }
  38.  
  39. // Executed when the applet is destroyed.
  40. public void destroy() {
  41. System.out.println("destroy()");
  42. }
  43.  
  44. // Executed after the applet is created; and also whenever
  45. // the browser returns to the page containing the applet.
  46. public void start() {
  47. System.out.println("start(): begin");
  48. if ( t == null ) {
  49. System.out.println("start(): creating thread");
  50. t = new Thread( this );
  51. System.out.println("start(): starting thread");
  52. threadSuspended = false;
  53. t.start();
  54. }
  55. else {
  56. if ( threadSuspended ) {
  57. threadSuspended = false;
  58. System.out.println("start(): notifying thread");
  59. synchronized( this ) {
  60. notify();
  61. }
  62. }
  63. }
  64. System.out.println("start(): end");
  65. }
  66.  
  67. // Executed whenever the browser leaves the page containing the applet.
  68. public void stop() {
  69. System.out.println("stop(): begin");
  70. threadSuspended = true;
  71. }
  72.  
  73. // Executed within the thread that this applet created.
  74. public void run() {
  75. System.out.println("run(): begin");
  76. try {
  77. while (true) {
  78. System.out.println("run(): awake");
  79.  
  80. // Here's where the thread does some work
  81.  
  82.  
  83.  
  84. if(j==9)
  85. {
  86. j=0;
  87. i++; // this is shorthand for "i = i+1;"
  88.  
  89. }
  90. if ( i == 10 ) {
  91. //i = 0;
  92. stop();
  93. destroy();
  94. }
  95.  
  96. if(myIntArray[j+1]<myIntArray[j])
  97. {
  98. int temp;
  99. temp=myIntArray[j];
  100. myIntArray[j]=myIntArray[j+1];
  101. myIntArray[j+1]=temp;
  102. //first=j+1;
  103. second=j;
  104. }
  105. j++;
  106. str=Arrays.toString(myIntArray);
  107. //bubblesort();
  108.  
  109.  
  110. showStatus( "i is " + i + j );
  111.  
  112. // Now the thread checks to see if it should suspend itself
  113. if ( threadSuspended ) {
  114. synchronized( this ) {
  115. while ( threadSuspended ) {
  116. System.out.println("run(): waiting");
  117. wait();
  118. }
  119. }
  120. }
  121. System.out.println("run(): requesting repaint");
  122. repaint();
  123. System.out.println("run(): sleeping");
  124. Thread.sleep( 2000 ); // interval given in milliseconds
  125. }
  126. }
  127. catch (InterruptedException e) { }
  128. System.out.println("run(): end");
  129. }
  130.  
  131. // Executed whenever the applet is asked to redraw itself.
  132. public void paint( Graphics g ) {
  133. System.out.println("paint()");
  134. width = getSize().width;
  135. height = getSize().height;
  136.  
  137.  
  138. for(int s=0;s<=9;s++)
  139. {
  140. //g.drawRect(10*s, 10, 8, myIntArray[s]*4);
  141. if(s==second+1 || s==second+2)
  142. g.setColor( Color.red );
  143.  
  144. else{
  145. g.setColor(Color.green);
  146. }
  147. g.fillRect(s*(10+(width/20)), 10, 8+(width/20), ((myIntArray[s]+(height/20))*10));
  148.  
  149. g.setFont(new Font("Monospaced", Font.PLAIN, (width/20)+(height/20)));
  150. g.drawString(Integer.toString(myIntArray[s]), s*(10+(width/20)), (width/10)+((myIntArray[s]+(height/20))*10));
  151.  
  152. //g.draw3DRect(19*s, 10, 15, myIntArray[s]*7, true);
  153. //Image img = createImage();
  154. // g.drawImage(img, 10*s, 10,this);
  155. }
  156.  
  157. first=0;
  158. second=0;
  159. //g.drawRect(0, 0,10, i);
  160. //g.drawLine( width, height, i*width/10 , 0 );
  161. }
  162. public void bubblesort()
  163. {
  164.  
  165. }
  166. /*
  167. private Image createImage(){
  168. BufferedImage bufferedImage = new BufferedImage(5,5,BufferedImage.TYPE_INT_RGB);
  169. Graphics g = bufferedImage.getGraphics();
  170. g.drawString("H", 20,20);
  171.  
  172. return bufferedImage;
  173. }
  174. */
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement