尝试写PRG地图切换失败,repaint无法正常生效,请大家赐教

龙宝小圆润 2018-07-02 10:45:23
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class MapPanel extends JPanel implements KeyListener{
int CS=32;
int offx=0;
int offy=0;
int direction;
Image floor;
Image wall;
Image hero;
boolean mapflag=false;
int vax=2;
int vay=2;
int[][] map;
public MapPanel(int row,int col){
setPreferredSize(new Dimension(160,160));
loadImage();
addKeyListener(this);
new MoveThread().start();
map=new int[row][col];
}

public void loadImage(){
ImageIcon icon=new ImageIcon(getClass().getResource("image/floor.jpg"));
floor=icon.getImage();
icon=new ImageIcon(getClass().getResource("image/wall.jpg"));
wall=icon.getImage();
icon=new ImageIcon(getClass().getResource("image/hero.jpg"));
hero=icon.getImage();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
drawMap(g);
}
public void drawMap(Graphics g){
for(int x=0;x<map.length;x++){
for(int y=0;y<map[0].length;y++){
if(this.map[x][y]==0){
g.drawImage(floor,y*CS+offx,x*CS+offy,this);
}else if(this.map[x][y]==1){
g.drawImage(wall,y*CS+offx,x*CS+offy,this);
}
}
}
g.drawImage(hero,2*CS,2*CS,3*CS,3*CS,0,0,CS,CS,this);
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){
int code=e.getKeyCode();
switch(code){
case KeyEvent.VK_UP:
direction=0;
if(isAllow(direction)){
mapflag=true;
}
break;
case KeyEvent.VK_RIGHT:
direction=1;
if(isAllow(direction)){
mapflag=true;
}
break;
case KeyEvent.VK_DOWN:
direction=2;
if(isAllow(direction)){
mapflag=true;
}
break;
case KeyEvent.VK_LEFT:
direction=3;
if(isAllow(direction)){
mapflag=true;
}
break;
}
}//按下
public void keyReleased(KeyEvent e){
int code=e.getKeyCode();
switch(code){
case KeyEvent.VK_UP:
mapflag=false;
break;
case KeyEvent.VK_RIGHT:
mapflag=false;
break;
case KeyEvent.VK_DOWN:
mapflag=false;
break;
case KeyEvent.VK_LEFT:
mapflag=false;
break;
}
}//弹起
public boolean isAllow(int i){
switch(i){
case 0:
if(map[vax-1][vay]==1){
return false;
}else{
return true;
}
case 1:
if(map[vax][vay+1]==1){
return false;
}else{
return true;
}
case 2:
if(map[vax+1][vay]==1){
return false;
}else{
return true;
}
case 3:
if(map[vax][vay-1]==1){
return false;
}else{
return true;
}
default: return false;
}
}
public void moveMap(int i){
switch(i){
case 0:offy++;
break;
case 1:offx--;
break;
case 2:offy--;
break;
case 3:offx++;
break;
}
}

public void moveRole(int i){
switch(i){
case 0:vax--;
break;
case 1:vay++;
break;
case 2:vax++;
break;
case 3:vay--;
break;
}
}
class MoveThread extends Thread{
public void run(){
while(true){
while(mapflag){
moveRole(direction);
for(int i=0;i<32;i++){
moveMap(direction);
try{
Thread.sleep(10);
}catch(Exception e){}
repaint();
}
}
}
}
}
}
public class HateMap extends JFrame{
boolean mapgo=true;
MapPanel map1;
MapPanel map2;
Container cont;
public HateMap(){
cont=getContentPane();
map1=new MapPanel(5,8);
int a[][]={
{1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1},
{1,1,0,0,1,1,0,1},
{1,0,0,0,1,0,0,3},
{1,1,1,1,1,1,1,1},
};

map1.map=a;
map2=new MapPanel(6,8);
map2.vax=1;
map2.vay=0;
int b[][]={
{1,1,1,1,1,1,1,1},
{3,0,0,0,0,0,0,1},
{1,1,1,0,1,1,0,1},
{1,0,0,0,0,0,0,1},
{1,0,1,0,1,0,0,1},
{1,1,1,1,1,1,1,1},
};
map2.map=b;
map2.setBackground(Color.blue);
cont.add(map1);
map1.setFocusable(true);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMap();
}
public void setMap(){//这里这个类,就是窗口用来切换场景的一个类
while(true){
if(map1.vax==3&&map1.vay==7&&mapgo){
mapgo=false;
remove(map1);
add(map2);
setFocusable(true);
repaint();
}else{}
}
}
/* class MapThread extends Thread{
public void run(){
while(true){
try{
repaint();
Thread.sleep(100);
}catch(Exception e){
}
}
}
}*/
public static void main(String[] args){
new HateMap();
}
}
...全文
77 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,599

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧