0% found this document useful (0 votes)
3 views48 pages

Introduction To Multimedia

Uploaded by

Ted tan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views48 pages

Introduction To Multimedia

Uploaded by

Ted tan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Introduction

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

◼ In Java, each class can extend exactly one


other class. A superclass may have many
subclasses

◼ By default, it automatically inherits from a


class java.lang.Object, which serves as the
universal superclass in Java
4
class Animal{
void eat(){System.out.println("eating...");}
}

class Dog extends Animal{


void bark(){System.out.println("barking...");}
}

class BabyDog extends Dog{


void weep(){System.out.println("weeping...");}
}

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.

◼ Interfaces do not have constructors and they


cannot be directly instantiated.
❑ When a class implements an interface, it must implement all of
the methods declared in the interface.

6
interface Drawable{
void draw();
}

class Rectangle implements Drawable{


public void draw(){System.out.println("drawing
rectangle");}
}

class Circle implements Drawable{


public void draw(){System.out.println("drawing circle");}
}

7
Abstract class

◼ Abstract class serves a role somewhat between


that of a traditional class and that of an
interface

◼ An abstract class may define signatures with


empty body, and may define one or more fields
and some methods with implementation.

◼ Abstract classes is limited to single inheritance

8
interface A{
void a();
void b();
void c();
void d();
}

abstract class B implements A{


public void c(){System.out.println("I am c");}
}

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.

◼ An exception might result due to an unavailable


resource, unexpected input from a user, or
simply a logical error on the part of the
programmer.

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' };

System.out.println( "Printing Integer Array" );


printArray( intArray );

System.out.println( "Printing Character Array" );


printArray( charArray );
}
}

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

◼ DrJava is a simple editor (tool for entering


program text) and interaction space so that
you can try things out in DrJava and create
new programs (methods) and classes.
◼ DrJava is available for free under the DrJava
Open Source License

https://drjava.sourceforge.net/

16
2. Introduction to Multimedia
18
19
Media

◼ Information can be conveyed in the form


of different media
❑ Text
❑ Still images
❑ Web pages
❑ Video
❑ Sound

20
Media type

◼ Time-based media: exhibit change over time


❑ Video
❑ Animation
❑ Sound
◼ Static media: do not exhibit change over time
❑ Still images
❑ Text

21
◼ Each medium has its own characteristics,
leading to distinctive strengths and
weaknesses.
◼ Always choose the most appropriate medium
for your purpose.

22
Multimedia

◼ Media may be combined into multimedia


❑ World Wide Web: combines text, images, and
time-based media

◼ Digital media can be manipulated as data by


programs
❑ Users can interact with digital multimedia in
novel ways, leading to non-linear structures

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

◼ Groups of bits can be interpreted as numbers


to base 2, but can also be treated as
characters, colours, etc.

27
◼ Compression must often be applied to media
data
❑ Lossless
❑ Lossy

◼ Different compression algorithms are


applicable to different types of media data.
Their effectiveness depends on the
characteristics of the data itself.

28
29
Digital Representation of Media

◼ There are established ways of representing


images, video, animation, sound and text in
bits

30
Working with Media

1. Drawing

❑ In Java each new class is usually defined in a file


with the same name as the class and an
extension of “.java".
❑ Class names start with a capital letter and the first
letter of each additional word is capitalized.

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

public void drawSquare()


{
this.turnRight();
this.forward(30);
this.turnRight();
this.forward(30);
this.turnRight();
this.forward(30);
this.turnRight();
this.forward(30);
}

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

Picture picture1 = new Picture();


picture1.show();

Create an empty picture


The default width is 200
The default height is 100
The default is that all of the pixels in the picture
are white

42
Open a picture and show it
 Method 1: using method directly

new Picture(FileChooser.pickAFile()).show();

43
❑ Method 2: declare a variable

String pictureFile = FileChooser.pickAFile();


Picture fish = new Picture(pictureFile);
fish.show();

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

> Picture myPicture = new Picture(myFileName);


> System.out.println(myPicture);
Picture, filename C:\intro-prog-
java\mediasources\katie.jpg height 360 width 381

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

You might also like