/*** * * Copyright (C) 2008 Alessandro La Rosa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Contact: alessandro.larosa@gmail.com * * Author: Alessandro La Rosa */ import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; public class SlideIconsMenu { // selected item index public int selectedIndex = 0; // icon label color public int textColor = 0xff0000; // menu bg color public int bgColor = 0xffffff; // icon label font public Font textFont = Font.getDefaultFont(); // menu right and left Images public Image slideRightImage = null; public Image slideLeftImage = null; // menu size int width = 0; int height = 0; // item labels String[] labels = null; // item icons Image[] icons = null; // previous item index (during menu translation) int prevIndex = 0; // menu sliding translation properties public int translationDuration = 500; long startTranslationTime = 0; public SlideIconsMenu(String[] labels, Image[] icons, int width, int height) throws Exception { this.width = width; this.height = height; this.labels = labels; this.icons = icons; slideRightImage = Image.createImage("/slide_right.png"); slideLeftImage = Image.createImage("/slide_left.png"); } public void slideItem(int delta) { if(!isTranslating() && selectedIndex + delta >= 0 && selectedIndex + delta < labels.length) { prevIndex = selectedIndex; selectedIndex += delta; startTranslationTime = System.currentTimeMillis(); } } public boolean isTranslating() { return prevIndex != selectedIndex; } public void paint(Graphics g) { g.setColor(bgColor); g.fillRect(0, 0, width, height); g.setColor(textColor); if(selectedIndex > 0) { g.drawImage(slideLeftImage, 2, height / 2, Graphics.LEFT | Graphics.VCENTER); } if(selectedIndex < icons.length - 1) { g.drawImage(slideRightImage, width - 2, height / 2, Graphics.RIGHT | Graphics.VCENTER); } g.drawString(labels[selectedIndex], width / 2, height - 2, Graphics.BOTTOM | Graphics.HCENTER); g.setClip(slideLeftImage.getWidth(), 0, width - 2 * slideLeftImage.getWidth(), height); if(selectedIndex != prevIndex) { int diff = (int)(System.currentTimeMillis() - startTranslationTime); if(diff > translationDuration) { diff = translationDuration; } int coeff = selectedIndex > prevIndex ? 1 : - 1; int currentX = width / 2 - coeff * diff * width / translationDuration; int nextX = currentX + width * coeff; g.drawImage(icons[prevIndex], currentX, height / 2, Graphics.VCENTER | Graphics.HCENTER); g.drawImage(icons[selectedIndex], nextX, height / 2, Graphics.VCENTER | Graphics.HCENTER); if(diff >= translationDuration) { prevIndex = selectedIndex; } } else { g.drawImage(icons[selectedIndex], width / 2, height / 2, Graphics.VCENTER | Graphics.HCENTER); } } }