Java Student Project         

Message
Ability Level: Beginner 
Estimated Time: 45 minutes
Objectives:
  • To learn to make an applet which prints out a message.

 

Materials & Resources:
  • C++ Software
  • Textbook
Overview: Write an applet which will print out a message from a web browser. Your text should use a different color text, different font, be bold, and italicized. Be positive with your message! 
Do not use the example font or color.

Instructions:
  1. From the start, programs mneu, click on  Microsoft Visual J++.

  2. Click on New Project, under the New tab, click Web Pages and Applet on HTML.

  3. Enter the name and location and click open.

  4. After it loads, click file, new file, Java J++ and Java File, and click open.

  5. Write your code.

  6. Click on "Save Java File1 As" and be sure you are in the correct location, enter the name of the class you created with the extension of .java.

  7. An Applet.java is created along with the file.java you created. Click on the Applet.java and press delete to remove it. 

  8. Once this has been done, the file.java which you created copies to the project section. Now you may delete the miscellaneous files by highlighting it and pressing delete. 

  9. Be sure the Page1.htm file includes the name of your class in the code statement. Example: code=mymessage.class

  10. Be sure your settings have the output going to C drive and click the arrow to output your message.

  11. Use this template to help you start:

import java.awt.Color;  //calls color library
import java.awt.Graphics;  //allows graphics library to be used
import java.applet.Applet; //states that an applet is being made
import java.awt.Font;  // allows us to change the font

public class message extends Applet //message is the name of the applet

     public void init()  //  method to set the color
    {
        setBackground(Color.white); 
    }//closing init

    public void paint(Graphics g) // method called paint using graphics 
   {
      g.setColor (new Color(0,0,255));  // sets the text color to blue
         // colors are red, green, blue and color intensity ranges 0 to 255
      g.setFont (new Font("SansSerif", Font.BOLD | Font.ITALIC, 20));
         // changed font to SansSerif and called Bold and pipe indicates
         // or, which means and/or to also call italics
      g.drawString ("Hello Class!", 20, 50);
   }// closing paint
}//closing class and ending applet
 

 

 
Hints: 1. Import is necessary to include library files.  Be sure to include Applet.
An example : import java.awt.Color;

2. Class must be declared with the name and extends Applet to indicate Applet is being created.               
Example: public class mymessage extends Applet 

3. A method (like a function in C++) is needed for each constructor, which is a block of code whenever a new object is created. Open and close braces enclose the code.
Example: public void init() {setBackground(Color.white); }

4. To use graphics the method must be declared.
Example: public void paint (Graphics g) {code in here }

5. There are many commands available such as g.setFont, g.setColor, and g.drawString which should be used in the paint method.

6. The pipe character ( | ) is used for "or" and allows the use of two different font sets. The pipe is generally the shift of the backslash on most keyboards.

7. Color uses red, green, blue sequentially. The maximum color is 255 and the minimum is 0. Three parameters are necessary.
Example of the parameter: (new Color(255,0,0)