1 / 16

Introduction to Java

Introduction to Java. In this section we will learn how how to use Java and write our first Java Applet :  The Java Development Kit  Running a Java Program  Java Applets  Creating an Applet  Creating the Web Page  Running the Program  Running in a Web Browser

harry
Download Presentation

Introduction to Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development Kit  Running a Java Program  Java Applets  Creating an Applet  Creating the Web Page  Running the Program  Running in a Web Browser  What does it mean?  Things to remember Introduction to Java

  2. The Java Development Kit Java was developed by Sun Microsystems in 1995. To use Java we will use the Java 2 Platform Standard Edition (J2SE) Software Development Kit (J2SDK) (previously the Java Development Kit (JDK)). There are several (confusing) version of Java: Java 1 : JDK 1.0.x, JDK 1.1.x Java 2 : J2SDK 1.2.x, J2SDK 1.3.x, J2SDK 1.4.x, ... We will be using J2SDK 1.4.0 Note: Not all web browsers support all versions. You can download the JDK free from http://java.sun.com/ However for Windows it is 35MB plus 30MB for the documentation! Introduction to Java

  3. You save the file with a .java extension You write Java code using an editor Text Editor Java code: MyProg.java You run the Java compiler 'javac' javac MyProg.java This creates a file of bytecode with a .class extension Bytecode: MyProg.class You execute the bytecode with the command 'java' java MyProg Output Running a Java Application Introduction to Java

  4. Java Applets • Applets are programs designed to run as part of a Web Page (Applet = little application). • Applets are similar to normal Java Applications but have extra security features to prevent a downloaded Applet damaging your computer or transmitting information from it. For instance an Applet cannot: • Access local files • Delete local files • Run another program • Find out your name • Connect to another host Introduction to Java

  5. Java code: MyApp.java Web page: MyApp.html Bytecode: MyApp.class Text Editor Window Running a Java Applet You save the file with a .java extension You write Java code using an editor Text Editor This creates a file of bytecode with a .class extension You run the Java compiler 'javac' javac MyApp.java You save the file with a .html extension You can view the web page from a web browser You write a web page in html using an editor Web Browser You can view the applet with the command 'appletviewer' appletviewer MyApp.html Introduction to Java

  6. Creating an Applet  Open "Notepad" (Start  Programs  Other  Notepad) import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 50, 50); } }  Type this in:  Save As "Greetings.java" (Put the " " round the name otherwise it adds .txt to the end!)  Open a DOS Window (Start  MS-DOS Prompt) G:\> javac Greetings.java G:\>  Type javac Greetings.java  If you type dir Greetings.* you should see Greetings.java and Greetings.class If it gives an error check you typed it in exactly right. Introduction to Java

  7. Creating the Web Page • In order to run an applet you have to embed it in a web page using a special <applet> tag e.g: <applet code="name.class" width=www height=hhh></applet> Using Notepad type in the following and save it as "Greetings.html": Size of the applet in pixels <html> <head> <title>Greetings Applet</title> </head> <body> <applet code="Greetings.class" width=300 height=200 ></applet> </body> </html> Introduction to Java

  8. Running the Program In the DOS window type appletviewer Greetings.html G:\> appletviewer Greetings.html You should see something like this: Introduction to Java

  9. Running in a Web Browser In Netscape go to the File menu then Open Page ... Press Choose File... Find your file Greetings with the Netscape symbol alongside it (Greetings.html) - click on it and press Open (or double click on it) Back in the Open Page dialog press Open You should see something like: Title Your greeting Message Introduction to Java

  10. What does it mean? These 2 lines tell the computer to include (import) two standard libraries awt (Abstract Window Toolkit) and applet. This line announces that the program (class) can be run by anyone (public), is called Greetings and is an Applet. import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 50, 50); } } This line declares what follows in the { } as a method called paint. This line tells the computer to display some text ( a string) on the screen. This is where it is displayed in pixels across and down from the top left hand corner This is what is displayed Introduction to Java

  11. Things to remember Everything in Java is case sensitive - Paint is not the same as paint. The name of the class i.e. Greetings should match the name of the file Greetings.java (not greetings.java). Curly brackets { and } are used to group parts of the program called blocks together. Blocks can be nested inside other blocks but each { must be matched with a }. Most statements require a semi-colon ; at the end. A statement can continue on the next line if necessary. Spaces are not important - it is recommended to indent blocks for clarity. import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 50, 50); } } Introduction to Java

  12. An easier way -Java IDE's In this section we will briefly learn how about Integrated Development Environments (IDE) for Java:  What are IDE's  Metrowerks CodeWarrior  Borland JBuilder  Eclipse  PhysEdit Introduction to Java

  13. What are IDEs? • Integrated Development Environments are programs that are designed to simplify the development of large programs. Several features they might have are: • The ability to edit several files at once. • Syntax highlighting - different parts of the code are shown in different colours e.g. language keywords (public, class etc) in blue. • The ability to compile the code using a button or menu item and to see the messages in another window. • To run the program using a button or menu item. • To drag components from a 'toolkit' to a design window. • The ability to automatically generate lines of code. • To show a tree like view of your classes. • Extensive debugging facilities. Introduction to Java

  14. Java IDEs Visual Basic is an IDE. For C++ some popular IDEs are Borland C++ and Microsoft Visual C++. For Java there are several including Metrowerks, CodeWarrior, Borland JBuilder and Eclipse. They are really designed for large complex programs and are somewhat over complicated for simple programs. They are also hard to install on the Lancaster University servers! Steve Lloyd developed a simple IDE for a similar course called PhysEdit which has much less functionality than the others but is hopefully reasonably straightforward and sufficient for our needs. You can download it for your home computer if you wish (assuming you have Windows), although no liability is taken! Introduction to Java

  15. PhysEdit Introduction to Java

  16. PhysEdit - Creating a Project 1. Click on New Project (or File  New Porject...) 6. View the applet (executes appletviewer for you). 2. Enter the name of the project e.g. Greetings 5. Compile the code (executes javac for you). 3. The program creates some skeleton code and a html file. 4. Add your code to the Java file. Introduction to Java

More Related