본문 바로가기
Java/Swing 미니프로젝트

[JAVA Project] 미니 프로젝트 리듬게임 만들기 4 - 시작하기버튼, main곡화면설정

by dev수니 2021. 4. 19.
반응형

 

 

시작 버튼으로 start 버튼과 quit 버튼을 그림판으로 만들어줬다. 맨아래에 첨부파일 넣어놓겠다.

start 버튼을 누르면 main 화면으로 바뀌도록 설정한다.

이 또한 첨부파일에 첨부해두었다.

wallpaperswide.com/

 

 

그리고 게임으로 실행할 음악 파일을 3개 다운로드 해줬다. Bensound 사이트를 이용했다.

www.bensound.com/royalty-free-music/3

 

Royalty Free Music by Bensound | Stock Music

Popular Newest Shortest Longest

www.bensound.com

 

 

또 start 버튼을 누른 후 실행할 음악을 고를 수 있도록 목록 이미지와 음악을 누르면 실행하는 동안 나타날 배경이미지를 다운받아주었다. 이 또한 아래 첨부파일에 넣어놓겠다.

wallpaperswide.com/

 

WallpapersWide.com - Free 4K & 8K UltraHD Desktop Backgound Wallpapers for UHD TV, Ultra Widescreen Desktop, Tablet, Smartphone

 

wallpaperswide.com

 

그리고 곡 목록을 바꿀수 있는 left, right 버튼은 그림판으로 만들어 주었다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package beat;
 
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class DynamicBeat extends JFrame { // JFrame 라이브러리를 사용해준다.
 
    private Image screenImage;
    private Graphics screenGraphic;
    // 더블 버퍼링을 위해 이미지를 화면에 담는 인스턴스
 
    private ImageIcon exitButtonBasicImage = new ImageIcon(Main.class.getResource("../images/exitButtonBasic.png"));
    // 버튼의 이미지 아이콘 생성(기본이미지 아이콘)
    private ImageIcon exitButtonEnterImage = new ImageIcon(Main.class.getResource("../images/exitButtonEntered.png"));
    // 버튼의 이미지 아이콘 생성(Entered 아이콘)
    
    // intro 화면에서 START 버튼 이미지 아이콘
    private ImageIcon startButtonBasicImage = new ImageIcon(Main.class.getResource("../images/startButtonBasic.png"));
    private ImageIcon startButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/startButtonEntered.png"));
    // intro 화면에서 Quit 버튼 이미지 아이콘
    private ImageIcon quitButtonBasicImage = new ImageIcon(Main.class.getResource("../images/quitButtonBasic.png"));
    private ImageIcon quitButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/quitButtonEntered.png"));
    // main 화면에서 left 버튼 이미지 아이콘
    private ImageIcon leftButtonBasicImage = new ImageIcon(Main.class.getResource("../images/leftButtonBasic.png"));
    private ImageIcon leftButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/leftButtonEntered.png"));
    // main 화면에서 right 버튼 이미지 아이콘
    private ImageIcon rightButtonBasicImage = new ImageIcon(Main.class.getResource("../images/rightButtonBasic.png"));
    private ImageIcon rightButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/rightButtonEntered.png"));
    
    // 이미지 파일을 담을 수 있는 객체 생성
    private Image background = new ImageIcon(Main.class.getResource("../images/introBackground.jpg")).getImage();
    private Image titleImage = new ImageIcon(Main.class.getResource("../images/buddy_Title_Image.png")).getImage();
    private Image selectedImage = new ImageIcon(Main.class.getResource("../images/buddy_Start_Image.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 boolean isMainScreen = false;
    // 메인 화면으로 이동했을 경우 이 값을 true로 설정
 
    private int mouseX, mouseY;    // 메뉴바를 드래그 할 때 위치가 변경되도록 설정하기위해 마우스 좌표 변수 설정
    
    public DynamicBeat() {
        setUndecorated(true);
        // 실행했을 때 기본적으로 존재하는 메뉴바가 사라짐.
        setTitle("DynamicBeat");
        // 타이틀 설정
        setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
        // main에서 설정해준 가로세로 설정
        setResizable(false);
        // 한번 만들어진 게임창을 사용자가 줄이거나 늘릴수 없게함.
        setLocationRelativeTo(null);
        // 게임창이 컴퓨터의 정중장에 위치
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 게임창을 종료했을 때 프로그램 전체가 종료됨.
        setVisible(true);
        // 눈에 게임창이 보이게 함.
        setBackground(new Color(0000));    //paintComponents 했을 때 배경이 하얀색으로 바뀜.
        setLayout(null);    // Button이나 JLabel을 넣었을 때 위치 그대로 설정되게함.
        
        // 나가기 버튼 부분
        exitButton.setBounds(124503030);    // 버튼 위치 설정
        exitButton.setBorderPainted(false);        // 버튼 테두리 색상 없게 설정
        exitButton.setContentAreaFilled(false);    // 버튼 공간 색상 설정
        exitButton.setFocusPainted(false);        // 버튼에 포커스가 맞춰졌을 때 색상 설정
        exitButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {    // 마우스가 버튼위에 올라갔을 때
                exitButton.setIcon(exitButtonEnterImage);    // 해당 Enter이미지로 설정
                exitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));    // 손가락모양 마우스로 설정
                Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);    //한번만 음악이 실행되도록
                buttonEnteredMusic.start();
            }
            public void mouseExited(MouseEvent e) {    // 마우스가 다시 나왔을 때
                exitButton.setIcon(exitButtonBasicImage);    // 다시 Basic이미지로 설정
                exitButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));    // 기본 마우스
            }
            public void mousePressed(MouseEvent e) {    // 마우스를 눌렀을 경우
                Music buttonPressedMusic = new Music("buttonPressedMusic.mp3",false);
                buttonPressedMusic.start();
                try {
                    Thread.sleep(500);    // 버튼을 누르자마자 종료되면 음악X 일정시간 지난후 종료되도록 설정
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                System.exit(0);    // 게임종료
            }
        });
        add(exitButton);
        
        // START 버튼부분
        startButton.setBounds(40200400100);    // 버튼 위치 설정
        startButton.setBorderPainted(false);        // 버튼 테두리 색상 없게 설정
        startButton.setContentAreaFilled(false);    // 버튼 공간 색상 설정
        startButton.setFocusPainted(false);        // 버튼에 포커스가 맞춰졌을 때 색상 설정
        startButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {    // 마우스가 버튼위에 올라갔을 때
                startButton.setIcon(startButtonEnteredImage);    // 해당 Enter이미지로 설정
                startButton.setCursor(new Cursor(Cursor.HAND_CURSOR));    // 손가락모양 마우스로 설정
                Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);    //한번만 음악이 실행되도록
                buttonEnteredMusic.start();
            }
            public void mouseExited(MouseEvent e) {    // 마우스가 다시 나왔을 때
                startButton.setIcon(startButtonBasicImage);    // 다시 Basic이미지로 설정
                startButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));    // 기본 마우스
            }
            public void mousePressed(MouseEvent e) {    // 마우스를 눌렀을 경우
                Music buttonPressedMusic = new Music("buttonPressedMusic.mp3",false);
                buttonPressedMusic.start();
                startButton.setVisible(false);    // start 버튼 안보이도록 설정
                quitButton.setVisible(false);    // quit 버튼 안보이도록 설정
                leftButton.setVisible(true);    // left 버튼 나타나도록 설정
                rightButton.setVisible(true);    // right 버튼 나타나도록 설정
                background = new ImageIcon(Main.class.getResource("../images/mainBackground.jpg")).getImage();    // 배경이미지 체인지
                isMainScreen = true;
            }
        });
        add(startButton);
        
        // Quit 버튼 부분
        quitButton.setBounds(40330400100);    // 버튼 위치 설정
        quitButton.setBorderPainted(false);        // 버튼 테두리 색상 없게 설정
        quitButton.setContentAreaFilled(false);    // 버튼 공간 색상 설정
        quitButton.setFocusPainted(false);        // 버튼에 포커스가 맞춰졌을 때 색상 설정
        quitButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {    // 마우스가 버튼위에 올라갔을 때
                quitButton.setIcon(quitButtonEnteredImage);    // 해당 Enter이미지로 설정
                quitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));    // 손가락모양 마우스로 설정
                Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);    //한번만 음악이 실행되도록
                buttonEnteredMusic.start();
            }
            public void mouseExited(MouseEvent e) {    // 마우스가 다시 나왔을 때
                quitButton.setIcon(quitButtonBasicImage);    // 다시 Basic이미지로 설정
                quitButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));    // 기본 마우스
            }
            public void mousePressed(MouseEvent e) {    // 마우스를 눌렀을 경우
                Music buttonPressedMusic = new Music("buttonPressedMusic.mp3",false);
                buttonPressedMusic.start();
                try {
                    Thread.sleep(500);    // 버튼을 누르자마자 종료되면 음악X 일정시간 지난후 종료되도록 설정
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                System.exit(0);    // 게임종료
            }
        });
        add(quitButton);
        
        // left 버튼 부분
        leftButton.setVisible(false);
        leftButton.setBounds(1403106060);    // 버튼 위치 설정
        leftButton.setBorderPainted(false);        // 버튼 테두리 색상 없게 설정
        leftButton.setContentAreaFilled(false);    // 버튼 공간 색상 설정
        leftButton.setFocusPainted(false);        // 버튼에 포커스가 맞춰졌을 때 색상 설정
        leftButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {    // 마우스가 버튼위에 올라갔을 때
                leftButton.setIcon(leftButtonEnteredImage);    // 해당 Enter이미지로 설정
                leftButton.setCursor(new Cursor(Cursor.HAND_CURSOR));    // 손가락모양 마우스로 설정
                Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);    //한번만 음악이 실행되도록
                buttonEnteredMusic.start();
            }
            public void mouseExited(MouseEvent e) {    // 마우스가 다시 나왔을 때
                leftButton.setIcon(leftButtonBasicImage);    // 다시 Basic이미지로 설정
                leftButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));    // 기본 마우스
            }
            public void mousePressed(MouseEvent e) {    // 마우스를 눌렀을 경우
                Music buttonPressedMusic = new Music("buttonPressedMusic.mp3",false);
                buttonPressedMusic.start();
                // left버튼 이벤트
            }
        });
        add(leftButton);
        
        // right 버튼 부분
        rightButton.setVisible(false);
        rightButton.setBounds(10803306060);    // 버튼 위치 설정
        rightButton.setBorderPainted(false);        // 버튼 테두리 색상 없게 설정
        rightButton.setContentAreaFilled(false);    // 버튼 공간 색상 설정
        rightButton.setFocusPainted(false);        // 버튼에 포커스가 맞춰졌을 때 색상 설정
        rightButton.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {    // 마우스가 버튼위에 올라갔을 때
                rightButton.setIcon(rightButtonEnteredImage);    // 해당 Enter이미지로 설정
                rightButton.setCursor(new Cursor(Cursor.HAND_CURSOR));    // 손가락모양 마우스로 설정
                Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3",false);    //한번만 음악이 실행되도록
                buttonEnteredMusic.start();
            }
            public void mouseExited(MouseEvent e) {    // 마우스가 다시 나왔을 때
                rightButton.setIcon(rightButtonBasicImage);    // 다시 Basic이미지로 설정
                rightButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));    // 기본 마우스
            }
            public void mousePressed(MouseEvent e) {    // 마우스를 눌렀을 경우
                Music buttonPressedMusic = new Music("buttonPressedMusic.mp3",false);
                buttonPressedMusic.start();
                // right 버튼 이벤트
            }
        });
        add(rightButton);
 
        menuBar.setBounds(00128030);    // 메뉴바의 위치 설정
        menuBar.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) { // 마우스 이벤트가 발생했을 때 설정
                mouseX = e.getX();
                mouseY = e.getY();
            }
        });
        menuBar.addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {    // 드래그 이벤트 발생했을때 설정
                int x = e.getXOnScreen();    // 스크린의 x좌표를 가져오고
                int y = e.getYOnScreen();    // 스크린의 y좌표를 가져온다.
                setLocation(x - mouseX, y - mouseY);    // JFrame의 위치 자체를 x와 y로 바꿔준다.
            }
        });    // 이렇게 하면 드래그 할 때마다 순간순간의 좌표값을 가져와서 창의 위치를 바꿔준다.
        add(menuBar);    // JFrame에 메뉴바가 추가됨.
 
        Music introMusic = new Music("introMusic.mp3"true);
        // 시작화면에서 음악이 무한반복하게 해줌. true값을 넣어줘서 직접종료시키기 전에는 계속 반복하게된다.
        introMusic.start();
        // 게임의 실행과 동시에 게임이 종료
    }
 
    public void paint(Graphics g) { // 프로그램이 실행되는 순간 가장 첫번째로 화면에 그려지는 약속된 메서드
        screenImage = createImage(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT); // 크기만큼의 이미지를 만들고 변수에 넣어준다.
        screenGraphic = screenImage.getGraphics(); // 그래픽객체를 가져옴.
        screenDraw(screenGraphic);
        g.drawImage(screenImage, 00null); // 스크린 이미지가 화면 창에 그려짐
    }
 
    public void screenDraw(Graphics g) {
        g.drawImage(background, 00null); // 전체이미지에 그려질 수 있도록한다.(동적이미지)
        if(isMainScreen) {
            g.drawImage(selectedImage, 340100null); // 전체이미지에 그려질 수 있도록한다.(동적이미지)
            g.drawImage(titleImage, 340500null);    // 곡의 타이틀 이미지 생성.(동적이미지)
        }
        paintComponents(g); // 설정해준 이미지 이외의 그림을 그릴 수 있게 해줌. 항상존재하고 역동적이지 않기 때문에 이걸로 지정해준다.
        this.repaint();
        // 페인트함수를 다시 불러와 다시 화면에 그려줌. 프로그램이 실행되는 매 순간마다 프로그램이 종료되는 순간까지 반복됨.
 
    }
}
cs

 

 

bensound-buddy.mp3
1.63MB
bensound-clearday.mp3
1.20MB
bensound-creativeminds.mp3
2.81MB
mainBackground.jpg
0.21MB
quitButtonBasic.png
0.00MB
quitButtonEntered.png
0.00MB
startButtonBasic.png
0.00MB
startButtonEntered.png
0.00MB
leftButtonBasic.png
0.00MB
leftButtonEntered.png
0.00MB
rightButtonBasic.png
0.00MB
rightButtonEntered.png
0.00MB
buddy_Game_Image.jpg
0.12MB
buddy_Start_Image.jpg
0.02MB
clearday_Game_Image.jpg
0.15MB
clearday_Start_Image.jpg
0.03MB
creativeminds_Game_Image.jpg
0.44MB
creativeminds_Start_Image.jpg
0.08MB
leftButtonBasic.png
0.00MB
leftButtonEntered.png
0.00MB
buddy_Title_Image.jpg.png
0.01MB
clearday_Title_Image.jpg.png
0.01MB
creativminds_Title_Image.jpg.png
0.02MB

반응형

댓글