Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  on Nov 24 In Java Category.

  
Question Answered By: Adah Miller   on Nov 24

Your First Program
Your first program will be short and sweet. It is going to create a drawing area and draw a diagonal line across it. To create this program you will need to:

Open notepad and type in (or cut and paste) the program
Save the program
Compile the program with the Java compiler to create a Java Applet
Fix any problems
Create an HTML web page to "hold" the Java Applet you created
Run the Java applet
Here is the program we will use for this demonstration:

import java.awt.Graphics;
public class FirstApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
g.drawLine(0, 0, 200, 200);
}
}
Step 1 - Type in the Program

Create a new directory to hold your program. Open up Notepad (or any other text editor that can create TXT files). Type or cut and paste the program into the Notepad window. This is important: when you type the program in , CASE MATTERS. That means that you must type the upper and lower case characters exactly as they appear in the program. Review the programmer's creed above. If you do not type it EXACTLY as shown, it is not going to work.
Step 2 - Save the File

Save the file to the filename FirstApplet.java in the directory that you created in step 1. CASE MATTERS in the filename. Make sure the 'F' and 'A' are upper case and all other characters are lower case, as shown.
Step 3 - Compile the Program

Open an MS-DOS window. Change directory ("cd") to the directory containing FirstApplet.java. type:

javac FirstApplet.java
CASE MATTERS. Either it will work, and in that case nothing will be printed to the window, or there will be errors. If there are no errors a file named FirstApplet.class will be created in the directory right next to FirstApplet.java.

[Make sure that the file saved to the name FirstApplet.java and not FirstApplet.java.txt. This is most easily done by typing dir in the MS-DOS window and looking at the file name. If it has a .txt extension, remove it by renaming the file. Or run the Windows Explorer and select the Options option in the View menu. Make sure that the "Hide MD-DOS File Extensions for file types that are registered" check box is NOT checked, and then look at the filename with the explorer. Change it if necessary.]

Step 4 - Fix Any Problems

If there are errors fix them. Compare your program to the program above and get them to match exactly. Keep recompiling until you see no errors. If javac seems to not be working, look back at the previous section and fix your installation.
Step 5 - Create an HTML Page

Create an HTML page to hold the applet. Open another Notepad window. Type into it the following:

<html>
<body>
<applet code=FirstApplet.class width=200 height=200> </applet>
</body>
</html>
Save this file in the same directory with the name applet.htm.

Step 6 - Run the Applet

In your MS-DOS window type


appletviewer applet.htm


You should see a diagonal line running from the upper left corner to the lower right corner:

Pull the applet viewer a little bigger to see the whole line. You should also be able to load the HTML page into any modern browser like Netscape Navigator or Microsoft Internet Explorer and see approximately the same thing.

You have successfully created your first program!!!

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on How Computer Programs Work Or get search suggestion and latest updates.


Tagged: