잡동사니에도 사랑을

[21.08.23] 경마 게임 (Racer, RunRace) 본문

카테고리 없음

[21.08.23] 경마 게임 (Racer, RunRace)

luvforjunk 2021. 8. 24. 23:01
728x90
반응형

////////Racer

 

package thread_;

 

import java.awt.Canvas;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.Toolkit;

 

// 1마리

public class Racer extends Canvas implements Runnable { // Canvas 따로따로 잡고 들어가는

         private Image img;

         private String horseName;

         int pos;

 

         public Racer(String horseName) {

                  img = Toolkit.getDefaultToolkit().getImage("image/.gif");

                  this.horseName = horseName;

         }

 

         @Override

         public void paint(Graphics g) {

                  super.paint(g);

                  g.drawImage(img, pos, 0, 25, this.getSize().height, this);

                  // this.getSize().height에서 this Canvas

                  g.drawLine(0, this.getSize().height - 1,

                           this.getSize().width, this.getSize().height - 1); // 라인 그리기

         }

 

         @Override

         public void run() {

                  for (int i = 0; i < 600; i += ((int) (Math.random() * 10) + 1)) {

                           // 0부터 600 이전까지 랜덤으로 1~10만큼 움직여 엎치락 뒤치락하게끔

                           pos = i; // 움직임이 계속 다르게 들어가게 하기 위해 g.drawImage x값을 고쳐줌

                           repaint();

 

                           try {

                                   Thread.sleep(100);

                           } catch (InterruptedException e) {

                                   e.printStackTrace();

                           }

                  } // for

 

                  // 등수

                  RunRace r = new RunRace();

                  System.out.println(r.rank + " " + horseName);

                  r.rank++;

         }

}

 

////////RunRace

 

package thread_;

 

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Scanner;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class RunRace extends JFrame implements ActionListener {

         public static int rank = 1;

         private int count;

         private JButton btn;

         private Racer[] racer; // Canvas

 

         public RunRace(int count) {

                  this.count = count;

                  btn = new JButton("시작");

                  JPanel southP = new JPanel(new FlowLayout(FlowLayout.RIGHT));

                  southP.add(btn);

 

                  Scanner scan = new Scanner(System.in);

 

                  JPanel centerP = new JPanel(new GridLayout(count, 1)); // FlowLayout

                  racer = new Racer[count]; // 개수만큼 필요하다

                  String horseName = null;

 

                  for (int i = 0; i < count; i++) {

                           System.out.print(" 이름 입력 : ");

                           horseName = scan.next();

 

                           racer[i] = new Racer(horseName);

                           centerP.add(racer[i]);

                  }

                 

                  this.add("Center", centerP);

                  this.add("South", southP);

 

                  setBounds(900, 200, 600, 400);

                  setResizable(false); // 고정

                  setVisible(true);

                  setDefaultCloseOperation(EXIT_ON_CLOSE); // 종료

 

                  // 이벤트

                  btn.addActionListener(this);

         }

 

         public RunRace() {

         }

 

         @Override

         public void actionPerformed(ActionEvent e) {

                  btn.setEnabled(false);

 

                  // 시작버튼을 눌렀을 Thread !

                  Thread[] t = new Thread[count]; // 배열 생성

                  for (int i = 0; i < count; i++) {

                           t[i] = new Thread(racer[i]); // 스레드 생성

                           // ***Thread 되고 싶은 Racer/

                           // Racer new해서 생성해주는 곳은 Racer.java 아닌 RunRace.java이다

                           // 따라서 생성자가 있는 곳에서 Thread만들어 주는 것이 맞다

 

                           // Thread 우선 순위 부여 1~10

                           int num = (int) (Math.random() * 10) + 1;

                           t[i].setPriority(num);

                           t[i].start();// 스레드 시작

                  }

         }

 

         public static void main(String[] args) {

                  Scanner scan = new Scanner(System.in);

                  System.out.print(" 마리 입력 : ");

                  int count = scan.nextInt();

 

                  new RunRace(count);

         }

}

728x90
반응형