关于java用Canvas载入图片。。为什么载入不了??求救 新手

publicclassp1extendsJFrame{privateBufferedImageimg;publicp1()throwsIOException{super(... public class p1 extends JFrame{

private BufferedImage img;

public p1() throws IOException
{
super("拼图游戏");
String s;
final DrawCanvas drawArea = new DrawCanvas();
Container c=getContentPane();
JButton b1,b2;
s="载入图片";
b1= new JButton(s);
c.setLayout(new FlowLayout(FlowLayout.LEFT));
c.add(b1);
s="开始游戏";
b2= new JButton(s);
c.add(b2);
c.add(drawArea);
img=ImageIO.read(new File("11.png"));
drawArea.repaint();

}
class DrawCanvas extends Canvas
{
public void paint(Graphics g)
{

g.drawImage(img,200,20,null);
}

}
public static void main(String[] args) {
try {
p1 frm = new p1();
frm.setSize(1000, 700);
frm.setResizable(false);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
展开
 我来答
llscp
推荐于2016-09-20 · 超过16用户采纳过TA的回答
知道答主
回答量:63
采纳率:0%
帮助的人:27.6万
展开全部

给你一段实例代码也是拼图游戏

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class PPuzzle extends Applet{
    Image imgPuzzle,buff;
    Point fifteen=new Point(3,3);
    int[][] map={{0,4,8,12},{1,5,9,3},{2,6,10,14},{3,7,11,15}};
    int sx,sy;
    Canvas screen;
    Graphics gs,gb;
    boolean running=false;
    Button bStart=new Button("新游戏");
    Button bSee=new Button("显示正确的图像");
    public void init(){
        prepareImage();                    //准备图像
        sx=imgPuzzle.getWidth(this)/4;
        sy=imgPuzzle.getHeight(this)/4;
        setBackground(Color.blue);
        initScreen();                     //初始化画面
        initButtons();                    //初始化按钮
        add(screen);                      //向Applet添加组件
        add(bStart);
        add(bSee);
    }
    void prepareImage(){                  //准备图像的方法
        imgPuzzle=getImage(getCodeBase(),"temp/xxx.jpg");
        MediaTracker mt=new MediaTracker(this);
        mt.addImage(imgPuzzle, 0);        //等待至图像全部加载完毕
        try{
            mt.waitForAll();
        }catch(Exception e){}
        //创建buffer并获取其Graphics对象
        buff=createImage(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this));
        gb=buff.getGraphics();
    }
    void initMap(){                       //初始化map
        java.util.Random rnd=new java.util.Random();
        int temp,x1,y1,x2,y2;
        for(int i=0;i<100;i++){
            x1=rnd.nextInt(4);
            x2=rnd.nextInt(4);
            y1=rnd.nextInt(4);
            y2=rnd.nextInt(4);
            temp=map[x1][y1];
            map[x1][y1]=map[x2][y2];
            map[x2][y2]=temp;
        }
        outer:for(int j=0;j<4;j++)
            for(int i=0;i<4;i++)
                if(map[i][j]==15){
                    fifteen.setLocation(i,j);
                    break outer;
                }
    }
    void initScreen(){                   //初始化画面
        screen=new Canvas(){             //创建screen对象
            public void paint(Graphics g){  //覆盖paint()方法
                if(gs==null)gs=getGraphics();//获取screen的Graphics对象
                if(running)
                    drawScreen();
                else
                //若游戏未开始,则显示整幅图像
                    g.drawImage(imgPuzzle,0,0,this);
            }
        };
        //设定screen的大小
        screen.setSize(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this));
        
        screen.addMouseListener(new MouseAdapter(){  //处理鼠标事件
            public void mousePressed(MouseEvent me){
                if(!running) return;
                int x=me.getX()/sx,y=me.getY()/sy;
                int fx=(int)fifteen.getX(),fy=(int)fifteen.getY();
                if(Math.abs(fx-x)+Math.abs(fy-y)>=2)return;
                if (map[x][y]==15)return;
                map[fx][fy]=map[x][y];
                map[x][y]=15;
                fifteen.setLocation(x,y);
                drawScreen();
            }
        });
    }
    void initButtons(){                               //初始化按钮
        //“新游戏”按钮事件的处理
        bStart.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                initMap();                            //初始化mpa
                drawScreen();                         //绘制画面
                running=true;                        //代表游戏正在进行中
                bSee.setLabel("显示正确的图像");      //改变bSee按钮的标题
            }
        });
        //“显示正确图像”按钮事件处理
        bSee.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                //bSee按钮标题为“继续游戏”
                if(bSee.getLabel().equals("继续游戏")){
                 drawScreen();                         //绘制画面
//                 bSee.setLabel("显示正确图像");      //标题更改为“显示正确图像”  
                 }
                else{
                    //bSee的标题为“显示正确图像”
                    gs.drawImage(imgPuzzle,0,0,screen);   //显示整幅图像
                    bSee.setLabel("继续游戏");
                }
            }
        });
    }
    void drawScreen(){                                  //绘制画面的方法
        gb.clearRect(0, 0, sx*4, sy*4);                 //清空buffer
        //将制定位置的图像块绘制至buffer中
        int i;
        for(int j=0;j<4;j++)
            for(i=0;i<4;i++)
                if(map[i][j]!=15)drawSegment(map[i][j],i,j);
        //向Screen绘制buffer中的图像
        gs.drawImage(buff, 0, 0, screen);
    }
    void drawSegment(int seg, int x, int y){
        
        int dx=seg%4*sx,dy=seg/4*sy;
        
        gb.drawImage(imgPuzzle,x*sx,y*sy,x*sx+sx-1,y*sy+sy-1,dx,dy,dx+sx-1,dy+sy-1,screen);
    }
    
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式