質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

Q&A

解決済

4回答

628閲覧

最後に、ゲームオーバーを表示したいのですが、できません。

akirasada1972

総合スコア41

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Swing

SwingはJavaに標準で付属するグラフィック関連のクラスライブラリを指します。

0グッド

0クリップ

投稿2018/06/22 11:45

java

1package luna.sexydesign; 2 3import javax.swing.*; 4import java.awt.*; 5import java.awt.event.*; 6 7class Subthread extends Thread { 8 9 private ScreenToucher toucher; 10 11public Subthread(ScreenToucher toucher) { 12 this.toucher = toucher; 13} 14 15@Override 16public void run() {// TODO Auto-generated constructor stub 17 try { 18 sleep(10000); 19 } catch (InterruptedException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23 SwingUtilities.invokeLater(() -> 24 toucher.setPanel3()); 25 } 26} 27 28public class ScreenToucher extends JFrame { 29 30 int i = 0; 31 static int width = 500; 32 static int height = 500; 33 private MyPanel1 p1; 34 private MyPanel2 p2; 35 36 public static void main(String args[]) { 37 38 ScreenToucher frame = new ScreenToucher("タイトル"); 39 frame.setVisible(true); 40 } 41 42 ScreenToucher(String title) { 43 setTitle(title); 44 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 45 setBounds(100, 100, width, height); 46 47 p1 = new MyPanel1(); 48 p2 = new MyPanel2(this); 49 50 Container contentPane = getContentPane(); 51 contentPane.add(p1, BorderLayout.NORTH); 52 contentPane.add(p2, BorderLayout.CENTER); 53 54 Subthread thread = new Subthread(this); 55 thread.start(); 56} 57 58 public void setCount(int count) { 59 p1.setCount(count); 60 } 61 62 public void setPanel3(){ 63 MyPanel3 p3 = new MyPanel3(); 64 remove(p2); 65 add(p3, BorderLayout.CENTER); 66 } 67} 68 69class MyPanel1 extends JPanel { 70 71 int i; 72 73 private JLabel jl1; 74 75 MyPanel1() { 76 JPanel jp1 = new JPanel(); 77 jl1 = new JLabel(); 78 jp1.setBackground(Color.green); 79 Integer j = new Integer(i); 80 String text = j.toString(); 81 jl1.setText(text); 82 jp1.add(jl1); 83 add(jp1); 84 } 85 86 public void setCount(int count) { 87 jl1.setText(Integer.toString(count)); 88 } 89 90} 91 92class MyPanel2 extends JPanel { 93 94 static int width = 500; 95 static int height = 500; 96 static int i = 0; 97 static int r = 60; 98 static int x; 99 static int y; 100 101 final static Color bc = Color.black; 102 final static Color dc = Color.green; 103 104 private ScreenToucher owner; 105 106 public MyPanel2(ScreenToucher owner) { 107 setBackground(Color.black); 108 this.owner = owner; 109 repaint(); 110 MouseListener(); 111 } 112 113 void MouseListener() { 114 addMouseListener(new MouseAdapter() { 115 public void mousePressed(MouseEvent e) { 116 double mouseX = e.getX(); 117 double mouseY = e.getY(); 118 if (mouseX > x && mouseX < x + 2 * r) { 119 if (mouseY > y && mouseY < y + 2 * r) { 120 repaint(); 121 owner.setCount(Count()); 122 } 123 } 124 } 125 }); 126 } 127 128 protected void paintComponent(Graphics g) { 129 super.paintComponent(g); 130 x = (int) (Math.random() * width); 131 y = (int) (Math.random() * height) + 30; 132 if ((x < width - 2 * r) && (y < height - 2 * r)) { 133 g.setColor(dc); 134 g.fillOval(x, y, r, r); 135 } else { 136 repaint(); 137 } 138 } 139 140 int Count() { 141 i += 100; 142 return i; 143 } 144} 145 146class MyPanel3 extends JPanel { 147 148 public MyPanel3() { 149 setBackground(new Color(0,0,0,100)); 150 } 151 152 public void paintComponent(Graphics g) { 153 super.paintComponent(g); 154 g.setColor(Color.white); 155 g.drawString("GAME OVER", 100, 200); 156 } 157 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2018/06/22 11:57

コードだけ丸投げて終わるのではなく、調べてみたこと、やっみたこと、起きていることをきちんと記載してください。
akirasada1972

2018/06/22 17:39

10秒後に、パネルを入れ替えたいのですが、うまく表示されません。プログラム自体は、止まるのですが、ゲームオーバーの画面が、出ません。
m.ts10806

2018/06/22 20:46

質問本文を編集して追記してください。
guest

回答4

0

自己解決

皆さんのアドバイスを参考にして、無事完成させることが、できました。
ありがとうございました。

java

1import javax.swing.JFrame; 2 3import javax.swing.*; 4import java.awt.*; 5import java.awt.event.*; 6 7// ゲームモード 8enum GameMode { 9 MENU, PLAY, OVER, 10} 11 12public class ScreenToucher extends JFrame { 13 14 int i = 0; 15 // private finalにして代入を抑止する 16 private final static int width = 500; 17 private final static int height = 500; 18 private GameMode mode = GameMode.MENU; 19 // CardLayoutを使って切り替え 20 private final JPanel gamePanel = new JPanel(new CardLayout()); 21 // finalで代入を抑止 22 public static JLabel l1; 23 private int score = 0; 24 25 public static Timer timer; // タイマー 26 27 JButton btn1 = new JButton("ゲーム開始"); 28 JButton btn2 = new JButton("ゲーム終了"); 29 30 public static void main(String args[]) { 31 // mainスレッドなので、SwingUtilities#invokeLaterを使う。 32 SwingUtilities.invokeLater(() -> { 33 ScreenToucher frame = new ScreenToucher("Screen Toucher"); 34 frame.setVisible(true); 35 }); 36 } 37 38 ScreenToucher(String title) { 39 40 setTitle(title); 41 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 42 setBounds(100, 100, width, height); 43 44 JPanel p1 =new JPanel(); 45 p1.setLayout(new GridLayout(1,3,20,10)); 46 47 JPanel p2 = new JPanel(); 48 p2.setBackground(Color.gray); 49 50 btn1.addActionListener(new Listener1()); 51 p1.add(btn1); 52 53 l1 = new JLabel(); 54 // 1行に 55 l1.setText("0"); 56 p1.add(l1); 57 58 btn2.addActionListener(new Listener2()); 59 p1.add(btn2); 60 61 // カードレイアウトに登録 62 gamePanel.add(p2, GameMode.MENU.name()); 63 gamePanel.add(new MyPanel3(this), GameMode.PLAY.name()); 64 gamePanel.add(new GameOverPanel(), GameMode.OVER.name()); 65 66 67 add(p1, BorderLayout.NORTH); 68 add(gamePanel, BorderLayout.CENTER); 69 70 // Subthreadクラスを削除して、javax.swing.Timerを使用 71 // 10000ms=10秒後 72 timer = new Timer(10000, (e) -> { 73 showPanel(GameMode.OVER); 74 }); 75 // ここではtimer#startをしない。 76 showPanel(GameMode.MENU); 77} 78 79 public void addCount() { 80 score += 100; 81 l1.setText(Integer.toString(score)); 82 } 83 84 // setCountで内部の変数を同期 85 void setCount(int count) { 86 score = count; 87 l1.setText(Integer.toString(score)); 88 } 89 90 public void showPanel(GameMode m) { 91 final CardLayout card = (CardLayout) gamePanel.getLayout(); 92 card.show(gamePanel, m.name()); 93 this.mode = m; // パネル表示時にゲームモードを変更 94 } 95 96 97 public class Listener1 implements ActionListener { 98 99 public void actionPerformed(ActionEvent e) { 100 101 if(e.getSource()==btn1) { 102 setCount(0); 103 showPanel(GameMode.PLAY); 104 timer.start(); 105 } 106 } 107 } 108 109 public class Listener2 implements ActionListener { 110 111 public void actionPerformed(ActionEvent e) { 112 113 if(e.getSource()==btn2) { 114 115 System.exit(0); 116 117 } 118 } 119 } 120} 121// クラス名はクリッカーの方が良いかもです。 122class MyPanel3 extends JPanel { 123 124 static int width = 500; 125 static int height = 500; 126 static int r = 60; 127 static int i = 0; 128 static int x; 129 static int y; 130 131 final static Color bc = Color.black; 132 final static Color dc = Color.magenta; 133 134 private ScreenToucher toucher; 135 // privateに変数名をiからscoreに変更 136 private int count = 0; 137 138 public MyPanel3(ScreenToucher toucher) { 139 setBackground(bc); 140 this.toucher = toucher; 141 MouseListener(); 142 repaint(); 143 } 144 145 void MouseListener() { 146 addMouseListener(new MouseAdapter() { 147 public void mousePressed(MouseEvent e) { 148 double mouseX = e.getX(); 149 double mouseY = e.getY(); 150 if (mouseX > x && mouseX < x + 2 * r) { 151 if (mouseY > y && mouseY < y + 2 * r) { 152 repaint(); 153 toucher.addCount(); 154 } 155 } 156 } 157 }); 158 } 159 160 protected void paintComponent(Graphics g) { 161 super.paintComponent(g); 162 x = (int) (Math.random() * width); 163 y = (int) (Math.random() * height) + 30; 164 if ((x < width - 2 * r) && (y < height - 2 * r)) { 165 g.setColor(dc); 166 g.fillOval(x, y, r, r); 167 } else { 168 repaint(); 169 } 170 } 171} 172 173// MyPanel4 →GameOverPanelに名前を変更 174class GameOverPanel extends JPanel { 175 176 public GameOverPanel() { 177 setBackground(new Color(0, 0, 0, 100)); 178 } 179 180 public void paintComponent(Graphics g) { 181 super.paintComponent(g); 182 g.setColor(Color.white); 183 g.drawString("GAME OVER", 100, 200); 184 } 185} 186

投稿2018/08/22 16:57

akirasada1972

総合スコア41

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

なんかいろいろ違う気がする・・・
すぐに思い上がった点をいくつか。
1.run()関数を使うときはimplements Runnableを使って、ScreenToucherの中かどこかに入れちゃったほうがいいかも。
2.sleep(2000);のところは、引数がms(1000で1s)だったはずだから、十秒なら10000じゃないかな?もしくは、Timerを使おう。
3.めんどくさくても、class1つにファイルを一つ使ったほうがいいと思います。
4.思っていた挙動と違う(書いてある内容と)。その点上でほかの人が書いてますし、詳しく書きましょう。
5.プログラムの終了だけならSystem.exit(0);でできますよ。

投稿2018/08/20 15:50

編集2018/08/20 23:54
yukkuri

総合スコア624

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

なんとか、ゲームオーバーは出るのですが、ゲームが終わりません。

java

1package luna.sexydesign; 2 3import javax.swing.*; 4import java.awt.*; 5import java.awt.event.*; 6 7class Subthread extends Thread { 8 9 private ScreenToucher toucher; 10 11public Subthread(ScreenToucher toucher) { 12 this.toucher = toucher; 13} 14 15@Override 16public void run() {// TODO Auto-generated constructor stub 17 try { 18 sleep(2000); 19 } catch (InterruptedException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } 23// SwingUtilities.invokeLater(() -> 24 toucher.setPanel3(); 25 } 26} 27 28public class ScreenToucher extends JFrame { 29 30 int i = 0; 31 static int width = 500; 32 static int height = 500; 33 private MyPanel1 p1; 34 35 public static void main(String args[]) { 36 37 ScreenToucher frame = new ScreenToucher("タイトル"); 38 frame.setVisible(true); 39 } 40 41 ScreenToucher(String title) { 42 setTitle(title); 43 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 44 setBounds(100, 100, width, height); 45 46 p1 = new MyPanel1(); 47 MyPanel2 p2 = new MyPanel2(this); 48 49 Container contentPane = getContentPane(); 50 contentPane.add(p1, BorderLayout.NORTH); 51 contentPane.add(p2, BorderLayout.CENTER); 52 53 Subthread thread = new Subthread(this); 54 thread.start(); 55} 56 57 public void setCount(int count) { 58 p1.setCount(count); 59 } 60 61 public void setPanel3(){ 62 MyPanel3 p3 = new MyPanel3(); 63 add(p3, BorderLayout.CENTER); 64 } 65} 66 67class MyPanel1 extends JPanel { 68 69 int i; 70 71 private JLabel jl1; 72 73 MyPanel1() { 74 JPanel jp1 = new JPanel(); 75 jl1 = new JLabel(); 76 jp1.setBackground(Color.green); 77 Integer j = new Integer(i); 78 String text = j.toString(); 79 jl1.setText(text); 80 jp1.add(jl1); 81 add(jp1); 82 } 83 84 public void setCount(int count) { 85 jl1.setText(Integer.toString(count)); 86 } 87 88} 89 90class MyPanel2 extends JPanel { 91 92 static int width = 500; 93 static int height = 500; 94 static int i = 0; 95 static int r = 60; 96 static int x; 97 static int y; 98 99 final static Color bc = Color.black; 100 final static Color dc = Color.green; 101 102 private ScreenToucher owner; 103 104 public MyPanel2(ScreenToucher owner) { 105 setBackground(Color.black); 106 this.owner = owner; 107 repaint(); 108 MouseListener(); 109 } 110 111 void MouseListener() { 112 addMouseListener(new MouseAdapter() { 113 public void mousePressed(MouseEvent e) { 114 double mouseX = e.getX(); 115 double mouseY = e.getY(); 116 if (mouseX > x && mouseX < x + 2 * r) { 117 if (mouseY > y && mouseY < y + 2 * r) { 118 repaint(); 119 owner.setCount(Count()); 120 } 121 } 122 } 123 }); 124 } 125 126 protected void paintComponent(Graphics g) { 127 super.paintComponent(g); 128 x = (int) (Math.random() * width); 129 y = (int) (Math.random() * height) + 30; 130 if ((x < width - 2 * r) && (y < height - 2 * r)) { 131 g.setColor(dc); 132 g.fillOval(x, y, r, r); 133 } else { 134 repaint(); 135 } 136 } 137 138 int Count() { 139 i += 100; 140 return i; 141 } 142} 143 144class MyPanel3 extends JPanel { 145 146 public MyPanel3() { 147 setBackground(new Color(0,0,0,100)); 148 } 149 150 public void paintComponent(Graphics g) { 151 super.paintComponent(g); 152 g.setColor(Color.white); 153 g.drawString("GAME OVER", 100, 200); 154 } 155 }

投稿2018/06/22 20:54

編集2018/06/22 20:59
akirasada1972

総合スコア41

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

maybe request doesn't received by paintcomponent

投稿2018/06/22 18:18

AliHassan

総合スコア351

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問