📄 settings.java
字号:
package org.bubblebreaker.config;
import org.bubblebreaker.model.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class Settings
{
public static int ballsType=Ball.IMAGE;
public static int level = 0;
public static int modelBuilder = Model.RANDOM_BUILDER;
public static int musicStateOnOrOff=1;
public static boolean isMusicOff(){
return musicStateOnOrOff == 1;
}
public static void setBallsType(int type)
{
ballsType=type;
Ball.createBalls(type);
}
public static int getBallsType()
{
return ballsType;
}
public static int getModelBuilder()
{
return modelBuilder;
}
public static int getLevel()
{
return level;
}
public static Form getSettingsForm()
{
ChoiceGroup cgroup=new ChoiceGroup(
"Ball type",
ChoiceGroup.EXCLUSIVE,
new String[]{
"2D",
"Solid",
"Glass"
},
null
);
cgroup.setSelectedIndex(ballsType,true);//select the existing
ChoiceGroup cgroup1=new ChoiceGroup(
"Board type",
ChoiceGroup.EXCLUSIVE,
new String[]{
"Random",
"Square"
},
null);
cgroup1.setSelectedIndex(0,true);//select the existing
ChoiceGroup cgroup2=new ChoiceGroup(
"Music",
ChoiceGroup.EXCLUSIVE,
new String[]{
"On",
"Off"
},
null);
cgroup2.setSelectedIndex(musicStateOnOrOff,true);
Item[] levelItem = {
new Gauge("Level", true, 5, level),
cgroup,
cgroup1,
cgroup2
};
Form f = new Form("Settings", levelItem);
return f;
}
public static void applySettingsForm(Form f)
{
Gauge g = (Gauge)f.get(0);
level=g.getValue();//TODO : set it in canvas
ChoiceGroup cgroup=(ChoiceGroup)f.get(1);
int index=cgroup.getSelectedIndex();
switch(index)
{
case 1:
setBallsType(Ball.IMAGE);
break;
case 2:
setBallsType(Ball.IMAGE_GLASS);
break;
case 0:
default:
setBallsType(Ball.DRAW2D);
break;
}
cgroup=(ChoiceGroup)f.get(2);
index=cgroup.getSelectedIndex();
switch(index)
{
case 0:
modelBuilder=Model.RANDOM_BUILDER;
break;
case 1:
modelBuilder=Model.SQUARE_BUILDER;
break;
}
cgroup=(ChoiceGroup)f.get(3);
musicStateOnOrOff = cgroup.getSelectedIndex();
writeToRecordStore();
}
public static void readFromRecordStore()
{
try
{
RecordStore store=RecordStore.openRecordStore("Bubble Breaker", true);
int size=store.getNumRecords();
for(int i=0;i<size;i++)
{
byte data[]=store.getRecord(i);
String str=new String(data);
if(str.startsWith("balls type:")){
ballsType=Integer.parseInt(str.substring(10));
}else if(str.startsWith("level:")){
level=Integer.parseInt(str.substring(5));
}else if(str.startsWith("modelBuilder:")){
modelBuilder=Integer.parseInt(str.substring(12));
}else if(str.startsWith("music:")){
musicStateOnOrOff=Integer.parseInt(str.substring(5));
}
}
store.closeRecordStore();
}catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
private static void writeToRecordStore()
{
try
{
RecordStore.deleteRecordStore("Bubble Breaker");
RecordStore store=RecordStore.openRecordStore("Bubble Breaker", true);
addRecord("balls type:"+ ballsType, store);
addRecord("level:"+ level, store);
addRecord("modelBuilder:"+ modelBuilder, store);
addRecord("music:"+ musicStateOnOrOff, store);
store.closeRecordStore();
}catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
private static void addRecord(String str, RecordStore store) throws Exception
{
byte []data=str.getBytes();
store.addRecord(data,0,data.length);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -