前提・実現したいこと
Jframeを使用してオセロ板を表示したい。
(現段階では背景の黒画像のみが表示される)
いざ実行してみるとフレームは表示されるが、肝心の背景の黒四角が表示されません。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
エラーメッセージは特に出ておりません。
該当のソースコード
Java
1//Begin of OthelloBoard class 2import java.io.*; 3public class OthelloBoard implements Runnable{ 4 5 public OthelloGUI gui; 6 7 public OthelloBoard() { 8 gui = new OthelloGUI(); 9 gui.BGtoFG(); 10 11 gui.windowShow(); 12 13 } //end of OthelloBoardコンストラクタ 14 15 16 public void run() { 17 try { 18 while (true) { 19 Thread.sleep(1000); 20 } 21 }catch(Exception ex) { 22 ex.printStackTrace(); 23 } 24 }//runメソッド終了 25 26 27 28 public static void main(String[] args) { 29 Thread othellob = new Thread(new OthelloBoard()); 30 31 try { 32 Thread.sleep(1000); 33 }catch(Exception ex){ 34 ex.printStackTrace(); 35 } 36 37 38 othellob.start(); 39 40 } //main関数終わり 41} //end of class Tetris 42 43//Begin of OthelloGUI Class 44import java.awt.*; 45import javax.swing.*; 46 47public class OthelloGUI { 48 49 private int ScreenX = 150; 50 private int ScreenY = 150; 51 private int ScreenW = 500; 52 private int ScreenH = 500; 53 54 private JFrame frame; 55 private JPanel panel; 56 private Image image; 57 58 private Graphics fg; // フロント画面 59 private Graphics bg; // バック画面 60 61 public OthelloGUI() { 62 63 frame = new JFrame("Othello"); 64 frame.setBounds(ScreenX,ScreenY,ScreenW,ScreenH); 65 frame.setResizable(false); 66 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 67 68 Container cont = frame.getContentPane(); 69 cont.setLayout(new FlowLayout(FlowLayout.CENTER,0,0)); 70 71 panel = new JPanel(); 72 panel.setPreferredSize(new Dimension(ScreenW,ScreenH)); 73 cont.add(panel); 74 75 frame.pack(); 76 77 image = panel.createImage(ScreenW,ScreenH); 78 79 fg = panel.getGraphics(); 80 bg = image.getGraphics(); 81 bg.setColor(Color.BLACK); 82 bg.fillRect(0,0,ScreenW,ScreenH); 83 84 }//OthelloGUIコンストラクタ終わり 85 86 87 88 public void BGtoFG() { 89 fg.drawImage(image,0,0,panel); 90 } // BGtoFG終わり 91 92 public void windowShow() { 93 frame.show(); 94 } // windowShow終わり 95 96} // OhelloGUI 終わり 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
試したこと
ググってみたところ、一瞬で画像が消えてる可能性があるそうだったので、
様々なところにThread.sleepを置いて処理をいったん停止させてみた。
(OthelloGUIクラスのwindowShowメソッドの最後などに)
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/08 10:29 編集