Presentation is loading. Please wait.

Presentation is loading. Please wait.

PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development.

Similar presentations


Presentation on theme: "PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development."— Presentation transcript:

1 PHY281Introduction to JavaSlide 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

2 PHY281Introduction to JavaSlide 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/http://java.sun.com/ However for Windows it is 35MB plus 30MB for the documentation!

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

4 PHY281Introduction to JavaSlide 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

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

6 PHY281Introduction to JavaSlide 6 Creating an Applet  Open "Notepad" (Start  Programs  Other  Notepad)  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)  Type javac Greetings.java G:\> javac Greetings.java G:\> If it gives an error check you typed it in exactly right. import java.awt.*; import java.applet.Applet; public class Greetings extends Applet { public void paint(Graphics g) { g.drawString("Hello World!", 50, 50); }  If you type dir Greetings.* you should see Greetings.java and Greetings.class

7 PHY281Introduction to JavaSlide 7 Creating the Web Page In order to run an applet you have to embed it in a web page using a special tag e.g: Greetings Applet Using Notepad type in the following and save it as "Greetings.html": Size of the applet in pixels

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

9 PHY281Introduction to JavaSlide 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

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

11 PHY281Introduction to JavaSlide 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); }

12 PHY281Introduction to JavaSlide 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

13 PHY281Introduction to JavaSlide 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.

14 PHY281Introduction to JavaSlide 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!

15 PHY281Introduction to JavaSlide 15 Metrowerks CodeWarrior

16 PHY281Introduction to JavaSlide 16 Borland JBuilder

17 PHY281Introduction to JavaSlide 17 Eclipse See www.eclipse.org

18 PHY281Introduction to JavaSlide 18 PhysEdit

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


Download ppt "PHY281Introduction to JavaSlide 1 Introduction to Java In this section we will learn how how to use Java and write our first Java Applet:  The Java Development."

Similar presentations


Ads by Google