質問編集履歴

6

誤字

2024/01/14 08:47

投稿

ShunKitajima
ShunKitajima

スコア5

test CHANGED
File without changes
test CHANGED
@@ -115,7 +115,7 @@
115
115
  }
116
116
  ```
117
117
 
118
- **簡略化したソースコード(+ デバッグをtxtファイルに出力する)**
118
+ **簡略化したソースコード(+ エラーをtxtファイルに出力する)**
119
119
  ---
120
120
  Main.java
121
121
  ```Java

5

簡略化したソースコードの更新

2024/01/14 08:27

投稿

ShunKitajima
ShunKitajima

スコア5

test CHANGED
File without changes
test CHANGED
@@ -115,7 +115,7 @@
115
115
  }
116
116
  ```
117
117
 
118
- **簡略化したソースコード**
118
+ **簡略化したソースコード(+ デバッグをtxtファイルに出力する)**
119
119
  ---
120
120
  Main.java
121
121
  ```Java
@@ -123,6 +123,8 @@
123
123
 
124
124
  import java.awt.GraphicsDevice;
125
125
  import java.awt.GraphicsEnvironment;
126
+ import java.io.FileWriter;
127
+ import java.io.IOException;
126
128
 
127
129
  import javax.swing.JFrame;
128
130
 
@@ -132,19 +134,37 @@
132
134
  private static final GamePanel gp = new GamePanel();
133
135
  private static GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
134
136
  private static GraphicsDevice device = env.getDefaultScreenDevice();
137
+
138
+ private static FileWriter fileWriter;
135
139
 
136
140
  public static void main(String[] args) {
137
-
141
+ try {
142
+ try {
143
+ fileWriter = new FileWriter("該当フォルダのパス\\Debug_Main_main.txt", true);
144
+ fileWriter.write("Start");
145
+ fileWriter.close();
146
+ } catch (IOException e) {
147
+ e.printStackTrace();
148
+ }
149
+
138
- gameFrame = new JFrame();
150
+ gameFrame = new JFrame();
139
- gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
151
+ gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140
- gameFrame.setResizable(false);
141
- gameFrame.setTitle("MyGame");
142
- gameFrame.add(gp);
152
+ gameFrame.add(gp);
143
-
153
+
144
- gameFrame.setUndecorated(true);
154
+ gameFrame.setUndecorated(true);
145
- device.setFullScreenWindow(gameFrame);
155
+ device.setFullScreenWindow(gameFrame);
146
-
156
+
147
- gp.startGame();
157
+ gp.startGame();
158
+
159
+ } catch (Exception e) {
160
+ try {
161
+ fileWriter = new FileWriter("該当フォルダのパス\\Debug_Main_main.txt", true);
162
+ fileWriter.write(e.getMessage());
163
+ fileWriter.close();
164
+ } catch (IOException e1) {
165
+ e1.printStackTrace();
166
+ }
167
+ }
148
168
 
149
169
  }
150
170
  }
@@ -156,7 +176,8 @@
156
176
 
157
177
  import java.awt.Graphics;
158
178
  import java.awt.Graphics2D;
159
- import java.awt.Toolkit;
179
+ import java.io.FileWriter;
180
+ import java.io.IOException;
160
181
 
161
182
  import javax.swing.JPanel;
162
183
 
@@ -167,34 +188,44 @@
167
188
  long lastTime = 0;
168
189
  long currentTime = 0;
169
190
 
170
- //SCREEN SETTINGS
171
- public final int monitorWidth = Toolkit.getDefaultToolkit().getScreenSize().width; //スクリーンの横幅
172
- public final int monitorHeight = Toolkit.getDefaultToolkit().getScreenSize().height; //スクリーンの縦幅
173
-
174
191
  // SYSTEM
175
192
  private Graphics2D g2;
176
193
  private Thread gameThread;
177
-
178
- //パネルの初期設定
179
- public GamePanel() {
180
- // this.setPreferredSize(new Dimension(monitorWidth, monitorHeight)); //コンポーネントの適切なサイズを設定
181
- this.setDoubleBuffered(true);
194
+ private FileWriter fileWriter;
182
- }
183
195
 
184
196
  public void startGame() { //Mainクラスから呼び出される
197
+ try {
185
- //パネルのThreadを開始 => run()
198
+ //パネルのThreadを開始 => run()
186
- gameThread = new Thread(this);
199
+ gameThread = new Thread(this);
187
- gameThread.start();
200
+ gameThread.start();
188
-
201
+ } catch (Exception e) {
202
+ try {
203
+ fileWriter = new FileWriter("該当フォルダのパス\\Debug_GamePanel_startGame.txt", true);
204
+ fileWriter.write(e.getMessage());
205
+ fileWriter.close();
206
+ } catch (IOException e1) {
207
+ e1.printStackTrace();
208
+ }
209
+ }
189
210
  }
190
211
 
191
212
  @Override
192
213
  public void run() {
193
214
  while(gameThread != null) {
215
+ try {
194
- currentTime = System.nanoTime();
216
+ currentTime = System.nanoTime();
195
- if (currentTime - lastTime >= drawInterval) {
217
+ if (currentTime - lastTime >= drawInterval) {
196
- repaint(0, 0, monitorWidth, monitorHeight);
218
+ repaint(0, 0, 1920, 1080);
197
- lastTime = currentTime;
219
+ lastTime = currentTime;
220
+ }
221
+ } catch (Exception e) {
222
+ try {
223
+ fileWriter = new FileWriter("該当フォルダのパス\\Debug_GamePanel_run.txt", true);
224
+ fileWriter.write(e.getMessage());
225
+ fileWriter.close();
226
+ } catch (IOException e1) {
227
+ e1.printStackTrace();
228
+ }
198
229
  }
199
230
  }
200
231
  }
@@ -202,15 +233,26 @@
202
233
  //repaintメソッドから呼び出される
203
234
  @Override
204
235
  public void paintComponent(final Graphics g) {
236
+ try {
205
- super.paintComponent(g);
237
+ super.paintComponent(g);
206
-
238
+
207
- g2 = (Graphics2D)g;
239
+ g2 = (Graphics2D)g;
208
-
240
+
209
- g2.drawString("A", monitorWidth/2, monitorHeight/2);
241
+ g2.drawString("A", 960, 540);
210
-
242
+
211
- g2.dispose();
243
+ g2.dispose();
244
+ } catch (Exception e) {
245
+ try {
246
+ fileWriter = new FileWriter("該当フォルダのパス\\Debug_GamePanel_paintComponent.txt", true);
247
+ fileWriter.write(e.getMessage());
248
+ fileWriter.close();
249
+ } catch (IOException e1) {
250
+ e1.printStackTrace();
212
- }
251
+ }
213
- }
252
+ }
253
+ }
254
+ }
255
+
214
256
  ```
215
257
 
216
258
  ### 試したこと・調べたこと

4

簡略化したソースコードの追加

2024/01/14 07:35

投稿

ShunKitajima
ShunKitajima

スコア5

test CHANGED
File without changes
test CHANGED
@@ -113,9 +113,105 @@
113
113
 
114
114
  g2.dispose();
115
115
  }
116
-
117
- ```
116
+ ```
117
+
118
-
118
+ **簡略化したソースコード**
119
+ ---
120
+ Main.java
121
+ ```Java
122
+ package main;
123
+
124
+ import java.awt.GraphicsDevice;
125
+ import java.awt.GraphicsEnvironment;
126
+
127
+ import javax.swing.JFrame;
128
+
129
+ public class Main {
130
+
131
+ private static JFrame gameFrame = new JFrame();
132
+ private static final GamePanel gp = new GamePanel();
133
+ private static GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
134
+ private static GraphicsDevice device = env.getDefaultScreenDevice();
135
+
136
+ public static void main(String[] args) {
137
+
138
+ gameFrame = new JFrame();
139
+ gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140
+ gameFrame.setResizable(false);
141
+ gameFrame.setTitle("MyGame");
142
+ gameFrame.add(gp);
143
+
144
+ gameFrame.setUndecorated(true);
145
+ device.setFullScreenWindow(gameFrame);
146
+
147
+ gp.startGame();
148
+
149
+ }
150
+ }
151
+ ```
152
+
153
+ GamePanel.java
154
+ ```Java
155
+ package main;
156
+
157
+ import java.awt.Graphics;
158
+ import java.awt.Graphics2D;
159
+ import java.awt.Toolkit;
160
+
161
+ import javax.swing.JPanel;
162
+
163
+ public class GamePanel extends JPanel implements Runnable {
164
+ //FPS
165
+ public final int FPS = 60;
166
+ private final double drawInterval = 1000000000.0/FPS;
167
+ long lastTime = 0;
168
+ long currentTime = 0;
169
+
170
+ //SCREEN SETTINGS
171
+ public final int monitorWidth = Toolkit.getDefaultToolkit().getScreenSize().width; //スクリーンの横幅
172
+ public final int monitorHeight = Toolkit.getDefaultToolkit().getScreenSize().height; //スクリーンの縦幅
173
+
174
+ // SYSTEM
175
+ private Graphics2D g2;
176
+ private Thread gameThread;
177
+
178
+ //パネルの初期設定
179
+ public GamePanel() {
180
+ // this.setPreferredSize(new Dimension(monitorWidth, monitorHeight)); //コンポーネントの適切なサイズを設定
181
+ this.setDoubleBuffered(true);
182
+ }
183
+
184
+ public void startGame() { //Mainクラスから呼び出される
185
+ //パネルのThreadを開始 => run()
186
+ gameThread = new Thread(this);
187
+ gameThread.start();
188
+
189
+ }
190
+
191
+ @Override
192
+ public void run() {
193
+ while(gameThread != null) {
194
+ currentTime = System.nanoTime();
195
+ if (currentTime - lastTime >= drawInterval) {
196
+ repaint(0, 0, monitorWidth, monitorHeight);
197
+ lastTime = currentTime;
198
+ }
199
+ }
200
+ }
201
+
202
+ //repaintメソッドから呼び出される
203
+ @Override
204
+ public void paintComponent(final Graphics g) {
205
+ super.paintComponent(g);
206
+
207
+ g2 = (Graphics2D)g;
208
+
209
+ g2.drawString("A", monitorWidth/2, monitorHeight/2);
210
+
211
+ g2.dispose();
212
+ }
213
+ }
214
+ ```
119
215
 
120
216
  ### 試したこと・調べたこと
121
217
  - [x] teratailやGoogle等で検索した

3

補足の追加

2024/01/14 07:00

投稿

ShunKitajima
ShunKitajima

スコア5

test CHANGED
File without changes
test CHANGED
@@ -3,7 +3,6 @@
3
3
 
4
4
  ### 発生している問題・分からないこと
5
5
  Eclipseを用いてエクスポートした実行可能jarファイルを、jpackageを使ってJVM引数を-Dsun.java2d.opengl=Trueと設定してインストーラを作り、それを使ってアプリケーションをインストールして実行すると、以下のメソッドを使ってフルスクリーン表示させた時に、Jpanelインスタンス上にdrawImageやdrawStringを用いて描画したものが表示されなくなる。加えて、カーソルも表示されない。しかし、jarファイルをコマンドプロンプトで実行した時(JVM引数は同じ)は、正常に描画される。-Dsun.java2d.opengl=Falseと設定してインストーラを作った時にも正常に描画される。
6
-
7
6
 
8
7
 
9
8
  ### 該当のソースコード
@@ -150,4 +149,5 @@
150
149
  これらのステップを踏むことで、問題の原因を特定し、適切な解決策を見つけることができるはずです。また、問題が解決しない場合は、Javaの開発者コミュニティやフォーラムで支援を求めることも検討してください。
151
150
 
152
151
  ### 補足
153
- Java17
152
+ 開発や実行はすべてWindows11(64ビット)を用いて行い、JDK17とEclipseを用いてJava17のライブラリのawtとswingを使って2Dゲームを作成。awtのrepaintメソッドが1秒間に60回実行される(以下のrunメソッド参照)ことで、Graphics2DのdrawImageやdrawStringを用いた描画(paintComponentメソッドは以下参照)。jpackageを使うにあたってWiXのv3.11.2.4516をダウンロードし、以下のようなコマンドをコマンドプロンプトで実行してインストーラを作成。
153
+ 「jpackage --type exe --vendor "Shun" --win-menu --win-dir-chooser --win-shortcut --win-shortcut-prompt --input Jarファイルがあるディレクトリのパス --name MyGame --main-jar MyGame.jar --main-class main.Main --java-options "-Dsun.java2d.dpiaware=true" --java-options "-Dsun.java2d.uiScale=1.0" --java-options "-Xmx2048m" --java-options "-Dsun.java2d.opengl=True" --dest Jarファイルがあるディレクトリのパス」

2

当該コードの追加

2024/01/14 06:58

投稿

ShunKitajima
ShunKitajima

スコア5

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,88 @@
36
36
  }
37
37
  ```
38
38
 
39
+ ```Java
40
+
41
+ // Threadのインスタンスのstartメソッドから呼び出される
42
+ // 1/60秒ごとにupdateメソッドとrepaintメソッドを実行する
43
+ @Override
44
+ public void run() {
45
+
46
+ gameIsRunning = true; //gameIsRunningがtrueの時のみキーを操作可能にする
47
+
48
+ while(gameThread != null) {
49
+ currentTime = System.nanoTime();
50
+ if (currentTime - lastTime >= drawInterval) { //drawInterval == 1000000000.0/60.0
51
+ update(); //各インスタンスのupdateメソッドを実行する
52
+ repaint(widthOfBlackScreen, 0, monitorWidth-2*widthOfBlackScreen, monitorHeight); //paintComponentメソッドを呼び出す(widthOfBlackScreenは、ゲーム画面が描画される部分を横:縦=3:4にするために両脇に表示させる黒い部分の幅)
53
+ lastTime = currentTime;
54
+ }
55
+ }
56
+ }
57
+
58
+ ```
59
+
60
+ ```Java
61
+
62
+ @Override
63
+ public void paintComponent(final Graphics g) {
64
+ super.paintComponent(g); //コンポーネントの背景をクリア
65
+
66
+ g2 = (Graphics2D)g;
67
+
68
+ // Debug
69
+ drawStartNanosecond = 0;
70
+ if (keyH.getDrawTimeIsBeingChecked() == true) {drawStartNanosecond = System.nanoTime();}
71
+
72
+ // Background
73
+ bgHandler.draw(g2);
74
+
75
+
76
+ if (gameState != titleState) {
77
+ // タイルの描画
78
+ tileM.draw(g2);
79
+
80
+ // Itemのサブクラスのインスタンスの描画
81
+ for (int i = 0; i < item.length; i++) {
82
+ if (item[i] != null) {item[i].draw(g2);}
83
+ }
84
+
85
+ // worldYによってentitiesの描画する順番を変える
86
+ entities[0] = player;
87
+ for (int i = 1; i < 1+npc.length; i++) {
88
+ if (npc[i-1] != null) {entities[i] = npc[i-1];}
89
+ }
90
+ for (int i = 1+npc.length; i < 1+npc.length+animatePicture.length; i++) {
91
+ if (animatePicture[i-1-npc.length] != null) {entities[i] = animatePicture[i-1-npc.length];}
92
+ }
93
+ for (int i = 1+npc.length+animatePicture.length; i < entities.length; i++) {
94
+ if (obj[i-1-npc.length-animatePicture.length] != null) {entities[i] = obj[i-1-npc.length-animatePicture.length];}
95
+ }
96
+ UtilityTool.sort(entities, this);
97
+ for (int i = 0; i < entities.length; i++) {
98
+ if (entities[i] != null) {entities[i].draw(g2);}
99
+ }
100
+ }
101
+
102
+ // UI描画
103
+ uiDrawer.draw(g2);
104
+
105
+ // Debug
106
+ if (keyH.getDrawTimeIsBeingChecked() == true) {
107
+ drawEndNanosecond = System.nanoTime();
108
+ passedNanosecond = drawEndNanosecond - drawStartNanosecond;
109
+ g2.setColor(Color.white);
110
+ g2.setFont(uiDrawer.maruMonica.deriveFont(50F));
111
+ String s = (Integer.toString((int)passedNanosecond));
112
+ g2.drawString("DrawingTime: "+s+"ns"+"("+monitorWidth+":"+monitorHeight+")", widthOfBlackScreen+tileSize/2, UtilityTool.getCenteredTextY(s.toString(), this, g2));
113
+ }
114
+
115
+ g2.dispose();
116
+ }
117
+
118
+ ```
119
+
120
+
39
121
  ### 試したこと・調べたこと
40
122
  - [x] teratailやGoogle等で検索した
41
123
  - [x] ソースコードを自分なりに変更した

1

誤字

2024/01/14 01:17

投稿

ShunKitajima
ShunKitajima

スコア5

test CHANGED
File without changes
test CHANGED
@@ -43,8 +43,8 @@
43
43
  - [x] その他
44
44
 
45
45
  ##### 上記の詳細・結果
46
- - リストGoogleで「Java OpenGL フルスクリーン」などと調べ、setExtendedStateメソッドを使ったフルスクリーンの方法も試したが、解決しなかった。
46
+ - Googleで「Java OpenGL フルスクリーン」などと調べ、setExtendedStateメソッドを使ったフルスクリーンの方法も試したが、解決しなかった。
47
- - リストChatGPT(GPT-4)を用いて以上の質問を入力し、以下のような結果がいくつか得られたが、解決には至らなかった。
47
+ - ChatGPT(GPT-4)を用いて以上の質問を入力し、以下のような結果がいくつか得られたが、解決には至らなかった。
48
48
 
49
49
  [ChatGPTの回答]
50
50
  問題の状況について理解しました。jpackageを用いて作成したJavaアプリケーションでフルスクリーン時に問題が発生するが、コマンドプロンプトから同じJVM引数で実行すると問題が発生しないということですね。この問題は、jpackageでパッケージ化されたアプリケーションの設定や環境が、直接コマンドプロンプトから実行する場合と異なることに起因している可能性があります。