Sunteți pe pagina 1din 2

Creating a Basic Java Applet in Eclipse CSCI 201 I. Setting up the files and folders in Eclipse 1.

. Create a new Java project in Eclipse. 2. Right-click on the name of the project in the Package Explorer and create a new folder called Resources. This is where images, sound and any other resources you might use in your applet need to go. 3. Right click on the src folder and create a new package. Right click on this package and create a new class. This is where your code will go. You may want to create multiple classes and/or packages if your project gets big enough. II. Writing the code 4. A Java applet is like any other Java class except that it extends Applet. So the class declaration should look something like:
public class MyAppletName extends Applet {

5. You need to implement two methods (Eclipse should prompt you to do this):
i. The init( ) method. Think of this as a constructor. It gets called when your applet starts. This

is a good place to initialize variables.


public void init() { ii. The paint( ) method. This method handles all the printing to screen. It takes a Graphics

object as a parameter. You dont need to worry about how this method gets called and where the Graphics object comes from. You just need to know that this object has methods that allow you to draw shapes, images, etc. It gets called automatically every few milliseconds.
public void paint (Graphics g) {

6. You can test your applet out by hitting the green play button. IV. Creating the Jar We will create a .jar file that contains all your code and resources and use this to embed your applet in a browser. 7. In your home directory, navigate to the public_html folder. Create a folder called cs201 in here. This is where files for your webpage will go. 8. In Eclipse, select your project in the Package Explorer. Go to File > Export. Under the Java folder, select JAR file. Change the destination to ~/public_html/cs201/myApp.jar or whatever folder you created in section III. Make sure your project is selected where it asks you to Select the resources to export. Hit Finish.

V. Setting up the webpage 9. Open up a text editor and enter in the code for your html webpage. In order to embed your applet you must include the following code:
<applet code=myPackage/myAppletName.class archive=myApp.jar height=400 width=400> </applet>

where your applet class myAppletName lies in the package myPackage in the src folder in your project folder in Eclipse and myApp.jar is the name of the .jar file you saved in the folder ~/public_html/cs201/ in step 7. 10. If all went well your applet should now be viewable at http://www.cs.middlebury.edu/~yourusername/cs201/ VI. Using the sample code 11. Download cs201Project.zip and unzip it. 12. In Eclipse, go to File > Project.... Under Java, select Java Project From Existing Ant Buildfile and hit next. 13. Hit browse. Browse to the unzipped cs201Project folder. There is a file called build.xml in it. Select this file and hit open. 14. Hit finish. If all went well, you should now have a copy of the sample cs201Project in the Package Explorer.
!"#$$%&'$"($")&*+,-#.-"&/011&

S-ar putea să vă placă și