Dam pozostałe klasy dla pewności, bo właśnie to co podałem - to jest klasa z timerem wystartowanym który działa z częstotliwością obliczoną na podstawie podanej ilości bpm przez użytkownika.
Klasa główna:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author Damian
*/
public class Main extends MIDlet implements CommandListener {
public Main() {
design = new InterfaceDesign();
Command exit = new Command("Koniec", Command.EXIT, 0);
design.addCommand(exit);
startButton = new Command("Start/Stop", Command.OK, 0);
design.addCommand(startButton);
design.setCommandListener(this);
Display mobileDisplay = Display.getDisplay(this);
mobileDisplay.setCurrent(design);
}
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable s) {
if(c.getCommandType() == Command.EXIT)
notifyDestroyed();
if(c.getCommandType() == Command.OK) {
design.changeState();
}
}
private InterfaceDesign design;
private Command startButton;
}
Klasa w której startuje timer, rysuje gui i takie tam.
import javax.microedition.lcdui.*;
import java.util.Timer;
/**
* @author Damian
*/
public class InterfaceDesign extends Canvas implements CommandListener {
/**
* constructor
*/
public InterfaceDesign() {
metronomState = "Start";
isWorking = Boolean.FALSE;
bpmString = "90";
howManyBpm = 90;
calculateInterval = 60000/(howManyBpm);
timer = new Timer();
metrum = 3;
metrumString = "3";
try {
// Set up this canvas to listen to command events
setCommandListener(this);
// Add the Exit command
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* paint
*/
public void paint(Graphics g) {
Font f1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
Font f2 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
Font f3 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 0);
g.drawString(metronomState, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.LEFT);
g.drawString("BPM: " + bpmString, getWidth()/2, (getHeight()/2)-20, Graphics.TOP | Graphics.LEFT);
g.drawString("Metrum: " + metrumString, getWidth()/2, (getHeight()/2)-40, Graphics.TOP | Graphics.LEFT);
g.drawString("Metronom v3.0\nDamian Smilgin(C)", 0, 0, Graphics.TOP | Graphics.LEFT);
}
/**
* Called when a key is pressed.
*/
protected void keyPressed(int keyCode) {
if(keyCode == -5) {
changeState();
repaint();
}
else if(keyCode == -1 && isWorking == Boolean.FALSE) {
howManyBpm++;
bpmString = Integer.toString(howManyBpm);
repaint();
}
else if(keyCode == -2 && isWorking == Boolean.FALSE) {
howManyBpm--;
bpmString = Integer.toString(howManyBpm);
repaint();
}
else if((keyCode == -4) && (isWorking == Boolean.FALSE)) {
metrum++;
metrumString = Integer.toString(metrum);
repaint();
}
else if((keyCode == -3) && (isWorking == Boolean.FALSE) && (metrum > 2)) {
metrum--;
metrumString = Integer.toString(metrum);
repaint();
}
}
public void changeState() {
if(isWorking == Boolean.FALSE) {
isWorking = Boolean.TRUE;
metronomState = "Stop";
startMetronome();
}
else if(isWorking == Boolean.TRUE) {
isWorking = Boolean.FALSE;
metronomState = "Start";
stopMetronome();
}
repaint();
}
protected void startMetronome() {
calculateInterval = 60000/(howManyBpm);
startTimerEvents = new MetronomeTimerEvent(metrum);
timer.schedule(startTimerEvents, 1000, calculateInterval);
}
protected void stopMetronome() {
startTimerEvents.cancel();
}
/**
* Called when a key is released.
*/
protected void keyReleased(int keyCode) {
}
/**
* Called when a key is repeated (held down).
*/
protected void keyRepeated(int keyCode) {
}
/**
* Called when the pointer is dragged.
*/
protected void pointerDragged(int x, int y) {
}
/**
* Called when the pointer is pressed.
*/
protected void pointerPressed(int x, int y) {
}
/**
* Called when the pointer is released.
*/
protected void pointerReleased(int x, int y) {
}
/**
* Called when action should be handled
*/
public void commandAction(Command command, Displayable displayable) {
}
private String metronomState;
public Boolean isWorking;
private int howManyBpm;
private String bpmString;
private int calculateInterval;
private Timer timer;
private MetronomeTimerEvent startTimerEvents;
public byte metrum;
public String metrumString;
}
Więc nie bardzo wiem jak dodać ten drugi timer w istniejącym już timerTask i jeszcze by działał cyklicznie.