> Interface > GUI construction > JM-550 Drum Machine: Simple AWT GUI | |||||||
JM-550 Drum Machine: Simple AWT GUIThis series of classes introduces a simple, but extendible, drum machine
program. This tutorial introduces the AWT (Abstract Windowing Toolkit) classes.
Here is the GUI generated by this program on a Mac: There are two other classes required for this tutorial, This tutorial concerns the class JM_550b. Click here to view the source. Lets have a closer look.
In order to use the awt classes we need to import two packages- java.awt.*;
and java.awt.event.*;
In the class declaration there are two important things to notice regarding the awt GUI. Firstly, the class extends the Frame class (java.awt.Frame) which allows this class to be a window. Secondly, it implements the ActionListener (java.awt.event.ActionListener) to capture and handle user interaction with the GUI. After declaring the class two class variables are declared - two buttons which will be used to start and stop playback. The main() method calls the constructor which draws the GUI (more about that below), then creates an instance of the EightBeat1 class which creates a drum pattern, and thridly the playback() method (which EightBeat1 inherited from Basic_550) is invoked, which does the obvious thing - hopefully starts playing the drum pattern over and over!
In the constructor all the GUI setup work is done. super("JM-550 Drum Machine"); is a call to run the constructor of the
Frame class, which is the super class of JM_550b because it extends it. A Panel is an awt class than is a blank area on which other componenrts can be put. A Panel instance called transport is created. Two button objects are created. One is given the name and title "Start"
and the other the name and title "Stop". The panel (now with its buttons) is added to the framed window with the line. this.add(transport); The keyword 'this' again refers to THIS class, the JM_550b class - which as you know IS an instance of Frame, because it extends the Frame class thus inherting the ability to be a window. Finally, this frame is packed (i.e. made as small as reasonable), and made visiable to the user.
To deal with clicks on the start and stop button we add functionality
to the actionperformed() method. The method is called automatically each time a user clicks on a button registered to the ActionListener - which our two buttons are. When called we check to see which button was clicked and make appropriate calls our pat (which is an instance of EightBeat1, which in turn is an extension of Basic_550). These buttons do the obvious thing: start and stop the drum loop from playing. Note that to avoid multiple streams of play, when the play button is clicked the Play class is checked to make sure nothing is already playing. If nothing is playing, the the playback method will be invoked. We now have a simple and somewhat groovy drum machine :) Move on to the next tutorial which adds a graphical
user interface to these classes: |
|||||||