欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lwuit实现九宫图特效程序

程序员文章站 2024-03-14 13:50:46
...
最近在网上看到很多九宫图的程序说明,但大多数是转载的,一些知识点的地方对于初学者来说还是比较难懂的,我在原来的程序上修改了一下,也加了一些注释,希望对大家有所帮助吧!

程序截图
[img]http://dl.iteye.com/upload/attachment/199168/b382b03a-2dec-30de-b162-753bcf7d491a.png[/img] [img]http://dl.iteye.com/upload/attachment/199170/cba77a07-8b5e-3c6d-a178-1b68f5641343.png[/img]

package com.mopietek;

import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;

public class UIDemoMIDlet extends MIDlet implements ActionListener{

public static Command exitCommand = new Command("exit",1);
//设置九宫图图标的名称
private String[] button_name = new String[]{"Image 1","Image 2","Image 3","Image 4","Image 5","Image 6","Image 7","Image 8","Image 9"};

private static Transition componentTransition;
private static Form mainForm; //主窗体

private int cols;
private int elementWidth;

public static Transition getComponentTransition() {
return componentTransition;
}

public static void setComponentTransition(Transition componentTransition) {

UIDemoMIDlet.componentTransition = componentTransition;
//设置主窗体动画效果为false
mainForm.setSmoothScrolling(false);
}

protected void startApp() throws MIDletStateChangeException {

Display.init(this);
try {
Resources res = Resources.open("/resources.res");
MainForm(res);
} catch (IOException e) {
e.printStackTrace();
Dialog.show("Exception", e.getMessage(), "OK",null);
}
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

public void MainForm(Resources r){

UIManager.getInstance().setThemeProps(r.getTheme("businessTheme"));
mainForm = new Form("九宫图主窗体");
//主窗体的宽度
int width = Display.getInstance().getDisplayWidth();
elementWidth = 0;

//实现按钮3D翻转效果
mainForm.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 500));
mainForm.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 500));
UIDemoMIDlet.setComponentTransition(Transition3D.createCube(400, false));

//定义按钮选中时的图片
Image [] selectedImages = new Image[button_name.length];
//定义未m被选中时的图片
Image [] unselectedImages = new Image[button_name.length];

for(int i=0;i<button_name.length;i++){
// 读取选中时的图片
selectedImages[i] = r.getImage(button_name[i]);
//读取未被选中时的图片
unselectedImages[i] = r.getImage(button_name[i]);

// final Button button = new Button(button_name[i],unselectedImages[i]){
final Button button = new Button("图标"+(i+1),unselectedImages[i]){
//当鼠标按下时图标变小的动画效果
public Image getPressedIcon(){
Image i = getIcon();
//按钮图标缩小为原来的0.8倍
return i.scaled((int) (i.getWidth()*0.8), (int)(i.getHeight()*0.8));
}

};
//设置按钮翻转
button.setRolloverIcon(selectedImages[i]);
//得到按钮的风格
Style s = button.getStyle();
s.setBorder(null);
s.setBgTransparency(0);
s.setBgColor(0xffffff);
button.setAlignment(Label.CENTER); //对齐方式
button.setTextPosition(Label.BOTTOM);
mainForm.addComponent(button);
button.addActionListener(this);

//按钮焦点处理,用于实现按钮特效
button.addFocusListener(new FocusListener(){
//按钮得到焦点时的特效(事件)处理
public void focusGained(Component cmp) {

if(componentTransition != null){
mainForm.replace(button, button, componentTransition);
}
}
//按钮失去焦点时的特效(事件)处理
public void focusLost(Component arg0) {

}

});

elementWidth = Math.max(button.getPreferredW(), elementWidth);
}

cols = width / elementWidth;
int rows = button_name.length / cols;
mainForm.setLayout(new GridLayout(rows,cols));
mainForm.addCommand(exitCommand);

mainForm.setCommandListener(this);
mainForm.show();


}



public static Form getMainForm() {
return mainForm;
}

public static void setMainForm(Form mainForm) {
UIDemoMIDlet.mainForm = mainForm;
}

public void actionPerformed(ActionEvent evt) {

if(evt.getCommand() == exitCommand){
this.notifyDestroyed();
}

}

}


[color=red]关于资源文件可以在附加中下载[/color]
[color=red]LwuitDemo7.jar文件是实例[/color],大家可以下载测试一下,看看有哪些款式机子不支持,小弟在此谢过各位啦!
相关标签: SUN