📄 scrollbar.java
字号:
/* I BlueTooth You -- Simple BlueTooth talker Copyright (C) 2007 Jan Tomka This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. */package net.sf.btw.ibtu.ui;import javax.microedition.lcdui.Graphics;/** * This class defines a simple vertical scrollbar. It is a graphics entity to be * displayed on {@link Graphics}. It visually represents the position and * height of currently displayed portion relative to the actual height of * another graphics entity being scrolled. * * @author Jan Tomka * * TODO Scrollbar should only paint its thumb, painting the frame should be * left to the owner. */public class Scrollbar { /** * Horizontal X-coordinate of a scrollbar. */ private final int x; /** * Zero-based vertical Y-coordinate of a scrollbar. */ private final int y; /** * Horizontal width of a scrollbar. */ private final int width; /** * Vertical height of a scrollbar. */ private final int height; /** * Refrence to the object whose scrolling properties Scrollbar is notified * about. */ private IScrollNotifier notifier; /** * Creates new Scrollbar object with given position and dimensions. * * @param x * Initial horizontal X-coordinate of a scrollbar. * @param y * Initial vertical Y-coordinate of a scrollbar. * @param width * Initial horizontal width of a scrollbar. * @param height * Initial vertical height of a scrollbar. */ public Scrollbar(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } /** * Returns the zero-based horizontal X-coordinate of a scrollbar within the * parent {@link Graphics}. * * @return The X-coordinate of a scrollbar. */ public int getX() { return x; } /** * Returns the zero-based vertical Y-coordinate of a scrollbar within the * parent {@link Graphics}. * * @return The Y-coordinate of a scrollbar. */ public int getY() { return y; } /** * Returns the horizontal width of a scrollbar. * * @return Width of scrollbar. */ public int getWidth() { return width; } /** * Returns the vertical height of a scrollbar. * * @return Height of scrollbar. */ public int getHeight() { return height; } /** * Saves reference to {@link IScrollNotifier} object, whose current * scrolling-related properties will be notified to the Scrollbar. * * @param sn * Reference to scroll notifier. If <code>null</code>, * existing notifier is removed. */ public void setIScrollNotifier(IScrollNotifier sn) { notifier = sn; } /** * Paints the scrollbar. Draws a rectangle given by {@link Scrollbar#getX()}, * {@link Scrollbar#getY()}, {@link Scrollbar#getWidth()} and * {@link Scrollbar#getHeight()}. Scrollbar thumb is then drawn so that it * represents the position and height of currently visible portion, relative * to the height of the entire area being scrolled. * * @param g * {@link Graphics} to draw Scrollbar on. */ // TODO Optimize repainting process. public void paint(Graphics g) { if (notifier == null) return; int visLen = notifier.getVisibleLength(); int actLen = notifier.getActualLength(); int curPos = notifier.getCurrentPosition(); int thumbHeight; if (actLen == 0) thumbHeight = height - 3; else { thumbHeight = ((1000 * height) / actLen * visLen) / 1000; if (thumbHeight < 1) thumbHeight = 1; else if (thumbHeight > height - 3) thumbHeight = height - 3; } int thumbY; if (curPos == 0) thumbY = y + 2; else { if ((actLen <= visLen) || curPos >= (actLen - visLen)) // Adjust curPos within the last screen thumbY = y + height - 1 - thumbHeight; else thumbY = y + 1 + ((1000 * height) / actLen * curPos) / 1000; if (thumbY < y + 2) thumbY = y + 2; } g.drawRect(x, y, width, height); g.fillRect(x + 2, thumbY, width - 3, thumbHeight); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -