<?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 + Applet</title>
	<atom:link href="http://ankit.co/tag/thread-applet/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>Banner Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/banner-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=banner-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/banner-applet#comments</comments>
		<pubDate>Sun, 06 Jan 2013 04:02:24 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[Banner]]></category>
		<category><![CDATA[Thread + Applet]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=173</guid>
		<description><![CDATA[<p>Banner Applet is an applet application which continuously rotates the text chars after given time interval. For Example, This is Demo. his is Demo.T is is Demo.Th &#8230; How to write program for this? We need applet first. Will follow the applet life cycle We want to draw a String on applet area. Which can [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/banner-applet">Banner Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Banner Applet is an applet application which continuously rotates the text chars after given time interval. </p>
<p><strong>For Example,</strong></p>
<p>This is Demo.<br />
his is Demo.T<br />
is is Demo.Th<br />
&#8230;</p>
<h2>How to write program for this?</h2>
<ol>
<li>We need applet first. Will follow the applet life cycle
<pre class="brush: java; title: ; notranslate">
import java.applet.*;
import java.awt.*;

public class bannerDemo extends Applet
{
	public void init()
	{
	}
	public void paint(Graphics g)
	{
	}		
}
/*
&lt;applet code=&quot;bannerDemo&quot; width=&quot;500&quot; height=&quot;150&quot;&gt;&lt;/applet&gt;
*/
</pre>
</li>
<li>We want to draw a String on applet area. Which can be done by calling drawString() method of Graphics class inside paint() method.
<pre class="brush: java; title: ; notranslate">
	public void paint(Graphics g)
	{
		Font f = new Font(&quot;Arial&quot;,Font.BOLD,36);
		g.setFont(f);
		g.drawString(&quot;Ankit Virparia (ankit.co)&quot;,50,75);
	}	
</pre>
</li>
<li>We want continuous change in the text which we have printed so, Will go for multithreading.
<pre class="brush: java; title: ; notranslate">
public class bannerDemo extends Applet implements Runnable
{
	Thread t;
        public void run()
	{}
}
</pre>
</li>
<li>Here one extra thread will be given a responsibility to update the text given (which is stored in a global variable &#8211; I mean common between paint() and run() methods)
<pre class="brush: java; title: ; notranslate">
public class bannerDemo extends Applet implements Runnable
{
	Thread t;
	String msg = &quot;Ankit Virparia (ankit.co)&quot;;
	
	public void run()
	{
		try
		{
			while(true)
			{
				t.sleep(1000);
				char c = msg.charAt(0);
				String tmp = msg.substring(1,msg.length());
				msg = tmp + c;
				repaint();
			}
		}
		catch(Exception e)
		{
			System.out.println(e);	
		}
	}
		
	public void paint(Graphics g)
	{
		Font f = new Font(&quot;Arial&quot;,Font.BOLD,36);
		g.setFont(f);
		g.drawString(msg,50,75);
	}	
	
}
</pre>
</li>
<li>We need to start the thread in the beginning. Which can be written in init() method.
<pre class="brush: java; title: ; notranslate">
	Thread t;
	public void init()
	{
		t = new Thread(this);
		t.start();
	}
</pre>
</li>
<li>paint() is invoked only when a user does GUI related change. But here in our case we want to invoke paint() at the interval of 1 second.
<pre class="brush: java; title: ; notranslate">
</pre>
</li>
<li>We are ready to go!</li>
</ol>
<hr/>
<h3>Program for Banner Applet</h3>
<p><strong>File Name:</strong>bannerDemo.java</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;
import java.awt.*;

public class bannerDemo extends Applet implements Runnable
{
	Thread t;
	String msg = &quot;Ankit Virparia (ankit.co)&quot;;
	public void init()
	{
		t = new Thread(this);
		t.start();
	}
	
	public void run()
	{
		try
		{
			while(true)
			{
				t.sleep(1000);
				char c = msg.charAt(0);
				String tmp = msg.substring(1,msg.length());
				msg = tmp + c;
				repaint();
			}
		}
		catch(Exception e)
		{
			System.out.println(e);	
		}
	}
		
	public void paint(Graphics g)
	{
		Font f = new Font(&quot;Arial&quot;,Font.BOLD,36);
		g.setFont(f);
		g.drawString(msg,50,75);
	}	
	
}

/*
&lt;applet code=&quot;bannerDemo&quot; width=&quot;500&quot; height=&quot;150&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><img src="http://ankit.co/wp-content/uploads/2013/01/p5.jpg" alt="p5" width="500" height="104" class="aligncenter size-full wp-image-3542" /></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/banner-applet">Banner Applet</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/applet/banner-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rotating Colorful Squares</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-squares?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rotating-colorful-squares</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-squares#comments</comments>
		<pubDate>Sun, 06 Jan 2013 05:15:42 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[Method communication]]></category>
		<category><![CDATA[Multithreading]]></category>
		<category><![CDATA[Thread + Applet]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=191</guid>
		<description><![CDATA[<p>Definition: Write an applet that draws four squares of equal size &#038; of different colors such that they cover up the whole applet area. The applet should operate correctly even if it is resized. Output:</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-squares">Rotating Colorful Squares</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<h3>Definition:</h3>
<p> Write an applet that draws four squares of equal size &#038; of different colors such that they cover up the whole applet area. The applet should operate correctly even if it is resized.</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;
import java.awt.*;

public class rotateSquares extends Applet implements Runnable
{
	Color[] clr = {Color.red, Color.green, Color.pink, Color.blue};
	int cnt = 0;
	Thread t;
	public void init()
	{
		t = new Thread(this);
		t.start();
		setBackground(Color.black);	
	}
	
	public void run()
	{
		try
		{
			while(true)
			{
				
				repaint();
				t.sleep(1000);
				cnt++;	
				if(cnt&gt;3){cnt=0;}
			}
		}
		catch(Exception e)
		{
			System.out.println(e);	
		}
	}
		
	public void paint(Graphics g)
	{
		int w = getWidth();
		int h = getHeight();
		
		int tmp = cnt;
		for(int i=0;i&lt;4;i++)
		{
		 g.setColor(clr[tmp++]);
		 if(tmp&gt;3)tmp=0;
		 
		 if(i==0) g.fillRect(0,0,w/2,h/2);
		 else if(i==1) g.fillRect(w/2,0,w/2,h/2);
		 else if(i==2) g.fillRect(w/2,h/2,w/2,h/2);
		 else if(i==3) g.fillRect(0,h/2,w/2,h/2); 
		}
	}	
	
}

/*
&lt;applet code=&quot;rotateSquares&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><img src="http://ankit.co/wp-content/uploads/2013/01/p4.jpg" alt="p4" width="500" height="500" class="aligncenter size-full wp-image-3539" /></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-squares">Rotating Colorful Squares</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/applet/rotating-colorful-squares/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rotating Colorful Disk</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-disk?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rotating-colorful-disk</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-disk#comments</comments>
		<pubDate>Sun, 06 Jan 2013 04:57:24 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[Method communication]]></category>
		<category><![CDATA[Multithreading]]></category>
		<category><![CDATA[Thread + Applet]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=189</guid>
		<description><![CDATA[<p>Once again, One more tutorial on Method communication. But here we need communication between Applet Life cycle methods and Thread-> run() method. To draw a disc, We have used 4 Arcs of 90 degrees each. For display color rotation, We have used Color[]. Rest everything is self explanatory. You may leave a comment in case [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-disk">Rotating Colorful Disk</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Once again, One more tutorial on Method communication. But here we need communication between Applet Life cycle methods and Thread-> run() method. To draw a disc, We have used 4 Arcs of 90 degrees each. For display color rotation, We have used Color[]. Rest everything is self explanatory. You may leave a comment in case you face any difficulty.</p>
<p><strong>File Name:</strong> rotateArc.java</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;
import java.awt.*;

public class rotateArc extends Applet implements Runnable
{
	Color[] clr = {Color.red, Color.green, Color.pink, Color.blue};
	int cnt = 0;
	Thread t;
	public void init()
	{
		t = new Thread(this);
		t.start();
		setBackground(Color.black);	
	}
	
	public void run()
	{
		try
		{
			while(true)
			{
				
				repaint();
				t.sleep(1000);
				cnt++;	
				if(cnt&gt;3){cnt=0;}
			}
		}
		catch(Exception e)
		{
			System.out.println(e);	
		}
	}
		
	public void paint(Graphics g)
	{
		int tmp = cnt;
		for(int i=0;i&lt;4;i++)
		{
		 g.setColor(clr[tmp++]);
		 if(tmp&gt;3)tmp=0;
		 g.fillArc(0,0,500,500,90*i,90);
		}
	}	
	
}

/*
&lt;applet code=&quot;rotateArc&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><img src="http://ankit.co/wp-content/uploads/2013/01/p3.jpg" alt="p3" width="500" height="500" class="aligncenter size-full wp-image-3536" /></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/rotating-colorful-disk">Rotating Colorful Disk</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/applet/rotating-colorful-disk/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
