Error » Certification & Programming center Error !! » Programming tutorials » Java: turn your .class into an applet

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  Java: turn your .class into an applet
LinkBack Thread Tools Display Modes
Old 03-Dec-2006, 11:32 PM   #1 (permalink)
Administrator
 
Admin's Avatar

Posts: 876
Join Date: Oct 2005
Rep Power: 10 Admin has disabled reputation

IM:
Default Java: turn your .class into an applet

How to modify your existing java GUIs into applets to share with the web viewing world.

Do note that most file transfers are not possible in applets, it just doesn't work like that. There are ways around it to do some read or write operations, but that is beyond this simple transformation recipe.

An applet is basically the GUI you already made, except it doesn't have a frame. The frame is supplied by the browser opening the .class file.

It is quite a simple task, assuming your code was written using panels, instead of frames (as you should have). This will allow for easy transfer from java code, to applet.

There are a few methods that are required to make an applet work:
public void init() {}
this method is run when the applet is being loaded into memory or constructed. Think of this as the applet constructor.

public void start() {}
this is run once all preparations are done, and the code is actually beginning to run. Place any splash screens or intro stuff here.

public void stop() {}
this is run when the applet has been terminated, while memory is being freed and the code is stopping.

public void destroy() {}
this method is called just before a system exit. Notifying that the applet has finished and cannot be run again without reloading it.

CODE Example (applet.class):


import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class applet extends JApplet{
static final int WIDTH = 500;
static final int HEIGHT = 60;

JTextField title;

public class display extends JPanel{
public display(){
Container container;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints layoutConstraints = new GridBagConstraints();
getContentPane().setLayout(layout);
container = getContentPane();

JLabel label1 = new JLabel("Name:");
layoutConstraints.gridx = 0; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 1; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 2.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(label1, layoutConstraints);
container.add(label1);

title = new JTextField();
layoutConstraints.gridx = 1; layoutConstraints.gridy = 0;
layoutConstraints.gridwidth = 2; layoutConstraints.gridheight = 1;
layoutConstraints.fill = GridBagConstraints.BOTH;
layoutConstraints.insets = new Insets(1,1,1,1);
layoutConstraints.anchor = GridBagConstraints.CENTER;
layoutConstraints.weightx = 40.0; layoutConstraints.weighty = 1.0;
layout.setConstraints(title, layoutConstraints);
container.add(title);
}
}

public void init() {
System.out.println("Applet initializing");
display d = new display();
getContentPane().add(d);
}
public void start() {
System.out.println("Applet starting");
}
public void stop() {
System.out.println("Applet stopping");
}
public void destroy() {
System.out.println("Applet destroyed");
}
public static void main(String args[]){
JFrame frame = new JFrame("Code Converter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
applet a = new applet();

a.init(); // Do what browser normally does
frame.getContentPane().add(a); // Add applet to frame

frame.setSize(WIDTH, HEIGHT); // Set the size of the frame
frame.setVisible(true); // Show the frame
}
}


*Note i have added the JFrame information into the main of the java file, this allows us to test our program as we go along, without having to run the html file.

CODE Example (runApplet.html):


<html>
<head>
<applet code="applet.class" width=600 height=30></applet>
</head>
<body>
</body>
</html>


A simple applet tage designating the source and display size is all that is necessary within the head of your html file.

NOTE your applet has more than 1 .class file, but the main file is the only one that needs to be called. The html and class files, should be located in the same directory to ensure all .class files can be found.
Admin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Java JDBC Tutorials - Topics include Java, Database, JDBC, Driver, ODBC and more! Anilrgowda Graphic tutorials 0 04-Sep-2007 01:02 AM
Little Johnny's class pic driverdownloads Fun N Light Error! 0 27-Mar-2007 03:38 AM
Insults with class driverdownloads Fun N Light Error! 0 27-Mar-2007 01:28 AM
The Javalog.txt file is created in the Windows\Java folder when Java logging is enabl Anilrgowda Microsoft windows vista error 0 29-Jan-2007 10:09 AM
Search Your Computer Quickly With the Deskbar Applet on Ubuntu Anilrgowda OS tutorials 0 20-Dec-2006 03:45 AM


All times are GMT -8. The time now is 07:45 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228