Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.*;
- import java.awt.*;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.awt.image.BufferedImage;
- import javax.swing.JOptionPane;
- import java.awt.Image;
- public class Bubble extends Applet implements Runnable {
- int width, height;
- int i = 0,j=0;
- int first;
- int second;
- Thread t = null;
- boolean threadSuspended;
- int[] myIntArray = {9,8,7,6,5,4,3,2,1,0};
- String str;
- String val;
- // Executed when the applet is first created.
- public void init() {
- /* for(int m=0;m<10;m++)
- {
- val = JOptionPane.showInputDialog("Enter value>> ");
- int x = Integer.parseInt(val);
- myIntArray[m]=x;
- }*/
- System.out.println("init(): begin");
- width = getSize().width;
- height = getSize().height;
- setBackground( Color.black );
- System.out.println("init(): end");
- }
- // Executed when the applet is destroyed.
- public void destroy() {
- System.out.println("destroy()");
- }
- // Executed after the applet is created; and also whenever
- // the browser returns to the page containing the applet.
- public void start() {
- System.out.println("start(): begin");
- if ( t == null ) {
- System.out.println("start(): creating thread");
- t = new Thread( this );
- System.out.println("start(): starting thread");
- threadSuspended = false;
- t.start();
- }
- else {
- if ( threadSuspended ) {
- threadSuspended = false;
- System.out.println("start(): notifying thread");
- synchronized( this ) {
- notify();
- }
- }
- }
- System.out.println("start(): end");
- }
- // Executed whenever the browser leaves the page containing the applet.
- public void stop() {
- System.out.println("stop(): begin");
- threadSuspended = true;
- }
- // Executed within the thread that this applet created.
- public void run() {
- System.out.println("run(): begin");
- try {
- while (true) {
- System.out.println("run(): awake");
- // Here's where the thread does some work
- if(j==9)
- {
- j=0;
- i++; // this is shorthand for "i = i+1;"
- }
- if ( i == 10 ) {
- //i = 0;
- stop();
- destroy();
- }
- if(myIntArray[j+1]<myIntArray[j])
- {
- int temp;
- temp=myIntArray[j];
- myIntArray[j]=myIntArray[j+1];
- myIntArray[j+1]=temp;
- //first=j+1;
- second=j;
- }
- j++;
- str=Arrays.toString(myIntArray);
- //bubblesort();
- showStatus( "i is " + i + j );
- // Now the thread checks to see if it should suspend itself
- if ( threadSuspended ) {
- synchronized( this ) {
- while ( threadSuspended ) {
- System.out.println("run(): waiting");
- wait();
- }
- }
- }
- System.out.println("run(): requesting repaint");
- repaint();
- System.out.println("run(): sleeping");
- Thread.sleep( 2000 ); // interval given in milliseconds
- }
- }
- catch (InterruptedException e) { }
- System.out.println("run(): end");
- }
- // Executed whenever the applet is asked to redraw itself.
- public void paint( Graphics g ) {
- System.out.println("paint()");
- width = getSize().width;
- height = getSize().height;
- for(int s=0;s<=9;s++)
- {
- //g.drawRect(10*s, 10, 8, myIntArray[s]*4);
- if(s==second+1 || s==second+2)
- g.setColor( Color.red );
- else{
- g.setColor(Color.green);
- }
- g.fillRect(s*(10+(width/20)), 10, 8+(width/20), ((myIntArray[s]+(height/20))*10));
- g.setFont(new Font("Monospaced", Font.PLAIN, (width/20)+(height/20)));
- g.drawString(Integer.toString(myIntArray[s]), s*(10+(width/20)), (width/10)+((myIntArray[s]+(height/20))*10));
- //g.draw3DRect(19*s, 10, 15, myIntArray[s]*7, true);
- //Image img = createImage();
- // g.drawImage(img, 10*s, 10,this);
- }
- first=0;
- second=0;
- //g.drawRect(0, 0,10, i);
- //g.drawLine( width, height, i*width/10 , 0 );
- }
- public void bubblesort()
- {
- }
- /*
- private Image createImage(){
- BufferedImage bufferedImage = new BufferedImage(5,5,BufferedImage.TYPE_INT_RGB);
- Graphics g = bufferedImage.getGraphics();
- g.drawString("H", 20,20);
- return bufferedImage;
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement