Java Beyond Drjava Announcements for This Lecture
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 25 Java Beyond DrJava Announcements for This Lecture This Week Next Week • Reading: Chapter 16 • Submit a course evaluation • Assignment A6 graded . Will get an e-mail for this . Mean: 86.8, Median: 90 . Part of the “participation . Mean: 10.5h, Median: 10h grade” (e.g. clicker grade) th • No new lab this week • Final, May 10 9:00-11:30 . Review posted later this week . Turn in lab from last week . Work on assignment A7 • Conflict with Final Exam? . e.g. > 2 finals in 24 hours • Assignment A7 due Saturday . Submit conflicts on CMS Announcements for This Lecture This Week Next Week • Reading: Chapter 16 • SubmitReview asessions course nextevaluation week • Assignment A6 graded . WillStill lining get an up e-mail times for this . Mean: 86.8, Median: 90 . Part3 sessions/day of the “participation in 1 hour slots . Mean: 10.5h, Median: 10h . Monday,grade” (e.g. Tuesday clicker 1-4 grade) . Either Sundayth or Wednesday • No new lab this week • Final, May 10 9:00-11:30 • Topics. Review posted posted Thursday later this week . Turn in lab from last week . Work on assignment A7 • Conflict with Final Exam? . e.g. > 2 finals in 24 hours • Assignment A7 due Saturday . Submit conflicts on CMS Steganography Observation message • Most you preferred end /** terminates markers to using length … • But few cons to length * Format: ## message ## . Only requires two pixels" */ (e.g. <= 999,999) public ImageProcessor { . Hard part: conversion … • Markers okay if not printable } . Non-printable chars: <= 32 . Or 3-digits numbers > 255 • Bad if marker is in message Tried to “hide” your source code . reveal will terminate early 05/01/12 Beyond DrJava 4 Java Outside the Interactions Pane • Every Java program is either an application or an applet. public class C { • Application: class with a … special static method (main) public static void main(String[] args) { • Run the application by // top method to invoke invoking this method … . Interactions pane } . OS command line … . Double-clicking on it? } The parameter, an array of Strings, is " used to pass information to the program 05/01/12 Beyond DrJava 5 Executing Java from Command Line Java Code Command Line public class C { > cd <folder> … public static void main(String[] args) { (moves to that folder) // top method to invoke > dir (Windows) or ls (OS X … (list of files) } … > java C } (executes C.main(null)) Can type in Interactions page 05/01/12 Beyond DrJava 6 “Simplest” Java Application public class Simple { public static void main(String[] args) { System.out.println(“Hello World”) } } Execute with “java Simple” 05/01/12 Beyond DrJava 7 Writing a Java Application: Classic Way edits the javac C.java .java file Text Editor Command Line java C Command Line 05/01/12 Beyond DrJava 8 Applications: A Slightly Harder Way Eclipse IDE The standard Java IDE : 05/01/12 Beyond DrJava 9 To Use an IDE or Not? Advantages Disadvantages • Organize all your classes • Overwhelming! . MVC needs multiple classes . Organize them as a “Project” • Auto-generated code . GUI design . API auto-completion • Sometimes you just want a • Interactive debugging single, simple class . Breakpoints . No Projects . Variable watches . No “workspaces” 05/01/12 Beyond DrJava 10 Java JAR Files • Goal: “double-clickable” app • JAR: Java Archive File . Compressed file collection . Similar to a ZIP file . Except it can be executed • Jar files contain • manifest: noun . All the necessary class files . list of passengers . Any image or sound files . invoice of cargo . Any other necessary files • Identifies the class with main . A manifest file . Might have more than one 05/01/12 Beyond DrJava 11 Executing a Java File • Double-click it! • Command line • Supported in most OSs • Type:" . But error if no manifest java -jar <jar-file> 05/01/12 Beyond DrJava 12 Creating a JAR File 1. Navigate to the directory that contains the .class files. 2. Create a text file x.mf with one line (ending in a line-feed): Main-class: <name of class> 3. In the directory, type: jar -cmf x.mf app.jar *.class *.au Create anything else? Manifest name of " name of file " expands to name " manifest file to create all the .class files File 05/01/12 Beyond DrJava 13 Inspecting JAR File Contents • List files in a Jar file: > jar tf acm.jar acm/graphics/ jar -tf images.jar acm/graphics/G3DRect.class acm/graphics/ArcRenderer.class acm/graphics/GArc.class acm/graphics/GMouseEvent.class File acm/graphics/GCanvasListener.class type (list) acm/graphics/GCanvas.class name of the acm/graphics/GCompound.class jar file acm/graphics/GIterator.class acm/graphics/GContainer.class acm/graphics/GDimension.class acm/graphics/GFillable.class … 05/01/12 Beyond DrJava 14 Applets vs. Applications public class C { • Applet: Java program public static void main(String[] args) { run in a web browser …} . Needs an html page } application import javax.swing.*; Four inherited procedures: public class A extends JApplet { • called to initialize public void init() { … } • called to start processing public void start() { …} • called to stop processing public void stop() { … } public void destroy() { … } • called to destroy resources " (just before killing the applet) } applet 05/01/12 Beyond DrJava 15 TemperatureConverter Example Application Latest Version Applet can be both! private void initAsApplication() { JFrame frame =" public void init() {! new JFrame("Temperature Converter");! !getContentPane().add(view);! frame.setDefaultCloseOperation(" } ! JFrame.EXIT_ON_CLOSE );! frame.getContentPane().add(view);! public void start() { /* Do nothing */ }! frame.pack();! public void stop() { /* Do nothing */ }! frame.setVisible(true);! }! public void destroy() { /* Do nothing */ }! 05/01/12 Beyond DrJava 16 An Applet HTML Page <html>! tags !<head>! ! !<title>FacultyApplet</title>! <html> start an html page !</head>! <head> start the “heading” !<body>! <title> the title for the page !<h2>This is an <i>Applet!</i></h2>! !<p>! <body> start the body, content, of the page !<applet archive="temperature.jar” " code="converter.TemperatureConverter"" <hx> begin heading level x width="600" height="100">! <p> begin a paragraph !</applet>! !</p>! <b> begin boldface </body>! <i> begin italics </html>! <applet> start a Java applet 05/01/12 Beyond DrJava 17 What Happened to Applets? The Browser Wars Modern Day Web • Java supported as “plug-in” • Browsers support Javascript . Java controlled by Sun" . Very different language! (now Oracle) . But is what Java “promised” . Browsers made by 3rd party . Name for marketing reasons • Could not ensure up to date • Java is used on the back-end . Install is harder than Flash . e.g. code on the servers . Requires OS-level access • GWT: Google Web Toolkit . Think about your install! . Java for browser & server • People no longer bothered . Browser side code compiles . Applets almost non-existent to JavaScript (can do that!)" 05/01/12 Beyond DrJava 18 .