java swing을 이용해 간단한 게임을 만들었는데...

조회수 911회

안녕하세요 이번에 java swing을 이용해서 같은그림 찾기 프로그램을 만들어봤는데 실행을 시키면 화면도 잘나오고 하는데 문제가 그림이 안나옵니다 파일탐색기 코드로 찾아봐도 정상이라고 뜨는데 이게 왜이러는걸까요?

package AwtSwing;

import java.awt.; import java.awt.event.; import java.io.; import javax.swing.; import javax.swing.Timer; import java.util.Random;

public class BingoGame {

static JPanel panelNorth; //제일 위 view
static JPanel panelCenter; //중간 view
static JLabel labelMessage; //텍스트 표시
static JButton[] buttons = new JButton[16];
static String[] images = {
    "001-pin.png","002-navigation.png","003-location.png","004-flag.png",
    "005-navigation.png","006-turn.png","007-favourite.png","008-location.png",
    "001-pin.png","002-navigation.png","003-location.png","004-flag.png",
    "005-navigation.png","006-turn.png","007-favourite.png","008-location.png",
        //빙고 게임에 들어갈 이미지들 각각2개가 필요하므로 8개X2
};
static int openCount = 0; //Opened Count : 0, 1, 2
static int buttonIndexSave1 = 0; //First Opened Card Index: 0~15
static int buttonIndexSave2 = 0; //Second Opened Card Index: 0~15
static Timer timer;
static int tryCount = 0; //Try Count
static int successCount = 0; //Bingo Count : 0~8

static class MyFrame extends JFrame implements ActionListener{
    public MyFrame(String title) {
        super(title); 
        this.setLayout(new BorderLayout()); //상하좌우 구역이 나누어진것
        this.setSize(400, 500); //게임 크기
        this.setVisible(true); 
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initUI(this); // 화면에 나타날 UI메소드
        mixcard(); //Mix image card

        this.pack(); //비어있는 공간들을 잡아줌
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        if(openCount == 2) {
            return;
        }

        JButton btn = (JButton)e.getSource();
        int index = getButtonIndex(btn);
        btn.setIcon(changeImage(images[index]));

        openCount++;
        if(openCount == 1) { //첫번째 카드인지
            buttonIndexSave1 = index;
        }
        else if(openCount == 2) { //두번쨰 카드인지
            buttonIndexSave2 = index;
            tryCount++;
            labelMessage.setText("같은 사진들을 찾아보세요! " + "  시도횟수 : " + tryCount);

            //Judge Logic 판정 로직
            boolean isBingo = checkCard(buttonIndexSave1, buttonIndexSave2);
            if(isBingo == true) {
                openCount = 0;
                successCount++;
                if(successCount == 8) {
                    labelMessage.setText("Game Over " + " Try " + tryCount);
                }
            }else {
                backToQuestion();
            }
        }
    }

    public void backToQuestion() {
        //timer 1Second
        timer = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Timer.");

                openCount = 0;
                buttons[buttonIndexSave1].setIcon(changeImage("question.png"));
                buttons[buttonIndexSave2].setIcon(changeImage("question.png"));
                timer.stop();
            }
        });
        timer.start();
    }

    public boolean checkCard(int index1, int index2) {
        if(index1 == index2) {
            return false;
        }
        if(images[index1].equals(images[index2])) {
            return true;
        }else {
            return false;
        }
    }

    public int getButtonIndex(JButton btn) {
        int index = 0;
        for(int i = 0; i < 16; i++) {
            if(buttons[i] == btn) { //같은 모양인지
                index = i;
            }
        }
        return index;
    }
}
static void mixcard() {
    Random rand = new Random();
    for(int i = 0; i<1000; i++) {
        int random = rand.nextInt(15) + 1; //1~15 card
        //swap
        String temp = images[0];
        images[0] = images[random];
        images[random] = temp;
    }
}

static void initUI(MyFrame myFrame) {
    panelNorth = new JPanel();
    panelNorth.setPreferredSize(new Dimension(400, 100)); //크기를 지정하는 명령어
    panelNorth.setBackground(Color.black);
    labelMessage = new JLabel("같은 사진들을 찾아보세요! " + "  시도횟수 : 0");
    labelMessage.setPreferredSize(new Dimension(400, 100));
    labelMessage.setForeground(Color.white); //글자색 변경
    labelMessage.setFont(new Font("NanumBarunpenR", Font.BOLD, 17));
    labelMessage.setHorizontalAlignment(JLabel.CENTER); //수평 정렬 배치
    panelNorth.add(labelMessage);
    myFrame.add("North", panelNorth);

    panelCenter = new JPanel();
    panelCenter.setLayout(new GridLayout(4, 4)); //그림이 들어갈 공간
    panelCenter.setPreferredSize(new Dimension(400,400));
    for(int i = 0; i<16 ; i++) {
        buttons[i] = new JButton();
        buttons[i].setPreferredSize(new Dimension(100,100));
        buttons[i].setIcon(changeImage("./image/017-favourite.png"));
        buttons[i].addActionListener(myFrame);
        panelCenter.add(buttons[i]);
    }
    myFrame.add("Center", panelCenter);

}

static ImageIcon changeImage(String filename) {
    ImageIcon icon = new ImageIcon("./img/" + filename);
    Image originImage = icon.getImage();
    Image changedImage = originImage.getScaledInstance(80, 80, Image.SCALE_SMOOTH);
    ImageIcon icon_new = new ImageIcon(changedImage);
    return icon_new;
}

public static void main(String[] args) {
    new MyFrame("Bingo Game");

    File f = new File("./image/008-location.png");
    System.out.println(f.exists()?"Exists":"doesnt exists");
}

}

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)