簡體   English   中英

Java中的拼圖游戲

[英]Picture piece puzzle game in Java

這是我正在從事的高中項目。 我找不到在框架上顯示圖像的方法。 這是我的第一個問題。 我相信以后還會有更多問題。 下面是我當前的代碼。

第一部分:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JApplet;

public class GameVer2 extends JApplet implements ActionListener{
    private int sizeOfGame = 16;
    private ImageIcon[] picts = {new ImageIcon("back.png"),
    new ImageIcon("field.png"),new ImageIcon("front.png"),
    new ImageIcon("gym.png"),new ImageIcon("mermaid.png"),
    new ImageIcon("mickey_pirate.png"),new ImageIcon("path.png"),
    new ImageIcon("tweety.png"),new ImageIcon("back.png"),
    new ImageIcon("field.png"),new ImageIcon("front.png"),
    new ImageIcon("gym.png"),new ImageIcon("mermaid.png"),
    new ImageIcon("mickey_pirate.png"),new ImageIcon("path.png"),
    new ImageIcon("tweety.png")};

    private GamePiece[] tiles = new GamePiece[sizeOfGame];
    private int[] tilesReady;
    private int turn = 0;
    private GamePiece[] data = new GamePiece[2];

    public GameVer2(){
        this.setVisible(true);
        this.setLayout(new GridLayout(4,4));
        buildGamePieces();
    }

    public void buildGamePieces(){
        for(int i=0; i<tiles.length; i++){
            tiles[i] = new GamePiece(picts[i],i);
            this.getContentPane().add(tiles[i]);
            tiles[i].addActionListener(this);
            tiles[i].setActionCommand(i+"");
            this.getContentPane().add(tiles[i]);
            tiles[i].getImage();
        }
    }
    @Override
    public void actionPerformed(ActionEvent arg0){
        System.out.println("hello");
    }

    public void init(){
        GameVer2 go = new GameVer2();
    }
}

第二部分:

import javax.swing.ImageIcon;
import javax.swing.JButton;


public class GamePiece extends JButton{

    private ImageIcon image;
    private int id;

    //GamePiece b = new GamePiece("gym.png",1);
    //GamePiece c = new GamePiece("front.png",2);
    // b.match(c);

    public boolean match(GamePiece a){
        return this.getImage().toString().equals(a.getImage().toString());
    }

    public ImageIcon getImage(){
        return image;
    }

    public void setImage(ImageIcon image){
        this.image = image;
    }

    public int getID(){
        return id;
    }

    public void setId(int id){
        this.id = id;
    }

    public GamePiece (ImageIcon pict, int i){
        image = pict;
        id = i;
    }
}

setIcon添加到構造函數方法中,然后進行相應的更改。

  public GamePiece (ImageIcon pict, int i){
        setIcon(pict);
    }

還要確保新ImageIcon()中的圖像文件位置正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM