“ゲーム開始”のボタンを押すと、ゲームが始まるようにしたいのですが、できません。
java
1 package luna.sexydesign; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 7 class Subthread extends Thread { 8 9 private ScreenToucher toucher1; 10 11 public Subthread(ScreenToucher toucher1) { 12 this.toucher1 = toucher1; 13 } 14 15 @Override 16 public 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 toucher1.setPanel4(); 25 } 26 } 27 28 public 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("Screen Toucher"); 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 48 JPanel p2 = new JPanel(); 49 p2.setBackground(Color.gray); 50 51 JButton btn = new JButton("ゲーム開始"); 52 btn.addActionListener(new ClickAction()); 53 p2.add(btn); 54 55 add(p1, BorderLayout.NORTH); 56 add(p2, BorderLayout.CENTER); 57 58 } 59 60 public void setCount(int count) { 61 p1.setCount(count); 62 } 63 64 public void setPanel4(){ 65 MyPanel4 p4 = new MyPanel4(); 66 add(p4, BorderLayout.CENTER); 67 } 68 69 } 70 71 class MyPanel1 extends JPanel { 72 73 int i; 74 75 private JLabel l1; 76 77 MyPanel1() { 78 JPanel p1 = new JPanel(); 79 80 l1 = new JLabel(); 81 p1.setBackground(Color.green); 82 Integer j = new Integer(i); 83 String text = j.toString(); 84 l1.setText(text); 85 p1.add(l1); 86 87 add(p1); 88 } 89 90 public void setCount(int count) { 91 l1.setText(Integer.toString(count)); 92 } 93 94 } 95 96 class MyPanel3 extends JPanel { 97 98 static int width = 500; 99 static int height = 500; 100 static int i = 0; 101 static int r = 60; 102 static int x; 103 static int y; 104 105 final static Color bc = Color.black; 106 final static Color dc = Color.green; 107 108 private ScreenToucher toucher2; 109 110 public MyPanel3(ScreenToucher toucher2) { 111 setBackground(Color.black); 112 this.toucher2 = toucher2; 113 MouseListener(); 114 repaint(); 115 } 116 117 void MouseListener() { 118 addMouseListener(new MouseAdapter() { 119 public void mousePressed(MouseEvent e) { 120 double mouseX = e.getX(); 121 double mouseY = e.getY(); 122 if (mouseX > x && mouseX < x + 2 * r) { 123 if (mouseY > y && mouseY < y + 2 * r) { 124 repaint(); 125 toucher2.setCount(Count()); 126 } 127 } 128 } 129 }); 130 } 131 132 protected void paintComponent(Graphics g) { 133 super.paintComponent(g); 134 x = (int) (Math.random() * width); 135 y = (int) (Math.random() * height) + 30; 136 if ((x < width - 2 * r) && (y < height - 2 * r)) { 137 g.setColor(dc); 138 g.fillOval(x, y, r, r); 139 } else { 140 repaint(); 141 } 142 } 143 144 int Count() { 145 i += 100; 146 return i; 147 } 148 149 } 150 151 class MyPanel4 extends JPanel { 152 153 public MyPanel4() { 154 setBackground(new Color(0,0,0,100)); 155 } 156 157 public void paintComponent(Graphics g) { 158 super.paintComponent(g); 159 g.setColor(Color.white); 160 g.drawString("GAME OVER", 100, 200); 161 } 162 } 163 164 class ClickAction implements ActionListener { 165 166 @Override 167 public void actionPerformed(ActionEvent e) { 168 /** 169 Subthread thread = new Subthread(this); 170 thread.start(); 171 172 MyPanel3 p3 = new MyPanel3(p1); 173 add(p3, BorderLayout.CENTER); 174 */ 175 } 176 177 } 178 179
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/20 19:54
2018/08/20 20:19 編集
2018/08/20 20:43