-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
101 lines (81 loc) · 2.59 KB
/
Main.java
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
package game;
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.swing.*;
import sun.audio.*;
public class Main extends JFrame implements WindowListener
{
private static int DEFAULT_FPS = 80;
private GamePanel gp;
private int pWidth, pHeight;
public Main(long period)
{
super("Game");
pack();
setResizable(false);
calcSizes();
setResizable(true);
Container c = getContentPane();
gp = new GamePanel(period, pWidth, pHeight);
c.add(gp, "Center");
pack();
addWindowListener(this);
addComponentListener(new ComponentAdapter()
{
public void componentMoved(ComponentEvent e)
{
setLocation(0, 0);
}
});
setResizable(false);
setVisible(true);
}
private void calcSizes()
{
GraphicsConfiguration gc = getGraphicsConfiguration();
Rectangle screenRect = gc.getBounds();
Toolkit tk = Toolkit.getDefaultToolkit();
Insets desktopInsets = tk.getScreenInsets(gc);
Insets frameInsets = getInsets();
pWidth = screenRect.width - (desktopInsets.left + desktopInsets.right) - (frameInsets.left + frameInsets.right)+12;
pHeight = screenRect.height - (desktopInsets.top + desktopInsets.bottom) - (frameInsets.top + frameInsets.bottom)+12;
}
@SuppressWarnings("restriction")
public static void music()
{
AudioPlayer MGP = AudioPlayer.player;
AudioStream BGM;
AudioData MD;
ContinuousAudioDataStream loop = null;
try
{
InputStream test = new FileInputStream("music.wav");
BGM = new AudioStream(test);
AudioPlayer.player.start(BGM);
//MD = BGM.getData();
//loop = new ContinuousAudioDataStream(MD);
}
catch(Exception e){
System.out.print(e);
}
MGP.start(loop);
}
// ----------------------------------------- main ----------------------------------------- //
public static void main(String args[])
{
music();
int fps = DEFAULT_FPS;
long period = (long) 1000.0 / fps;
new Main(period * 1000000L); // convert ms to nanosecs
}
// -------------------------------- WindowListener methods -------------------------------- //
public void windowActivated(WindowEvent arg0) { gp.resumeGame(); }
public void windowDeiconified(WindowEvent arg0) { gp.resumeGame(); }
public void windowDeactivated(WindowEvent arg0) { gp.pauseGame(); }
public void windowIconified(WindowEvent arg0) { gp.pauseGame(); }
public void windowClosing(WindowEvent arg0) { gp.stopGame(); }
public void windowOpened(WindowEvent arg0) {}
public void windowClosed(WindowEvent arg0) {}
}