[JavaFX] 리듬게임 만들기
https://blog.naver.com/kimwonho001/221932223608
1.Main 클래스
package dynamic_beat_15;
public class Main {
//상수 정의
public static final int SCREEN_WIDTH = 1200;
public static final int SCREEN_HEIGHT = 720;
public static final int NOTE_SPEED = 3;
public static final int SLEEP_TIME = 10;
public static final int REACH_TIME = 2;
public static void main(String[] args) {
new DynamicBeat();
}
}
2. DynamicBeat 클래스
package dynamic_beat_15;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DynamicBeat extends JFrame { // GUI 그래픽 기반으로 개발하기위해 JFrame 상속받는다.
private Image screeanImage;
private Graphics screenGraphic;
private ImageIcon exitButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/exitButtonEntered.png"));
private ImageIcon exitButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/exitButtonBasic.png"));
private ImageIcon startButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/startButtonBasic.png"));
private ImageIcon startButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/startButtonEntered.png"));
private ImageIcon quitButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/quitButtonBasic.png"));
private ImageIcon quitButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/quitButtonEntered.png"));
private ImageIcon leftButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/leftButtonBasic.png"));
private ImageIcon leftButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/leftButtonEntered.png"));
private ImageIcon rightButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/rightButtonBasic.png"));
private ImageIcon rightButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/rightButtonEntered.png"));
private ImageIcon easyButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/easyButtonBasic.png"));
private ImageIcon easyButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/easyButtonEntered.png"));
private ImageIcon hardButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/hardButtonBasic.png"));
private ImageIcon hardButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/hardButtonEntered.png"));
private ImageIcon backButtonBasicImage = new ImageIcon(Main.class.getResource("../Images/backButtonBasic.png"));
private ImageIcon backButtonEnteredImage = new ImageIcon(Main.class.getResource("../Images/backButtonEntered.png"));
private Image background = new ImageIcon(Main.class.getResource("../Images/introBackground.jpg")).getImage();
private JLabel menuBar = new JLabel(new ImageIcon(Main.class.getResource("../Images/menuBar.png")));
private JButton exitButton = new JButton(exitButtonBasicImage);
private JButton startButton = new JButton(startButtonBasicImage);
private JButton quitButton = new JButton(quitButtonBasicImage);
private JButton leftButton = new JButton(leftButtonBasicImage);
private JButton rightButton = new JButton(rightButtonBasicImage);
private JButton easyButton = new JButton(easyButtonBasicImage);
private JButton hardButton = new JButton(hardButtonBasicImage);
private JButton backButton = new JButton(backButtonBasicImage);
private int mouseX, mouseY;
private boolean isMainScreen = false;
private boolean isGameScreen = false;
ArrayList<Track> trackList = new ArrayList<Track>();
private Image titleImage;
private Image selectedImage;
private Music selectedMusic;
private Music introMusic = new Music("IntroMusic.mp3", true);
private int nowSelected = 0;
//public static Game game = new Game();
public static Game game;
public DynamicBeat() {
trackList.add(new Track("Wild Flower Title Image.png", "Wild Flower Start Image.png",
"Wild Flower Game Image.png", "Wild Flower selected.mp3", "Joakim Karud - Wild Flower.mp3","Joakim Karud - Wild Flower"));
trackList.add(new Track("Mighty Love Title Image.png", "Mighty Love Start Image.png",
"Mighty Love Game Image.png", "Mighty Love selected.mp3", "Joakim Karud - Mighty Love.mp3","Joakim Karud - Mighty Love"));
trackList.add(new Track("Energy Title Image.png", "Energy Start Image.png",
"Energy Game Image.png", "Energy selected.mp3", "Bensound - Energy.mp3","Bensound - Energy"));
setUndecorated(true);// 실행 후 메뉴바가 안보인다.
setTitle("Dynamic Beat");
setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);// 창크기
setResizable(false);// 사용자가 못줄이게
setLocationRelativeTo(null); // 중앙에 뜨게
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 완전 종료
setVisible(true);
setBackground(new Color(0, 0, 0, 0));// 패인트 빽그라운드했을때 전부 흰색으로 보임
setLayout(null);
addKeyListener(new KeyListener());
introMusic.start();
exitButton.setBounds(1170, 0, 30, 30);//컨트롤 쉬프트 F 자동정렬
exitButton.setBorderPainted(false);
exitButton.setContentAreaFilled(false);
exitButton.setFocusPainted(false);
exitButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
exitButton.setIcon(exitButtonEnteredImage); //마우스 버튼 이미지 교체
exitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
exitButton.setIcon(exitButtonBasicImage);
exitButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.exit(0);
}
});
add(exitButton);
startButton.setBounds(40, 200, 400, 100);//컨트롤 쉬프트 F 자동정렬
startButton.setBorderPainted(false);
startButton.setContentAreaFilled(false);
startButton.setFocusPainted(false);
startButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
startButton.setIcon(startButtonEnteredImage); //마우스 버튼 이미지 교체
startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
startButton.setIcon(startButtonBasicImage);
startButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
//introMusic.close();
enterMain();
}
});
add(startButton);
quitButton.setBounds(40, 330, 400, 100);//컨트롤 쉬프트 F 자동정렬
quitButton.setBorderPainted(false);
quitButton.setContentAreaFilled(false);
quitButton.setFocusPainted(false);
quitButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
quitButton.setIcon(quitButtonEnteredImage); //마우스 버튼 이미지 교체
quitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
quitButton.setIcon(quitButtonBasicImage);
quitButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.exit(0);
}
});
add(quitButton);
leftButton.setVisible(false);
leftButton.setBounds(140, 310, 60, 60);//컨트롤 쉬프트 F 자동정렬
leftButton.setBorderPainted(false);
leftButton.setContentAreaFilled(false);
leftButton.setFocusPainted(false);
leftButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
leftButton.setIcon(leftButtonEnteredImage); //마우스 버튼 이미지 교체
leftButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
leftButton.setIcon(leftButtonBasicImage);
leftButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
//왼쪽 버튼 이벤트
selectLeft();
}
});
add(leftButton);
rightButton.setVisible(false);
rightButton.setBounds(1080, 310, 60, 60);//컨트롤 쉬프트 F 자동정렬
rightButton.setBorderPainted(false);
rightButton.setContentAreaFilled(false);
rightButton.setFocusPainted(false);
rightButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
rightButton.setIcon(rightButtonEnteredImage); //마우스 버튼 이미지 교체
rightButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
rightButton.setIcon(rightButtonBasicImage);
rightButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
//오른쪽 버튼 이벤트
selectRight();
}
});
add(rightButton);
easyButton.setVisible(false);
easyButton.setBounds(375, 580, 250, 67);//컨트롤 쉬프트 F 자동정렬
easyButton.setBorderPainted(false);
easyButton.setContentAreaFilled(false);
easyButton.setFocusPainted(false);
easyButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
easyButton.setIcon(easyButtonEnteredImage); //마우스 버튼 이미지 교체
easyButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
easyButton.setIcon(easyButtonBasicImage);
easyButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
//난이도 쉬운 이벤트
gameStart(nowSelected, "Easy");
}
});
add(easyButton);
hardButton.setVisible(false);
hardButton.setBounds(655, 580, 250, 67);//컨트롤 쉬프트 F 자동정렬
hardButton.setBorderPainted(false);
hardButton.setContentAreaFilled(false);
hardButton.setFocusPainted(false);
hardButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
hardButton.setIcon(hardButtonEnteredImage); //마우스 버튼 이미지 교체
hardButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
hardButton.setIcon(hardButtonBasicImage);
hardButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
//난이도 어려울때 이벤트
gameStart(nowSelected, "Hard");
}
});
add(hardButton);
backButton.setVisible(false);
backButton.setBounds(20, 50, 60, 60);//컨트롤 쉬프트 F 자동정렬
backButton.setBorderPainted(false);
backButton.setContentAreaFilled(false);
backButton.setFocusPainted(false);
backButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
backButton.setIcon(backButtonEnteredImage); //마우스 버튼 이미지 교체
backButton.setCursor(new Cursor(Cursor.HAND_CURSOR));//손가락 효과
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mouseExited(MouseEvent e) {
backButton.setIcon(backButtonBasicImage);
backButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);//효과음
buttonEnteredMusic.start();
}
@Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3",false);
buttonEnteredMusic.start();
//메인으로 돌아가는
backMain();
}
});
add(backButton);
menuBar.setBounds(0, 0, 1280, 30); // 메뉴바 위치 설정
menuBar.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
});
menuBar.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen();
int y = e.getYOnScreen();
setLocation(x - mouseX, y - mouseY);
}
});
add(menuBar);
// 이미지 가져오기
// introBackground = new
// ImageIcon(Main.class.getResource("../Images/background.jpg")).getImage();
}
public void paint(Graphics g) {
// 이미지 화면크기만큼 크기 설정
screeanImage = createImage(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
screenGraphic = screeanImage.getGraphics();
screenDraw((Graphics2D) screenGraphic); //글자 깨질 시 Graphics2D 라이브러리 사용해서 형변환해줌
g.drawImage(screeanImage, 0, 0, null);
}
private void screenDraw(Graphics2D g) {
g.drawImage(background, 0, 0, null); // 백그라운드는 이걸 활용
if(isMainScreen)
{
g.drawImage(selectedImage, 340, 100, null);
g.drawImage(titleImage, 340, 70, null);
}
if(isGameScreen)
{
game.screenDraw(g);
}
paintComponents(g); // 고정된 이미지이기때문에 이걸 활용해 그려준다.
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
this.repaint();
}
public void selectTrack(int nowSelected) {
if(selectedMusic != null)
selectedMusic.close();
titleImage = new ImageIcon(Main.class.getResource("../Images/" + trackList.get(nowSelected).getTitleImage())).getImage();
selectedImage = new ImageIcon(Main.class.getResource("../Images/"+ trackList.get(nowSelected).getStartImage())).getImage();
selectedMusic = new Music(trackList.get(nowSelected).getStartMusic(),true);
selectedMusic.start();
}
public void selectLeft() {
if(nowSelected == 0)
nowSelected = trackList.size() -1;
else
nowSelected--;
selectTrack(nowSelected);
}
public void selectRight() {
if(nowSelected == trackList.size() - 1)
nowSelected = 0;
else
nowSelected++;
selectTrack(nowSelected);
}
public void gameStart(int nowSelected, String difficulty) {
if(selectedMusic != null)
selectedMusic.close();
isMainScreen = false;
leftButton.setVisible(false);
rightButton.setVisible(false);
easyButton.setVisible(false);
hardButton.setVisible(false);
background = new ImageIcon(Main.class.getResource("../Images/"+ trackList.get(nowSelected).getGameImage())).getImage();
backButton.setVisible(true);
isGameScreen = true;
game = new Game(trackList.get(nowSelected).getTitleName(),difficulty,trackList.get(nowSelected).getGameMusic());
game.start();
setFocusable(true);
}
public void backMain() {
isMainScreen = true;
leftButton.setVisible(true);
rightButton.setVisible(true);
easyButton.setVisible(true);
hardButton.setVisible(true);
background = new ImageIcon(Main.class.getResource("../Images/mainBackground.jpg")).getImage();
backButton.setVisible(false);
selectTrack(nowSelected);
isGameScreen = false;
game.close();
}
public void enterMain() {
//다음곡으로 재생
//Music selectedMusic = new Music("Mighty Love selected.mp3",true);//무한박복
//selectedMusic.start();
//게임 시작 이벤트
//이미지 교체
startButton.setVisible(false);
quitButton.setVisible(false);
background = new ImageIcon(Main.class.getResource("../Images/MainBAckground.jpg")).getImage();
isMainScreen = true;
leftButton.setVisible(true);
rightButton.setVisible(true);
easyButton.setVisible(true);
hardButton.setVisible(true);
introMusic.close();
selectTrack(0);
}
}
3. Music 클래스
package dynamic_beat_15;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class Music extends Thread {
private Player player;
private boolean isLoop;
private File file;
private FileInputStream fis;
private BufferedInputStream bis;
public Music(String name, boolean isLoop) {
try {
this.isLoop = isLoop;
file = new File(Main.class.getResource("../music/"+name).toURI());
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
//현재 음악이 어디까지 플레이중인지
public int getTime() {
if(player == null)
return 0;
return player.getPosition();
}
//음악이 즉시 종료할수있게 해주는거
public void close() {
isLoop = false;
player.close();
this.interrupt();//해당 쓰레드를 중지상태로 만든다.
}
@Override
public void run() {
try {
do {
player.play();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
} while (isLoop);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
3.Track 클래스
package dynamic_beat_15;
public class Track {
private String titleImage;//제목 부분 이미지
private String startImage;//게임 선택 창 표지 이미지
private String gameImage; //해당 곡을 실행했을때 표지 이밎
private String startMusic;//게임 선택 창 음악
private String gameMusic;//해당 곡을 실행했을 때 음악
private String titleName;//곡 제목
public String getTitleImage() {
return titleImage;
}
public void setTitleImage(String titleImage) {
this.titleImage = titleImage;
}
public String getStartImage() {
return startImage;
}
public void setStartImage(String startImage) {
this.startImage = startImage;
}
public String getGameImage() {
return gameImage;
}
public void setGameImage(String gameImage) {
this.gameImage = gameImage;
}
public String getStartMusic() {
return startMusic;
}
public void setStartMusic(String startMusic) {
this.startMusic = startMusic;
}
public String getGameMusic() {
return gameMusic;
}
public void setGameMusic(String gameMusic) {
this.gameMusic = gameMusic;
}
public String getTitleName() {
return titleName;
}
public void setTitleName(String titleName) {
this.titleName = titleName;
}
public Track(String titleImage, String startImage, String gameImage, String startMusic, String gameMusic,String titleName) {
super();
this.titleImage = titleImage;
this.startImage = startImage;
this.gameImage = gameImage;
this.startMusic = startMusic;
this.gameMusic = gameMusic;
this.titleName = titleName;
}
}
4.Track 클래스
package dynamic_beat_15;
public class Track {
private String titleImage;//제목 부분 이미지
private String startImage;//게임 선택 창 표지 이미지
private String gameImage; //해당 곡을 실행했을때 표지 이밎
private String startMusic;//게임 선택 창 음악
private String gameMusic;//해당 곡을 실행했을 때 음악
private String titleName;//곡 제목
public String getTitleImage() {
return titleImage;
}
public void setTitleImage(String titleImage) {
this.titleImage = titleImage;
}
public String getStartImage() {
return startImage;
}
public void setStartImage(String startImage) {
this.startImage = startImage;
}
public String getGameImage() {
return gameImage;
}
public void setGameImage(String gameImage) {
this.gameImage = gameImage;
}
public String getStartMusic() {
return startMusic;
}
public void setStartMusic(String startMusic) {
this.startMusic = startMusic;
}
public String getGameMusic() {
return gameMusic;
}
public void setGameMusic(String gameMusic) {
this.gameMusic = gameMusic;
}
public String getTitleName() {
return titleName;
}
public void setTitleName(String titleName) {
this.titleName = titleName;
}
public Track(String titleImage, String startImage, String gameImage, String startMusic, String gameMusic,String titleName) {
super();
this.titleImage = titleImage;
this.startImage = startImage;
this.gameImage = gameImage;
this.startMusic = startMusic;
this.gameMusic = gameMusic;
this.titleName = titleName;
}
}
5.Note 클래스
package dynamic_beat_15;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Note extends Thread {
private Image noteBasicImage = new ImageIcon(Main.class.getResource("../Images/noteBasic.png")).getImage();
private int x, y = 580 - (1000 / Main.SLEEP_TIME * Main.NOTE_SPEED) * Main.REACH_TIME;
private String noteType;
private boolean proceeded = true;
public String getNoteType() {
return noteType;
}
public boolean isProceeded() {
return proceeded;
}
public void close() {
proceeded = false;
}
public Note(String noteType) {
if(noteType.equals("S")) {
x = 228;
}
else if(noteType.equals("D")) {
x = 332;
}
else if(noteType.equals("F")) {
x = 436;
}
else if(noteType.equals("Space")) {
x = 540;
}
else if(noteType.equals("J")) {
x = 744;
}
else if(noteType.equals("K")) {
x = 848;
}
else if(noteType.equals("L")) {
x = 952;
}
this.noteType = noteType;
}
public void screenDraw(Graphics2D g) {
if(!noteType.equals("Space")) {
g.drawImage(noteBasicImage, x, y, null);
}
else if(noteType.equals("long")) {
g.drawImage(noteBasicImage, x, y, null);
g.drawImage(noteBasicImage, x+100, y, null);
}
}
public void drop() {
y += Main.NOTE_SPEED;
if(y>620) {
System.out.println("Miss");
close();
}
}
@Override
public void run() {
try {
while (true) {
drop();
if(proceeded) {
Thread.sleep(Main.SLEEP_TIME);
}
else {
interrupt();
break;
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public String judge() {
if(y >= 613) {
System.out.println("Late");
close();
return "Late";
}
else if(y >= 600) {
System.out.println("Good");
close();
return "Good";
}
else if(y >= 587) {
System.out.println("Great");
close();
return "Great";
}
else if(y >= 573) {
System.out.println("Perfect");
close();
return "Perfect";
}
else if(y >= 565) {
System.out.println("Great");
close();
return "Great";
}
else if(y >= 550) {
System.out.println("Good");
close();
return "Good";
}
else if(y >= 535) {
System.out.println("Early");
close();
return "Early";
}
return "None";
}
public int getY() {
return y;
}
}
5.Beat 클래스
package dynamic_beat_15;
public class Beat {
private int time;
private String noteName;
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public String getNoteName() {
return noteName;
}
public void setNoteName(String noteName) {
this.noteName = noteName;
}
public Beat(int time, String noteName) {
super();
this.time = time;
this.noteName = noteName;
}
}
7.KeyListener 클래스
package dynamic_beat_15;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
if(DynamicBeat.game == null) {
return;
}
if(e.getKeyCode() == KeyEvent.VK_S) {
DynamicBeat.game.pressS();
}
else if(e.getKeyCode() == KeyEvent.VK_D) {
DynamicBeat.game.pressD();
}
else if(e.getKeyCode() == KeyEvent.VK_F) {
DynamicBeat.game.pressF();
}
else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
DynamicBeat.game.pressSpace();
}
else if(e.getKeyCode() == KeyEvent.VK_J) {
DynamicBeat.game.pressJ();
}
else if(e.getKeyCode() == KeyEvent.VK_K) {
DynamicBeat.game.pressK();
}
else if(e.getKeyCode() == KeyEvent.VK_L) {
DynamicBeat.game.pressL();
}
}
@Override
public void keyReleased(KeyEvent e) {
if(DynamicBeat.game == null) {
return;
}
if(e.getKeyCode() == KeyEvent.VK_S) {
DynamicBeat.game.releaseS();
}
else if(e.getKeyCode() == KeyEvent.VK_D) {
DynamicBeat.game.releaseD();
}
else if(e.getKeyCode() == KeyEvent.VK_F) {
DynamicBeat.game.releaseF();
}
else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
DynamicBeat.game.releaseSpace();
}
else if(e.getKeyCode() == KeyEvent.VK_J) {
DynamicBeat.game.releaseJ();
}
else if(e.getKeyCode() == KeyEvent.VK_K) {
DynamicBeat.game.releaseK();
}
else if(e.getKeyCode() == KeyEvent.VK_L) {
DynamicBeat.game.releaseL();
}
}
}
8.Game 클래스
package dynamic_beat_15;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.ImageIcon;
public class Game extends Thread{
//private Image noteBasicImage = new ImageIcon(Main.class.getResource("../Images/noteBasic.png")).getImage();
private Image noteRouteLineImage = new ImageIcon(Main.class.getResource("../Images/noteRouteLine.png")).getImage();
private Image judgementLineImage = new ImageIcon(Main.class.getResource("../Images/judgementLine.png")).getImage();
private Image gameInfoImage = new ImageIcon(Main.class.getResource("../Images/gameinfo.png")).getImage();
private Image noteRouteSImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image noteRouteDImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image noteRouteFImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image noteRouteSpace1Image = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image noteRouteSpace2Image = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image noteRouteJImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image noteRouteKImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image noteRouteLImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
private Image blueFlareImage;
private Image judgeImage;
private Image keyPadSImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private Image keyPadDImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private Image keyPadFImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private Image keyPadSpace1Image = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private Image keyPadSpace2Image = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private Image keyPadJImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private Image keyPadKImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private Image keyPadLImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
private String titleName;
private String difficulty;
private String musicTitle;
private Music gameMusic;
ArrayList<Note> noteList = new ArrayList<Note>();
public Game(String titleName, String difficulty,String musicTitle) {
this.titleName = titleName;
this.difficulty = difficulty;
this.musicTitle = musicTitle;
gameMusic = new Music(this.musicTitle,false);
}
public void screenDraw(Graphics2D g) {
g.drawImage(noteRouteSImage, 228, 30, null);
g.drawImage(noteRouteDImage, 332, 30, null);
g.drawImage(noteRouteFImage, 436, 30, null);
g.drawImage(noteRouteSpace1Image, 540, 30, null);
g.drawImage(noteRouteSpace2Image, 640, 30, null);
g.drawImage(noteRouteJImage, 744, 30, null);
g.drawImage(noteRouteKImage, 848, 30, null);
g.drawImage(noteRouteLImage, 952, 30, null);
g.drawImage(noteRouteLineImage, 224, 30, null);
g.drawImage(noteRouteLineImage, 328, 30, null);
g.drawImage(noteRouteLineImage, 432, 30, null);
g.drawImage(noteRouteLineImage, 536, 30, null);
g.drawImage(noteRouteLineImage, 740, 30, null);
g.drawImage(noteRouteLineImage, 844, 30, null);
g.drawImage(noteRouteLineImage, 948, 30, null);
g.drawImage(noteRouteLineImage, 1052, 30, null);
g.drawImage(gameInfoImage, 0, 600, null);
g.drawImage(judgementLineImage, 0, 580, null);
for(int i = 0; i < noteList.size(); i++) {
Note note = noteList.get(i);
if(note.getY() > 620) {
judgeImage = new ImageIcon(Main.class.getResource("../Images/judegeMiss.png")).getImage();
}
if(!note.isProceeded()) {
noteList.remove(i);
i--;
}
else {
note.screenDraw(g);
}
}
/*
g.drawImage(noteBasicImage, 228, 120, null);
g.drawImage(noteBasicImage, 332, 580, null);
g.drawImage(noteBasicImage, 436, 500, null);
g.drawImage(noteBasicImage, 540, 340, null);
g.drawImage(noteBasicImage, 640, 340, null);
g.drawImage(noteBasicImage, 744, 325, null);
g.drawImage(noteBasicImage, 848, 305, null);
g.drawImage(noteBasicImage, 952, 305, null);
*/
g.setColor(Color.white);
//안티엘리어싱 적용
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g.setFont(new Font("Arial",Font.BOLD,30));
g.drawString(titleName, 20, 702);
g.drawString(difficulty, 1190, 702);
g.setFont(new Font("Arial",Font.PLAIN,26));
g.setColor(Color.DARK_GRAY);
g.drawString("S", 270, 609);
g.drawString("D", 374, 609);
g.drawString("F", 478, 609);
g.drawString("Space Bar", 580, 609);
g.drawString("J", 784, 609);
g.drawString("K", 889, 609);
g.drawString("L", 993, 609);
g.setColor(Color.LIGHT_GRAY);
g.setFont(new Font("Elephant",Font.BOLD,30));
g.drawString("000000", 565, 702);
g.drawImage(blueFlareImage,320,430,null);
g.drawImage(judgeImage,460,420,null);
g.drawImage(keyPadSImage,228,580,null);
g.drawImage(keyPadDImage,332,580,null);
g.drawImage(keyPadFImage,436,580,null);
g.drawImage(keyPadSpace1Image,540,580,null);
g.drawImage(keyPadSpace2Image,640,580,null);
g.drawImage(keyPadJImage,744,580,null);
g.drawImage(keyPadKImage,848,580,null);
g.drawImage(keyPadLImage,952,580,null);
}
public void pressS() {
judge("S");
noteRouteSImage = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadSImage = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
new Music("drumSamll1.mp3",false).start();
}
public void releaseS() {
noteRouteSImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadSImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
}
public void pressD() {
judge("D");
noteRouteDImage = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadDImage = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
new Music("drumSamll2.mp3",false).start();
}
public void releaseD() {
noteRouteDImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadDImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
}
public void pressF() {
judge("F");
noteRouteFImage = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadFImage = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
new Music("drumSamll3.mp3",false).start();
}
public void releaseF() {
noteRouteFImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadFImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
}
public void pressSpace() {
judge("Space");
noteRouteSpace1Image = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadSpace1Image = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
noteRouteSpace2Image = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadSpace2Image = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
new Music("drumBig3.mp3",false).start();
}
public void releaseSpace() {
noteRouteSpace1Image = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadSpace1Image = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
noteRouteSpace2Image = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadSpace2Image = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
}
public void pressJ() {
judge("J");
noteRouteJImage = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadJImage = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
new Music("drumSamll3.mp3",false).start();
}
public void releaseJ() {
noteRouteJImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadJImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
}
public void pressK() {
judge("K");
noteRouteKImage = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadKImage = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
new Music("drumSamll2.mp3",false).start();
}
public void releaseK() {
noteRouteKImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadKImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
}
public void pressL() {
judge("L");
noteRouteLImage = new ImageIcon(Main.class.getResource("../Images/noteRoutePressed.png")).getImage();
keyPadLImage = new ImageIcon(Main.class.getResource("../Images/keyPadPressed.png")).getImage();
new Music("drumSamll1.mp3",false).start();
}
public void releaseL() {
noteRouteLImage = new ImageIcon(Main.class.getResource("../Images/noteRoute.png")).getImage();
keyPadLImage = new ImageIcon(Main.class.getResource("../Images/keyPadBasic.png")).getImage();
}
@Override
public void run() {
dropNotes(this.titleName);
}
public void close() {
gameMusic.close();
this.interrupt();
}
public void dropNotes(String titleName) {
Beat[] beats = null;
if(titleName.equals("Joakim Karud - Mighty Love") && difficulty.equals("Easy") ) {
int startTime = 4460 - Main.REACH_TIME * 1000;
int gap = 125;
beats = new Beat[] {
new Beat(startTime,"S"),
new Beat(startTime + gap * 2 ,"D"),
new Beat(startTime + gap * 4 ,"S"),
new Beat(startTime + gap * 6 ,"D"),
new Beat(startTime + gap * 8 ,"S"),
new Beat(startTime + gap * 10 ,"D"),
new Beat(startTime + gap * 12 ,"S"),
new Beat(startTime + gap * 14 ,"J"),
new Beat(startTime + gap * 18 ,"J"),
new Beat(startTime + gap * 20 ,"K"),
new Beat(startTime + gap * 22 ,"K"),
new Beat(startTime + gap * 24 ,"L"),
new Beat(startTime + gap * 26 ,"L"),
new Beat(startTime + gap * 28 ,"S"),
new Beat(startTime + gap * 30 ,"D"),
new Beat(startTime + gap * 32 ,"Space"),
new Beat(startTime + gap * 34 ,"J"),
};
}
if(titleName.equals("Joakim Karud - Mighty Love") && difficulty.equals("Hard") ) {
int startTime = 4460 - Main.REACH_TIME * 1000;
int gap = 125;
beats = new Beat[] {
new Beat(startTime,"S"),
new Beat(startTime + gap * 2 ,"D"),
};
}
else if(titleName.equals("Joakim Karud - Wild Flower") && difficulty.equals("Easy") ) {
int startTime = 1000;
beats = new Beat[] {
new Beat(startTime,"Space"),
};
}
else if(titleName.equals("Joakim Karud - Wild Flower") && difficulty.equals("Hard") ) {
int startTime = 1000;
beats = new Beat[] {
new Beat(startTime,"Space"),
};
}
else if(titleName.equals("Bendsound - Energy") && difficulty.equals("Easy") ) {
int startTime = 1000;
beats = new Beat[] {
new Beat(startTime,"Space"),
};
}
else if(titleName.equals("Bendsound - Energy") && difficulty.equals("Hard") ) {
int startTime = 1000;
beats = new Beat[] {
new Beat(startTime,"Space"),
};
}
/*
Beat[] beats = {
new Beat(1000,"S"),
new Beat(2000,"D"),
new Beat(3000,"F"),
new Beat(4000,"F"),
new Beat(5000,"F"),
new Beat(6000,"F"),
new Beat(7000,"F"),
};
*/
int i = 0;
gameMusic.start();
while(i < beats.length && !isInterrupted()) {
boolean dropped = false;
if(beats[i].getTime() <= gameMusic.getTime()) {
Note note = new Note(beats[i].getNoteName());
note.start();
noteList.add(note);
i++;
dropped = true;
}
if(!dropped) {
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void judge(String input) {
for(int i=0; i<noteList.size(); i++) {
Note note = noteList.get(i);
if(input.equals(note.getNoteType())) {
judgeEvent(note.judge());
break;
}
}
}
public void judgeEvent(String judge) {
if(!judge.equals("None")) {
blueFlareImage = new ImageIcon(Main.class.getResource("../Images/blueFlare.png")).getImage();
}
if(judge.equals("Miss")) {
judgeImage = new ImageIcon(Main.class.getResource("../Images/judgeMiss.png")).getImage();
}
else if(judge.equals("Late")) {
judgeImage = new ImageIcon(Main.class.getResource("../Images/judgeLate.png")).getImage();
}
else if(judge.equals("Good")) {
judgeImage = new ImageIcon(Main.class.getResource("../Images/judgeGood.png")).getImage();
}
else if(judge.equals("Great")) {
judgeImage = new ImageIcon(Main.class.getResource("../Images/judgeGreat.png")).getImage();
}
else if(judge.equals("Perfect")) {
judgeImage = new ImageIcon(Main.class.getResource("../Images/judgePerfect.png")).getImage();
}
else if(judge.equals("Early")) {
judgeImage = new ImageIcon(Main.class.getResource("../Images/judgeEarly.png")).getImage();
}
}
}
'JAVA > JAVA Tip' 카테고리의 다른 글
이클립스(eclipse)디버깅 단축키 (0) | 2023.11.23 |
---|---|
이클립스에서 자바프로젝트 열기 (0) | 2022.10.19 |
[JavaFX] 채팅프로그램 개발 (0) | 2022.10.17 |
JavaFX 설치 후 프로젝트 실행 오류 (0) | 2022.10.12 |
이클립스 설치 방법 (0) | 2022.10.11 |
댓글