JAVA Beginners part 15

Java Sound
1)get a sequencer
sequencer player = midisystem.getsequencer();

2)make a new sequence
sequence seq = new sequence(timing,4);

3)get a new track
track t = seq.createTrack();

4)fill in the track with midievents

the actual instructions of a song is held in the message.


144 says the message type, on or off (128)
1 says the channel like drum or piano
44 note to play, like of notes from 0 to 127
100 velocity, how fast and hard the key needs to be played.

message says what to do and the midievent says when to do it

Notes on exception
run time exceptions may not be handled.
declaration is sufficient , try/catch is not mandatory.
it is not mandatory to handle all exception , worst case the JVM may shut down.
not necessary to try/catch all the exceptions a declaration will be enough.


JFrame is the object that represents a window on the screen, you put all the buttons,check boxes etc.
Once you have a JFrame, you can put things in it by adding them. those things can be found in javax.swing package.


Simple Steps to make a GUI

1) Make a Frame.
JFrame frame = new JFrame();

2) Make a widget.
JButton button = new JButton(“click me”);

3) Add the widget to the frame. You dont add things to frame directly, think of frame as the trim around the window and you add things to the window pane.

4) display


How can i make sure something happens when i click?

a) a method to run when user clicks
b) a way to know when to trigger that method, basically to know when the user clicks

set the button to change its text when its clicked.

public void changeit()
button.setTest(” I have been clicked”);

The process of getting and handling a user event is called Event Handling. There a lot event types in Java.
Lot of them involve GUI user actions.

if you want your program to listen to events, then you need to implement an interface.
a listening interface is the bridge between the listener(you) and the source(button)
interface is where the call back method is declared.

an event is an object, user actions are converted into events which means objects.
java.awt.event packages has a list of event classes like mouse event, key event, action event and others.

event source like a button create an event object. it is likely that a lot of the times you will be listening to these events.
***** every event type has a matching Interface *****
mousevents has mouselistener interface
windowevents has windowlistener interface

point to note all these are interfaces , meaning when you implement an interface then you will have to define the methods in your code. 

re phrase. if class wants to know about button events then it has to implement the action listener interface.


things that you can do to the widget that has been added to the JFrame,
like a button or a menus etc

draw 2d graphics on a widget

put a JPEG on a widget.




Using graphics Object , we can also reference subclass of graphics which is graphics2d class which is its subclass through polymorphism.

There are lot things that we can do graphics2d that we cannot do with graphics.

Little bit of recap.

Using Super class reference.
Animal a = new dog();
a.bark() wont work, because the super class does not have a bark method. But you can do the below workaround

Dog d = (Dog)a;

Same logic is being applied to graphics here , pull out the dog out of the animal.

Graphics2d g2d = (Graphics2d) g;

Methods that can be referenced using Graphics


Methods that can be referenced using Graphics2D



the right way to add a widget to a frame is



how do you get action events for two different buttons when each button needs to do something different?
after checking various options, here is a new technique.

An inner class can use all the methods and variables of the outer class even the private ones. The Inner class gets to use those variables and methods just as if the methods and variables were declared within the inner class.

class outerclass
         class innerclass

The instance creation technique.
class myouter
private int x;

myinner inner = new myinner();

            public void dostuff()

           class myinner
                 void go()



Uses of the Inner class apart from the Interface example.
If you want the Dog to be an Animal and Button at the same time. That is not possible because
Dog can extend only one class that is Animal. So how do we make the Dog behave like a Button.
We can have a inner class thats acts on Dog’s behalf as a Button.

Outer class can be referenced to other inner classes but the inner class is tied to the one and only one outer class.

next page 400

More in next part.


Head First Java 2nd Edition




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s