{"id":603,"date":"2010-01-30T13:00:28","date_gmt":"2010-01-30T13:00:28","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=603"},"modified":"2010-01-30T13:18:14","modified_gmt":"2010-01-30T13:18:14","slug":"java-applet-hello-world","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/30\/java-applet-hello-world\/","title":{"rendered":"Java applet &#8211; hello world"},"content":{"rendered":"<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>\n<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>\n<ol>\n<li>init<\/li>\n<li>start<\/li>\n<li>paint<\/li>\n<li>destory<\/li>\n<li>stop<\/li>\n<\/ol>\n<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>\n<p>The main basic class will extend from a applet (java.applet.Applet) so you will need to import that class library<\/p>\n<pre lang=\"java\">\r\nimport java.applet.Applet;\r\n\r\n\/\/ basic class extends the applet\r\npublic class HelloWorld extends java.applet.Applet {\r\n<\/pre>\n<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>\n<pre lang=\"java\">\r\nimport java.awt.Graphics;\t\t\/\/ need this for the Graphics part of the paint function\r\nimport java.applet.Applet;\t\t\/\/ to extend from.\r\n\r\npublic class HelloWorld extends java.applet.Applet {\r\n\r\n    \/\/ resize the screen \r\n    public void init()\r\n    {\r\n\tresize(150,25);\r\n    }\r\n\r\n    public void start()\r\n    {\r\n    }\r\n    \r\n    \/\/ draw with the Graphics class object a string on the canvas, point 50, 25\r\n    public void paint(Graphics graphicsObj)\r\n    {\r\n\tgraphicsObj.drawString(\"Hello World!\", 50,25);\r\n    }\r\n    \r\n    public void stop()\r\n    {\r\n    }\r\n    \r\n    public void destroy()\r\n    {\r\n    }\r\n}\r\n<\/pre>\n<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>\n<pre lang=\"bash\">\r\njavac HelloWord.java\r\n<\/pre>\n<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>\n<pre lang=\"html\">\r\n<HTML>\r\n<head>\r\n<title>A hello world<\/title>\r\n<\/head>\r\n<body>\r\nTest output of the program\r\n<applet code=\"HelloWorld.class\" width=150 height=25>\r\n<\/applet>\r\n<\/body>\r\n<\/HTML>\r\n<\/pre>\n<p>The output would be<\/p>\n<p>Test output of the program <applet code=\"HelloWorld.class\" codebase=\"\/Appletsexamples\/\"  archive=\"HelloWorld.jar\" width=\"150\" height=\"25\"><\/applet><\/p>\n<p>Note: If you look at the source code of this page, it will say<\/p>\n<pre lang=\"HTML\">\r\n<applet code=\"HelloWorld.class\" codebase=\"\/Appletsexamples\/\"  archive=\"HelloWorld.jar\" width=\"150\" height=\"25\"><\/applet>\r\n<\/pre>\n<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>\n<pre lang=\"bash\">\r\njar cf <tarname> <file(s) name>\r\n\r\njar cf HelloWorld.jar HelloWorld.class\r\n<\/pre>\n<p>The jar file is basically a zipped archive.<\/p>\n","protected":false},"excerpt":{"rendered":"<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. In &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/01\/30\/java-applet-hello-world\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java applet &#8211; hello world<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,15],"tags":[78],"class_list":["post-603","post","type-post","status-publish","format-standard","hentry","category-html","category-java","tag-applet"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=603"}],"version-history":[{"count":15,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/603\/revisions"}],"predecessor-version":[{"id":618,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/603\/revisions\/618"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}