280 likes | 379 Views
Datalogi A 16: 21/11. CS A. Java. Play sounds in applications Compiling to jar-files Compiling to exe-files Exceptions Sorting, reading from a file, writing to a file A short overview of java. Sound. <uses new version of JCanvas> import javax.sound.sampled.Clip; public class PlaySound{
E N D
Datalogi A 16: 21/11 CS A
Java Play sounds in applications Compiling to jar-files Compiling to exe-files Exceptions Sorting, reading from a file, writing to a file A short overview of java
Sound <uses new version of JCanvas> import javax.sound.sampled.Clip; public class PlaySound{ public static void main(String args[]){ Clip crash = JCanvas.loadClip("crash.au"); JCanvas.playClip(crash); JCanvas.sleep(4000); } }
Sound playClip(Clip clip) starts the sound, and returns loadClip(String filename) format: .au, .wav, .aiff stopClip(Clip clip) loopClip(Clip clip,int n)
Jar files Compress and collect several files in an archive (zip like) >jar cf HelloWorld.jar HelloWorld.class Run it: >java –cp HelloWorld.jar HelloWorld cp: classpath: HelloWorld.jar, Main class: HelloWorld
Access to resources BufferedImage image = JCanvas.loadImage(”pict.jpg”); Clip sound = JCanvas.loadClip(”sound.wav”); Find it in local directory – same as the class file. More robust approach:
Access to resources Access from a URL: BufferedImage image = JCanvas.loadImage( PlaySound.class.getResource(”pict.jpg”)); Clip sound = JCanvas.loadClip( PlaySound.class.getResource(”sound.wav”)); Find the main class (PlaySound), from that class create a URL of a ressource local to that class.
Jar files with resources Put class files an resources in a jar file: > jar cf PlaySound.jar *.class *.au > java –cp PlaySound.jar PlaySound Works if you access sounds using ”getResource”.
JSmooth Java program launcher: JSmooth: takes a .jar file, an .ico file and generates an exe file. The exe file locates a java runtime environment on the machine and run the jar-file
Running JSmooth Skeleton: Windowed wrapper Executable: Executable Binary: full name of exe file Executable Icon: full name of icon file Current Directory: full name of directory Application Main Class: main class Embedded jar: yes, jar file System: save as Project: compile
Jar to exe file PlaySound.exe can be played on other computers with jre (java runtime environment)
Java: reading from a file How to open a file for reading: Scanner in=null; try{ in=new Scanner(new File("text.txt")); }catch(IOException e){ System.out.println("Cannot open file text.txt"); System.exit(0); } Scanner can now be used as with keyboard input
Java: exceptions try{ //try block }catch(Exception e){ // handle exceptions from try-block } Examples of exceptions: null pointer de-reference, class cast, array index, Arithmetic exception (division by zero).
Read lines from a file Scanner in=null; try{ in=new Scanner(new File("text.txt")); }catch(IOException e){ System.out.println("Cannot open file text.txt"); System.exit(0); } ArrayList<String> data=new ArrayList<String>(); while(in.hasNextLine()){ data.add(in.nextLine()); } in.close();
Write to a file import java.io.*; … PrintWriter out= null; try{ out=new PrintWriter(new FileWriter("text1.txt")); }catch(IOException e){ System.out.println("Cannot write to file text1.txt"); System.exit(0); } for(String s:data) out.println(s); out.close();
Java I/O import java.io.*; class File, class PrintWriter File: make directory listings: boolean isDirectory() is the file a directory Long length size of a file(bytes) String[] list() (list of files in a dir.)
Sorting The easy principle: Find the smallest and place at index 0, Find the smallest of the remaining and place at index 1,…
Sorting Sorting algorithm: int[] data=new int[10]; for(int i=0;i<data.length;i++) data[i]=random(1000); for(int i=0;i<data.length;i++){ int j=indexSmallest(data,i); swap(data,i,j); }
Index of smallest in array static int indexSmallest(int[] data,int from){ int index=from; for(int i=from+1;i<data.length;i++) if(data[i]<data[index])index=i; return index; } static void swap(int[] data,int i1, int i2){ int h=data[i1]; data[i1]=data[i2]; data[i2]=h; }
Timing the sorting algorithm size 1000 time 0ms size 2000 time 20ms size 3000 time 20ms size 4000 time 40ms size 5000 time 70ms size 6000 time 90ms size 7000 time 130ms size 8000 time 161ms size 9000 time 200ms size 10000 time 260ms
Java: overview Variables: a type and a name Simple types: int, float, double, boolean, String, char Objects: a class name Arrays: a type + ”[]”
Java: variables Local variable: void method(){ int i =64; while(i>0){ System.out.println(i); i=i/2; } }
Java: parameters Like local variables – but initialised at the call int sqare(int x){return x*x;} You may change the value of a parameter in the method – but it will not change the argument in the call.
Java: fields in objects class point{ int x,y; Point(int x1,int y1){ x=x1; y=y1 } } public class Application{ public static void main(String args[]){ Point p=new Point(2,3); }}
Java: static fields One instance per class. You may use them from static methods. public class Program{ static int data[]=new int[10]; public static void main(String args[]){ data[0]=1; …. } }
Java: statements if- if(x<0)x=-x; While while(x>1){y=y*2;x--;} for- for(int i=0;i<10;i++).. Assignments- Method calls- System.out.println(”hello”); Blocks
Java: arrays int[] data =new int[10] Point[] points=new Points[20]; for(int i=0;i<data.length;i++)data[i]=i*i; for(Point p:points)p.setY(0);
Java: classes class content: • fields, • Static fields • Methods • Static methods • Constructors