JAVA CODE
JAVA PROGRAMME
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Creating a Java Applet

Creating applets is different from creating a simple application. Java applets run and are displayed inside a Web page with other page elements, and therefore have special rules for how they behave. Because of these
special rules for applets, creating an applet may in many cases be more complex than creating an application.

For example, to create a simple Hello World applet, instead of merely being able to print a message as a set of characters, you have to make space for your message on the Web pages and then use special font and
graphics operations to paint the message to the screen.

Note
Actually, you can run a plain Java application as an applet, but the Hello World message will print to a special window or to a log file, depending on how the browser
has its output set up. You’ll learn more about this next week.

Creating the Source File

In this example, you’llĀ  create a simple Hello World applet, place it inside a Web page, and view the result. As with the Hello World application, you’ll first create the source file in a plain text editor. Listing 1.2 shows the
code for the example.

Listing 1.2. The Hello World applet.
1: import java.awt.Graphics;
2:
3: public class HelloWorldApplet extends java.applet.Applet {
4:
5: public void paint(Graphics g) {
6: g.drawString(“Hello world!”, 5, 25);
7: }
8:}

Save that file just as you did the Hello World application, with the filename exactly the same as the name of the class. In this case the class name is HelloWorldApplet, so the filename you save it to would be
HelloWorldApplet.java. As with the application, I put the file in a directory called TYJch01, but you can save it anywhere you like.

Compiling the Source File

The next step is to compile the Java applet file. Despite the fact that this is an applet, you compile the file exactly the same way you did the Java application, using one of the following procedures:
javac HelloWorldApplet.java
javac HelloWorldApplet.java

Windows
From inside a DOS shell, cd to the directory containing your applet source file, and use the javac command to compile it (watch those upper- and lowercase letters):

Macintosh
Drag and drop the HelloWorldApplet.java file onto the Java Compiler icon.

Salaris
From a command line, cd to the directory containing your applet source file and use the javac command to compile it:

Including the Applet in a Web Page

If you’ve typed the file correctly, you should end up with a file called HelloWorldApplet.class in the same directory as your source file. That’s your Java applet file; to have the applet run inside a Web page you must
refer to that class file inside the HTML code for that page using the <APPLET> tag. Listing 1.3 shows a simple HTML file you can use.

Listing 1.3. The HTML with the applet in it.
1: <HTML>
2: <HEAD>
3: <TITLE>Hello to Everyone!</TITLE>
4: </HEAD><BODY>
5: <P>My Java applet says:
6: <APPLET CODE=”HelloWorldApplet.class” WIDTH=150 HEIGHT=25>
7: </APPLET>
8: </BODY>
9: </HTML>

You’ll learn more about <APPLET> later in this book, but here are two things to note about it:
Use the CODE attribute to indicate the name of the class that contains your applet, here HelloWorldApplet.Class.
Use the WIDTH and HEIGHT attributes to indicate the size of the applet on the page. The browser uses these values to know how big a chunk of space to leave for the applet on the page. Here, a box 150 pixels
wide and 25 pixels high is created.

Save the HTML file in the same directory as your class file, with a descriptive name and an .html extension (for example, you might name your HTML file the same name as your applet-HelloWorldApplet.html).

Note
As mentioned earlier with the Java source files, your text editor may insist on naming your HTML files with a .txt extension if Windows does not understand what the
.html extension is used for. Select View|Options|File Types from any Windows Explorer window to add a new file type for HTML files to solve this problem.

Now you’re ready for the final test-actually viewing the result of running your applet. To view the applet, you need one of the following:
A browser that supports Java applets, such as Netscape 2.0 or Internet Explorer 3.0. If you’re running on the Macintosh, you’ll need Netscape 3.0 or later. If you’re running on Windows 95 or NT, you’ll need the
32-bit version of Netscape. And if you’re using Internet Explorer, you’ll need the 3.0 beta 5 or later (the final version will do just fine).
The appletviewer application, which is part of the JDK. The appletviewer is not a Web browser and won’t let you to see the entire Web page, but it’s acceptable for testing to see how an applet will look and
behave if there is nothing else available.
An applet viewer or runner tool that comes with your development environment.

If you’re using a Java-enabled browser such as Netscape to view your applet files, you can use the Open File… item under the File menu to navigate to the HTML file containing the applet (make sure you open the
HTML file and not the class file). In Internet Explorer, select File|Open and then Browse to find the file on your disk. You don’t need to install anything on a Web server yet; all this works on your local system. Note that
the Java applet may take a while to start up after the page appears to be done loading; be patient. Figure 1.8 shows the result of running the applet in Netscape.

Figure 1.8 : The applet running in Netscape.

If you don’t have a Web browser with Java capabilities built into it, you can use the JDK’s appletviewer program to view your Java applet.

appletviewer HTML/HelloWorldApplet.html

Windows or Solaris
To run the appletviewer in Windows or Solaris versions of the JDK, cd to the directory where your HTML and class files are contained and use the appletviewer
command with the name of the HTML file you just created:

The appletviewer will show you only the applet itself, not the HTML text around the applet. Although the appletviewer is a good way to do simple tests of Java applets, it’s a better idea to get a Java-enabled
browser so that you can see your applet on its page in its full glory.

More Info

Creating a Java applet involves similar steps to creating a Java application, but with a focus on embedding the applet within a web page. Here’s a basic guide to creating a Java applet:

1. **Set Up Your Development Environment**:
– Install Java Development Kit (JDK) as mentioned before.
– Choose an Integrated Development Environment (IDE) or a text editor.

2. **Write Your Java Code**:
– Create a new Java class that extends `java.applet.Applet`. This class will represent your applet.
– Override the `init()` method to initialize your applet. This is where you can set up any initializations or configurations.
– Override the `paint(Graphics g)` method to define what your applet should display.

3. **Compile Your Code**:
– Once you’ve written your code, compile it into bytecode using `javac`.

4. **Create an HTML File**:
– Create an HTML file where you’ll embed your Java applet. This file will contain the `<applet>` tag, which is used to embed Java applets in web pages.
– Specify the attributes of the `<applet>` tag such as the width, height, and the name of the main Java class.

5. **Run Your Applet**:
– Open the HTML file in a web browser that supports Java applets (Note: Modern web browsers have dropped support for Java applets, so you might need to use an older browser or configure your browser to run applets).
– The browser should load and display your Java applet embedded within the web page.

6. **Debug Your Applet** (optional):
– Debug your applet code using the debugging tools provided by your IDE or by adding print statements to your code.

7. **Test Your Applet**:
– Test your applet in various web browsers to ensure compatibility and proper functionality.

8. **Package and Distribute Your Applet** (if necessary):
– If you want to distribute your applet, you can package it into a JAR file and provide the HTML file along with it.

It’s important to note that Java applets have largely fallen out of favor due to security concerns and the deprecation of browser plugins like Java applets. However, if you’re interested in learning about them for historical purposes or for compatibility with older systems, this guide should help you get started.

prime number
Scroll to Top