Home > Projects > Hello World



Java


For larger projects I use the Eclipse IDE for Java Developers, version Kepler release 2

For smaller projects I use the jGrasp IDE (http://www.jgrasp.org/). The examples on this page were written in version version 2.0.3 of jGrasp.

The Java version is 1.8.0_91

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
  }
}


Hello World Ascii Variation


public class HelloWordVariation {
public static void main(String[] args) {
drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(6); drawPound(3); drawSpace(5); drawPound(1); drawSpace(7); 
drawPound(1); drawSpace(2); drawPound(3); drawSpace(2); drawPound(4); drawSpace(2); drawPound(1); drawSpace(5); drawPound(4); drawSpace(3); drawLastPound(); drawPound(1); drawSpace(3); drawPound(1); 
drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(4); drawPound(1); drawSpace(3); drawPound(1); 
drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); 
drawSpace(3); drawPound(1); drawSpace(2); drawLastPound(); drawPound(5); drawSpace(1); drawPound(5); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); 
drawPound(1); drawSpace(4); drawPound(1); drawSpace(3); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(4); drawSpace(2); 
drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(2); drawLastPound(); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(5); 
drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(5); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); 
drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(2); 
drawLastSpace(); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); drawPound(5); drawSpace(1); drawPound(5); drawSpace(2); drawPound(3); drawSpace(2); 
drawPound(1); drawSpace(4); drawPound(1); drawSpace(2); drawPound(1); drawSpace(5); drawPound(3); drawSpace(2); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); 
drawPound(4); drawSpace(3); drawLastPound(); drawSpace(29); drawPound(1); drawSpace(38); drawLastSpace();
   }

public static void drawPound(int a){
for(int i=1; i <= a; i++) {
System.out.print("#");
   }
}   
   
public static void drawSpace(int a){
for(int i=1; i <= a; i++) {
System.out.print(" ");
   }
}

public static void drawLastPound(){
System.out.print("#\n");
}

public static void drawLastSpace(){
System.out.print(" \n");
}
}

Here is the output from the above java program:


#   # ##### #     #      ###     #       #  ###  ####  #     ####   #

#   # #     #     #     #   #    #   #   # #   # #   # #     #   #  #

##### ##### #     #     #   #    #   #   # #   # ####  #     #   #  #

#   # #     #     #     #   #     #  #  #  #   # #  #  #     #   #   

#   # ##### ##### #####  ###  #    #  #     ###  #   # ##### ####   #

                             #                                       



Next is a demonstration of simple graphics, with another variation of Hello World.


The Java source files can be downloaded here.


The font type is set to "Jokerman" at 36 pt.

Variable sw holds the length of the string "Hello, World!" that will be used for later calculations

To draw the text string in the center of the panel, calculate the x,y coordinates on lines 12 and 13

       
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class HelloWorldPanel extends JFrame {
	public static void main(String[] args) {
        HelloWorldPanel panel = new HelloWorldPanel();
        panel.setSize(300, 150);
        panel.setVisible(true);
        panel.getContentPane().setBackground(Color.yellow);
        Graphics g = panel.getGraphics();
        String st = "Hello, World!";
        Font myFont = new Font ("Jokerman", 1, 36);
        FontMetrics fm = g.getFontMetrics ( myFont );
        int sw = fm.stringWidth ( st );
        int intx = ( panel.getWidth() + sw ) / 2 - sw;
        int inty = panel.getHeight() / 2;
        

Color can be controlled by accessing the Color class. The Color class yields the class constants shown in the table below. The expression Color.RED gives the Color constant for red. Images are drawn in the current color until the color is changed. Changing the color does not affect the color of previously drawn images.


The basic standard colors are:


However, you can fine tune the colors by controlling the RGB (red/green/blue) density values. Each of these 3 colors has 256 shades. It is possible to "mix" a new shade of color by selecting an integer from 0 to 255 and passing it to a Color constructor. The value 0 indicates the absence of a color in the mixture, and the value 255 indicates the maximum saturation of that color. The color black has RGB(0,0,0) and the color white has RGB(255,255,255). There are 256 * 256 *256 = 224 possible colors in this scheme.


See HTML Color Codes for details on HTML colors.

 
 g.setFont (myFont);
        g.setColor (Color.RED);
        g.drawString ( st , intx , inty );
        for (int i = 1; i <= 13; i++) {
         if (i == 1) { 
            g.setColor (Color.RED);
            } else if (i == 2){
            g.setColor (Color.BLUE);
            } else if (i == 3){
            Color customColor = new Color(255,82,51);  //red orange
            g.setColor (customColor);
            } else if (i == 4){
            g.setColor (Color.ORANGE);
            } else if (i == 5){
            Color customColor = new Color(36,205,46);  //green
            g.setColor (customColor);
            } else if (i == 6){
            g.setColor (Color.PINK);
            } else if (i == 7){
            g.setColor (Color.CYAN);
            } else if (i == 8){
            g.setColor (Color.MAGENTA);
            } else if (i == 9){
            g.setColor (Color.BLACK);
            } else if (i == 10){
            g.setColor (Color.WHITE);
            } else if (i == 11){
            g.setColor (Color.GRAY);
            } else if (i == 12){
            g.setColor (Color.lightGray); //exact spelling required here
            } else if (i == 13){
            g.setColor (Color.darkGray);
            } else {
            g.setColor (Color.RED);
            }

Use Thread.sleep(milliseconds) to make the current executing Java program delay briefly as each colored string is displayed.

Then resume and draw the string.

       
         try {
			         Thread.sleep(400);
   		      } catch (InterruptedException e) {
			         Thread.currentThread().interrupt();
		         }
		         
		        g.drawString ( st , intx , inty ); 
         }
         

The next part of the example is to print the Hello, World! string where each letter is a different color. To start, calculate the individual letter lengths of the string.

 
         int widthH = fm.stringWidth ( "H" );
         int widthE = fm.stringWidth ( "e" );
         int widthL = fm.stringWidth ( "l" );
         int widthO = fm.stringWidth ( "o" );
         int widthC = fm.stringWidth ( "," ); //abreviation for comma is C
         int widthSp = fm.stringWidth ( " " );
         int widthW = fm.stringWidth ( "W" );
         int widthR = fm.stringWidth ( "r" );
         int widthD = fm.stringWidth ( "d" );

Then sum the letter lengths one-by-one, and group and store them. This will help calculate the postion of the next letter to work with, when the program is running and changing letter colors.


For example: to calculate the postion of "W" in the "Hello, World!", you need to calculate the length of "H" and "e" and "l" and "l" and "o" and "," and " " then total the lengths of ("H"+"e"+"l"+"l"+"o"+","+" ") to locate the position of the letter "W". See variable ellocspw below.

  
         int e = intx + widthH;
         int el = intx + widthH + widthE;
         int ell = intx + widthH + widthE + widthL;
         int ello = intx + widthH + widthE + widthL +  widthL;
         int elloc = intx + widthH + widthE + widthL +  widthL + widthO;
         int ellocsp = intx + widthH + widthE + widthL +  widthL + widthO + widthC;
         int ellocspw = intx + widthH + widthE + widthL +  widthL + widthO + widthC + widthSp;
         int ellocspwo = intx + widthH + widthE + widthL +  widthL + widthO + widthC + widthSp + widthW;
         int ellocspwor = intx + widthH + widthE + widthL +  widthL + widthO + widthC + widthSp + widthW + widthO;
         int ellocspworl = intx + widthH + widthE + widthL +  widthL + widthO + widthC + widthSp + widthW + widthO + widthR;
         int ellocspworld = intx + widthH + widthE + widthL +  widthL + widthO + widthC + widthSp + widthW + widthO + widthR + widthL;
         int ellocspworldx = intx + widthH + widthE + widthL +  widthL + widthO + widthC + widthSp + widthW + widthO + widthR + widthL + widthD;

Next loop through the letters assigning each one a new color. Again you can see the example "W" highlighted below.

                        
          g.setFont (myFont);   
          g.setColor (Color.BLACK);
          g.drawString ( "H" , intx , inty );
          g.setFont (myFont);
          g.setColor (Color.BLUE);
          g.drawString ( "e" , e , inty );
          g.setFont (myFont);
          g.setColor (Color.ORANGE);
          g.drawString ( "l" , el , inty );
          
          g.setFont (myFont);
          g.setColor (Color.MAGENTA);
          g.drawString ( "l" , ell , inty );
          
          g.setFont (myFont);
          g.setColor (Color.CYAN);
          g.drawString ( "o" , ello , inty );
          
          g.setFont (myFont);
          g.setColor (Color.RED);
          g.drawString ( "," , elloc , inty );
          
          g.setFont (myFont);
          g.setColor (Color.PINK);
          g.drawString ( " " , ellocsp , inty );
          
          g.setFont (myFont);
          g.setColor (Color.BLUE);
          g.drawString ( "W" , ellocspw , inty );
          
          g.setFont (myFont);
          g.setColor (Color.YELLOW);
          g.drawString ( "o" , ellocspwo , inty );
          
          g.setFont (myFont);
          g.setColor (Color.MAGENTA);
          g.drawString ( "r" , ellocspwor , inty );
          
          g.setFont (myFont);
          g.setColor (Color.ORANGE);
          g.drawString ( "l" , ellocspworl , inty );
          
          g.setFont (myFont);
          g.setColor (Color.BLACK);
          g.drawString ( "d" ,  ellocspworld , inty );
          
          g.setFont (myFont);
          g.setColor (Color.RED);
          g.drawString ( "!" ,  ellocspworldx , inty );
        
     }
}

I initially converted the Java code above to an Applet but ran into a variety of issues regarding applets and plugins, etc.

In the end it is easier to just download the source files.


Once the files are downloaded, unzip them into a folder (like javademo) and then from the console type the following at the command prompt:


C:\javademo> java -cp . HelloWorldPanel



You should see the string "Hello, World!" printed in 13 alternating colors, followed by a final string with the individual letters colored as shown in the screenshot below: