로그인 바로가기

중앙 내용으로 바로가기

수성컴퓨터학원

본문내용

본문

자바안드로이드게임
+ Home > 커뮤니티 > 자바안드로이드게임
111
  • 작성자
    김천종
  • 등록일
    2022-11-22 20:52:31
    조회수
    126

 

package javastudy1;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Problem_36 {
 
 static String[] imagename =
  {
    "son.jpg", "kane.jpg", "perisic.jpg",
    "tesla.jpg", "model.jpg", "ben.jpg",
    "galarxy.jpg", "star.jpg", "ioniq.jpg"
  };
 
 static ImageIcon[]img = new ImageIcon[9];
 static JButton[]btn = new JButton[9];
 
 static class MyGUI extends JFrame {
  
  MyGUI() {
   setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
   setTitle("랜덤이미지");
   
   this.setLayout(new GridLayout(3, 3, 10, 10));
   
   this.addKeyListener(new KeyAdapter() {
    public void keyPressed(KeyEvent e) {
     getContentPane().removeAll();
     int randnum[] = makerandom();
     
     for (int i = 0; i < 9; i++) {
      img[i]  = new ImageIcon("e:\\images\\" + imagename[randnum[i]]);
      
      btn[i] = new JButton(img[i]);
      MyGUI.this.add(btn[i]);
      
     }
     validate();
    }
   });
   
   setSize(256, 256);
   setVisible(true);
  }
 }
 
 public static int[] makerandom() {
  int[]rand = { -1, -1, -1, -1, -1, -1, -1, -1, -1};
  int i, k, num;
  char dupl = 'N';
  for(i = 0; i<9;) {
   num = (int) ((Math.random() * 9));
   
   for (k = 0; k < 9; k++)
    if(rand[k] == num)
     dupl = 'Y';
  
    if (dupl == 'N')
     rand[i++] = num;
    else
     dupl = 'N';
   }
  return rand;
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub

  new MyGUI();
 }

 

 

목록보기
답글달기수정하기
삭제하기

관련 쪽지글

이전글이 없습니다.