
Опытный
 
Профиль
Группа: Участник
Сообщений: 333
Регистрация: 21.11.2007
Репутация: 1 Всего: 1
|
Ну всё вроде готово =) Незнаю насколько изящно получилось, но это моё первое приложение которое не просто выводит форму или ещё что, а уже список с передвижением =) вот код может кому то пригодится! основной MIDlet | Код | package tcity;
import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class Tcity extends MIDlet implements CommandListener{
private Display display; private static Tcity instance; private Command exitCommand = new Command("Выход", Command.EXIT, 0); private Command backCommand = new Command("Назад", Command.BACK, 1); DrawList dl = new DrawList(); DrawMenu m = new DrawMenu(); //Создание подкласса DrawMenu public static Tcity getInstance() { if(instance==null) { instance = new Tcity(); } return instance; }
public void startApp() { instance = this; display = Display.getDisplay(this); display.setCurrent(m); // Сразу после загрузки мидлета переходит на класс DrawMenu }
public void commandAction(Command c, Displayable d) { if(c==exitCommand) { destroyApp(true); } if(c==backCommand) { Display.getDisplay(this).setCurrent(m); } }
public void showKey(int key) { display.getDisplay(this); if(key==1) { dl.addCommand(exitCommand); dl.addCommand(backCommand); dl.setCommandListener(this); display.setCurrent(dl); } if (key==2) { display.setCurrent(new ViewFormBookmarks("Закладки")); } if (key==3) { display.setCurrent(new ViewFormHelp("Помощь")); } if (key==4) { destroyApp(true); } }
public void pauseApp() {}
public void destroyApp(boolean _boolean) { notifyDestroyed(); } }
|
Canvas меню | Код | package tcity;
import javax.microedition.lcdui.*;
class DrawMenu extends Canvas{ //Вот и сам класс DrawMenu int index = 1; // Индекс выделенной кнопочки int heightScreen, widthScreen; // Берут разрешение экрана int xPosition; // Позиция "x" рисования меню int yPosition; // Позиция "y" рисования меню int stepY = 30; int columnH = 21; int fontSize = 20; int paddingStr = 2; public DrawMenu() { } public void paint(Graphics g) { heightScreen = g.getClipHeight(); //Определяем высоту окна widthScreen = g.getClipWidth(); //Определяем ширину окна xPosition = widthScreen/2; yPosition = heightScreen/5;
if(heightScreen < (stepY*3+columnH*4)) { stepY = 16; columnH = 16; paddingStr = 1; }
g.setColor(255, 255, 255); // Задаем цвет прямоугольника g.fillRect(0, 0, 300, 300); //Заливаем прямоугольник
g.setColor(55, 60, 62); g.drawString("Справочник", xPosition, yPosition + paddingStr, g.TOP | g.HCENTER); // Рисуем первую строку меню g.drawString("Закладки", xPosition, yPosition + stepY + paddingStr, g.TOP | g.HCENTER); // Рисуем второю строку меню g.drawString("Помощь", xPosition, yPosition + stepY * 2 + paddingStr, g.TOP | g.HCENTER); // Рисуем третью строку меню g.drawString("Выход" , xPosition, yPosition + stepY * 3 + paddingStr, g.TOP | g.HCENTER); // Рисуем третью строку меню if (index == 1) { // Если индекс равен 1 int yPos = yPosition + paddingStr; drawLabel(g, 0x8ec736, 0x373c3e, "Справочник", xPosition, yPos, yPosition, columnH, widthScreen); } if (index == 2) { // Если индекс равен 2 int yPos = yPosition + stepY + paddingStr; int yPosRect = yPosition + stepY; drawLabel(g, 0x8ec736, 0x373c3e, "Закладки", xPosition, yPos, yPosRect, columnH, widthScreen); }
if (index == 3) { // Если индекс равен 3 int yPos = yPosition + stepY*2 + paddingStr; int yPosRect = yPosition + stepY*2; drawLabel(g, 0x8ec736, 0x373c3e, "Помощь", xPosition, yPos, yPosRect, columnH, widthScreen); } if (index == 4) { // Если индекс равен 4 int yPos = yPosition + stepY*3 + paddingStr; int yPosRect = yPosition + stepY*3; drawLabel(g, 0x8ec736, 0x373c3e, "Выход", xPosition, yPos, yPosRect, columnH, widthScreen); } }
private void drawLabel(Graphics g, int bgColor, int strColor, String title, int xPosStr, int yPosStr, int yPosRect, int columnH, int widthScreen) { g.setColor(bgColor); g.fillRect(0, yPosRect, widthScreen, columnH); //Заливаем прямоугольник №3 g.setColor(strColor); g.drawString(title, xPosStr, yPosStr, g.TOP | g.HCENTER); }
public void drawAnything(String title) { }
public void keyPressed(int KeyCode) { int press = getGameAction(KeyCode); //Заносим в переменную код нажатой кнопки switch(press) { case Canvas.DOWN: if (index > 3) { index = 1; } else index++; repaint(); break; case Canvas.UP: if (index < 2) { index = 4; } else { index--; // Перемещаем прямоугольники вверх } repaint(); break; case Canvas.FIRE: Tcity.getInstance().showKey(index); break; } } }
|
и Canvas списка | Код | package tcity;
import javax.microedition.lcdui.*;
public class DrawList extends Canvas implements CommandListener{ private Command exitCommand = new Command("Выход", Command.EXIT, 0); private Command backCommand = new Command("Назад", Command.BACK, 0); private int index = 0; private int width; private int height; private int hStr = 20; private int yPosStr; private int yPosRect; private int oneStep; private int allStep; private int hScroll; private int stepNow; private int yPosScroll; private int beginCount; private int endCount; Display display; String str[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18t", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29","30", "31", "32", "33", "34", "35", "36", "37", "38", "39"}; public void paint(Graphics g) { width = g.getClipWidth(); height = g.getClipHeight(); oneStep = height/hStr; allStep = str.length/oneStep; if(allStep * oneStep < str.length) { allStep++; } hScroll = height/allStep; beginCount = 0; stepNow = 1; if(beginCount + oneStep - 1 < str.length) { endCount = beginCount + oneStep - 1; } else endCount = str.length; g.setColor(255, 255 , 255); g.fillRect(0, 0, width, height); g.setColor(55, 60, 62); int i=0; while(beginCount < endCount+1) { if(i==0) { yPosStr = 3; yPosRect = 0; } else { yPosStr = ((hStr * (i+1))-hStr)+3; yPosRect = ((hStr * (i+1))-hStr); } g.setColor(0x373c3e); g.drawString(str[beginCount], 5, yPosStr, g.TOP | g.LEFT); if(beginCount==index) { drawLabel(g, 0x8ec736, 0x373c3e, str[index], 5, yPosStr, yPosRect, hStr, width); } if(index > endCount && index<str.length) { if(beginCount < str.length) { beginCount++; g.setColor(255, 255 , 255); g.fillRect(0, 0, width, height); g.setColor(0x373c3e); g.drawString(str[beginCount], 5, yPosStr, g.TOP | g.LEFT); //repaint(); } if(endCount<str.length-1) endCount++; i=0; } else { beginCount++; i++; } if(stepNow==1) { yPosScroll = 0; } if(stepNow>1) { yPosScroll = (stepNow-1) * hScroll; } if(index > (oneStep * stepNow) + 1) { stepNow++; g.setColor(255, 255 , 255); g.fillRect(width-8, 0, width, height); drawScroll(g, 0xffffff, 0x373c3e, 0x8ec736, yPosScroll); } drawScroll(g, 0xffffff, 0x373c3e, 0x8ec736, yPosScroll); } }
private void drawLabel(Graphics g, int bgColor, int strColor, String title, int xPosStr, int yPosStr, int yPosRect, int columnH, int widthScreen) { g.setColor(bgColor); g.fillRect(0, yPosRect, widthScreen-8, columnH); //Заливаем прямоугольник №3 g.setColor(strColor); g.drawString(title, xPosStr, yPosStr, g.TOP | g.LEFT); }
public void drawScroll(Graphics g, int borderClr, int barCrl, int scrollClr, int yPosScrl) { g.setColor(borderClr); g.drawRect(width-8, 0, width, height); g.setColor(barCrl); g.fillRect(width-7, 0, width, height); g.setColor(scrollClr); g.fillRect(width-7, yPosScroll, width, hScroll); }
public void keyPressed(int KeyCode) { int press = getGameAction(KeyCode); //Заносим в переменную код нажатой кнопки switch(press) { case Canvas.DOWN: if (index > str.length-2) { index = 0; } else { index++; } repaint(); break; case Canvas.UP: if (index < 1) { index = str.length-1; } else { index--; // Перемещаем прямоугольники вверх } repaint(); break; case Canvas.FIRE: //Tcity.getInstance().showKey(index); break; } } public void commandAction(Command c, Displayable d) { if(c==backCommand) { repaint(); Tcity.getInstance().startApp(); } if(c==exitCommand) { Tcity.getInstance().destroyApp(false); } }
}
|
|