📄 menu.java
字号:
}
/**
* Returns selected item or null if no item is currently selected.
*
* @return Selected item or null.
*/
public PageItem getSelectedItem() {
MenuPage curPage = getCurrentPage();
if (curPage != null) {
return curPage.itemAt(curPage.getSelectedIndex());
} else {
return null;
}
}
/**
* Returns index of selected item or -1 if no item is currently selected.
*
* @return Index of selected item or -1.
*/
public int getSelectedItemIndex() {
MenuPage curPage = getCurrentPage();
if (curPage != null) {
return curPage.getSelectedIndex();
} else {
return -1;
}
}
/**
* Sets index of selected item.
*
* @param itemIndex
* The index of the item to select.
*/
public void setSelectedItemIndex(int itemIndex) {
MenuPage curPage = getCurrentPage();
if (curPage != null) {
PageItem oldItem = curPage.itemAt(curPage.getSelectedIndex());
curPage.setSelectedIndex(itemIndex);
PageItem newItem = curPage.itemAt(curPage.getSelectedIndex());
if (m_listener != null) {
m_listener.itemSelected(curPage, oldItem, newItem);
}
if (m_canvas != null) {
m_canvas.repaint();
}
}
}
/**
* Returns the canvas this menu is drawn upon.
*
* @return The canvas drawing this menu.
*/
public Canvas getCanvas() {
return m_canvas;
}
/**
* Sets the canvas this menu is drawn upon.
*
* @param canvas
* The canvas drawing this menu.
*/
public void setCanvas(Canvas canvas) {
m_canvas = canvas;
}
/**
* Returns the listener.
*
* @return The menu listener.
*/
public MenuListener getListener() {
return m_listener;
}
/**
* Sets the listener which is reported on menu events.
*
* @param listener
* A menu listener.
*/
public void setListener(MenuListener listener) {
m_listener = listener;
}
/**
* Returns the painter used to paint the menu.
*
* @return The painter.
*/
public MenuPainter getPainter() {
return m_painter;
}
/**
* Sets the painter used to paint the menu.
*
* @param painter
* The painter.
*/
public void setPainter(MenuPainter painter) {
m_painter = painter;
}
/**
* Returns the frame delay in milliseconds.
*
* @return The delay between each frame update.
*/
public long getFrameDelay() {
return m_frameDelay;
}
/**
* Returns number of frames in a page switch.
*
* @return Number of frames in a page switch.
*/
public int getFrames() {
return m_frames;
}
/**
* Returns height of this menu.
*
* @return The height.
*/
public int getHeight() {
return m_height;
}
/**
* Returns start page of this menu.
*
* @return The start page.
*/
public MenuPage getStartPage() {
return m_startPage;
}
/**
* Returns width of this menu.
*
* @return The width.
*/
public int getWidth() {
return m_width;
}
/**
* Returns x offset of this menu.
*
* @return The x offset.
*/
public int getX() {
return m_x;
}
/**
* Returns y offset of this menu.
*
* @return The y offset.
*/
public int getY() {
return m_y;
}
/**
* Sets the location of this menu.
*
* @param x
* The x offset.
* @param y
* The y offset.
*/
public void setLocation(int x, int y) {
m_x = x;
m_y = y;
}
/**
* Sets the size of this menu. If width and height are zero, these values
* will be collected from the canvas that this menu is painted upon.
*
* @param width
* The width.
* @param height
* The height.
*/
public void setDimensions(int width, int height) {
m_width = width;
m_height = height;
}
/**
* Sets the values used in a transition between to pages. A transition
* consists of a number of frames with a delay between each frame. A full
* transition will take <param>nbrOfFrames</param> * (<param>frameDelay</param> +
* time to paint frame) milliseconds.
*
* @param nbrOfFrames
* Number of frames in a transition.
* @param frameDelay
* Delay in milliseconds in each transition.
*/
public void setFrameData(int nbrOfFrames, long frameDelay) {
m_frames = nbrOfFrames;
m_frameDelay = frameDelay;
}
/**
* Starts a new transition between pages. If there is an ongoing transition,
* this is immediately pushed to its end and the new one is started.
*
* @param fromPage
* Transition source page.
* @param toPage
* Transition destination page.
* @param back
* True if it is a transition back to destination page, false
* otherwise.
*/
protected void startTransition(MenuPage fromPage, MenuPage toPage,
boolean back) {
synchronized (ANIM_LOCK) {
m_curPage = fromPage;
m_transPage = toPage;
// Wait for ongoing transition to end
if (m_inTransition) {
ANIM_LOCK.notifyAll();
}
while (m_inTransition) {
try {
ANIM_LOCK.wait();
} catch (InterruptedException e) {
}
}
m_transitionBack = back;
// Start new transition
m_inTransition = true;
ANIM_LOCK.notifyAll();
}
}
/**
* <code>Runnable</code> implementation, invokes the
* <code>MenuPainter</code> on transitions.
*/
public void run() {
while (m_running) {
synchronized (ANIM_LOCK) {
// Wait for a transition to be requested
while (!m_inTransition) {
try {
ANIM_LOCK.wait();
} catch (InterruptedException e) {
}
}
// Transition initiated
m_inTransition = true;
int frames = m_frames;
long delay = m_frameDelay;
MenuPage fromPage = m_curPage;
MenuPage toPage = m_transPage;
boolean backFlag = m_transitionBack;
if (m_listener != null) {
m_listener.transitionStarted(fromPage, toPage, delay,
frames, backFlag);
}
// Do each frame in transition
for (; m_frame < frames; m_frame++) {
try {
ANIM_LOCK.wait(delay);
} catch (InterruptedException e1) {
}
if (m_canvas != null) {
m_canvas.repaint();
}
// Check if someone changed parameters for transition,
// in that case interrupt current transition
if (m_curPage != fromPage || m_transPage != toPage) {
break;
}
}
m_frame = 0;
m_curPage = toPage;
if (m_canvas != null)
m_canvas.repaint();
m_inTransition = false;
if (m_listener != null) {
m_listener.transitionStopped(fromPage, toPage);
}
if (m_canvas != null) {
m_canvas.repaint();
}
// Notify that this transition has ended
ANIM_LOCK.notifyAll();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -