<?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>Coding Friends &#187; applet</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/applet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Mon, 06 Sep 2010 20:18:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Java applet &#8211; hello world</title>
		<link>http://www.codingfriends.com/index.php/2010/01/30/java-applet-hello-world/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=java-applet-hello-world</link>
		<comments>http://www.codingfriends.com/index.php/2010/01/30/java-applet-hello-world/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 13:00:28 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[applet]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=603</guid>
		<description><![CDATA[A java applet is a web object that allows you to do some funky things within a web page, you could even have a game on the screen with a java applet. But as normal you have to start from the start as such, the most used starts are the classic &#8220;hello world&#8221; example. In [...]]]></description>
			<content:encoded><![CDATA[<p>A java applet is a web object that allows you to do some funky things within a web page, you could even have a game on the screen with a java applet.  But as normal you have to start from the start as such, the most used starts are the classic &#8220;hello world&#8221; example.</p>
<p>In a java applet, it is basically a class called Applet that you extend from and this has all of base code inside it so you only need to implement the virtual functions </p>
<ol>
<li>init</li>
<li>start</li>
<li>paint</li>
<li>destory</li>
<li>stop</li>
</ol>
<p>And these are the basic functions that allow you to call other classes of your choice.  The init &#8211; (initialize), setups up the environment of your choice, start is where the application (GUI interface) will run within, paint is what is painted to the applet screen, destory is when you destory the applet you may need to clean up some code, stop is when the application/applet is stopped.</p>
<p>The main basic class will extend from a applet (java.applet.Applet) so you will need to import that class library</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.applet.Applet</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// basic class extends the applet</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">applet</span>.<span style="color: #003399;">Applet</span> <span style="color: #009900;">&#123;</span></pre></div></div>

<p>and then implement the functions start,init,stop,destory and paint if required.  Below is more code that will resize the basic applet object and display (paint) the words Hello World using the Graphics class object <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html#drawString%28java.lang.String,%20int,%20int%29">drawString</a> method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Graphics</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// need this for the Graphics part of the paint function</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.applet.Applet</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// to extend from.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">applet</span>.<span style="color: #003399;">Applet</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// resize the screen </span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	resize<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">150</span>,<span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// draw with the Graphics class object a string on the canvas, point 50, 25</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #003399;">Graphics</span> graphicsObj<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	graphicsObj.<span style="color: #006633;">drawString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span>, <span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Is you save that as &#8220;HelloWorld.java&#8221; and then you need to compile it into a class file (which is a common object file that java can read to run within the virtual machine).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">javac HelloWord.java</pre></div></div>

<p>which will output the HelloWorld.class file, which you will need to include into the html code applet tag, below is the HTML code</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;HTML&gt;
&lt;head&gt;
&lt;title&gt;A hello world&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Test output of the program
&lt;applet code=&quot;HelloWorld.class&quot; width=150 height=25&gt;
&lt;/applet&gt;
&lt;/body&gt;
&lt;/HTML&gt;</pre></div></div>

<p>The output would be</p>
<p>Test output of the program <applet code="HelloWorld.class" codebase="/Appletsexamples/"  archive="HelloWorld.jar" width="150" height="25"></applet></p>
<p>Note: If you look at the source code of this page, it will say</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;applet code=&quot;HelloWorld.class&quot; codebase=&quot;/Appletsexamples/&quot;  archive=&quot;HelloWorld.jar&quot; width=&quot;150&quot; height=&quot;25&quot;&gt;&lt;/applet&gt;</pre></div></div>

<p>I had to add in the codebase and archive because of the hosting, to create a jar file you just need to do</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">jar cf <span style="color: #000000; font-weight: bold;">&lt;</span>tarname<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> name<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
jar cf HelloWorld.jar HelloWorld.class</pre></div></div>

<p>The jar file is basically a zipped archive.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/01/30/java-applet-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
