create account

Programming - Java Web Applets by drifter1

View this thread on: hive.blogpeakd.comecency.com
· @drifter1 · (edited)
$15.06
Programming - Java Web Applets
<html>
<p><code><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;<br>
</code></p>
<p><img src="https://www3.ntu.edu.sg/home/ehchua/programming/java/images/Applet_HelloWorld.gif" width="393" height="284"/></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;I'm back! :) And here we are again with another Tutorial. I wanted to upload the Solution, but I need more time to check some things inside the Code that doesn't seem to work right or are to complicated! Today, we will talk about <strong>Java Applets for Browsers</strong>. I will show you how to write the Code in Java and how you can call such an Applet using HTML inside of a Website. So, without further do, let's get started!</p>
<h1>Differences to normal Java Coding:</h1>
<p>There are some major differences between Applets and Standalone Java Applications.&nbsp;</p>
<p>Those are:</p>
<ul>
  <li>An Applet extends the java.applet.Applet class</li>
  <li>There is no main method invoked neither defined</li>
  <li>Applets are designed to be run inside of an HTML Page</li>
</ul>
<p>An Applet has an <strong>Lifecycle</strong> and we use the following methods to do stuff with it:</p>
<ul>
  <li><strong>init()</strong>, for initialization purposes, called first</li>
  <li><strong>start()</strong>, that is called automatically after the init() method and whenever the user returns to the page containing the applet after gone off to other pages</li>
  <li><strong>stop()</strong>, that is called automatically when the user moves off the page in which the applet sits</li>
  <li><strong>destroy()</strong>, that is called when the browser shuts down normally</li>
  <li><strong>paint()</strong>, that is invoked after the start() method and everytime the applet wants to repaint itself</li>
</ul>
<p>&nbsp;&nbsp;&nbsp;&nbsp;We don't have to write Code for any of the methods. The init method can be changed to initialize values, create or setup GUI Elements and so on... The most important one is paint that let's us draw our Applet.</p>
<h1>Basic Applet Structure:</h1>
<p>&nbsp;&nbsp;&nbsp;&nbsp;An basic Applet needs to extend the Applet (awt) or JApplet (swing) class and contains at least 2 methods called init() and paint().</p>
<pre><code>import java.applet.Applet; // import javax.swing.JApplet</code></pre>
<pre><code>import java.awt.Graphics;</code></pre>
<pre><code>public class MyApplet extends Applet { // or JApplet&nbsp;</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;public void init() {</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// INITIALIZATIONS</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;}</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;public void paint(Graphics g) {</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Draw the Applet using the Graphics g Object</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;}</code></pre>
<pre><code>}</code></pre>
<p>&nbsp;&nbsp;&nbsp;&nbsp;Because, our Applet is an GUI Component/Element we can do stuff like resizing, setting Color, adding Frames, Panels, Buttons etc. the same way we did it with other GUI Elements. Using the draw methods that the Graphics Object inside of the paint method gives us, we can now draw even more stuff.</p>
<h1>Graphics Object:</h1>
<p>&nbsp;&nbsp;&nbsp;&nbsp;The Graphics Object contains many methods that let us draw Lines, Ovals, Rectangles, Strings in different Sizes, Places and Colors into the Applet. The Parameters specify the Locations in (x, y) pixel coordinates, where x and y are &nbsp;0 in the top left of our applet and x indicating the width and y the height.</p>
<p>Some of those methods are:</p>
<ul>
  <li>g.setColor(Color c) that let's us change the Color of our drawing "Tool"</li>
  <li>g.drawLine(x1, y1, x2, y2) that let's us draw a Line from (x1, y1) to (x2, y2)</li>
  <li>g.drawOval(x, y, width, height) that let's us draw a Oval with Center (x, y) and specific width, height</li>
  <li>g.drawRect(x, y, width, height) that let's us draw a Rectangle with Origin Point (x, y) and specific width and heigth values from there</li>
  <li>g.drawString(string, x, y) that let's us draw a String with Origin Point (x,y)</li>
</ul>
<p>We could use all of those methods inside of loops to draw Mathematic Equations and other stuff we like.</p>
<h1>Example Applet:</h1>
<pre><code>import javax.swing.JApplet;</code></pre>
<pre><code>import java.awt.Graphics;</code></pre>
<pre><code>import java.awt.Color;</code></pre>
<pre><code>public class MyApplet extends JApplet { // MyApplet.java</code></pre>
<pre><code>	public void init() {</code></pre>
<pre><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// set up applet</code></pre>
<pre><code>		this.setSize(500, 500);</code></pre>
<pre><code>		this.setBackground(Color.white);</code></pre>
<pre><code>	}</code></pre>
<pre><code>	public void paint(Graphics g) {</code></pre>
<pre><code>		// drawing one time</code></pre>
<pre><code>		g.setColor(Color.black);</code></pre>
<pre><code>		g.drawLine(0, 50, 50, 100);</code></pre>
<pre><code>		g.drawString("Hello World!", 100, 200);</code></pre>
<pre><code>		g.drawOval(300, 300, 50, 50);</code></pre>
<pre><code>		g.drawRect(250, 150, 50, 100);</code></pre>
<pre><code>		// drawing with loops</code></pre>
<pre><code>		g.setColor(Color.orange);</code></pre>
<pre><code>		int counter = 1;</code></pre>
<pre><code>		while (counter &lt;= 10) {</code></pre>
<pre><code>			g.drawLine(10, 10, 500, counter * 10);</code></pre>
<pre><code>			++counter;</code></pre>
<pre><code>		}</code></pre>
<pre><code>		g.setColor(Color.blue);</code></pre>
<pre><code>		for (int i = 0; i &lt; 10; i++) {</code></pre>
<pre><code>			g.drawLine(0, 500, 500, i * 50);</code></pre>
<pre><code>		}</code></pre>
<pre><code>		g.setColor(Color.DARK_GRAY);</code></pre>
<pre><code>		for (int i = 0; i &lt; 25; i++) {</code></pre>
<pre><code>			g.drawOval(250 - 10 * i, 250 - 10 * i, 20 * (i + 1), 20 * (i + 1));</code></pre>
<pre><code>		}</code></pre>
<pre><code>	}</code></pre>
<pre><code>}</code></pre>
<h1>Invoke inside of a HTML Website:</h1>
<p>To call an Applet inside of an HTML File we have to use the following Code:</p>
<pre><code>[applet code = "MyApplet.class" width = "500" height = "500"]<br>
<br>
[/applet]</code></pre>
<p>(change the [] with &lt;&gt; &nbsp;from HTML!)</p>
<p>Where you can put more stuff inside then the width and height I put in this example!</p>
<p><br></p>
<p>This was the end of today's Tutorial!&nbsp;</p>
<p>Hope you enjoyed it!</p>
</html>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authordrifter1
permlinkprogramming-java-web-applets
categoryprogramming
json_metadata{"tags":["programming","java","applet","gui","web"],"image":["https://www3.ntu.edu.sg/home/ehchua/programming/java/images/Applet_HelloWorld.gif"],"app":"steemit/0.1","format":"html"}
created2017-08-12 15:08:24
last_update2017-08-16 08:02:27
depth0
children4
last_payout2017-08-19 15:08:24
cashout_time1969-12-31 23:59:59
total_payout_value11.503 HBD
curator_payout_value3.554 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,513
author_reputation98,202,866,830,354
root_title"Programming - Java Web Applets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id11,606,740
net_rshares4,520,744,190,562
author_curate_reward""
vote details (47)
@hrsagar ·
$0.52
nice post, thanks
👍  ,
properties (23)
authorhrsagar
permlinkre-drifter1-programming-java-web-applets-20170812t151815495z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-08-12 15:18:15
last_update2017-08-12 15:18:15
depth1
children1
last_payout2017-08-19 15:18:15
cashout_time1969-12-31 23:59:59
total_payout_value0.388 HBD
curator_payout_value0.127 HBD
pending_payout_value0.000 HBD
promoted0.010 HBD
body_length17
author_reputation17,621,764,740,397
root_title"Programming - Java Web Applets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id11,607,526
net_rshares155,016,441,033
author_curate_reward""
vote details (2)
@danlupi ·
you received  an up vote from <a href = "/@danlupi">danlupi</a> with voting power of 100.00%
properties (22)
authordanlupi
permlinkre-re-drifter1-programming-java-web-applets-20170812t151815495z-20170818t215204
categoryprogramming
json_metadata""
created2017-08-18 21:52:06
last_update2017-08-18 21:52:06
depth2
children0
last_payout2017-08-25 21:52:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length92
author_reputation40,807,748,620,275
root_title"Programming - Java Web Applets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,211,864
net_rshares0
@raviraj4you ·
Thank you for taking the time to create this article. Always great to get insight and help for newcomers like myself 👏
properties (22)
authorraviraj4you
permlinkre-drifter1-2017812t203743774z
categoryprogramming
json_metadata{"tags":"programming","app":"esteem/1.4.6","format":"markdown+html","community":"esteem"}
created2017-08-12 15:09:36
last_update2017-08-12 15:09:36
depth1
children1
last_payout2017-08-19 15:09:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length118
author_reputation-181,820,563,375
root_title"Programming - Java Web Applets"
beneficiaries
0.
accountesteemapp
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id11,606,819
net_rshares0
@drifter1 ·
No need to thank me, I thank you for supporting me :)
It's my pleasure to upload Posts that help others like you. 
I have to remind myself things I nearly forgotten or even learn new stuff to explain it better.
We all learn together :D
properties (22)
authordrifter1
permlinkre-raviraj4you-re-drifter1-2017812t203743774z-20170812t151551102z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-08-12 15:15:51
last_update2017-08-12 15:15:51
depth2
children0
last_payout2017-08-19 15:15:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length235
author_reputation98,202,866,830,354
root_title"Programming - Java Web Applets"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id11,607,333
net_rshares0