<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ankit Virparia &#187; Thread</title>
	<atom:link href="http://ankit.co/tag/thread/feed" rel="self" type="application/rss+xml" />
	<link>http://ankit.co</link>
	<description>A Programmer, Designer and Trainer</description>
	<lastBuildDate>Sun, 11 May 2014 04:15:47 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>Color Chooser &#8211; multiple event handling</title>
		<link>http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=color-chooser-multiple-event-handling</link>
		<comments>http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling#comments</comments>
		<pubDate>Fri, 11 Jan 2013 01:21:28 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Event Handling]]></category>
		<category><![CDATA[ActionListener]]></category>
		<category><![CDATA[Applet]]></category>
		<category><![CDATA[Listener]]></category>
		<category><![CDATA[NullLayout]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=411</guid>
		<description><![CDATA[<p>This tutorial is on applying/handling multiple events in Applet based Application. What we are going to develop is a utility which selects the value of Red, Green and Blue in the range of 0 &#8211; 255 by using Scrollbar components. After setting up those three values, user can set the generated Color as Background. And [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling">Color Chooser &#8211; multiple event handling</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>This tutorial is on applying/handling multiple events in Applet based Application. What we are going to develop is a utility which selects the value of Red, Green and Blue in the range of 0 &#8211; 255 by using Scrollbar components. After setting up those three values, user can set the generated Color as Background. And once the color is set, we gonna make it black in 10 seconds by a smooth change in Color intensity.</p>
<p><strong>If you are not clear with the program aim then scroll down and see the output and read again! </strong></p>
<p><strong>Declaring and Defining Array of components:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;

public class ColorChooserApplet extends Applet 
{
	Button btn = new Button(&quot;Run&quot;);
	Label lbl[] = new Label[3];
	Scrollbar sc[] = new Scrollbar[3];
	TextField tf[] = new TextField[3];
	String[] slbl = {&quot;Red&quot;,&quot;Green&quot;,&quot;Blue&quot;};
	public void init()
	{
		for(int i=0;i&lt;3;i++)
		{
			lbl[i] = new Label(slbl[i]);	
			sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275);
			tf[i] = new  TextField(&quot;0&quot;);
			add(lbl[i]);
			add(sc[i]);
			add(tf[i]);
		}
		add(btn);
	}
}

</pre>
<hr/>
<strong>Applying null layout:</strong><br />
Java based desktop applications support various Layouts. Default Layout of Applet application is FlowLayout in which components are attached one after another as per the direction set by use(default:center). Now in case we need exact positioning of components, We can remove the layout by calling a method setLayout(null) and add the components. After setting null layout if we try to add components, they will not be visible in output. To make it visible we have to call a method named setBounds(int x_cord, int y_cord, int component_width, int component_height) for each component object.</p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ColorChooserApplet extends Applet 
{
	Button btn = new Button(&quot;Run&quot;);
	Label lbl[] = new Label[3];
	Scrollbar sc[] = new Scrollbar[3];
	TextField tf[] = new TextField[3];
	String[] slbl = {&quot;Red&quot;,&quot;Green&quot;,&quot;Blue&quot;};
	public void init()
	{
		setLayout(null);

		for(int i=0;i&lt;3;i++)
		{
			lbl[i] = new Label(slbl[i]);	
			sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275);
			tf[i] = new  TextField(&quot;0&quot;);
			add(lbl[i]);
			add(sc[i]);
			add(tf[i]);
			lbl[i].setBounds(20,(75*i)+50, 50,30);
			sc[i].setBounds(70,(75*i)+50, 350,30);
			tf[i].setBounds(450,(75*i)+50, 50, 30);

		}
		add(btn);
		btn.setBounds(200,400,100,50);
	}
	
}

</pre>
<hr/>
<p><strong>Code to sync Scrollbars and TextFields:</strong><br />
In our application, When user moves Scrollbar the current value of scroll bar is to be displayed/updated in text field and vice versa.</p>
<pre class="brush: java; title: ; notranslate">
        public void adjustmentValueChanged(AdjustmentEvent e) 
        {
		for(int i=0;i&lt;3;i++)
		{
			tf[i].setText(sc[i].getValue()+&quot;&quot;);	
		}
			
        }
	 
        public void keyReleased(KeyEvent e) 
        {
		System.out.println(&quot;Release:&quot;+tf[0].getText());

		for(int i=0;i&lt;3;i++)
		{
			sc[i].setValue(Integer.parseInt(tf[i].getText()));	
		}
        }
</pre>
<hr/>
<p><strong>Full Program:</strong></p>
<pre class="brush: java; title: ; notranslate">
import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class ColorChooserApplet extends Applet implements AdjustmentListener, KeyListener, ActionListener, Runnable
{
	int r=0,g=0,b=0;
	Thread t;
	Button btn = new Button(&quot;Run&quot;);
	Label lbl[] = new Label[3];
	Scrollbar sc[] = new Scrollbar[3];
	TextField tf[] = new TextField[3];
	String[] slbl = {&quot;Red&quot;,&quot;Green&quot;,&quot;Blue&quot;};
	public void init()
	{
		setLayout(null);
		
		for(int i=0;i&lt;3;i++)
		{
			lbl[i] = new Label(slbl[i]);	
			sc[i] = new Scrollbar(Scrollbar.HORIZONTAL, 0, 20, 0, 275);
			tf[i] = new  TextField(&quot;0&quot;);
			add(lbl[i]);
			add(sc[i]);
			add(tf[i]);
			lbl[i].setBounds(20,(75*i)+50, 50,30);
			sc[i].setBounds(70,(75*i)+50, 350,30);
			tf[i].setBounds(450,(75*i)+50, 50, 30);
			
			sc[i].addAdjustmentListener(this);
			tf[i].addKeyListener(this);
		}
		add(btn);
		btn.setBounds(200,400,100,50);
		btn.addActionListener(this);
	}
	public void adjustmentValueChanged(AdjustmentEvent e) 
	{
		for(int i=0;i&lt;3;i++)
		{
			tf[i].setText(sc[i].getValue()+&quot;&quot;);	
		}
			
	}
	public void	keyPressed(KeyEvent e)
	{
		System.out.println(&quot;Pressed:&quot;+tf[0].getText());
		
	} 
    public void	keyReleased(KeyEvent e) 
	{
		System.out.println(&quot;Release:&quot;+tf[0].getText());

		for(int i=0;i&lt;3;i++)
		{
			sc[i].setValue(Integer.parseInt(tf[i].getText()));	
		}
	}
    public void	keyTyped(KeyEvent e)
	{
		System.out.println(&quot;Typed:&quot;+tf[0].getText());

	} 
	public void actionPerformed(ActionEvent e)
	{
		r = sc[0].getValue();
		g = sc[1].getValue();
		b = sc[2].getValue();
		Color clr = new Color(r,g,b);	
		setBackground(clr);
		t = new Thread(this);
		t.start();
	}
	
	public void run()
	{
		try
		{
			for(int i=0;i&lt;10;i++)
			{
				t.sleep(1000);
				r = r - sc[0].getValue()/10;
				g = g - sc[1].getValue()/10;
				b = b - sc[2].getValue()/10;
				Color clr = new Color(r,g,b);	
				setBackground(clr);			
			}
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		
	}
}

/*
&lt;applet code=&quot;ColorChooserApplet&quot; width=&quot;550&quot; height=&quot;550&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling">Color Chooser &#8211; multiple event handling</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/event-handling/color-chooser-multiple-event-handling/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Thread?</title>
		<link>http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-thread</link>
		<comments>http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread#comments</comments>
		<pubDate>Thu, 03 Jan 2013 09:19:38 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Multithreading]]></category>
		<category><![CDATA[GTU Core Java]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=66</guid>
		<description><![CDATA[<p>Thread is a process in execution which can be scheduled or managed. A normal Java program by default has a main thread which start executing public static void main(String[] s) method. When we launch our program by java.exe or any other launcher it is invoked. But here we are suppose to control or maintain the [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread">What is Thread?</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Thread is a process in execution which can be scheduled or managed. A normal Java program by default has a main thread which start executing public static void main(String[] s) method. When we launch our program by java.exe or any other launcher it is invoked. But here we are suppose to control or maintain the single threaded application.</p>
<p>To schedule/manage any thread we must have object of Thread class. As such main thread is not created by developer we have to get the object from Runtime by calling a simple method.</p>
<pre class="brush: java; title: ; notranslate">
class mainThread
{
	public static void main(String[] s)
	{
		Thread t = Thread.currentThread();
	}
}
</pre>
<p>Now object &#8220;t&#8221; can be used to call any method of Thread class. Which will ultimately control the main Thread. e.g.</p>
<pre class="brush: java; title: ; notranslate">
class mainThread
{
	public static void main(String[] s) throws InterruptedException
	{
		Thread t = Thread.currentThread();
		System.out.println(&quot;Thread Started&quot;);
		t.sleep(1000);
		System.out.println(&quot;After Sleep&quot;);
	}
}
</pre>
<p>On calling t.sleep(1000) method the execution will be paused for 1second and then it will execute further. Method sleep(int) throws an InterruptedException which can be handled by applying &#8220;throws InterruptedException&#8221; clause.</p>
<p style="text-align: center;">If we really want to track the execution, We can print the Date object as below:</p>
<p><a class="highslide"  onclick="return hs.expand(this)" href="../../../wp-content/uploads/2013/01/DatePrint_SingleThread.bmp" rel="attachment wp-att-76"><img class="size-full wp-image-76 aligncenter" alt="Single Thread Date Printing" src="../../../wp-content/uploads/2013/01/DatePrint_SingleThread.bmp" width="470" height="238" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread">What is Thread?</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ankit.co/tutorials/java-tutorials/multithreading/what-is-thread/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
