• R/O
  • SSH

Commit

Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

Commit MetaInfo

Revision08fff8cb70271536143e2260729c04896c742753 (tree)
Zeit2015-07-02 07:24:58
AutorMirrgieRiana
CommiterMirrgieRiana

Log Message

add mirrg.applet.nitrogen

Ändern Zusammenfassung

Diff

diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/.classpath
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/.classpath Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,7 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<classpath>
3+ <classpathentry kind="src" path="src"/>
4+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+ <classpathentry combineaccessrules="false" kind="src" path="/mirrg.event.nitrogen"/>
6+ <classpathentry kind="output" path="bin"/>
7+</classpath>
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/.project
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/.project Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,17 @@
1+<?xml version="1.0" encoding="UTF-8"?>
2+<projectDescription>
3+ <name>mirrg.applet.nitrogen</name>
4+ <comment></comment>
5+ <projects>
6+ </projects>
7+ <buildSpec>
8+ <buildCommand>
9+ <name>org.eclipse.jdt.core.javabuilder</name>
10+ <arguments>
11+ </arguments>
12+ </buildCommand>
13+ </buildSpec>
14+ <natures>
15+ <nature>org.eclipse.jdt.core.javanature</nature>
16+ </natures>
17+</projectDescription>
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/AppletNitrogen.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/AppletNitrogen.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,118 @@
1+package mirrg.applet.nitrogen;
2+
3+import java.awt.Graphics;
4+
5+import javax.swing.JApplet;
6+
7+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
8+import mirrg.applet.nitrogen.events.NitrogenEventComponent;
9+import mirrg.event.nitrogen.HNitrogenEvent;
10+import mirrg.event.nitrogen.INitrogenEventManager;
11+
12+/**
13+ * コンストラクタ内やインスタンスの生成時で以下のことようなをすると、
14+ * イベントを受け取れる。
15+ *
16+ * <pre>
17+ * getEventManager().register(NitrogenEventApplet.Init.class, event -&gt; {
18+ * System.out.println(&quot;on init&quot;);
19+ * });
20+ * </pre>
21+ */
22+public class AppletNitrogen extends JApplet
23+{
24+
25+ private static final long serialVersionUID = 155419117197430297L;
26+
27+ public AppletNitrogen()
28+ {
29+ eventManager = HNitrogenEvent.createInstance();
30+
31+ eventManager.register(NitrogenEventApplet.Start.class, event -> {
32+ eventManager.post(new NitrogenEventApplet.Rebuffer(
33+ this,
34+ event.applet.getWidth(),
35+ event.applet.getHeight()));
36+ });
37+ eventManager.register(NitrogenEventComponent.Resized.class, event -> {
38+ eventManager.post(new NitrogenEventApplet.Rebuffer(
39+ this,
40+ event.componentEvent.getComponent().getWidth(),
41+ event.componentEvent.getComponent().getHeight()));
42+ });
43+ }
44+
45+ //
46+
47+ private INitrogenEventManager eventManager;
48+
49+ public INitrogenEventManager getEventManager()
50+ {
51+ return eventManager;
52+ }
53+
54+ //
55+
56+ @Override
57+ public final void init()
58+ {
59+ getEventManager().post(new NitrogenEventApplet.Init(this));
60+ }
61+
62+ @Override
63+ public final void start()
64+ {
65+ isRunning = true;
66+ getEventManager().post(new NitrogenEventApplet.Start(this));
67+ }
68+
69+ @Override
70+ public final void stop()
71+ {
72+ isRunning = false;
73+ getEventManager().post(new NitrogenEventApplet.Stop(this));
74+ }
75+
76+ @Override
77+ public final void destroy()
78+ {
79+ getEventManager().post(new NitrogenEventApplet.Destroy(this));
80+ }
81+
82+ //
83+
84+ private boolean isRunning = false;
85+
86+ @Override
87+ public final void paint(Graphics graphics)
88+ {
89+ if (!isRunning) {
90+ super.paint(graphics);
91+ return;
92+ }
93+
94+ NitrogenEventApplet.Paint.Pre event = new NitrogenEventApplet.Paint.Pre(this);
95+ getEventManager().post(event);
96+
97+ if (!event.cancelled) super.paint(graphics);
98+
99+ getEventManager().post(new NitrogenEventApplet.Paint(this, graphics));
100+ }
101+
102+ @Override
103+ public final void update(Graphics graphics)
104+ {
105+ if (!isRunning) {
106+ super.update(graphics);
107+ return;
108+ }
109+
110+ NitrogenEventApplet.Update.Pre event = new NitrogenEventApplet.Update.Pre(this);
111+ getEventManager().post(event);
112+
113+ if (!event.cancelled) super.update(graphics);
114+
115+ getEventManager().post(new NitrogenEventApplet.Update(this, graphics));
116+ }
117+
118+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/BackBuffer.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/BackBuffer.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,104 @@
1+package mirrg.applet.nitrogen;
2+
3+import java.awt.Graphics2D;
4+import java.awt.image.BufferedImage;
5+
6+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
7+import mirrg.event.nitrogen.INitrogenEventManager;
8+
9+/**
10+ * サイズ可変イメージバッファ
11+ */
12+public class BackBuffer implements IBuffer
13+{
14+
15+ private int imageType;
16+ private BufferedImage buffer;
17+ private Graphics2D graphics;
18+
19+ public BackBuffer(int imageType)
20+ {
21+ this.imageType = imageType;
22+ }
23+
24+ public synchronized void rebuffer(int width, int height)
25+ {
26+ if (width <= 0) width = 1;
27+ if (height <= 0) height = 1;
28+
29+ @SuppressWarnings("hiding")
30+ BufferedImage buffer = new BufferedImage(width, height, imageType);
31+ @SuppressWarnings("hiding")
32+ Graphics2D graphics = buffer.createGraphics();
33+
34+ if (this.buffer != null) {
35+ graphics.drawImage(this.buffer, 0, 0, null);
36+ }
37+
38+ set(buffer, graphics);
39+ }
40+
41+ public synchronized void set(BufferedImage bufferSafety, Graphics2D graphicsSafety)
42+ {
43+ this.buffer = bufferSafety;
44+ this.graphics = graphicsSafety;
45+ }
46+
47+ /**
48+ * 読み取り用のバッファ。
49+ */
50+ @Override
51+ public synchronized BufferedImage getBuffer()
52+ {
53+ return buffer;
54+ }
55+
56+ @Override
57+ public synchronized Graphics2D getGraphics()
58+ {
59+ return graphics;
60+ }
61+
62+ public synchronized int getWidth()
63+ {
64+ return buffer.getWidth();
65+ }
66+
67+ public synchronized int getHeight()
68+ {
69+ return buffer.getHeight();
70+ }
71+
72+ /**
73+ * バッファを空にする。
74+ */
75+ public void clear()
76+ {
77+ getGraphics().clearRect(0, 0, getWidth(), getHeight());
78+ }
79+
80+ public synchronized void flip(BackBuffer other)
81+ {
82+ {
83+ BufferedImage tmp = this.buffer;
84+ this.buffer = other.buffer;
85+ other.buffer = tmp;
86+ }
87+ {
88+ Graphics2D tmp = this.graphics;
89+ this.graphics = other.graphics;
90+ other.graphics = tmp;
91+ }
92+ }
93+
94+ /**
95+ * @see NitrogenEventApplet.Rebuffer
96+ */
97+ public void subscribeEvent(INitrogenEventManager eventManager)
98+ {
99+ eventManager.register(NitrogenEventApplet.Rebuffer.class, event -> {
100+ rebuffer(event.width, event.height);
101+ });
102+ }
103+
104+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/FPSAdjuster.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/FPSAdjuster.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,111 @@
1+package mirrg.applet.nitrogen;
2+
3+public class FPSAdjuster
4+{
5+
6+ private long previousTimeMs = 0;
7+ private double smoothedLoadFactor = 0;
8+ private double smoothedFps = 0;
9+ private GameThread gameThread;
10+
11+ /** スムージング定数 */
12+ private final static double K = 0.10;
13+
14+ public FPSAdjuster(GameThread gameThread)
15+ {
16+ this.gameThread = gameThread;
17+ }
18+
19+ private final void sleepForNextTickFirst()
20+ {
21+ smoothedFps = getObjectiveFps();
22+ smoothedLoadFactor = 1.0;
23+ previousTimeMs = System.currentTimeMillis();
24+ }
25+
26+ public void waitForNextTick() throws InterruptedException
27+ {
28+ // 初回呼出しである場合、別のルーチンを実行
29+ if (previousTimeMs == 0) {
30+ sleepForNextTickFirst();
31+ return;
32+ }
33+
34+ // 処理 Sleep
35+ // PTM ms
36+ // `-----------'
37+ // PLTM |
38+ // `-----------|----------'
39+ // dms
40+ // |
41+
42+ // 現在の時刻
43+ long ms = System.currentTimeMillis();
44+
45+ // 直前のSleep終了から現在までの長さ
46+ long previousLoopTimeMs = ms - previousTimeMs;
47+
48+ // 適正1ループ時間
49+ long dms = (long) (1000 / getObjectiveFps());
50+
51+ {
52+
53+ // ######################### Wait ########################
54+ // Sleepするべき時間ぶんだけSleep
55+ if (dms - previousLoopTimeMs - 2 >= 0) {
56+ Thread.sleep(dms - previousLoopTimeMs - 2);
57+ }
58+
59+ // 微調整
60+ while (System.currentTimeMillis() < previousTimeMs + dms)
61+ ;
62+ // #######################################################
63+
64+ }
65+
66+ // 処理 Sleep
67+ // PTM ms ms2
68+ // `-----------' |
69+ // PLTM |
70+ // `----------------------'
71+ // dms |
72+ // |
73+
74+ // 現在の時刻
75+ long ms2 = System.currentTimeMillis();
76+
77+ // 直前の計測からのFPS
78+ double fps = (ms2 - previousTimeMs == 0) ? 1000 : (1000.0 / (ms2 - previousTimeMs));
79+
80+ // FPSを滑らかに反映
81+ smoothedFps = (1 - K) * smoothedFps + K * fps;
82+
83+ // 負荷率
84+ double loadFactor = (double) previousLoopTimeMs / dms;
85+
86+ // 負荷率を反映
87+ smoothedLoadFactor = (1 - K) * smoothedLoadFactor + K * loadFactor;
88+
89+ previousTimeMs = ms;
90+ }
91+
92+ public double getLoadFactor()
93+ {
94+ if (smoothedLoadFactor == 0) return 1.0;
95+
96+ return smoothedLoadFactor;
97+ }
98+
99+ public double getFPS()
100+ {
101+ if (smoothedFps == 0) return getObjectiveFps();
102+
103+ return smoothedFps;
104+ }
105+
106+ public double getObjectiveFps()
107+ {
108+ return gameThread.objectiveFps;
109+ }
110+
111+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/GameThread.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/GameThread.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,8 @@
1+package mirrg.applet.nitrogen;
2+
3+public class GameThread
4+{
5+
6+ public double objectiveFps = 50;
7+
8+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/HAppletNitrogen.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/HAppletNitrogen.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,192 @@
1+package mirrg.applet.nitrogen;
2+
3+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
4+import mirrg.applet.nitrogen.events.NitrogenEventGameThread;
5+import mirrg.applet.nitrogen.events.NitrogenEventKey;
6+import mirrg.applet.nitrogen.events.NitrogenEventMouse;
7+import mirrg.applet.nitrogen.events.NitrogenEventMouseMotion;
8+import mirrg.applet.nitrogen.events.NitrogenEventMouseWheel;
9+
10+public class HAppletNitrogen
11+{
12+
13+ public static ResponceApplyStandard applyStandard(AppletNitrogen appletNitrogen)
14+ {
15+ appletNitrogen.setFocusable(true);
16+
17+ HAppletNitrogenEvent.adaptMouseEvent(appletNitrogen);
18+ HAppletNitrogenEvent.adaptMouseMotionEvent(appletNitrogen);
19+ HAppletNitrogenEvent.adaptMouseWheelEvent(appletNitrogen);
20+ HAppletNitrogenEvent.adaptKeyEvent(appletNitrogen);
21+ HAppletNitrogenEvent.adaptComponentEvent(appletNitrogen);
22+
23+ ResponceApplyStandard responce = new ResponceApplyStandard();
24+
25+ responce.gameThread = HAppletNitrogen.applyGameThread(appletNitrogen);
26+ responce.fpsAdjuster = HAppletNitrogen.applyFPS(appletNitrogen, responce.gameThread);
27+ responce.tripleBuffer = HAppletNitrogen.applyTripleBuffer(appletNitrogen);
28+ responce.inputStatus = HAppletNitrogen.applyInputStatus(appletNitrogen);
29+
30+ return responce;
31+ }
32+
33+ public static class ResponceApplyStandard
34+ {
35+
36+ public GameThread gameThread;
37+ public FPSAdjuster fpsAdjuster;
38+ public TripleBuffer tripleBuffer;
39+ public InputStatus inputStatus;
40+
41+ }
42+
43+ /**
44+ * @see NitrogenEventGameThread
45+ */
46+ public static GameThread applyGameThread(AppletNitrogen appletNitrogen)
47+ {
48+ GameThread gameThread = new GameThread();
49+ Thread[] thread = {
50+ null,
51+ };
52+
53+ appletNitrogen.getEventManager().register(NitrogenEventApplet.Start.class, event -> {
54+
55+ thread[0] = new Thread(() -> {
56+
57+ while (true) {
58+
59+ {
60+ NitrogenEventGameThread.Sleep event2 =
61+ new NitrogenEventGameThread.Sleep(gameThread, thread[0]);
62+ appletNitrogen.getEventManager().post(event2);
63+
64+ if (!event2.cancelled) {
65+ try {
66+ Thread.sleep((long) (1000 / gameThread.objectiveFps));
67+ } catch (InterruptedException e) {
68+ break;
69+ }
70+ }
71+ if (event2.interrupted) break;
72+ }
73+
74+ appletNitrogen.getEventManager().post(
75+ new NitrogenEventGameThread.Tick(gameThread, thread[0]));
76+
77+ appletNitrogen.getEventManager().post(
78+ new NitrogenEventGameThread.PostTick(gameThread, thread[0]));
79+
80+ appletNitrogen.getEventManager().post(
81+ new NitrogenEventGameThread.Render(gameThread, thread[0]));
82+
83+ }
84+
85+ });
86+ thread[0].setDaemon(true);
87+
88+ appletNitrogen.getEventManager().post(
89+ new NitrogenEventGameThread.Init(gameThread, thread[0]));
90+
91+ thread[0].start();
92+ });
93+ appletNitrogen.getEventManager().register(NitrogenEventApplet.Stop.class, event -> {
94+ thread[0].interrupt();
95+ });
96+
97+ return gameThread;
98+ }
99+
100+ /**
101+ * 画面の更新レートと描画の処理レートを違いの吸収のために2枚のバッファを追加します。
102+ * <ul>
103+ * <li>本来用意されている出力バッファ
104+ * <li>任意のタイミングで更新および読み取りが可能なバッファ(safety)
105+ * <li>任意のタイミングで初期化及び書き換えが可能で、任意のタイミングでない読み取りが可能なバッファ(dirty)
106+ * </ul>
107+ * この機能は次の機能を前提としています。
108+ * <ul>
109+ * <li>{@link HAppletNitrogenEvent#adaptComponentEvent(AppletNitrogen)}
110+ * <li>{@link HAppletNitrogen#applyGameThread(AppletNitrogen)}
111+ * </ul>
112+ */
113+ public static TripleBuffer applyTripleBuffer(AppletNitrogen appletNitrogen)
114+ {
115+ TripleBuffer tripleBuffer = new TripleBuffer();
116+
117+ appletNitrogen.getEventManager().register(NitrogenEventApplet.Paint.Pre.class, event -> {
118+ event.cancelled = true;
119+ });
120+ tripleBuffer.subscribeEvent(appletNitrogen.getEventManager());
121+
122+ return tripleBuffer;
123+ }
124+
125+ /**
126+ * この機能は次の機能を前提としています。
127+ * <ul>
128+ * <li>{@link HAppletNitrogenEvent#adaptMouseEvent(AppletNitrogen)}
129+ * <li>{@link HAppletNitrogenEvent#adaptMouseMotionEvent(AppletNitrogen)}
130+ * <li>{@link HAppletNitrogenEvent#adaptMouseWheelEvent(AppletNitrogen)}
131+ * <li>{@link HAppletNitrogenEvent#adaptKeyEvent(AppletNitrogen)}
132+ * </ul>
133+ */
134+ public static InputStatus applyInputStatus(AppletNitrogen appletNitrogen)
135+ {
136+ InputStatus inputStatus = new InputStatus();
137+
138+ appletNitrogen.getEventManager().register(NitrogenEventMouse.Pressed.class, event -> {
139+ inputStatus.mousePressed(
140+ event.mouseEvent.getX(),
141+ event.mouseEvent.getY(),
142+ event.mouseEvent.getButton());
143+ });
144+ appletNitrogen.getEventManager().register(NitrogenEventMouse.Released.class, event -> {
145+ inputStatus.mouseReleased(
146+ event.mouseEvent.getX(),
147+ event.mouseEvent.getY(),
148+ event.mouseEvent.getButton());
149+ });
150+ appletNitrogen.getEventManager().register(NitrogenEventMouseMotion.class, event -> {
151+ inputStatus.mouseMotion(
152+ event.mouseEvent.getX(),
153+ event.mouseEvent.getY());
154+ });
155+ appletNitrogen.getEventManager().register(NitrogenEventMouseWheel.Moved.class, event -> {
156+ inputStatus.mouseWheelMoved(event.mouseWheelEvent.getWheelRotation());
157+ });
158+ appletNitrogen.getEventManager().register(NitrogenEventKey.Pressed.class, event -> {
159+ inputStatus.keyPressed(event.keyEvent.getKeyCode());
160+ });
161+ appletNitrogen.getEventManager().register(NitrogenEventKey.Released.class, event -> {
162+ inputStatus.keyReleased(event.keyEvent.getKeyCode());
163+ });
164+
165+ return inputStatus;
166+ }
167+
168+ /**
169+ * この機能は次の機能を前提としています。
170+ * <ul>
171+ * <li>{@link HAppletNitrogen#applyGameThread(AppletNitrogen)}
172+ * </ul>
173+ */
174+ public static FPSAdjuster applyFPS(AppletNitrogen appletNitrogen, GameThread gameThread)
175+ {
176+ FPSAdjuster fpsAdjuster = new FPSAdjuster(gameThread);
177+
178+ appletNitrogen.getEventManager().register(NitrogenEventGameThread.Sleep.class, event -> {
179+ event.cancelled = true;
180+
181+ try {
182+ fpsAdjuster.waitForNextTick();
183+ } catch (Exception e) {
184+ event.interrupted = true;
185+ }
186+
187+ });
188+
189+ return fpsAdjuster;
190+ }
191+
192+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/HAppletNitrogenEvent.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/HAppletNitrogenEvent.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,190 @@
1+package mirrg.applet.nitrogen;
2+
3+import java.awt.event.ComponentEvent;
4+import java.awt.event.ComponentListener;
5+import java.awt.event.KeyEvent;
6+import java.awt.event.KeyListener;
7+import java.awt.event.MouseEvent;
8+import java.awt.event.MouseListener;
9+import java.awt.event.MouseMotionListener;
10+import java.awt.event.MouseWheelEvent;
11+import java.awt.event.MouseWheelListener;
12+
13+import javax.swing.JApplet;
14+
15+import mirrg.applet.nitrogen.events.NitrogenEventComponent;
16+import mirrg.applet.nitrogen.events.NitrogenEventKey;
17+import mirrg.applet.nitrogen.events.NitrogenEventMouse;
18+import mirrg.applet.nitrogen.events.NitrogenEventMouseMotion;
19+import mirrg.applet.nitrogen.events.NitrogenEventMouseWheel;
20+
21+public class HAppletNitrogenEvent
22+{
23+
24+ /**
25+ * 発生したAWTEventをNitrogenEventに変換してpostする。
26+ *
27+ * @see NitrogenEventMouse
28+ */
29+ public static void adaptMouseEvent(AppletNitrogen appletNitrogen)
30+ {
31+ appletNitrogen.addMouseListener(new MouseListener() {
32+
33+ @Override
34+ public void mouseReleased(MouseEvent mouseEvent)
35+ {
36+ appletNitrogen.getEventManager().post(
37+ new NitrogenEventMouse.Released(mouseEvent));
38+ }
39+
40+ @Override
41+ public void mousePressed(MouseEvent mouseEvent)
42+ {
43+ appletNitrogen.getEventManager().post(
44+ new NitrogenEventMouse.Pressed(mouseEvent));
45+ }
46+
47+ @Override
48+ public void mouseExited(MouseEvent mouseEvent)
49+ {
50+ appletNitrogen.getEventManager().post(
51+ new NitrogenEventMouse.Exited(mouseEvent));
52+ }
53+
54+ @Override
55+ public void mouseEntered(MouseEvent mouseEvent)
56+ {
57+ appletNitrogen.getEventManager().post(
58+ new NitrogenEventMouse.Entered(mouseEvent));
59+ }
60+
61+ @Override
62+ public void mouseClicked(MouseEvent mouseEvent)
63+ {
64+ appletNitrogen.getEventManager().post(
65+ new NitrogenEventMouse.Clicked(mouseEvent));
66+ }
67+
68+ });
69+ }
70+
71+ /**
72+ * 発生したAWTEventをNitrogenEventに変換してpostする。
73+ *
74+ * @see NitrogenEventMouseMotion
75+ */
76+ public static void adaptMouseMotionEvent(AppletNitrogen appletNitrogen)
77+ {
78+ appletNitrogen.addMouseMotionListener(new MouseMotionListener() {
79+
80+ @Override
81+ public void mouseDragged(MouseEvent mouseEvent)
82+ {
83+ appletNitrogen.getEventManager().post(
84+ new NitrogenEventMouseMotion.Dragged(mouseEvent));
85+ }
86+
87+ @Override
88+ public void mouseMoved(MouseEvent mouseEvent)
89+ {
90+ appletNitrogen.getEventManager().post(
91+ new NitrogenEventMouseMotion.Moved(mouseEvent));
92+ }
93+
94+ });
95+ }
96+
97+ /**
98+ * 発生したAWTEventをNitrogenEventに変換してpostする。
99+ *
100+ * @see NitrogenEventMouseWheel
101+ */
102+ public static void adaptMouseWheelEvent(AppletNitrogen appletNitrogen)
103+ {
104+ appletNitrogen.addMouseWheelListener(new MouseWheelListener() {
105+
106+ @Override
107+ public void mouseWheelMoved(MouseWheelEvent mouseWheelEvent)
108+ {
109+ appletNitrogen.getEventManager().post(
110+ new NitrogenEventMouseWheel.Moved(mouseWheelEvent));
111+ }
112+
113+ });
114+ }
115+
116+ /**
117+ * 発生したAWTEventをNitrogenEventに変換してpostする。
118+ * 実際にキー入力を有効にするには、別途{@link JApplet#setFocusable(boolean)}をtrueにしなければならない。
119+ *
120+ * @see NitrogenEventKey
121+ */
122+ public static void adaptKeyEvent(AppletNitrogen appletNitrogen)
123+ {
124+ appletNitrogen.addKeyListener(new KeyListener() {
125+
126+ @Override
127+ public void keyTyped(KeyEvent keyEvent)
128+ {
129+ appletNitrogen.getEventManager().post(
130+ new NitrogenEventKey.Typed(keyEvent));
131+ }
132+
133+ @Override
134+ public void keyPressed(KeyEvent keyEvent)
135+ {
136+ appletNitrogen.getEventManager().post(
137+ new NitrogenEventKey.Pressed(keyEvent));
138+ }
139+
140+ @Override
141+ public void keyReleased(KeyEvent keyEvent)
142+ {
143+ appletNitrogen.getEventManager().post(
144+ new NitrogenEventKey.Released(keyEvent));
145+ }
146+
147+ });
148+ }
149+
150+ /**
151+ * 発生したAWTEventをNitrogenEventに変換してpostする。
152+ *
153+ * @see NitrogenEventComponent
154+ */
155+ public static void adaptComponentEvent(AppletNitrogen appletNitrogen)
156+ {
157+ appletNitrogen.addComponentListener(new ComponentListener() {
158+
159+ @Override
160+ public void componentShown(ComponentEvent componentEvent)
161+ {
162+ appletNitrogen.getEventManager().post(
163+ new NitrogenEventComponent.Shown(componentEvent));
164+ }
165+
166+ @Override
167+ public void componentResized(ComponentEvent componentEvent)
168+ {
169+ appletNitrogen.getEventManager().post(
170+ new NitrogenEventComponent.Resized(componentEvent));
171+ }
172+
173+ @Override
174+ public void componentMoved(ComponentEvent componentEvent)
175+ {
176+ appletNitrogen.getEventManager().post(
177+ new NitrogenEventComponent.Moved(componentEvent));
178+ }
179+
180+ @Override
181+ public void componentHidden(ComponentEvent componentEvent)
182+ {
183+ appletNitrogen.getEventManager().post(
184+ new NitrogenEventComponent.Hidden(componentEvent));
185+ }
186+
187+ });
188+ }
189+
190+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/IBuffer.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/IBuffer.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,13 @@
1+package mirrg.applet.nitrogen;
2+
3+import java.awt.Graphics2D;
4+import java.awt.image.BufferedImage;
5+
6+public interface IBuffer
7+{
8+
9+ public BufferedImage getBuffer();
10+
11+ public Graphics2D getGraphics();
12+
13+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/InputStatus.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/InputStatus.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,141 @@
1+package mirrg.applet.nitrogen;
2+
3+public class InputStatus
4+{
5+
6+ private static final int MOUSE_BUTTONS_COUNT = 8;
7+ private static final int KEYS_COUNT = 1024;
8+
9+ private int mouseX, mouseXPrevious;
10+ private int mouseY, mouseYPrevious;
11+ private int mouseW, mouseWPrevious;
12+
13+ private Button mouseButtons = new Button(MOUSE_BUTTONS_COUNT);
14+ private Button keyBoard = new Button(KEYS_COUNT);
15+
16+ public int getMouseX()
17+ {
18+ return mouseX;
19+ }
20+
21+ public int getMouseXPrevious()
22+ {
23+ return mouseXPrevious;
24+ }
25+
26+ public int getMouseY()
27+ {
28+ return mouseY;
29+ }
30+
31+ public int getMouseYPrevious()
32+ {
33+ return mouseYPrevious;
34+ }
35+
36+ public int getMouseW()
37+ {
38+ return mouseW;
39+ }
40+
41+ public int getMouseWPrevious()
42+ {
43+ return mouseWPrevious;
44+ }
45+
46+ public Button getMouseButtons()
47+ {
48+ return mouseButtons;
49+ }
50+
51+ public Button getKeyBoard()
52+ {
53+ return keyBoard;
54+ }
55+
56+ public void spend()
57+ {
58+ mouseXPrevious = mouseX;
59+ mouseYPrevious = mouseY;
60+ mouseWPrevious = mouseW;
61+
62+ mouseButtons.spend();
63+ keyBoard.spend();
64+ }
65+
66+ public void mousePressed(int x, int y, int button)
67+ {
68+ mouseX = x;
69+ mouseY = y;
70+ mouseButtons.pressed(button);
71+ }
72+
73+ public void mouseReleased(int x, int y, int button)
74+ {
75+ mouseX = x;
76+ mouseY = y;
77+ mouseButtons.released(button);
78+ }
79+
80+ public void mouseMotion(int x, int y)
81+ {
82+ mouseX = x;
83+ mouseY = y;
84+ }
85+
86+ public void mouseWheelMoved(int wheelRotation)
87+ {
88+ mouseW += wheelRotation;
89+ }
90+
91+ public void keyPressed(int keyCode)
92+ {
93+ keyBoard.pressed(keyCode);
94+ }
95+
96+ public void keyReleased(int keyCode)
97+ {
98+ keyBoard.released(keyCode);
99+ }
100+
101+ public static class Button
102+ {
103+
104+ private int[] buttons;
105+
106+ public Button(int buttonsCount)
107+ {
108+ buttons = new int[buttonsCount];
109+ }
110+
111+ public void pressed(int index)
112+ {
113+ if (buttons[index] <= 0) buttons[index] = 1;
114+ }
115+
116+ public void released(int index)
117+ {
118+ if (buttons[index] >= 0) buttons[index] = -1;
119+ }
120+
121+ public int getState(int index)
122+ {
123+ return buttons[index];
124+ }
125+
126+ public int getCount()
127+ {
128+ return buttons.length;
129+ }
130+
131+ public void spend()
132+ {
133+ for (int i = 0; i < buttons.length; i++) {
134+ if (buttons[i] > 0) buttons[i]++;
135+ else if (buttons[i] < 0) buttons[i]--;
136+ }
137+ }
138+
139+ }
140+
141+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/TripleBuffer.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/TripleBuffer.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,82 @@
1+package mirrg.applet.nitrogen;
2+
3+import java.awt.image.BufferedImage;
4+
5+import mirrg.event.nitrogen.INitrogenEventManager;
6+
7+public class TripleBuffer
8+{
9+
10+ private BackBuffer safety = new BackBuffer(BufferedImage.TYPE_INT_RGB);
11+ private BackBuffer dirty = new BackBuffer(BufferedImage.TYPE_INT_RGB);
12+
13+ public void rebuffer(int width, int height)
14+ {
15+ safety.rebuffer(width, height);
16+ dirty.rebuffer(width, height);
17+ }
18+
19+ /**
20+ * 読み取り用のバッファ。
21+ */
22+ public synchronized BackBuffer getBufferSafety()
23+ {
24+ return safety;
25+ }
26+
27+ /**
28+ * 書き込み用のグラフィクス。
29+ */
30+ public synchronized BackBuffer getBufferDirty()
31+ {
32+ return dirty;
33+ }
34+
35+ public synchronized int getWidth()
36+ {
37+ return dirty.getWidth();
38+ }
39+
40+ public synchronized int getHeight()
41+ {
42+ return dirty.getHeight();
43+ }
44+
45+ /**
46+ * dirtyバッファとsafetyバッファを交換する(高速)。
47+ */
48+ public synchronized void flip()
49+ {
50+ dirty.flip(safety);
51+ }
52+
53+ /**
54+ * dirtyバッファの内容をdafetyバッファにコピーする。
55+ */
56+ public void copy()
57+ {
58+ safety.getGraphics().drawImage(dirty.getBuffer(), 0, 0, null);
59+ }
60+
61+ public void rollback()
62+ {
63+ rollback(0, 0);
64+ }
65+
66+ public void rollback(int x, int y)
67+ {
68+ rollback(x, y, getWidth(), getHeight());
69+ }
70+
71+ public void rollback(int x, int y, int width, int height)
72+ {
73+ dirty.getGraphics().drawImage(safety.getBuffer(), x, y, width, height, null);
74+ }
75+
76+ public void subscribeEvent(INitrogenEventManager eventManager)
77+ {
78+ dirty.subscribeEvent(eventManager);
79+ safety.subscribeEvent(eventManager);
80+ }
81+
82+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventApplet.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventApplet.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,142 @@
1+package mirrg.applet.nitrogen.events;
2+
3+import java.applet.Applet;
4+import java.awt.Graphics;
5+
6+public class NitrogenEventApplet
7+{
8+
9+ public Applet applet;
10+
11+ public NitrogenEventApplet(Applet applet)
12+ {
13+ this.applet = applet;
14+ }
15+
16+ public static class Init extends NitrogenEventApplet
17+ {
18+
19+ public Init(Applet applet)
20+ {
21+ super(applet);
22+ }
23+
24+ }
25+
26+ public static class Start extends NitrogenEventApplet
27+ {
28+
29+ public Start(Applet applet)
30+ {
31+ super(applet);
32+ }
33+
34+ }
35+
36+ public static class Stop extends NitrogenEventApplet
37+ {
38+
39+ public Stop(Applet applet)
40+ {
41+ super(applet);
42+ }
43+
44+ }
45+
46+ public static class Destroy extends NitrogenEventApplet
47+ {
48+
49+ public Destroy(Applet applet)
50+ {
51+ super(applet);
52+ }
53+
54+ }
55+
56+ /**
57+ * @see NitrogenEventApplet.Start
58+ * @see NitrogenEventComponent.Resized
59+ */
60+ public static class Rebuffer extends NitrogenEventApplet
61+ {
62+
63+ public int width;
64+ public int height;
65+
66+ public Rebuffer(Applet applet, int width, int height)
67+ {
68+ super(applet);
69+ this.width = width;
70+ this.height = height;
71+ }
72+
73+ }
74+
75+ //
76+
77+ public static class EventCancelable extends NitrogenEventApplet
78+ {
79+
80+ public boolean cancelled = false;
81+
82+ public EventCancelable(Applet applet)
83+ {
84+ super(applet);
85+ }
86+
87+ }
88+
89+ public static class EventPaintBase extends NitrogenEventApplet
90+ {
91+
92+ public Graphics graphics;
93+
94+ public EventPaintBase(Applet applet, Graphics graphics)
95+ {
96+ super(applet);
97+ this.graphics = graphics;
98+ }
99+
100+ }
101+
102+ public static class Paint extends EventPaintBase
103+ {
104+
105+ public Paint(Applet applet, Graphics graphics)
106+ {
107+ super(applet, graphics);
108+ }
109+
110+ public static class Pre extends EventCancelable
111+ {
112+
113+ public Pre(Applet applet)
114+ {
115+ super(applet);
116+ }
117+
118+ }
119+
120+ }
121+
122+ public static class Update extends EventPaintBase
123+ {
124+
125+ public Update(Applet applet, Graphics graphics)
126+ {
127+ super(applet, graphics);
128+ }
129+
130+ public static class Pre extends EventCancelable
131+ {
132+
133+ public Pre(Applet applet)
134+ {
135+ super(applet);
136+ }
137+
138+ }
139+
140+ }
141+
142+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventComponent.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventComponent.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,55 @@
1+package mirrg.applet.nitrogen.events;
2+
3+import java.awt.event.ComponentEvent;
4+
5+public class NitrogenEventComponent
6+{
7+
8+ public ComponentEvent componentEvent;
9+
10+ public NitrogenEventComponent(ComponentEvent componentEvent)
11+ {
12+ this.componentEvent = componentEvent;
13+ }
14+
15+ public static class Shown extends NitrogenEventComponent
16+ {
17+
18+ public Shown(ComponentEvent componentEvent)
19+ {
20+ super(componentEvent);
21+ }
22+
23+ }
24+
25+ public static class Resized extends NitrogenEventComponent
26+ {
27+
28+ public Resized(ComponentEvent componentEvent)
29+ {
30+ super(componentEvent);
31+ }
32+
33+ }
34+
35+ public static class Moved extends NitrogenEventComponent
36+ {
37+
38+ public Moved(ComponentEvent componentEvent)
39+ {
40+ super(componentEvent);
41+ }
42+
43+ }
44+
45+ public static class Hidden extends NitrogenEventComponent
46+ {
47+
48+ public Hidden(ComponentEvent componentEvent)
49+ {
50+ super(componentEvent);
51+ }
52+
53+ }
54+
55+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventGameThread.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventGameThread.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,77 @@
1+package mirrg.applet.nitrogen.events;
2+
3+import mirrg.applet.nitrogen.GameThread;
4+
5+public class NitrogenEventGameThread
6+{
7+
8+ public GameThread gameThread;
9+ public Thread thread;
10+
11+ public NitrogenEventGameThread(GameThread gameThread, Thread thread)
12+ {
13+ this.gameThread = gameThread;
14+ this.thread = thread;
15+ }
16+
17+ public static class Init extends NitrogenEventGameThread
18+ {
19+
20+ public Init(GameThread gameThread, Thread thread)
21+ {
22+ super(gameThread, thread);
23+ }
24+
25+ }
26+
27+ public static class Sleep extends NitrogenEventGameThread
28+ {
29+
30+ /**
31+ * 本来のSleepの処理を行わない場合にtrue。
32+ */
33+ public boolean cancelled = false;
34+
35+ /**
36+ * InterruptedExceptionなどでループを抜けなければならない場合にtrue。
37+ */
38+ public boolean interrupted = false;
39+
40+ public Sleep(GameThread gameThread, Thread thread)
41+ {
42+ super(gameThread, thread);
43+ }
44+
45+ }
46+
47+ public static class Tick extends NitrogenEventGameThread
48+ {
49+
50+ public Tick(GameThread gameThread, Thread thread)
51+ {
52+ super(gameThread, thread);
53+ }
54+
55+ }
56+
57+ public static class PostTick extends NitrogenEventGameThread
58+ {
59+
60+ public PostTick(GameThread gameThread, Thread thread)
61+ {
62+ super(gameThread, thread);
63+ }
64+
65+ }
66+
67+ public static class Render extends NitrogenEventGameThread
68+ {
69+
70+ public Render(GameThread gameThread, Thread thread)
71+ {
72+ super(gameThread, thread);
73+ }
74+
75+ }
76+
77+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventKey.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventKey.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,45 @@
1+package mirrg.applet.nitrogen.events;
2+
3+import java.awt.event.KeyEvent;
4+
5+public class NitrogenEventKey
6+{
7+
8+ public KeyEvent keyEvent;
9+
10+ public NitrogenEventKey(KeyEvent keyEvent)
11+ {
12+ this.keyEvent = keyEvent;
13+ }
14+
15+ public static class Typed extends NitrogenEventKey
16+ {
17+
18+ public Typed(KeyEvent keyEvent)
19+ {
20+ super(keyEvent);
21+ }
22+
23+ }
24+
25+ public static class Pressed extends NitrogenEventKey
26+ {
27+
28+ public Pressed(KeyEvent keyEvent)
29+ {
30+ super(keyEvent);
31+ }
32+
33+ }
34+
35+ public static class Released extends NitrogenEventKey
36+ {
37+
38+ public Released(KeyEvent keyEvent)
39+ {
40+ super(keyEvent);
41+ }
42+
43+ }
44+
45+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventMouse.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventMouse.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,65 @@
1+package mirrg.applet.nitrogen.events;
2+
3+import java.awt.event.MouseEvent;
4+
5+public class NitrogenEventMouse
6+{
7+
8+ public MouseEvent mouseEvent;
9+
10+ public NitrogenEventMouse(MouseEvent mouseEvent)
11+ {
12+ this.mouseEvent = mouseEvent;
13+ }
14+
15+ public static class Released extends NitrogenEventMouse
16+ {
17+
18+ public Released(MouseEvent mouseEvent)
19+ {
20+ super(mouseEvent);
21+ }
22+
23+ }
24+
25+ public static class Pressed extends NitrogenEventMouse
26+ {
27+
28+ public Pressed(MouseEvent mouseEvent)
29+ {
30+ super(mouseEvent);
31+ }
32+
33+ }
34+
35+ public static class Exited extends NitrogenEventMouse
36+ {
37+
38+ public Exited(MouseEvent mouseEvent)
39+ {
40+ super(mouseEvent);
41+ }
42+
43+ }
44+
45+ public static class Entered extends NitrogenEventMouse
46+ {
47+
48+ public Entered(MouseEvent mouseEvent)
49+ {
50+ super(mouseEvent);
51+ }
52+
53+ }
54+
55+ public static class Clicked extends NitrogenEventMouse
56+ {
57+
58+ public Clicked(MouseEvent mouseEvent)
59+ {
60+ super(mouseEvent);
61+ }
62+
63+ }
64+
65+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventMouseMotion.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventMouseMotion.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,35 @@
1+package mirrg.applet.nitrogen.events;
2+
3+import java.awt.event.MouseEvent;
4+
5+public class NitrogenEventMouseMotion
6+{
7+
8+ public MouseEvent mouseEvent;
9+
10+ public NitrogenEventMouseMotion(MouseEvent mouseEvent)
11+ {
12+ this.mouseEvent = mouseEvent;
13+ }
14+
15+ public static class Dragged extends NitrogenEventMouseMotion
16+ {
17+
18+ public Dragged(MouseEvent mouseEvent)
19+ {
20+ super(mouseEvent);
21+ }
22+
23+ }
24+
25+ public static class Moved extends NitrogenEventMouseMotion
26+ {
27+
28+ public Moved(MouseEvent mouseEvent)
29+ {
30+ super(mouseEvent);
31+ }
32+
33+ }
34+
35+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventMouseWheel.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/events/NitrogenEventMouseWheel.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,25 @@
1+package mirrg.applet.nitrogen.events;
2+
3+import java.awt.event.MouseWheelEvent;
4+
5+public class NitrogenEventMouseWheel
6+{
7+
8+ public MouseWheelEvent mouseWheelEvent;
9+
10+ public NitrogenEventMouseWheel(MouseWheelEvent mouseWheelEvent)
11+ {
12+ this.mouseWheelEvent = mouseWheelEvent;
13+ }
14+
15+ public static class Moved extends NitrogenEventMouseWheel
16+ {
17+
18+ public Moved(MouseWheelEvent mouseWheelEvent)
19+ {
20+ super(mouseWheelEvent);
21+ }
22+
23+ }
24+
25+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/Game1.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/Game1.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,73 @@
1+package mirrg.applet.nitrogen.samples;
2+
3+import java.awt.Color;
4+import java.awt.Component;
5+import java.awt.Graphics2D;
6+import java.util.ArrayList;
7+
8+class Game1
9+{
10+
11+ protected Component parent;
12+ protected ArrayList<double[]> points;
13+
14+ public Game1(Component parent)
15+ {
16+ this.parent = parent;
17+ }
18+
19+ public void init()
20+ {
21+ points = new ArrayList<>();
22+ for (int i = 0; i < 200; i++) {
23+ points.add(createPoint());
24+ }
25+ }
26+
27+ protected double[] createPoint()
28+ {
29+ double theta = Math.random() * 2 * Math.PI;
30+ double speed = Math.random() * 0.01 + 0.005;
31+
32+ return new double[] {
33+ 0.5,
34+ 0.5,
35+ speed * Math.cos(theta),
36+ speed * Math.sin(theta),
37+ };
38+ }
39+
40+ public void move()
41+ {
42+ for (int i = 0; i < points.size(); i++) {
43+ double[] point = points.get(i);
44+
45+ point[0] += point[2];
46+ point[1] += point[3];
47+
48+ if (point[0] < 0) points.set(i, createPoint());
49+ else if (point[0] > 1) points.set(i, createPoint());
50+ else if (point[1] < 0) points.set(i, createPoint());
51+ else if (point[1] > 1) points.set(i, createPoint());
52+
53+ }
54+ }
55+
56+ public void paint(Graphics2D graphics)
57+ {
58+ int width = parent.getWidth();
59+ int height = parent.getHeight();
60+
61+ graphics.setColor(Color.black);
62+ graphics.fillRect(0, 0, width, height);
63+
64+ graphics.setColor(Color.red);
65+ for (double[] point : points) {
66+ graphics.fillRect(
67+ (int) (point[0] * width) - 2,
68+ (int) (point[1] * height) - 2,
69+ 5, 5);
70+ }
71+ }
72+
73+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/Game2.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/Game2.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,122 @@
1+package mirrg.applet.nitrogen.samples;
2+
3+import java.awt.Color;
4+import java.awt.Component;
5+import java.awt.Font;
6+import java.awt.Graphics2D;
7+
8+class Game2 extends Game1
9+{
10+
11+ private static final double playerR = 0.01;
12+ private double[] player;
13+ private int score;
14+ private int scoreMax;
15+
16+ public Game2(Component parent)
17+ {
18+ super(parent);
19+ }
20+
21+ @Override
22+ public void init()
23+ {
24+ super.init();
25+
26+ player = new double[] {
27+ 0.5,
28+ 0.75,
29+ };
30+ }
31+
32+ @Override
33+ public void move()
34+ {
35+ super.move();
36+
37+ for (int i = 0; i < points.size(); i++) {
38+ double[] point = points.get(i);
39+
40+ double dist2 = pow2(point[0] - player[0]) + pow2(point[1] - player[1]);
41+
42+ if (dist2 < pow2(playerR)) {
43+ delete(player[0], player[1], pow2(0.5));
44+
45+ if (score > scoreMax) scoreMax = score;
46+ score = 0;
47+
48+ break;
49+ }
50+
51+ }
52+
53+ score++;
54+ }
55+
56+ /**
57+ * @param r2
58+ * r^2
59+ */
60+ private void delete(double x, double y, double r2)
61+ {
62+ for (int i = 0; i < points.size(); i++) {
63+ double[] point = points.get(i);
64+
65+ double dist2 = pow2(point[0] - x) + pow2(point[1] - y);
66+ if (dist2 < r2) {
67+
68+ points.set(i, createPoint());
69+
70+ }
71+
72+ }
73+ }
74+
75+ @Override
76+ protected double[] createPoint()
77+ {
78+ double theta = Math.random() * 2 * Math.PI;
79+ double speed = Math.random() * 0.01 + 0.005;
80+
81+ return new double[] {
82+ 0.5,
83+ 0.25,
84+ speed * Math.cos(theta),
85+ speed * Math.sin(theta),
86+ };
87+ }
88+
89+ private double pow2(double value)
90+ {
91+ return value * value;
92+ }
93+
94+ public void setPlayerPosition(double x, double y)
95+ {
96+ player[0] = x;
97+ player[1] = y;
98+ }
99+
100+ @Override
101+ public void paint(Graphics2D graphics)
102+ {
103+ super.paint(graphics);
104+
105+ int width = parent.getWidth();
106+ int height = parent.getHeight();
107+
108+ graphics.setColor(Color.white);
109+ graphics.fillOval(
110+ (int) ((player[0] - playerR) * width),
111+ (int) ((player[1] - playerR) * height),
112+ (int) (playerR * 2 * width),
113+ (int) (playerR * 2 * height));
114+
115+ graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 24));
116+ graphics.drawString("Score: " + score,
117+ 0, graphics.getFont().getSize());
118+ graphics.drawString("HiScore: " + scoreMax,
119+ 0, graphics.getFont().getSize() * 2);
120+ }
121+
122+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,55 @@
1+package mirrg.applet.nitrogen.samples;
2+
3+import java.awt.Graphics2D;
4+
5+import mirrg.applet.nitrogen.AppletNitrogen;
6+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
7+
8+public class SampleAppletNitrogenEvent extends AppletNitrogen
9+{
10+
11+ private static final long serialVersionUID = 6370904732290917883L;
12+
13+ private Game1 game;
14+ private Thread thread;
15+
16+ public SampleAppletNitrogenEvent()
17+ {
18+ getEventManager().register(NitrogenEventApplet.Init.class, event -> {
19+ game = new Game1(this);
20+ });
21+ getEventManager().register(NitrogenEventApplet.Start.class, event -> {
22+
23+ thread = new Thread(() -> {
24+
25+ while (true) {
26+ try {
27+ Thread.sleep(50);
28+ } catch (InterruptedException e) {
29+ break;
30+ }
31+
32+ game.move();
33+ repaint();
34+ }
35+
36+ });
37+
38+ game.init();
39+ thread.start();
40+ });
41+ getEventManager().register(NitrogenEventApplet.Stop.class, event -> {
42+ thread.interrupt();
43+ });
44+
45+ //
46+
47+ getEventManager().register(NitrogenEventApplet.Paint.Pre.class, event -> {
48+ event.cancelled = true;
49+ });
50+ getEventManager().register(NitrogenEventApplet.Paint.class, event -> {
51+ game.paint((Graphics2D) event.graphics);
52+ });
53+ }
54+
55+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent2.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent2.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,46 @@
1+package mirrg.applet.nitrogen.samples;
2+
3+import java.awt.Graphics2D;
4+
5+import mirrg.applet.nitrogen.AppletNitrogen;
6+import mirrg.applet.nitrogen.GameThread;
7+import mirrg.applet.nitrogen.HAppletNitrogen;
8+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
9+import mirrg.applet.nitrogen.events.NitrogenEventGameThread;
10+
11+public class SampleAppletNitrogenEvent2 extends AppletNitrogen
12+{
13+
14+ private static final long serialVersionUID = 6370904732290917883L;
15+
16+ private Game1 game;
17+
18+ public SampleAppletNitrogenEvent2()
19+ {
20+ GameThread gameThread = HAppletNitrogen.applyGameThread(this);
21+ gameThread.objectiveFps = 20;
22+
23+ getEventManager().register(NitrogenEventApplet.Init.class, event -> {
24+ game = new Game1(this);
25+ });
26+ getEventManager().register(NitrogenEventGameThread.Init.class, event -> {
27+ game.init();
28+ });
29+ getEventManager().register(NitrogenEventGameThread.Tick.class, event -> {
30+ game.move();
31+ });
32+ getEventManager().register(NitrogenEventGameThread.Render.class, event -> {
33+ repaint();
34+ });
35+
36+ //
37+
38+ getEventManager().register(NitrogenEventApplet.Paint.Pre.class, event -> {
39+ event.cancelled = true;
40+ });
41+ getEventManager().register(NitrogenEventApplet.Paint.class, event -> {
42+ game.paint((Graphics2D) event.graphics);
43+ });
44+ }
45+
46+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent3.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent3.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,50 @@
1+package mirrg.applet.nitrogen.samples;
2+
3+import mirrg.applet.nitrogen.AppletNitrogen;
4+import mirrg.applet.nitrogen.GameThread;
5+import mirrg.applet.nitrogen.HAppletNitrogen;
6+import mirrg.applet.nitrogen.HAppletNitrogenEvent;
7+import mirrg.applet.nitrogen.TripleBuffer;
8+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
9+import mirrg.applet.nitrogen.events.NitrogenEventGameThread;
10+
11+public class SampleAppletNitrogenEvent3 extends AppletNitrogen
12+{
13+
14+ private static final long serialVersionUID = 6370904732290917883L;
15+
16+ private Game1 game;
17+
18+ public SampleAppletNitrogenEvent3()
19+ {
20+ GameThread gameThread = HAppletNitrogen.applyGameThread(this);
21+ gameThread.objectiveFps = 50;
22+
23+ getEventManager().register(NitrogenEventApplet.Init.class, event -> {
24+ game = new Game1(this);
25+ });
26+ getEventManager().register(NitrogenEventGameThread.Init.class, event -> {
27+ game.init();
28+ });
29+ getEventManager().register(NitrogenEventGameThread.Tick.class, event -> {
30+ game.move();
31+ });
32+ getEventManager().register(NitrogenEventGameThread.Render.class, event -> {
33+ repaint();
34+ });
35+
36+ //
37+
38+ HAppletNitrogenEvent.adaptComponentEvent(this);
39+ TripleBuffer tripleBuffer = HAppletNitrogen.applyTripleBuffer(this);
40+
41+ getEventManager().register(NitrogenEventApplet.Paint.class, event -> {
42+
43+ game.paint(tripleBuffer.getBufferDirty().getGraphics());
44+ tripleBuffer.flip();
45+
46+ event.graphics.drawImage(tripleBuffer.getBufferSafety().getBuffer(), 0, 0, this);
47+ });
48+ }
49+
50+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent4.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent4.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,62 @@
1+package mirrg.applet.nitrogen.samples;
2+
3+import mirrg.applet.nitrogen.AppletNitrogen;
4+import mirrg.applet.nitrogen.GameThread;
5+import mirrg.applet.nitrogen.HAppletNitrogen;
6+import mirrg.applet.nitrogen.HAppletNitrogenEvent;
7+import mirrg.applet.nitrogen.TripleBuffer;
8+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
9+import mirrg.applet.nitrogen.events.NitrogenEventGameThread;
10+import mirrg.applet.nitrogen.events.NitrogenEventMouseMotion;
11+
12+public class SampleAppletNitrogenEvent4 extends AppletNitrogen
13+{
14+
15+ private static final long serialVersionUID = 6370904732290917883L;
16+
17+ private Game2 game;
18+
19+ public SampleAppletNitrogenEvent4()
20+ {
21+ GameThread gameThread = HAppletNitrogen.applyGameThread(this);
22+ gameThread.objectiveFps = 50;
23+
24+ getEventManager().register(NitrogenEventApplet.Init.class, event -> {
25+ game = new Game2(this);
26+ });
27+ getEventManager().register(NitrogenEventGameThread.Init.class, event -> {
28+ game.init();
29+ });
30+ getEventManager().register(NitrogenEventGameThread.Tick.class, event -> {
31+ game.move();
32+ });
33+ getEventManager().register(NitrogenEventGameThread.Render.class, event -> {
34+ repaint();
35+ });
36+
37+ HAppletNitrogenEvent.adaptMouseMotionEvent(this);
38+ getEventManager().register(NitrogenEventMouseMotion.class,
39+ this::setPlayerPosition);
40+
41+ //
42+
43+ HAppletNitrogenEvent.adaptComponentEvent(this);
44+ TripleBuffer tripleBuffer = HAppletNitrogen.applyTripleBuffer(this);
45+
46+ getEventManager().register(NitrogenEventApplet.Paint.class, event -> {
47+
48+ game.paint(tripleBuffer.getBufferDirty().getGraphics());
49+ tripleBuffer.flip();
50+
51+ event.graphics.drawImage(tripleBuffer.getBufferSafety().getBuffer(), 0, 0, this);
52+ });
53+ }
54+
55+ public void setPlayerPosition(NitrogenEventMouseMotion event)
56+ {
57+ game.setPlayerPosition(
58+ 1.0 * event.mouseEvent.getX() / getWidth(),
59+ 1.0 * event.mouseEvent.getY() / getHeight());
60+ }
61+
62+}
diff -r 9c9f9daf8e55 -r 08fff8cb7027 mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent5.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mirrg.applet.nitrogen/src/mirrg/applet/nitrogen/samples/SampleAppletNitrogenEvent5.java Thu Jul 02 07:24:58 2015 +0900
@@ -0,0 +1,75 @@
1+package mirrg.applet.nitrogen.samples;
2+
3+import java.awt.Font;
4+import java.awt.Graphics2D;
5+
6+import mirrg.applet.nitrogen.AppletNitrogen;
7+import mirrg.applet.nitrogen.FPSAdjuster;
8+import mirrg.applet.nitrogen.HAppletNitrogen;
9+import mirrg.applet.nitrogen.HAppletNitrogen.ResponceApplyStandard;
10+import mirrg.applet.nitrogen.events.NitrogenEventApplet;
11+import mirrg.applet.nitrogen.events.NitrogenEventGameThread;
12+
13+public class SampleAppletNitrogenEvent5 extends AppletNitrogen
14+{
15+
16+ private static final long serialVersionUID = 6370904732290917883L;
17+
18+ private Game2 game;
19+
20+ public SampleAppletNitrogenEvent5()
21+ {
22+ ResponceApplyStandard responce = HAppletNitrogen.applyStandard(this);
23+ responce.gameThread.objectiveFps = 30;
24+
25+ //
26+
27+ getEventManager().register(NitrogenEventApplet.Init.class, event -> {
28+ game = new Game2(this);
29+ });
30+ getEventManager().register(NitrogenEventGameThread.Init.class, event -> {
31+ game.init();
32+ });
33+ getEventManager().register(NitrogenEventGameThread.Tick.class, event -> {
34+ game.setPlayerPosition(
35+ 1.0 * responce.inputStatus.getMouseX() / getWidth(),
36+ 1.0 * responce.inputStatus.getMouseY() / getHeight());
37+ game.move();
38+ });
39+ getEventManager().register(NitrogenEventGameThread.PostTick.class, event -> {
40+ responce.inputStatus.spend();
41+ });
42+ getEventManager().register(NitrogenEventGameThread.Render.class, event -> {
43+ repaint();
44+ });
45+ getEventManager().register(NitrogenEventApplet.Paint.class, event -> {
46+
47+ {
48+ Graphics2D graphics = responce.tripleBuffer.getBufferDirty().getGraphics();
49+
50+ game.paint(graphics);
51+
52+ drawFPS(graphics, responce.fpsAdjuster);
53+ }
54+
55+ responce.tripleBuffer.flip();
56+
57+ event.graphics.drawImage(responce.tripleBuffer.getBufferSafety().getBuffer(), 0, 0, this);
58+ });
59+
60+ }
61+
62+ private void drawFPS(Graphics2D graphics, FPSAdjuster fpsAdjuster)
63+ {
64+ graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 20));
65+ graphics.drawString(String.format(
66+ "FPS: %.2f",
67+ fpsAdjuster.getFPS()),
68+ 0, getHeight());
69+ graphics.drawString(String.format(
70+ "CPU: %.2f%%",
71+ fpsAdjuster.getLoadFactor() * 100),
72+ 0, getHeight() - graphics.getFont().getSize());
73+ }
74+
75+}