📄 softbuttoncontrol.java
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind.
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)
/**
//MenuBar文档生成日期:2006.02.10
//
//(1)概述:
//类名称:SoftButtonControl
//类说明:
// 提供在Canvas上绘制二级菜单最下面的状态栏或者左右软键的能力,通过一个标志即可切换这两种风格。
*
//所在子系统:MenuBar
//
//系统总描述:
我们提供的MenuBar J2ME版本 就是这么一种概念:
一个可以下载到手机(例如Nokia7610已经确实可以下载安装并运行)的Java应用程序。
他模仿Opera Mini的界面风格以及操纵模式,未来试图向ucweb学习底边状态栏的绘制。
这种风格我们称之为二级菜单,甚至多级菜单。它可以在小小的手机屏幕上展示如何提供尽可能多的菜单命令。
//(2)历史记录:
//创建人: 郑昀(2006.02.10)
//联系我: Google Talk >> zhengyun@gmail.com
//Blogs: http://blog.csdn.net/zhengyun_ustc/以及http://www.cnblogs.com/zhengyun_ustc
//(3)版权声明:
//我这个版本的MenuBar,代码您可以借鉴,但不得用于商业用途,除非得到本人的授权。
////////////////////////////////////////////////////////////////////*/
package com.ultrapower.gui;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
//import bluegammon.Device;
/**
* A custom item for softbuttons. Has functionality for
* setting one <code>Command</code> per softbutton. This
* also binds the backbutton if any of the command's type
* is <code>Command.BACK</code>.
*
* @author Peter Andersson
*/
public class SoftButtonControl
{
/** Displayable that is used for drawing this control */
protected Displayable m_displayable;
/** Left command */
protected Command m_leftCommand;
/** Right command */
protected Command m_rightCommand;
/** Back command */
protected Command m_backCommand;
/** Command listener */
protected CommandListener m_listener;
/** Enabled/disabled flag for left command */
protected boolean m_leftCommandEnabled = true;
/** Enabled/disabled flag for right command */
protected boolean m_rightCommandEnabled = true;
/*
* SoftButton的两种界面风格。
* 如果是false,则代表最下面一行都是状态栏;
* 如果是true,则代表屏幕左右下角各绘制一个button。
*/
protected boolean m_bSoftbuttonOrStatusbar = false;
/** String of left softbutton */
protected char[] m_left;
/** String of right softbutton */
protected char[] m_right;
/** Font used when drawing softbutton strings */
protected Font m_font;
/** Biggest text width amongst the commands */
protected int m_maxWidth;
/** 绘制底边状态栏的最大宽度 */
protected int m_statusbarWidth;
protected int m_canvasHeight;
/*
* 左右软按钮的边框颜色
*/
protected static final int COL_BORDER = 0x88440000;
/*
* 左右软按钮的绘制底色,0xbb440000是棕褐色
*/
protected static final int COL_BG = 0x55440000;
/*
* 左右软按钮的字体颜色,0xffffff是白色
*/
protected static final int COL_COMMAND = 0xffffff;
/*
* 禁用时左右软按钮的字体颜色,0x888888是灰色
*/
protected static final int COL_DISABLED_COMMAND = 0x888888;
/*
* 对话框的确定按钮背景透明色,0x65ee0000是粉红色
*/
protected int m_buttonBackgroundColor = 0x65110000;
/*
* 边框的颜色,0xdd0000是红色
*/
protected int m_borderColor = 0xd6ffff;//0xdd0000;
/*
* 对话框内文本的字体颜色,0xffffff是白色
*/
protected int m_textColor = 0xffffff;
protected int[] m_transpBuf;
protected static int[] m_rgbButtonData;
protected int m_fontHeight;
/**
* Initializes this softbutton control.
*
* @param d The displayable that this control draw on.
* @param font The font used for rendering softbuttons.
* @param leftCommand The left softbutton command or null.
* @param rightCommand The right softbutton command or null.
* @param bSoftbuttonOrStatusbar 最后一个参数指绘制SoftButton的界面风格,是左右两个软键呢,还是一个长条的状态栏
*/
public void init(Displayable d, Font font, Command leftCommand, Command rightCommand,
boolean bSoftbuttonOrStatusbar)
{
System.out.println("SoftButton::init!");
m_displayable = d;
m_font = font;
m_fontHeight = m_font.getHeight();
m_statusbarWidth = m_displayable.getWidth();
m_canvasHeight = m_displayable.getHeight();
m_bSoftbuttonOrStatusbar = bSoftbuttonOrStatusbar;
setRightCommand(rightCommand);
setLeftCommand(leftCommand);
if(!m_bSoftbuttonOrStatusbar)
{
// 创建按钮的透明背景色rgb缓冲区 (4 lines)
// zhengyun 20051220
System.out.println("创建按钮的透明背景色rgb缓冲区!");
if (m_rgbButtonData == null || m_rgbButtonData.length != m_statusbarWidth * 4)
{
m_rgbButtonData = new int[m_statusbarWidth * 4];
for (int i = 0; i < m_rgbButtonData.length; i++)
{
m_rgbButtonData[i] = m_buttonBackgroundColor;
}
}
}
}
/**
* Returns the command listener.
* @return The command listener.
*/
public CommandListener getCommandListener()
{
return m_listener;
}
/**
* Sets the commandlistener that will be reported upon
* softkey presses.
* @param listener The listener.
*/
public void setCommandListener(CommandListener listener)
{
m_listener = listener;
}
/**
* Returns the command assigned to left softbutton.
* @return The left comamand.
*/
public Command getLeftCommand()
{
return m_leftCommand;
}
/**
* Sets the left softbutton command.
* @param c The command.
*/
public void setLeftCommand(Command c)
{
if (m_backCommand == m_leftCommand)
{
m_backCommand = null;
}
m_leftCommand = c;
if (m_leftCommand != null)
{
m_left = m_leftCommand.getLabel().toCharArray();
if (m_leftCommand.getCommandType() == Command.BACK)
{
m_backCommand = c;
}
enable(c, true);
}
calcWidth();
}
/**
* Returns the command assigned to right softbutton.
* @return The right comamand.
*/
public Command getRightCommand()
{
return m_rightCommand;
}
/**
* Sets the right softbutton command.
* @param c The command.
*/
public void setRightCommand(Command c)
{
if (m_backCommand == m_rightCommand)
{
m_backCommand = null;
}
m_rightCommand = c;
if (m_rightCommand != null)
{
m_right = m_rightCommand.getLabel().toCharArray();
if (m_rightCommand.getCommandType() == Command.BACK)
{
m_backCommand = c;
}
enable(c, true);
}
calcWidth();
}
/**
* Enables/disables a command.
* @param c The command.
* @param enable True for enable, false for disable.
*/
public void enable(Command c, boolean enable)
{
if (c == m_leftCommand)
{
m_leftCommandEnabled = enable;
}
if (c == m_rightCommand)
{
m_rightCommandEnabled = enable;
}
}
/**
* Call this from the displayable when a key is pressed to
* activate this control.
*
* @param keyCode The keycode reported in <code>Displayable</code>'s
* <code>keyPressed</code> method.
*/
public void keyPressed(int keyCode)
{
if (keyCode == Device.KEYCODE_LEFT_SOFT)
{
if (m_leftCommand != null && m_listener != null && m_leftCommandEnabled)
{
m_listener.commandAction(m_leftCommand, m_displayable);
}
}
else if (keyCode == Device.KEYCODE_RIGHT_SOFT)
{
if (m_rightCommand != null && m_listener != null && m_rightCommandEnabled)
{
m_listener.commandAction(m_rightCommand, m_displayable);
}
}
else if (keyCode == Device.KEYCODE_BACK)
{
if (m_backCommand != null && m_listener != null &&
(m_backCommand == m_leftCommand && m_leftCommandEnabled ||
m_backCommand == m_rightCommand && m_rightCommandEnabled))
{
m_listener.commandAction(m_backCommand, m_displayable);
}
}
}
/**
* Call this from the displayable a repaint is necessary to
* draw this control
*
* @param g The graphics context reported in <code>Displayable</code>'s
* <code>paint</code> method.
*/
public void paint(Graphics g)
{
System.out.println("SoftButton::paint!");
int w = m_displayable.getWidth();
int h = m_displayable.getHeight();
if(!m_bSoftbuttonOrStatusbar)
{
int y = m_canvasHeight - m_fontHeight - 2;
{
/*
* zhengyun 20051220 added
* 添加OK按钮上的背景色
*/
System.out.println("添加OK按钮上的背景色!");
g.setColor(m_borderColor);
g.drawRect(1, y, m_statusbarWidth - 1, m_fontHeight - 1);
g.setColor(m_textColor);
g.drawRect(2, y + 1, m_statusbarWidth - 2, m_fontHeight - 2);
// 绘制编辑框内的背景色,稍微浅色点
for (int yAlternative = y + 1; yAlternative < y + m_fontHeight - 1; yAlternative += 4)
{
int nTemp = y + m_fontHeight - yAlternative;
nTemp = 4>nTemp ? nTemp:4;
// public void drawRGB(int[] rgbData, int offset, int scanlength, int x, int y, int
// width, int height, boolean processAlpha).
g.drawRGB(m_rgbButtonData, 0, m_statusbarWidth - 4,
3, yAlternative, m_statusbarWidth - 4, nTemp, true);
}
}
}
g.setFont(m_font);
if (m_leftCommand != null)
{
paintCommand(g, m_left, w, h, m_leftCommandEnabled, false);
}
if (m_rightCommand != null)
{
paintCommand(g, m_right, w, h, m_rightCommandEnabled, true);
}
}
/**
* Calculate softbutton width.
*/
protected void calcWidth()
{
int twl = m_left == null ? 0: m_font.charsWidth(m_left,0,m_left.length);
int twr = m_right == null ? 0: m_font.charsWidth(m_right,0,m_right.length);
/*
* Math.max(twr, twl)在CLDC1.0上不能使用!
* 所以必须自己用if/else判断
* zhengyun 20051216
*/
int mw = twr>twl ? twr:twl;
if (m_maxWidth != mw)
{
m_maxWidth = mw;
recalcTransparantBuffer();
}
}
/**
* Paint a softbutton command.
* @param g Graphics context to draw to.
* @param text Text of command.
* @param w Width of softbutton.
* @param h Height of softbutton.
* @param enabled True if softbutton is enabled, false otherwise.
* @param rightAlign True if softbutton is to the right, false for left.
*/
protected void paintCommand(Graphics g, char[] text,
int w, int h, boolean enabled, boolean rightAlign)
{
System.out.println("SoftButton::paintCommand");
int x = 0;
if (rightAlign)
{
x = w - m_maxWidth - 2;
}
if(m_bSoftbuttonOrStatusbar)
{
g.drawRGB(m_transpBuf, 0, m_maxWidth+2,
x, h-m_fontHeight-1, m_maxWidth+2, m_fontHeight+1, true);
}
g.setColor(enabled ? COL_COMMAND : COL_DISABLED_COMMAND);
x += m_maxWidth / 2;
x += rightAlign ? 2 : 1;
g.drawChars(text,0,text.length, x, h, Graphics.BOTTOM | Graphics.HCENTER);
}
/**
* Calculate transparant background.
*/
protected void recalcTransparantBuffer()
{
System.out.println("SoftButton::recalcTransparantBuffer>>m_statusbarWidth="
+ m_statusbarWidth + ";m_fontHeight=" + m_fontHeight);
if(!m_bSoftbuttonOrStatusbar)
{
// zhengyun modified 20060215
m_transpBuf = new int[m_statusbarWidth * (m_fontHeight + 1)];//这个矩阵是为了底边整条状态栏
for (int i = m_statusbarWidth; i < m_transpBuf.length; i++)
{
m_transpBuf[i] = COL_BG;
}
for (int i = 0; i < m_statusbarWidth; i++)
{
m_transpBuf[i] = COL_BORDER;
}
for (int i = 0; i < m_fontHeight + 1; i++)
{
m_transpBuf[(i * (m_statusbarWidth))] = COL_BORDER;
m_transpBuf[(i * (m_statusbarWidth)) + m_statusbarWidth - 1] = COL_BORDER;
}
}
else
{
m_transpBuf = new int[(m_maxWidth+2) * (m_font.getHeight()+1)]; // 这个矩阵的计算是为了特定两个软键
for (int i = m_maxWidth+2; i < m_transpBuf.length; i++)
{
m_transpBuf[i] = COL_BG;
}
for (int i = 0; i < m_maxWidth+2; i++)
{
m_transpBuf[i] = COL_BORDER;
}
for (int i = 0; i < m_font.getHeight()+1; i++)
{
m_transpBuf[(i * (m_maxWidth+2))] = COL_BORDER;
m_transpBuf[(i * (m_maxWidth+2)) + m_maxWidth+1] = COL_BORDER;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -