Java 如何在表单和新日期实例中实现多个图像
在Java 如何在表单和新日期实例中实现多个图像,java,java-me,datepicker,midlet,lcdui,Java,Java Me,Datepicker,Midlet,Lcdui,在CalendarCanvas课程中,我们的想法是按下日历中的任何一天,表单将显示用户选择的当天的实际日期(不仅仅是现在的日期),表单下方是与表单相关的图像(例如6月21日(image.jpg) 我有两个问题 第一,这个程序只喜欢一张图片,那就是Men.img。它将以所有三种形式显示该图像。其他两个图像将完全不显示 我不明白,因为我首先从一个表单开始,然后是images.jpgimage,这个图像就工作了。我所做的就是创建2个新表单并切换图像顺序 我甚至缩小了尺寸(宽/高&KB),以与Men.j
CalendarCanvas
课程中,我们的想法是按下日历中的任何一天,表单将显示用户选择的当天的实际日期(不仅仅是现在的日期),表单下方是与表单相关的图像(例如6月21日(image.jpg
)
我有两个问题
第一,这个程序只喜欢一张图片,那就是Men.img
。它将以所有三种形式显示该图像。其他两个图像将完全不显示
我不明白,因为我首先从一个表单开始,然后是images.jpg
image,这个图像就工作了。我所做的就是创建2个新表单并切换图像顺序
我甚至缩小了尺寸(宽/高&KB),以与Men.jpg
相匹配,但它仍然没有显示出来。ImageItem构造函数中的字符串甚至不显示
最后,如何创建calendar.getSelectedDate().toString()
方法的新实例来显示新用户选择的日期,以便在用户选择不同的日期时将其置于图像上方
在按下键的方法中,注释掉的新Alert实例将为我执行此操作,但我不想要一个Alert,我希望它是与用户选择的日期相关的表单标题
下面是我的代码:
类日历MIDlet:
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* @author addylesson
*/
public class CalendarMidlet extends MIDlet {
private Display mydisplay;
private Command f;
private Displayable d;
public CalendarMidlet(){}
public void startApp() {
d = new CalendarCanvas(this);
f = new Command("Exit", Command.EXIT, 0);
d.addCommand(f);
d.setCommandListener(new CommandListener()
{
public void commandAction(Command c, Displayable s)
{
if(c == f)
notifyDestroyed();
}
} );
mydisplay = Display.getDisplay(this);
mydisplay.setCurrent(d);
}
public void startOver() {
final Displayable d = new CalendarCanvas(this);
d.addCommand(f);
d.setCommandListener(new CommandListener()
{
public void commandAction(Command c, Displayable s)
{
if(c == f)
notifyDestroyed();
}
} );
mydisplay.setCurrent(d);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
类日历画布:
import java.io.IOException;
import java.util.Date;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class CalendarCanvas extends Canvas
{
CalendarWidget calendar = null;
CalendarMidlet midlet = null;
private Alert alert;
private List mList;
private Command f,e,n;
private Display mydisplay;
private Form form;
private Form form2;
private Form form3;
private ImageItem imageItem;
private ImageItem imageItem2;
private ImageItem imageItem3;
public CalendarCanvas(final CalendarMidlet m)
{
String day = calendar.getSelectedDate().toString();
this.midlet = m;
calendar = new CalendarWidget(new Date());
calendar.headerFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
calendar.weekdayFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
calendar.weekdayBgColor = 0xccccff;
calendar.weekdayColor = 0x0000ff;
calendar.headerColor = 0xffffff;
form = new Form(day);
form2 = new Form("New Page");
form3 = new Form("New Page");
try
{
imageItem = new ImageItem(
"Men In Black 3: ",
Image.createImage("Men.jpg"),
ImageItem.LAYOUT_DEFAULT,
"DuKe");
form.append(imageItem);
} catch(Exception s) {}
try
{
imageItem2 = new ImageItem(
"Jordon vs Knicks: ",
Image.createImage("images.jpg"),
ImageItem.LAYOUT_DEFAULT,
"Not");
form2.append(imageItem2);
} catch(Exception s) {}
try
{
imageItem3 = new ImageItem(
"Horoscope: ",
Image.createImage("Men.jpg"),
ImageItem.LAYOUT_DEFAULT,
"DuKe");
form3.append(imageItem3);
} catch(Exception s) {}
alert = new Alert("Listen", "On this day "
+calendar.getSelectedDate().toString()+ "stuff happened", null, null);
alert.setTimeout(Alert.FOREVER);
n = new Command("Next", Command.SCREEN,0);
f = new Command("Back", Command.BACK, 0);
e = new Command("Exit", Command.EXIT, 1);
form.addCommand(f);
form.addCommand(n);
form2.addCommand(f);
form2.addCommand(n);
form3.addCommand(f);
form3.addCommand(e);
form.setCommandListener(new CommandListener()
{
public void commandAction(Command c, Displayable s)
{
if (c == f)
m.startOver();
if (c == e)
m.notifyDestroyed();
if (c == n)
Display.getDisplay(midlet).setCurrent(form2);
}
} );
form2.setCommandListener(new CommandListener()
{
public void commandAction(Command c, Displayable s)
{
if (c == f)
m.startOver();
if (c == e)
m.notifyDestroyed();
if (c == n)
Display.getDisplay(midlet).setCurrent(form3);
}
} );
form3.setCommandListener(new CommandListener()
{
public void commandAction(Command c, Displayable s)
{
if (c == f)
m.startOver();
if (c == e)
m.notifyDestroyed();
}
} );
calendar.initialize();
}
protected void keyPressed(int key)
{
int keyCode = getGameAction(key);
String day = calendar.getSelectedDate().toString();
if(keyCode == FIRE)
{
/*Display.getDisplay(midlet).setCurrent(
new Alert("Selected date",
day, null,
AlertType.CONFIRMATION)
);*/
Display.getDisplay(midlet).setCurrent(form);
}
else
{
calendar.keyPressed(keyCode);
repaint();
}
}
protected void paint(Graphics g)
{
g.setColor(0xff0000);
g.fillRect(0, 0, getWidth(), getHeight());
calendar.paint(g);
}
}
类日历小部件:
import java.util.Calendar;
import java.util.Date;
import javax.microedition.lcdui.*;
public class CalendarWidget
{
static final String[] MONTH_LABELS = new String[]{
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
};
static final String[] WEEKDAY_LABELS = new String[]{
"M", "T", "W", "T", "F", "S", "S"
};
/* starting week day: 0 for monday, 6 for sunday */
public int startWeekday = 0;
/* elements padding */
public int padding = 2;
/* cells border properties */
public int borderWidth = 4;
public int borderColor = 0x000000;
/* weekday labels properties */
public Font weekdayFont = Font.getDefaultFont();
public int weekdayBgColor = 0xcccccc;
public int weekdayColor = 0xffffff;
/* header (month-year label) properties */
public Font headerFont = Font.getDefaultFont();
public int headerBgColor = 0xff0000;
public int headerColor = 0xff0000;
/* cells properties */
public Font font = Font.getDefaultFont();
public int foreColor = 0xffffff;
public int bgColor = 0xcccccc;
public int selectedBgColor = 0x000000;
public int selectedForeColor = 0xff0000;
/* internal properties */
int width = 0;
int height = 0;
int headerHeight = 0;
int weekHeight = 0;
int cellWidth = 0;
int cellHeight = 0;
/* internal time properties */
long currentTimestamp = 0;
Calendar calendar = null;
int weeks = 0;
public CalendarWidget(Date date)
{
calendar = Calendar.getInstance();
//we'll see these 2 methods later
setDate(date);
initialize();
}
public Date getSelectedDate()
{
return calendar.getTime();
}
public void setDate(Date d)
{
currentTimestamp = d.getTime();
calendar.setTime(d);
//weeks number can change, depending on week starting day and month total days
this.weeks = (int)Math.ceil(((double)getStartWeekday() + getMonthDays()) / 7);
}
public void setDate(long timestamp)
{
setDate(new Date(timestamp));
}
void initialize()
{
//let's initialize calendar size
this.cellWidth = font.stringWidth("MM") + 3 * padding;
this.cellHeight = font.getHeight() + 15 * padding;
this.headerHeight = headerFont.getHeight() + 2 * padding;
this.weekHeight = weekdayFont.getHeight() + 2 * padding;
this.width = 7 * (cellWidth + borderWidth) + borderWidth;
initHeight();
}
void initHeight()
{
this.height =
headerHeight + weekHeight +
this.weeks * (cellHeight + borderWidth) + borderWidth;
}
int getMonthDays()
{
int month = calendar.get(Calendar.MONTH);
switch(month)
{
case 3:
case 5:
case 8:
case 10:
return 30;
case 1:
return calendar.get(Calendar.YEAR) % 4 == 0 && calendar.get(Calendar.YEAR) % 100 != 0 ? 29 : 28;
default:
return 31;
}
}
int getStartWeekday()
{
//let's create a new calendar with same month and year, but with day 1
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
c.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
c.set(Calendar.DAY_OF_MONTH, 1);
//we must normalize DAY_OF_WEEK returned value
return (c.get(Calendar.DAY_OF_WEEK) + 5) % 7;
}
public void keyPressed(int key)
{
switch(key)
{
case Canvas.UP:
go(-7);
break;
case Canvas.DOWN:
go(7);
break;
case Canvas.RIGHT:
go(1);
break;
case Canvas.LEFT:
go(-1);
break;
}
}
void go(int delta)
{
int prevMonth = calendar.get(Calendar.MONTH);
setDate(currentTimestamp + 86400000 * delta);
//we have to check if month has changed
//if yes, we have to recalculate month height
//since weeks number could be changed
if(calendar.get(Calendar.MONTH) != prevMonth)
{
initHeight();
}
}
public void paint(Graphics g)
{
//painting background
g.setColor(bgColor);
g.fillRect(0, 0, width, height);
//painting header (month-year label)
g.setFont(headerFont);
g.setColor(headerColor);
g.drawString(MONTH_LABELS[calendar.get(Calendar.MONTH)] + " " + calendar.get(Calendar.YEAR), width / 2, padding, Graphics.TOP | Graphics.HCENTER);
//painting week days labels
g.translate(0, headerHeight);
g.setColor(weekdayBgColor);
g.fillRect(0, 0, width, weekHeight);
g.setColor(weekdayColor);
g.setFont(weekdayFont);
for(int i = 0; i < 7; i++)
{
g.drawString(WEEKDAY_LABELS[(i + startWeekday) % 7],
borderWidth + i * (cellWidth + borderWidth) + cellWidth / 2,
padding,
Graphics.TOP | Graphics.HCENTER
);
}
g.translate(0, weekHeight);
g.setColor(borderColor);
for(int i = 0; i <= weeks; i++)
{
g.fillRect(0, i * (cellHeight + borderWidth), width, borderWidth);
}
for(int i = 0; i <= 7; i++)
{
g.fillRect(i * (cellWidth + borderWidth), 0, borderWidth, height - headerHeight - weekHeight);
}
int days = getMonthDays();
int dayIndex = (getStartWeekday() - this.startWeekday + 7) % 7;
g.setColor(foreColor);
int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
for(int i = 0; i < days; i++)
{
int weekday = (dayIndex + i) % 7;
int row = (dayIndex + i) / 7;
int x = borderWidth + weekday * (cellWidth + borderWidth) + cellWidth / 2;
int y = borderWidth + row * (cellHeight + borderWidth) + padding;
if(i + 1 == currentDay)
{
g.setColor(selectedBgColor);
g.fillRect(
borderWidth + weekday * (cellWidth + borderWidth),
borderWidth + row * (cellHeight + borderWidth),
cellWidth, cellHeight);
g.setColor(selectedForeColor);
}
g.drawString("" + (i + 1), x, y, Graphics.TOP | Graphics.HCENTER);
if(i + 1 == currentDay)
{
g.setColor(foreColor);
}
}
g.translate(0, - headerHeight - weekHeight);
}
}
import java.util.Calendar;
导入java.util.Date;
导入javax.microedition.lcdui.*;
公共类日历小部件
{
静态最终字符串[]月份标签=新字符串[]{
“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”
};
静态最终字符串[]工作日标签=新字符串[]{
“M”、“T”、“W”、“T”、“F”、“S”、“S”
};
/*开始工作日:周一0,周日6*/
公共int startWeekday=0;
/*元素填充*/
公共整数填充=2;
/*单元格边框属性*/
公共int-borderWidth=4;
public int borderColor=0x000000;
/*工作日标签属性*/
公共字体weekdayFont=Font.getDefaultFont();
公共int weekdayBgColor=0xCCCC;
公共int weekdayColor=0xffffff;
/*标题(月-年标签)属性*/
public Font headerFont=Font.getDefaultFont();
public int headerBgColor=0xff0000;
公共int头颜色=0xff0000;
/*细胞特性*/
public Font=Font.getDefaultFont();
public int foreColor=0xffffff;
公共int bgColor=0xCCCC;
public int selectedBgColor=0x000000;
public int selectedForeColor=0xff0000;
/*内部属性*/
整数宽度=0;
整数高度=0;
int headerHeight=0;
int周高=0;
int cellWidth=0;
int cellHeight=0;
/*内部时间属性*/
长currentTimestamp=0;
日历=空;
整数周=0;
公共日历小部件(日期)
{
calendar=calendar.getInstance();
//稍后我们将看到这两种方法
设定日期(日期);
初始化();
}
公共日期getSelectedDate()
{
返回calendar.getTime();
}
公共作废设置日期(日期d)
{
currentTimestamp=d.getTime();
日历设置时间(d);
//周数可以更改,具体取决于周开始日和月总天数
this.weeks=(int)Math.ceil(((double)getStartWeekday()+getMonthDays())/7);
}
公共无效设置日期(长时间戳)
{
设置日期(新日期(时间戳));
}
void initialize()
{
//让我们初始化日历大小
this.cellWidth=font.stringWidth(“MM”)+3*填充;
this.cellHeight=font.getHeight()+15*填充;
this.headerHeight=headerFont.getHeight()+2*填充;
this.weekHeight=weekdayFont.getHeight()+2*填充;
this.width=7*(单元格宽度+边框宽度)+边框宽度;
初始高度();
}
void initHeight()
{
这个高度=
头重+周重+
本周*(单元格高度+边框宽度)+边框宽度;
}
int getMonthDays()
{
int month=calendar.get(calendar.month);
开关(月)
{
案例3:
案例5:
案例8:
案例10:
返回30;
案例1:
返回calendar.get(calendar.YEAR)%4==0&&calendar.get(calendar.YEAR)%100!=0?29:28;
违约:
返回31;
}
}
int getStartWeekday()
{
//让我们用相同的月份和年份创建一个新日历,但第一天
Calendar c=Calendar.getInstance();
c、 set(Calendar.MONTH,Calendar.get(Calendar.MONTH));
c、 set(Calendar.YEAR,Calendar.get(Calendar.YEAR));
c、 设置(日历日/月/日,1);
//我们必须规范化每周一天的返回值
返回(c.get(日历、星期日)+5)%7;
}
按下公共无效键(int键)
{
开关(钥匙)
{
case Canvas.UP:
go(-7);
打破
case Canvas.DOWN:
go(7);
打破
case.Canvas.RIGHT:
go(1);
打破
case Canvas.LEFT:
go(-1);
打破
}
}
无效go(整数增量)
{
int prevMonth=calendar.get(calendar.MONTH);
设置日期(当前时间戳+86400000*增量);
//我们必须检查月份是否有变化
//如果是,我们必须重新计算月高
//因为周数可以更改
if(calendar.get(calendar.MONTH)!=prevMonth)
{
初始高度();
}
}
公共空间涂料(图g)
{
//绘画背景
g、 setColor(bgColor);
g、 fillRect(0,0,宽度,高度);
//喷漆标题(月-年标签)
g、 setFont(headerFont);
g、 setColor(headerColor);
g、 抽绳(月标签[calendar.get(calendar.MONTH)]+“”+calendar.get(calendar.YEAR),宽度/2,填充,Graphics.TOP | Graphics.HCENTER);
//绘制周日标签
g、 翻译(0,人头高度);
g、 setColor(工作日bgcolor);
g、 fillRect(0,0,宽度,周高);
g、 setColor(工作日颜色);
g、 setFont(工作日)
public class Log {
// utility class to keep logging code in one place
public static void log (String message) {
System.out.println(message);
// when debugging at real device, S.o.p above can be refactored
// - based on ideas like one used here (with Form.append):
// http://stackoverflow.com/questions/10649974
// - Another option would be to write log to RMS
// and use dedicated MIDlet to read it from there
// - If MIDlet has network connection, an option is
// to pass log messages over the network. Etc etc...
}
}
// ... other classes...
// ...
catch (Exception e) {
Log.log("unexpected exception: [" + e + "]");
}
// ...
public void commandAction(Command c, Displayable s) {
Log.log("command: [" + c.getCommandLabel()
+ "] at screen: [" + d.getTitle() + "]");
// ...
}
// ...
Log.log("set current: [" + someDisplayable.getTitle() + "]");
mydisplay.setCurrent(someDisplayable);
// ...
protected void keyPressed(int key) {
Log.log("key pressed: [" + getKeyName(key) + "]");
// ...
}