Duke Graphic Java

Java software runs on hundreds of hardware devices. The "Write Once, Run Anywhere" ability of Java™ is the result of Java code being compiled to an intermediate code called "bytecode" that runs on software called a Java Virtual Machine (JVM). The JVM translates the bytecode to processor-specific commands needed by the particular hardware platform. Google's Android is an example of a Java-technology used for mobile devices. See additional details about Java's usage and history. Also see Java SE Version 6 - API Specifications.

Java Installation JRE and JDK

The Java Runtime Environment (JRE) is required to run Java applications. The Java Development Kit (JDK) is required to develop Java applications. The JDK is a superset of the JRE. The JDK includes everything in the JRE plus the Java compiler, debugger, and example coding. See the Java SE Documentation for further details about the Java SE JRE and JDK. The Java SE Development Kit (JDK) can be downloaded from Oracle. Once the desired version of the JDK is installed, run: "java -version" in a DOS window. If the command is not recognized (most likely), you will need to add the path to "java.exe" to your "path" environment variable.

C:\Users\Kevin>java -version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) Client VM (build 20.5-b03, mixed mode, sharing)

Note: The JDK I am currently using is version 6 Update 30, 32 bit for Windows (jdk-6u30-windows-i586.exe, Windows x86, 82,761,496 bytes) which I downloaded from this Oracle website . After the JDK install, the JDK binary files were located in this binaries directory and the demonstration programs were located in this directory demo directory . Here is a link to Java SE version 6 API Specifications.

Development Environment Eclipse IDE

The Eclipse Foundation provides a public license for their Eclipse Integrated Development Environment (IDE). Java is the default language for Eclipse, so no additional Eclipse plugins should be required for Java development. Installing the desired version of the Eclipse IDE is the next step at setting up the Java development environment. Note, the Java JDK should be installed prior to the Eclipse software installation. Also, it is advised to match the same "bit version" of Eclipse with the JDK. If you installed a 32-bit version of the JDK, then install the 32-bit version of Eclipse. After the Eclipse software has been downloaded , extracted, and installed ... it should find the Java JDK installation. If it does not, you may need to add the path to the "javaw.exe" file in the Eclipse initialization file (eclipse.ini). Instructions for configuring eclipse can be found in documentation of the eclipse.ini file. Please see the Eclipse page for more information about installing the Eclipse IDE. Note: I am currently running Eclipse version 3.7.2 (Indigo) , which I downloaded from the Eclipse Indigo download website. (eclipse-SDK-3.7.2-win32.zip, 183,171,707 bytes). I extracted the downloaded file and then moved the resulting "eclipse" folder to the root of my C: drive. I created a shortcut on my desktop to C:\eclipse\eclipse.exe which I called "Eclipse - Indigo". Here are the version's release notes. I set my default Eclipse work space to "C:\Users\Kevin\workspace".

Java Packages Definition, Naming Conventions, JAR

A Java package is a grouping of Java classes stored as a unit under a particular user-created name. Some development environments will create a default package if the developer does not create a package. It is good programming practice to create a package with a meaningful name to organize the application's components and help with the distribution of the application. Oracle has recommended package naming conventions . These conventions include using all lower case for the package name. Then in order to avoid naming collisions, use the reverse Internet domain name as a prefix for the package. For example, if I created a package containing a number of classes related to graphics, I would name the package: "net.kcshadow.graphics". Java packages can be compressed into JAR (Java Archive) files which allow for increased security, faster downloading, and file versioning. JAR files contain the bytecode files (.class) and resources needed to execute the Java programs on any Java VM:

   C:\Program Files (x86)\Java\jdk1.6.0_30\demo\jfc\FileChooserDemo>java -jar FileChooserDemo.jar


First Java Program Hello World!
  1. Create Project and Package.
    • In the Package Explorer view of Eclipse, right-click and select New -> Java Project.
    • Enter Project Name (e.g. helloworld) and select finish.
  2. Create Class.
    • In Package Explorer view, right-click on the package you created and select New --> Class.
    • Enter the Package Name (e.g. net.kcshadow.helloworld) and Class Name (e.g. HelloWorld)
      {Package name convention specifies package should be all lower case; use of reverse domain as package name prefix is recommended.}
      {Class name convention specifies class names start with uppercase, and new words should also be in uppercase.}
    • Select the check box indicating you would like Eclipse to create: "public static void main(String[] args)". Select Finish.
  3. Enter Code.
    • Enter the following code inside the HelloWorld class: System.out.println("Hello World!");
  4. Run Program.
    • Hit <Ctrl><F11> to run the program. Output should be visible in the Console window.

Hello World Java Program


First Java Applet Hello Internet!
  1. Create Project and Package.
    • In the Package Explorer view of Eclipse, right-click and select New -> Java Project.
    • Enter Project Name (e.g. HelloInternet) and select finish.
  2. Create Class.
    • In Package Explorer view, right-click on the package you created and select New --> Class.
    • Enter the Package Name (e.g. net.kcshadow.hellointernet) and Class Name (e.g. HelloInternet)
      {Package name convention specifies package should be all lower case; use of reverse domain as package name prefix is recommended.}
      {Class name convention specifies class names start with uppercase, and new words should also be in uppercase.}
    • Press the "Browse" button next to the "Superclass" text box. Enter "applet" in the "Choose a type" text box and select the "Applet - java.applet" matching item. Click OK.
    • Select Finish to create the new class.
  3. Enter the "paint" method by:
    • Right-click in editor window below, inside the class code.
    • Select Source -> Override/Implement Methods ... Then inside the "Container" class select the "paint(Graphics)" method. Select OK to generate "paint" method.
    • Clean up the generated paint method by removing the two lines: "@Overrride" and "super.paint(g);".
  4. Enter Code.
    • Enter code inside the paint method:
      		      // Set Colors		
      		      setBackground(Color.white);
      		      g.setColor(Color.black);
      		      
      		      // Draw Hello Internet!
      		      for(int x=20,y=30;x<50;x+=10,y+=15) {
      		         g.drawString("Hello Internet!", x, y);
      		      }
      		      
      		      // Draw Random Colored Lines
      		      for(int x=15;x<150;x+=5) {
      		         g.setColor(new Color((int) (Math.random() * 255),  
      		                              (int) (Math.random() * 255), 
      		                              (int) (Math.random() * 255)));
      		         g.drawLine(x, 100, x, 200);
      		      }
      		
  5. Run Applet.
    • Hit <Ctrl><F11> to run the applet. Output should be visible in the Applet Viewer.

The Java applet is embedded below this paragraph, followed by an Eclipse screen capture of the applet project.

Hello Internet Java Applet





Top | Environment
API Specifications | Eclipse