3 Replies - 422 Views - Last Post: 23 October 2012 - 03:58 AM Rate Topic: -----

#1 lotusmisty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 12

how to pause for the user input during execution of a loop

Posted 23 October 2012 - 12:15 AM

hey! i have to stop the loop so that the program could wait for the button to be clicked and then proceed further and each time the "NEXT" button is clicked the screen is repainted with the updated array...I have tried using threads but it results into EXCEPTIONS :withstupid: :angry: :helpsmilie:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
/*<applet code="Heapsort" height=650 width=1350></applet>*/
public class Heapsort extends Applet implements ActionListener,Runnable
{
	Label lab1;
	TextField t1;
	 String no;
	String str;
	 int n,temp;
	Boolean clear,stop;
	int x[]=new int[12];
	Thread clockThread;
	int y[]=new int[12];

	Button start,go;
	int arr[]={0,87,23,1,99,45,9,31,77,15,25};
	int arr2[]=new int[11];
	Font f = new Font ("Serif", Font.BOLD, 25);
	Color background = new Color (210,111,121);


public void init()
{
	x[1]=550;
	y[1]=235;
	setLayout(new FlowLayout(FlowLayout.LEFT));
	
	setFont(f);
	setBackground(background);
	lab1 = new Label("Enter the number of elements to be sorted");
	add(lab1);
	t1 = new TextField("",5);
	add(t1);
	start=new Button("Make_Heap");
	add(start);
	start.addActionListener(this);	
	go=new Button("Next");
	add(go);
	go.addActionListener(this);	
	  clockThread= new Thread(this); 
	   clockThread.start(); 
}
  public void destroy() 
         { 
 running = false; 
clockThread = null; 
         } 
  public void run() 
     { 
  while (running) 
          { 
repaint();
   try 
               { 
                     // Wait 500milliseconds before continuing 
                    clockThread.sleep(500); 
               } 
               catch (InterruptedException e) 
               { 
                    System.out.println(e); 
                } 
}
}
public void actionPerformed(ActionEvent ae) 
{
	 str = ae.getActionCommand();
	if(str.equals("Make_Heap")) 
	{
		no=t1.getText();
		n=Integer.parseInt(no);
		arr2[0]=0;
		for(int i=1;i<=n;i++)
		{
			arr2[i]=arr[i];
		}
		repaint();
		 heapify(arr2,n);
		//swapper(arr2,n);
	}
	if(str.equals("Next"))
	{
		clear=true;
		swapper(arr2,n);
	}	 
}

 public void swapper(int arr2[],int n)
{
	for(int i=n;i>=2;i--)
                  {
                                    temp=arr2[i];
                                    arr2[i]=arr2[1]; repaint();
                                    arr2[1]=temp;
                                    adjust(arr2,1,i-1);
                   }
	stop=true;
	repaint();
}

public void heapify(int arr2[],int n)
{
	 int i;
            	for(i=(n/2);i>=1;i--)
            	{                     
                                  adjust(arr2,i,n);
                  } 
}		
public void adjust(int arr2[],int i, int n)
{
      int j,item;
                     
                       j=i*2;
                       item=arr2[i];
                         
                       while(j<=n)
                       { 
                                  if((j<n) && (arr2[j]<arr2[j+1]))
                                  { 
                                          j=j+1;
                                         
                                   }
                                          if(item>=arr2[j])
                                          {
                                          break;
                                          }
                                          else
                                          {
                                              arr2[j/2]=arr2[j];		repaint();	//do something                                          
j=2*j;
                                              }
                                  }

                                  arr2[j/2]=item;  			repaint();	//do something
}

public void paint(Graphics g)		
{
	g.drawString("OPERATIONS: ",1000,50);
	g.drawString("1.Make Heap(heapify)",1000,100);
	g.drawString("2.Swap 1st element with last ",1000,150);
	g.drawString("ORIGINAL ARRAY : ",100,100);
	for(int i=1;i<=10;i++)
	{ 
		g.drawString(""+arr[i],350+50*i,100);
	}
	for(int i=1;i<=5;i++)
	{
		x[2*i]=x[i]-100/i;
		x[2*i+1]=x[i]+100/i;
		y[2*i]=y[i]+90;
		y[2*i+1]=y[i]+90;
	}

}
public void update (Graphics g)
{
	//repaint();	
	g.drawString("SELECTED ARRAY : ",100,150);
	for(int i=1;i<=n;i++)
	{ 
		g.drawString(""+arr[i],300+50*i,150);
	}
	g.drawString("ARRAY after Operation : ",100,200);	
	for(int i=1;i<=n;i++)
	{ 
		g.drawString(""+arr2[i],350+50*i,200);
	}	
	for(int i=1;i<=n;i++)
	{
		g.drawOval(x[i],y[i],50,50);
		g.drawString(""+arr2[i],x[i]+10,y[i]+30);	
	}
for(int i=1;i<=n/2;i++)
{
g.drawLine(x[i]+15,y[i]+50,x[2*i]+15,y[2*i]);
if(i<n/2 || n%2!=0)
g.drawLine(x[i]+15,y[i]+50,x[2*i+1]+15,y[2*i+1]);
}
	if(clear==true)
	{
		g.setColor(background);
		g.fillRect(350,180,1700,1700);
		repaint();
		clear=false;
	}
	if(stop==true)
	{
		g.setColor(Color.BLACK);
		g.drawString(" SORTED ARRAY  : ",100,600);	
		for(int i=1;i<=n;i++)
		{ 
			g.drawString(""+arr2[i],350+50*i,600);
		}
		System.exit(1);
	}
}		
}		


Is This A Good Question/Topic? 0
  • +

Replies To: how to pause for the user input during execution of a loop

#2 lotusmisty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 12

Re: how to pause for the user input during execution of a loop

Posted 23 October 2012 - 12:44 AM

:surrender:
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: how to pause for the user input during execution of a loop

Posted 23 October 2012 - 03:36 AM

Point to the loop you're talking about. Then describe what the loop is doing and why it needs to wait for the user to make an input, where the input is made, how the user knows, etc. Describe what's going on and what you're trying to do.

You may be trying to use a technique from your experience with console user interfaces that is inappropriate in a GUI user interface. Since GUI user interfaces are event driven, always waiting for the next thing the user does, your question seems out of context.

This post has been edited by GregBrannon: 23 October 2012 - 05:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: how to pause for the user input during execution of a loop

Posted 23 October 2012 - 03:58 AM

You shouldn't use loop like that. Look at Swing Timer if ever you need to loop like you do which I am not convince.

You shouldn't overload the update(Graphics g) method. What you are doing in your upadet() method should be done in the paint() method

Don't see why you call repaint() at more than one place

You shuld seriously look at some GUI example here or on the net, you really missed basic concepts.
:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1