Опытный
 
Профиль
Группа: Участник
Сообщений: 401
Регистрация: 20.10.2006
Где: Украина, Херсон
Репутация: 7 Всего: 11
|
все очень просто... P.S. честно признаюсь прорисовку скрола взял из библиотеки J4ME. можешь на нее посмотреть - открытый код, нормальная лицензия Код | 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 hStr = 20; private int indexOff = 0; private int displayCount = 0; private int topPadding = 0;
private String str[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39" };
public DrawList() { displayCount = getHeight() / hStr;
topPadding = (getHeight() % hStr) / 2; }
public void paint(Graphics g) { drawBackground(g); int yPosStr = topPadding; for (int i = indexOff; i < indexOff + displayCount && i < str.length; i++) { if (i == index) { drawSelectedLabel(g, 0, yPosStr, str[i], 5, 5); } else { drawLabel(g, 0, yPosStr, str[i], 5, 5); } yPosStr += hStr; } if(isScroolNeed()) VerticalScrollBar.paintVerticalScrollbar(g, 0, topPadding, g.getClipWidth(), g.getClipHeight() - 2 * topPadding, indexOff * hStr, str.length * hStr); }
private void drawBackground(Graphics g) { int width = g.getClipWidth(); int height = g.getClipHeight(); g.setColor(255, 255, 255); g.fillRect(0, 0, width, height); }
/** * * @param g * @param x * @param y * @param label * @param padding - * отступ текста * @param margin - * отступ от края экрана */ private void drawSelectedLabel(Graphics g, int x, int y, String label, int padding, int margin) { x += margin; g.setColor(0, 255, 0); int width = g.getClipWidth(); if(isScroolNeed()){ width -= VerticalScrollBar.SCROLL_WIDTH; } g.fillRect(x, y, width - x - margin, hStr); g.setColor(255, 255, 255); g.drawString(label, x + padding, y, Graphics.TOP | Graphics.LEFT); }
private void drawLabel(Graphics g, int x, int y, String label, int padding, int margin) { g.setColor(0x373c3e); g.drawString(label, x + margin + padding, y, Graphics.TOP | Graphics.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 action = getGameAction(keyCode); // Заносим в переменную код // нажатой // кнопки switch (action) { case Canvas.DOWN: moveDown(); break; case Canvas.UP: moveUp(); break; case Canvas.FIRE: // Tcity.getInstance().showKey(index); break; } }
protected void keyRepeated(int keyCode) {// если держат нажатой кнопку. int action = getGameAction(keyCode); switch (action) { case Canvas.DOWN: moveDown(); break; case Canvas.UP: moveUp(); break; } }
public void commandAction(Command c, Displayable d) {
}
private void moveUp() { if (index == 0) { indexOff = str.length - displayCount; index = str.length - 1; } else { if (index == indexOff) { indexOff--; } index--; } repaint(); }
private void moveDown() { if (index == str.length - 1) { indexOff = 0; index = 0; } else { if (index == indexOff + displayCount - 1) { indexOff++; } index++; } repaint(); }
private boolean isScroolNeed(){ return str.length > displayCount; } private static class VerticalScrollBar {
public static final int SCROLL_WIDTH = 6;
private static final int SCROLL_BORDER_COLOR = 0xff0000;
private static final int SCROLL_BG_COLOR = 0x0000ff; private static final int SCROLL_TRACKBAR_COLOR = 0x00ff00;
public static void paintVerticalScrollbar(Graphics g, int x, int y, int width, int height, int offset, int formHeight) { // Make the scrollbar as wide as the rounding diameter. int left = x + width - SCROLL_WIDTH;
// Draw the scrollbar background. paintScrollbarBackground(g, left, y, SCROLL_WIDTH, height);
// Draw an edge to the scrollbar. g.setColor(SCROLL_BORDER_COLOR); g.drawLine(left, y, left, y + height);
// Calculate the height of the trackbar. int scrollableHeight = formHeight - height; double trackbarPercentage = (double) height / (double) formHeight; int trackbarHeight = round(height * trackbarPercentage); trackbarHeight = Math.max(trackbarHeight, 2 * SCROLL_WIDTH);
// Calculate the range and location of the trackbar. // The scrollbar doesn't actually go from 0% to 100%. The top // is actually 1/2 the height of the trackbar from the top of // the screen. The bottom is 1/2 the height from the bottom. int rangeStart = trackbarHeight / 2; int range = height - 2 * rangeStart;
double offsetPercentage = (double) offset / (double) scrollableHeight; int center = y + rangeStart + round(offsetPercentage * range);
// Draw the trackbar. paintTrackbar(g, left, center - rangeStart, SCROLL_WIDTH, trackbarHeight); }
protected static void paintTrackbar(Graphics g, int x, int y, int width, int height) { // This code would paint the scrollbar a solid background. g.setColor(SCROLL_TRACKBAR_COLOR); g.fillRect(x, y, width, height); }
protected static void paintScrollbarBackground(Graphics g, int x, int y, int width, int height) { g.setColor(SCROLL_BG_COLOR); g.fillRect(x, y, width, height); }
public static int round(double a) { return (int) Math.floor(a + 0.5); } } }
|
|