How to Edit Java Applets

How to Edit Java Applets thumbnail
Editing Java applets involves changing a program's ".java" files.

Editing Java applets involves making changes to a program's ".java" files, which provide the source code for the Java compiler. These changes can involve fixing syntax errors, altering the user interface (e.g., revising the text a button displays) or other alterations. Editing Java applets provides new Java students with a gentle introduction to writing their own programs. Students work directly with source code, and repeatedly perform the edit-compile-run cycle. These actions ingrain the steps needed for developing Java applications.

Things You'll Need

  • Java Development Kit
Show More

Instructions

    • 1

      Type or paste the following sample program into a new document in any word processor. The program has two errors. Fixing them and completing the full cycle of coding, compiling, running and fixing the applet will illustrate how to edit a Java applet.

      import java.applet.*;

      import java.awt.*;

      public class DrawingExample extends Applet{

      public void init(){

      }

      public void paint(Graphics gfx){

      gfx.drawOval(10,1575, 75);

      }

      }

    • 2

      Save the program as type "Plain Text," with the filename "DrawingTest.java."

    • 3

      Enter the following text into another document and save it as "DrawingTest.HTML," with the file type "Plain Text." Be sure to save it in the same folder as your "DrawingTest.java" file. The HTML file is the web page that displays the applet's content.

      <html>

      <head>

      <title>DrawingTest Applet</title>

      </head>

      <body>

      <h1>DrawingTest Applet</h1>

      <hr>

      <applet code="DrawingTest.class"

      width=500

      height=500

      codebase=".">

      </applet>

      <hr>

      </body>

      </html>

    • 4

      Open a command prompt by pressing "Start," then typing "cmd" to open the command prompt. Type:

      PATH=C:\program files\java\jdk_<VERSION>\bin

      where "<VERSION>" is the particular Java version number on your PC. You can determine this version number by entering "C:\program files\java" in the Windows Explorer address bar, then reading the number appended to the "jdk_" folder.

      This step tells Windows where the javac compiler is, which is needed to run the compiler.

    • 5

      Type "cd <DRAWING_TEST_FOLDER>" in the command prompt, where "<DRAWING_TEST_FOLDER>" refers to the folder containing the "DrawingTest.java" file you created.

    • 6

      Compile the program by typing "javac DrawingTest.java". The Java compiler will attempt to convert your program into bytecode, but will instead display two error messages. One pertains to the filename and the other relates to the "drawOval" method. You'll edit the applet's source code to fix both errors.

    • 7

      Type the text "DrawingTest" over the text "DrawingExample" in the "DrawingTest.java" file open in your word processor.

      The change you made makes the file's name and the Java class' name the same, which the compiler requires to convert your source code into executable bytecode.

    • 8

      Replace the "drawOval" statement with the following correction:

      gfx.drawOval(10,15,75, 75);

      This revision makes the "drawOval" function call agree with one of the drawOval calls documented in the Java API (application programming interface). The Java compiler can't link your source code's function calls with function definitions if those definitions don't exist.

    • 9

      Click the command prompt window and recompile the program by typing "javac DrawingTest.java". The compiler will display no errors this time.

    • 10

      Run the applet by double-clicking, in Windows Explorer, the "DrawingTest.html" file. The browser will display the oval drawn by your Java source code.

Related Searches:

References

Resources

  • Photo Credit binary world image by Attila Toro from Fotolia.com

Comments

You May Also Like

Related Ads

Featured