<?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; Applet</title>
	<atom:link href="http://ankit.co/tag/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>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 an Applet?</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-an-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet#comments</comments>
		<pubDate>Thu, 03 Jan 2013 17:38:25 +0000</pubDate>
		<dc:creator><![CDATA[Sonali Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[What is]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=83</guid>
		<description><![CDATA[<p>An applet is a small and portable application that runs under the restricted scope provided by JRE. It has dual compatibility as it can be executed on a web browser and also by applet viewer. JVM is required on the client machine to run an applet program. Five steps to be kept in mind when [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet">What is an Applet?</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>An applet is a small and portable application that runs under the restricted scope provided by JRE. It has dual compatibility as it can be executed on a web browser and also by applet viewer. JVM is required on the client machine to run an applet program.</p>
<p><strong>Five steps to be kept in mind when writing an applet program:</strong></p>
<p>1. import java.applet.*  (Need Applet class from java.applet package)</p>
<p>2. create a public class (Class name and File name must be same in case of public class)</p>
<p>3. extend the class from the base class Applet </p>
<p>4. do not write main method </p>
<p>5. write the applet life cycle methods (i.e.  init, start, stop, destroy, paint)</p>
<p><strong>File name:</strong> abc.java</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;

public class abc extends Applet
{
}
</pre>
<p><strong>Steps to compile and Run Applet program:</strong></p>
<ol>
<li>Compile your java code using javac.exe on console/command prompt.</li>
<li>To run the generated class file, we have to write &lt;applet&gt; tag in an additional file(e.g. run.txt). Later on that file name is to be passed as an argument to appletviewer.exe as shown below</li>
</ol>
<p><strong>File name:</strong> run.txt</p>
<pre class="brush: xml; title: ; notranslate">
&lt;applet code=&quot;abc&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/applet&gt;
</pre>
<p><strong>Commands to be executed:</strong> run.txt</p>
<pre class="brush: xml; title: ; notranslate">
c:&gt; javac abc.java
c:&gt; appletviewer run.txt
</pre>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/what-is-an-applet">What is an 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/what-is-an-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Difference between Applet and Application</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/difference-between-applet-and-application?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=difference-between-applet-and-application</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/difference-between-applet-and-application#comments</comments>
		<pubDate>Sat, 05 Jan 2013 18:10:57 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[Application]]></category>
		<category><![CDATA[Difference]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=161</guid>
		<description><![CDATA[<p>Applet Application Small Program Large Program Used to run a program on client Browser Can be executed on stand alone computer system Applet is portable and can be executed by any JAVA supported browser. Need JDK, JRE, JVM installed on client machine. Applet applications are executed in a Restricted Environment Application can access all the [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/difference-between-applet-and-application">Difference between Applet and Application</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<table width="500px" cellpadding="7px" style="width:500px;" border="1">
<tr>
<td width="250px" class="trheading">Applet</td>
<td width="250px" class="trheading">Application</td>
</tr>
<tr>
<td>Small Program </td>
<td>Large Program</td>
</tr>
<tr>
<td>Used to run a program on client Browser</td>
<td>Can be executed on stand alone computer system</td>
</tr>
<tr>
<td>Applet is portable and can be executed by any JAVA supported browser.</td>
<td>Need JDK, JRE, JVM installed on client machine.</td>
</tr>
<tr>
<td>Applet applications are executed in a Restricted Environment</td>
<td>Application can access all the resources of the computer</td>
</tr>
<tr>
<td>Applets are created by extending the java.applet.Applet</td>
<td>Applications are created by writing public static void main(String[] s) method.</td>
</tr>
<tr>
<td>Applet application has 5 methods which will be automatically invoked on occurance of specific event</td>
<td>Application has a single start point which is main method</td>
</tr>
<tr>
<td>
<p>Example:</p>
<pre>
import java.awt.*;
import java.applet.*;

public class Myclass extends Applet 
{
    public void init() { }
    public void start() { }
    public void stop() {}
    public void destroy() {}
    public void paint(Graphics g) {}
}
</pre>
</p>
</td>
<td>
<pre>
public class MyClass 
{
public static void main(String args[]) {}
}    
</pre>
</td>
</tr>
</table>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/difference-between-applet-and-application">Difference between Applet and Application</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/difference-between-applet-and-application/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Applet Life Cycle</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/applet-life-cycle?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=applet-life-cycle</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/applet-life-cycle#comments</comments>
		<pubDate>Thu, 03 Jan 2013 18:14:29 +0000</pubDate>
		<dc:creator><![CDATA[Sonali Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[Life Cycle]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=87</guid>
		<description><![CDATA[<p>Applet life cycle consists of 5 methods which will be automatically invoked by JRE when some action is done on the application/browser. List of Methods: public void init() public void start() public void stop() public void destroy() public void paint(Graphics g) Which method will be invoked when? init() Invoked only once Invoked when the application [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/applet-life-cycle">Applet Life Cycle</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Applet life cycle consists of 5 methods which will be automatically invoked by JRE when some action is done on the application/browser.</p>
<p><strong>List of Methods:</strong></p>
<ol>
<li>public void init()</li>
<li>public void start()</li>
<li>public void stop()</li>
<li>public void destroy()</li>
<li>public void paint(Graphics g)</li>
</ol>
<p><strong>Which method will be invoked when?</strong></p>
<p><strong>init()</strong></p>
<ul>
<li>Invoked only once</li>
<li>Invoked when the application is launched using appletviewer or browser</li>
<li>Should include code for component defination, Object creation, Layout settings, Basic look and feel</li>
</ul>
<p><strong>start()</strong></p>
<ul>
<li>Invoked when application is maximized</li>
<li>Default application state is Maximized window so this will also be invoked on launching</li>
<li>Should include code for starting/resuming thread, starting/resuming Graphical User Interface, etc&#8230;</li>
</ul>
<p><strong>stop()</strong></p>
<ul>
<li>Invoked when application is minimized</li>
<li>invoked also when the window is terminated while its in maximizes state.</li>
<li>Should include code for pausing/stopping thread, pausing/stopping Graphical User Interface, etc&#8230;</li>
</ul>
<p><strong>destroy()</strong></p>
<ul>
<li>Invoked when application is about to terminate</li>
<li>invoked when we close the application.</li>
<li>Should include code for connection closing, file closing, etc&#8230;</li>
</ul>
<p><strong>paint()</strong></p>
<ul>
<li>Invoked when application is to be refreshed in terms of GUI</li>
<li>invoked when we start, move or resize applet.</li>
<li>Should include code for GUI design</li>
</ul>
<p><strong>Some method calling scenarios:</strong></p>
<p>On <strong>Launching</strong> an Applet application:  <strong>init(), start(), paint()</strong></p>
<p>On <strong>Minimizing</strong> an Applet application:  <strong>stop()</strong></p>
<p>On <strong>Maximizing</strong> an Applet application: <strong>start(), paint()</strong></p>
<p>On <strong>Moving/Re-sizing</strong> an Applet application: <strong>paint(), paint(), &#8230;, paint()</strong></p>
<p>On <strong>Terminating</strong> an Applet application: <strong>stop(), destroy()</strong></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/applet-life-cycle">Applet Life Cycle</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/applet-life-cycle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shapes and Colors in Applet</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/shapes-and-colors-in-applet?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=shapes-and-colors-in-applet</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/shapes-and-colors-in-applet#comments</comments>
		<pubDate>Sat, 05 Jan 2013 12:47:01 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[Color]]></category>
		<category><![CDATA[Polygon]]></category>
		<category><![CDATA[Shapes]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=134</guid>
		<description><![CDATA[<p>Applet has a Life Cycle method public void paint(Graphics g) which can be used to design/draw shapes in Applet area. Like: Line, Circle, Oval, Arc, Text, Polygon, etc&#8230; Writing a String on Applet File Name: WriteString.java Output: Changing Text Font We need to invoke a method named: setFont(Font f) to change the Font size and [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/shapes-and-colors-in-applet">Shapes and Colors in Applet</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Applet has a Life Cycle method <strong>public void paint(Graphics g)</strong> which can be used to design/draw shapes in Applet area. Like: Line, Circle, Oval, Arc, Text, Polygon, etc&#8230;</p>
<h2>Writing a String on Applet</h2>
<p><strong>File Name:</strong> WriteString.java</p>
<pre class="brush: plain; title: ; notranslate"> 
import java.applet.*;
import java.awt.*;

public class WriteString extends Applet
{
	public void paint(Graphics g)
	{
		g.drawString(&quot;Ankit Virparia - www.ankit.co&quot;, 100, 100);	
	}	
}
/*
&lt;applet code=&quot;WriteString&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><br />
<img src="http://ankit.co/wp-content/uploads/2013/01/q1.jpg" alt="q1" width="500" height="200" class="aligncenter size-full wp-image-3551" /></p>
<h2>Changing Text Font</h2>
<p>We need to invoke a method named: <strong>setFont(Font f)</strong> to change the Font size and style. To create a Font object we have used a constructor which takes following arguments. 1) String FontName, 2) Font Style as int. Here, We have used Font.BOLD whereby BOLD is the static int which belongs to java.awt.Font class. 3) Font size as int.</p>
<p>To change Text Color we have to invoke a method named: <strong>setColor(Color c)</strong> of Graphics class. Color object can be passed in 2 ways<br />
1. By Creating Color object from the scratch like <strong>Color clr = new Color(255,0,0);</strong> Here, Constructor takes red, green, blue int values which can vary from 0 to 255.<br />
2. By Passing static color object Like Color.RED or Color.red</p>
<p><strong>File Name:</strong> FontString.java</p>
<pre class="brush: plain; title: ; notranslate"> 
import java.applet.*;
import java.awt.*;

public class FontString extends Applet
{
	public void paint(Graphics g)
	{
		Font f = new Font(&quot;Arial&quot;, Font.BOLD, 36);
		g.setFont(f);
		g.setColor(Color.GREEN);
		g.drawString(&quot;Ankit Virparia, www.ankit.co&quot;, 10, 100); 	
	}	
}
/*
&lt;applet code=&quot;FontString&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><br />
<a href="http://ankit.co/wp-content/uploads/2013/01/q2.jpg"><img src="http://ankit.co/wp-content/uploads/2013/01/q2.jpg" alt="q2" width="500" height="200" class="aligncenter size-full wp-image-3550" /></a></p>
<h2>Drawing Shapes</h2>
<p><strong>File Name:</strong> ShapeApplet.java</p>
<pre class="brush: plain; title: ; notranslate"> 
import java.applet.*;
import java.awt.*;

public class ShapeApplet extends Applet
{
	public void paint(Graphics g)
	{
		setBackground(Color.BLACK);    //setting background as BLACK
		g.setColor(new Color(255,255,255));
		g.drawLine(10, 10, 400, 10);  //g.drawLine(x1, y1, x2, y2);   	
		g.setColor(Color.red);
		g.drawRect(30,30,200,100);    //g.drawRect(x, y, w, h);
		g.setColor(new Color(80,10,124));
		g.drawOval(30,30,200,100);    //g.drawOval(x, y, w, h);
		g.setColor(Color.BLUE);
		g.drawArc(100, 20, 300, 300, 0, 90);    //g.drawArc(x, y, w, h, Starting Angle, Further Angle);
	}	
}
/*
&lt;applet code=&quot;ShapeApplet&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><br />
<img src="http://ankit.co/wp-content/uploads/2013/01/q3.jpg" alt="q3" width="500" height="200" class="aligncenter size-full wp-image-3549" /></p>
<h2>Filling Shapes</h2>
<p><strong>File Name:</strong> ShapeFillApplet.java</p>
<pre class="brush: plain; title: ; notranslate"> 
import java.applet.*;
import java.awt.*;

public class ShapeFillApplet extends Applet
{
	public void paint(Graphics g)
	{
		setBackground(Color.BLACK);    //setting background as BLACK
		g.setColor(new Color(255,255,255));
		g.drawLine(10, 10, 400, 10);  //g.drawLine(x1, y1, x2, y2);   	
		g.setColor(Color.red);
		g.fillRect(30,30,200,100);    //g.fillRect(x, y, w, h);
		g.setColor(new Color(80,10,124));
		g.fillOval(30,30,200,100);    //g.fillOval(x, y, w, h);
		g.setColor(Color.BLUE);
		g.fillArc(100, 20, 300, 300, 0, 90);    //g.fillArc(x, y, w, h, Starting Angle, Further Angle);
	}	
}
/*
&lt;applet code=&quot;ShapeFillApplet&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><br />
<img src="http://ankit.co/wp-content/uploads/2013/01/q4.jpg" alt="q4" width="500" height="200" class="aligncenter size-full wp-image-3548" /></p>
<h2>Drawing Polygon</h2>
<p><strong>File Name:</strong> DrawingPolygons.java</p>
<pre class="brush: plain; title: ; notranslate"> 
import java.awt.*;
import java.applet.*;

public class DrawingPolygons extends Applet
{
  public void paint(Graphics g)
  {
    int x[] = { 70, 150, 190, 80, 100 };
    int y[] = { 80, 110, 160, 190, 100 };
    g.drawPolygon (x, y, 5);
 
    int x1[] = { 210, 280, 330, 210, 230 };
    int y1[] = { 70, 110, 160, 190, 100 };
    g.fillPolygon (x1, y1, 5);
  }
}
/*
&lt;applet code=&quot;DrawingPolygons&quot; width=&quot;500&quot; height=&quot;200&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><br />
<a href="http://ankit.co/wp-content/uploads/2013/01/q5.jpg"><img src="http://ankit.co/wp-content/uploads/2013/01/q5.jpg" alt="q5" width="500" height="200" class="aligncenter size-full wp-image-3547" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/shapes-and-colors-in-applet">Shapes and Colors in 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/shapes-and-colors-in-applet/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Colorful Equal Bars</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/colorful-equal-bars?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=colorful-equal-bars</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/colorful-equal-bars#comments</comments>
		<pubDate>Sun, 06 Jan 2013 05:26:03 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[GTU Question]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=195</guid>
		<description><![CDATA[<p>Definition: Write an applet that draws four Vertical bars 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. Code: Output:</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/colorful-equal-bars">Colorful Equal Bars</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 Vertical bars 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>
<p>Code:</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;
import java.awt.*;

public class equalBars extends Applet
{
	Color[] clr = {Color.red, Color.green, Color.pink, Color.blue};
	int cnt = 0;
	public void init()
	{
		setBackground(Color.black);	
	}
	
	public void paint(Graphics g)
	{
		int w = getWidth();
		int h = getHeight();
		
		for(int i=0;i&lt;4;i++)
		{
			g.setColor(clr[i]);
			g.fillRect(w/4*i,0,w/4,h);
		}
	}	
	
}

/*
&lt;applet code=&quot;equalBars&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><br />
<a href="http://ankit.co/wp-content/uploads/2013/01/p6.jpg"><img src="http://ankit.co/wp-content/uploads/2013/01/p6.jpg" alt="p6" width="500" height="500" class="aligncenter size-full wp-image-3545" /></a></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/colorful-equal-bars">Colorful Equal Bars</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/colorful-equal-bars/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>Cartoon Character &#8211; Moving Hands</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/cartoon-character-moving-hands?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cartoon-character-moving-hands</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/cartoon-character-moving-hands#comments</comments>
		<pubDate>Sun, 06 Jan 2013 04:29:28 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[cartoon]]></category>
		<category><![CDATA[Method communication]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=184</guid>
		<description><![CDATA[<p>Our aim of this tutorial is to learn the communication between applet life cycle methods. What I mean by communication is: One of the method may generate some values which is used by other method/methods. Here we want a scenario that when user changes the Browser tab or minimize-maximize the appletviewer, Position of the hands(of [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/cartoon-character-moving-hands">Cartoon Character &#8211; Moving Hands</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Our aim of this tutorial is to learn the communication between applet life cycle methods. What I mean by communication is: One of the method may generate some values which is used by other method/methods. Here we want a scenario that when user changes the Browser tab or minimize-maximize the appletviewer, Position of the hands(of cartoon character!) should be toggled. And this will create an view like a cartoon character is doing exercise.</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;
import java.awt.*;

public class toonApplet extends Applet
{
	int y = 200;
	public void init()
	{	
		setBackground(Color.YELLOW);
	}
	
	public void stop()	
	{
		if(y==200)y = 50;
		else y = 200;
	}
	
	public void paint(Graphics g)
	{
		g.drawOval(200,50,100,100);
		g.drawRect(50,150,400,50);

		g.drawRect(50,y,50,100);
		g.drawRect(400,y,50,100);

		g.drawRect(150,200,200,300);

		g.drawRect(150,500,50,100);
		g.drawRect(300,500,50,100);
	}
}
/*
&lt;applet code=&quot;toonApplet&quot; width=&quot;500&quot; height=&quot;700&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><img src="http://ankit.co/wp-content/uploads/2013/01/p2.jpg" alt="p2" width="437" height="518" class="aligncenter size-full wp-image-3533" /></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/cartoon-character-moving-hands">Cartoon Character &#8211; Moving Hands</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/cartoon-character-moving-hands/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic AWT components</title>
		<link>http://ankit.co/tutorials/java-tutorials/applet/basic-awt-components?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=basic-awt-components</link>
		<comments>http://ankit.co/tutorials/java-tutorials/applet/basic-awt-components#comments</comments>
		<pubDate>Sun, 06 Jan 2013 06:44:14 +0000</pubDate>
		<dc:creator><![CDATA[Ankit Virparia]]></dc:creator>
				<category><![CDATA[Applet]]></category>
		<category><![CDATA[AWT]]></category>
		<category><![CDATA[Components]]></category>

		<guid isPermaLink="false">http://ankit.co/?p=200</guid>
		<description><![CDATA[<p>AWT: Abstract Window Toolkit Its&#8217;s built-in platform-independent windowing, graphics, and user-interface widget toolkit. The java.awt is now part of the Java Foundation Classes (JFC). Which can be used for a graphical user interface (GUI) for a Java program. AWT consists of many classes which can be said: Components. A component is a class which can [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/basic-awt-components">Basic AWT components</a> appeared first on <a rel="nofollow" href="http://ankit.co">Ankit Virparia</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><strong>AWT:</strong> Abstract Window Toolkit</p>
<p>Its&#8217;s built-in platform-independent windowing, graphics, and user-interface widget toolkit. The java.awt is now part of the Java Foundation Classes (JFC). Which can be used for a graphical user interface (GUI) for a Java program. AWT consists of many classes which can be said: Components. A component is a class which can create a event specific GUI like TextField, Button, Label, Checkbox,  TextArea, etc&#8230;</p>
<p>To add any component in Applet area, We have to call a add(Component c) method of Applet class. Which is discussed briefly in the below example.</p>
<pre class="brush: java; title: ; notranslate">
import java.applet.*;
import java.awt.*;

public class componentDemo extends Applet
{
	Button b;
	Label l;
	TextField tf;
	Scrollbar sb;
	TextArea ta;
	Checkbox c1,c2,c3;
	CheckboxGroup cbg = new CheckboxGroup();
	public void init()
	{
		tf = new TextField(&quot;Ankit&quot;,20);
		l = new Label(&quot;First Name:&quot;);
		b = new Button(&quot;Okay!&quot;);
		ta = new TextArea(7,40);
		sb = new Scrollbar(Scrollbar.VERTICAL, 100, 20, 0, 200);
		sb.setPreferredSize(new Dimension(30,200));
		c1 = new Checkbox(&quot;Cricket&quot;,true,cbg);
		c2 = new Checkbox(&quot;Hockey&quot;,false,cbg);
		c3 = new Checkbox(&quot;Football&quot;,false,cbg);
		add(l);
		add(tf);
		add(b);
		add(sb);
		add(ta);
		add(c1);
		add(c2);
		add(c3);
	}	
}

/*
&lt;applet code=&quot;componentDemo&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/applet&gt;
*/
</pre>
<p><strong>Output:</strong><br />
<img src="http://ankit.co/wp-content/uploads/2013/01/p1.jpg" alt="p1" width="527" height="518" class="aligncenter size-full wp-image-3530" /></p>
<p>The post <a rel="nofollow" href="http://ankit.co/tutorials/java-tutorials/applet/basic-awt-components">Basic AWT components</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/basic-awt-components/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
