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 }
コードだけ丸投げて終わるのではなく、調べてみたこと、やっみたこと、起きていることをきちんと記載してください。
10秒後に、パネルを入れ替えたいのですが、うまく表示されません。プログラム自体は、止まるのですが、ゲームオーバーの画面が、出ません。
質問本文を編集して追記してください。
また、「うまくいかない」だけでは現象が伝わらないので具体的に記載してください。 https://teratail.com/help/question-tips#questionTips3-4-1

回答4件
あなたの回答
tips
プレビュー