datalogi a 16: 21/11. java play sounds in applications compiling to jar-files compiling to exe-files...
TRANSCRIPT
JavaPlay 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); }}
SoundplayClip(Clip clip) starts the sound, and
returns
loadClip(String filename) format: .au, .wav, .aiff
stopClip(Clip clip)
loopClip(Clip clip,int n)
Jar filesCompress 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 resourcesBufferedImage 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 resourcesAccess 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 resourcesPut 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”.
JSmoothJava 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 JSmoothSkeleton: 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
Java: reading from a fileHow 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: exceptionstry{
//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 fileScanner 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 fileimport 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/Oimport 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.)
SortingThe easy principle:
Find the smallest and place at index 0,
Find the smallest of the remaining and place at index 1,…
SortingSorting 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 0mssize 2000 time 20mssize 3000 time 20mssize 4000 time 40mssize 5000 time 70mssize 6000 time 90mssize 7000 time 130mssize 8000 time 161mssize 9000 time 200mssize 10000 time 260ms
Java: overviewVariables:
a type and a name
Simple types:
int, float, double, boolean, String, char
Objects:
a class name
Arrays:
a type + ”[]”
Java: variablesLocal variable:
void method(){
int i =64;
while(i>0){
System.out.println(i);
i=i/2;
}
}
Java: parametersLike 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 objectsclass 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 fieldsOne 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: statementsif- 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: arraysint[] 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);