Introduction To Multimedia
Introduction To Multimedia
1. Java Review
Java class structure
◼ Class {
fields
constructors
methods
main method
}
Arrays 3
Inheritance
◼ Subclass: using extends keyword followed by
the name of its superclass
5
Interface
◼ The main structural element in Java that
enforces an application programming interface
(API) is an interface.
◼ An interface is a collection of method
declarations with no data and no bodies.
6
interface Drawable{
void draw();
}
7
Abstract class
8
interface A{
void a();
void b();
void c();
void d();
}
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am d");}
}
9
Exceptions
◼ Exceptions are unexpected events that occur
during the execution of a program.
10
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}
11
Generics
◼ Java includes support for writing generic classes and
methods that can operate on a variety of data types
while often avoiding the need for explicit casts.
◼ The generics framework allows us to define a class in
terms of a set of formal type parameters, which can
then be used as the declared type for variables,
parameters, and return values within the class
definition.
◼ Those formal type parameters are later specified when
using the generic class as a type elsewhere in a
program.
12
public class TestGenerics{
public static < E > void printArray(E[] elements) {
for ( E element : elements){
System.out.println(element );
}
System.out.println();
}
public static void main( String args[] ) {
Integer[] intArray = { 10, 20, 30, 40, 50 };
Character[] charArray = { 'J', 'A', 'V', 'A', 'T','P','O','I','N','T' };
13
Nested Classes
◼ Java allows a class definition to be nested inside the
definition of another class.
◼ The main use for nesting classes is when defining a
class that is strongly affiliated with another class.
❑ This can help increase encapsulation and reduce undesired
name conflicts.
◼ Nested classes are a valuable technique when
implementing data structures, as an instance of a
nested use can be used to represent a small portion
of a larger data structure, or an auxiliary class that
helps navigate a primary data structure.
14
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
15
DrJava
https://drjava.sourceforge.net/
16
2. Introduction to Multimedia
18
19
Media
20
Media type
21
◼ Each medium has its own characteristics,
leading to distinctive strengths and
weaknesses.
◼ Always choose the most appropriate medium
for your purpose.
22
Multimedia
23
24
25
◼ Digital multimedia can interact with other
sorts of data and computation, serving as a
user interface to databases and applications
◼ Multimedia is a relatively immature
technology, although its adoption is
accelerating with the increasing power of
computer systems
26
◼ Bit: unit of data
◼ Byte: eight bits
27
◼ Compression must often be applied to media
data
❑ Lossless
❑ Lossy
28
29
Digital Representation of Media
30
Working with Media
1. Drawing
31
❑ class Turtle: Turtle.java
❑ class World: World.java
❑ class Turtle inherits from a class called
SimpleTurtle
◼ Creating Objects
32
World worldObj = new World();
System.out.println(worldObj);
Turtle turtle1 = new Turtle(worldObj);
System.out.println(turtle1);
Turtle turtle2 = new Turtle(30,50, worldObj);
System.out.println(turtle2);
turtle1.forward(20);
turtle1.turnLeft();
turtle1.forward(100);
turtle1.turnRight();
33
turtle1.forward(40);
turtle1.turn(-45);
turtle1.forward(30);
turtle1.turn(90);
turtle1.forward(20);
turtle2.turnRight();
turtle2.forward(200);
turtle2.turnRight();
turtle2.forward(200);
34
World world1 = new World();
Turtle turtle1 = new Turtle(world1);
System.out.println(turtle1);
turtle1.setName(“Jane”);
turtle1.turnRight();
turtle1.forward(400);
System.out.println(turtle1);
System.out.println(world1.getWidth());
35
World world1 = new World();
Turtle turtle1 = new Turtle(50,50,world1);
turtle1.turnRight();
turtle1.forward(30);
turtle1.turnRight();
turtle1.forward(30);
turtle1.turnRight();
turtle1.forward(30);
turtle1.turnRight();
turtle1.forward(30);
36
turtle1.penUp();
turtle1.moveTo(200,200);
turtle1.hide();
turtle1.penDown();
turtle1.turnRight();
turtle1.forward(30);
turtle1.turnRight();
turtle1.forward(30);
turtle1.turnRight();
turtle1.forward(30);
turtle1.turnRight();
turtle1.forward(30);
turtle1.show();
37
◼ Creating Methods
38
World world1 = new World();
Turtle turtle1 = new Turtle(world1);
turtle1.drawSquare();
System.out.println(world1.getClass());
System.out.println(turtle1.getClass());
39
public void drawSquare2()
{
int width=30;
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
}
40
public void drawSquare(int width)
{
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
this.turnRight();
this.forward(width);
}
41
2. Pictures
42
Open a picture and show it
Method 1: using method directly
new Picture(FileChooser.pickAFile()).show();
43
❑ Method 2: declare a variable
44
2. Sounds
new Sound(FileChooser.pickAFile()).play();
45
◼ Backslashes and Slashes
❑ Java can figure out the path name when you use
slashes
❑ You can still use backslashes in the full path
name, but you need to double each one
46
>String myFileName = FileChooser.pickAFile();
> System.out.println(myFileName);
C:\intro-prog-java\mediasources\katie.jpg
47
String myFileName =
"C:/intro-prog-java/mediasources/katie.jpg";
String myFileName =
"C:\intro-prog-java\mediasources\katie.jpg";
String myFileName =
"C:\\intro-prog-java\\mediasources\\katie.jpg";
48