📄 sscanvas.java
字号:
import javax.microedition.lcdui.*;
import java.io.*;
public class SSCanvas extends Canvas {
private int curSlide = 5;
public void keyPressed(int keyCode) {
// 将物理按键代码转换为逻辑游戏按键代码
int action = getGameAction(keyCode);
switch (action) {
case FIRE://确认健
if (--curSlide < 0)
curSlide = 4;
repaint();//重新绘制整个Canvas
break;
}
}
public void paint(Graphics g) {
// 填充背景
int w = this.getWidth();
int h = this.getHeight();
g.setColor(0, 0, 0); // 黑
g.fillRect(0, 0, w, h);
switch (curSlide) {
case 0:
g.setColor(255, 255, 255);
g.fillRect(0, 50, w, h-90);
g.setColor(255, 0, 0);
g.fillArc(w/2-55, h/2-55, w/2-10, w/2-10, 0, 360);
g.setColor(255, 255, 255);
String s = "日本国旗";//字符串赋值
Font f = Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_PLAIN,
Font.SIZE_LARGE);
g.setFont(f);//设置字体
int stringWidth = f.stringWidth(s);//字符串宽度
int stringHeight = f.getHeight();//字符串高度
// 字符串位置
int i = (w - stringWidth)/2;
int j = 10;
g.drawString(s, i, j, Graphics.TOP|Graphics.LEFT);
break;
case 1:g.setColor(0, 0, 0);
g.fillRect(0, 0, w, h);
g.setColor(255, 255, 255);
g.fillRect(0, 50, w, h-90);
g.setColor(0, 140, 239);
g.fillRect(0, 70, w, h-260);
g.fillRect(0, 200, w, h-260);
g.fillTriangle(w/2-50,130,w/2+50,130,w/2,190);
g.fillTriangle(w/2,110,w/2-50,170,w/2+50,170);
String z = "以色列国旗";//字符串赋值
Font l = Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_PLAIN,
Font.SIZE_LARGE);
g.setFont(l);//设置字体
int stw = l.stringWidth(z);//字符串宽度
int sth = l.getHeight();//字符串高度
// 字符串位置
int x = (w - stw)/2;
int y = 10;
g.drawString(z, x, y, Graphics.TOP|Graphics.LEFT);
break;
case 2: g.setColor(0, 0, 0);
g.fillRect(0, 0, w, h);
g.setColor(255, 0, 0);
g.fillRect(0, 50, w, h-90);
g.setColor(255, 255, 255);
g.fillRect(w/2-80, h/2-20, w/2+40, h/2-100);
g.fillRect(w/2-20, h/2-70, w/2-80, h/2+20);break;
case 3:
Image img;
try{// 加载幻灯片图片
img=Image.createImage("/images/wnrl.jpg");
}catch(java.io.IOException e){
System.out.println("can not find wnrl.jpg");
img=null;
}
g.drawImage(img, getWidth() / 2, getHeight() /
2,
Graphics.HCENTER | Graphics.VCENTER);
break;
case 4:
Image img1;
try{// 加载幻灯片图片
img1=Image.createImage("/images/mei.jpg");
}catch(java.io.IOException e){
System.out.println("can not find mei.jpg");
img1=null;
}
g.drawImage(img1, getWidth() / 2, getHeight() /
2,
Graphics.HCENTER | Graphics.VCENTER);
break;
case 5:
g.setColor(255, 255, 255);
g.fillRect(0, 0, w, h);
g.setColor(0, 0, 0);
String k = "欢迎来到国旗观赏";//字符串赋值
String m = "请按select键看下一张图";
Font p = Font.getFont(Font.FACE_MONOSPACE,
Font.STYLE_PLAIN,
Font.SIZE_LARGE);
g.setFont(p);//设置字体
int sW= p.stringWidth(k);//字符串宽度
int sH = p.getHeight();//字符串高度
g.drawString(k, 50, sH/2, Graphics.TOP|Graphics.LEFT);
g.drawString(m, 30, sH/2+20, Graphics.TOP|Graphics.LEFT);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -