主题:[原创]地图不显示
为什么我编的地图在屏幕上不显示。刚刚学习有点晕。
(刚学 前辈们别笑话 小的啊)
有啥不对的地方 请多多指点。一定改。
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.nokia.mid.ui.FullCanvas;
public class MainCanvas extends FullCanvas implements Runnable {
private static final int MOVE []= {0,1,2};
Sprite mapS;
Sprite heroS;
private Image heroImage;
private Image mapImage;
private int mapW;
private int mapH;
private byte map[][];
private int numY=9;
private int numX=9;
public MainCanvas() {
new Thread(this).start();
try {
mapImage=Image.createImage("/hai.png");
heroImage=Image.createImage("/hero.png");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
heroS=new Sprite(heroImage,heroImage.getWidth()/6,heroImage.getHeight());
heroS.setFrameSequence(MOVE);
heroS.setPosition(getWidth()/2-heroS.getWidth()/2,getHeight()-1-heroS.getHeight());
mapS=new Sprite(mapImage,mapImage.getWidth()/6,mapImage.getHeight());
int x[]={0,1,2,3,4,5};
mapS.setFrameSequence(x);
setMap("/0.map");
}
protected void paint(Graphics g) {
g.setColor(0);
g.fillRect(0,0,getWidth(),getHeight());
drawMap(g);
drawHero(g);
}
private void drawMap(Graphics g) {
// TODO 自动生成方法存根
for(int j=0;j<numY;j++){
for(int i=0;i<numX;i++){
int x=mapW/2-numX/2+i;
int y=mapH-1-numY+1+j;
int sx=getWidth()/2-mapS.getWidth()/2+(-numX/2+i)*mapS.getWidth();
int sy=getHeight()-1-mapS.getHeight()+(-numY+1+j)*mapS.getHeight();
mapS.setFrame(map[x][y]);
mapS.setPosition(sx,sy);
mapS.paint(g);
}
}
}
private void drawHero(Graphics g) {
// TODO 自动生成方法存根
heroS.paint(g);
heroS.nextFrame();
}
void setMap(String s) {
// TODO 自动生成方法存根
InputStream Is=getClass().getResourceAsStream(s);
try {
mapW=Is.read();
mapH=Is.read();
map=new byte [mapW][mapH];
for(int j=0;j<mapH;j++){
for(int i=0;i<mapW;i++){
map[i][j]=(byte)Is.read();
}
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void run() {
// TODO 自动生成方法存根
while(true){
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}
(刚学 前辈们别笑话 小的啊)
有啥不对的地方 请多多指点。一定改。
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.nokia.mid.ui.FullCanvas;
public class MainCanvas extends FullCanvas implements Runnable {
private static final int MOVE []= {0,1,2};
Sprite mapS;
Sprite heroS;
private Image heroImage;
private Image mapImage;
private int mapW;
private int mapH;
private byte map[][];
private int numY=9;
private int numX=9;
public MainCanvas() {
new Thread(this).start();
try {
mapImage=Image.createImage("/hai.png");
heroImage=Image.createImage("/hero.png");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
heroS=new Sprite(heroImage,heroImage.getWidth()/6,heroImage.getHeight());
heroS.setFrameSequence(MOVE);
heroS.setPosition(getWidth()/2-heroS.getWidth()/2,getHeight()-1-heroS.getHeight());
mapS=new Sprite(mapImage,mapImage.getWidth()/6,mapImage.getHeight());
int x[]={0,1,2,3,4,5};
mapS.setFrameSequence(x);
setMap("/0.map");
}
protected void paint(Graphics g) {
g.setColor(0);
g.fillRect(0,0,getWidth(),getHeight());
drawMap(g);
drawHero(g);
}
private void drawMap(Graphics g) {
// TODO 自动生成方法存根
for(int j=0;j<numY;j++){
for(int i=0;i<numX;i++){
int x=mapW/2-numX/2+i;
int y=mapH-1-numY+1+j;
int sx=getWidth()/2-mapS.getWidth()/2+(-numX/2+i)*mapS.getWidth();
int sy=getHeight()-1-mapS.getHeight()+(-numY+1+j)*mapS.getHeight();
mapS.setFrame(map[x][y]);
mapS.setPosition(sx,sy);
mapS.paint(g);
}
}
}
private void drawHero(Graphics g) {
// TODO 自动生成方法存根
heroS.paint(g);
heroS.nextFrame();
}
void setMap(String s) {
// TODO 自动生成方法存根
InputStream Is=getClass().getResourceAsStream(s);
try {
mapW=Is.read();
mapH=Is.read();
map=new byte [mapW][mapH];
for(int j=0;j<mapH;j++){
for(int i=0;i<mapW;i++){
map[i][j]=(byte)Is.read();
}
}
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void run() {
// TODO 自动生成方法存根
while(true){
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
}