Guest User

Untitled

a guest
Jun 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  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 MyPanel2 p2;
  10.  
  11. public Subthread(MyPanel2 p2) {
  12. this.p2 = p2;
  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. p2.setPanel3();
  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. MyPanel2 p2 = new MyPanel2(this);
  48.  
  49. Subthread thread = new Subthread(p2);
  50. thread.start();
  51.  
  52. add(p1, BorderLayout.NORTH);
  53. add(p2, BorderLayout.CENTER);
  54. }
  55.  
  56.  
  57. public void setCount(int count) {
  58. p1.setCount(count);
  59. }
  60.  
  61. }
  62.  
  63. class MyPanel1 extends JPanel {
  64.  
  65. int i;
  66.  
  67. private JLabel jl1;
  68.  
  69. MyPanel1() {
  70. JPanel jp1 = new JPanel();
  71. jl1 = new JLabel();
  72. jp1.setBackground(Color.green);
  73. Integer j = new Integer(i);
  74. String text = j.toString();
  75. jl1.setText(text);
  76. jp1.add(jl1);
  77. add(jp1);
  78. }
  79.  
  80. public void setCount(int count) {
  81. jl1.setText(Integer.toString(count));
  82. }
  83.  
  84. }
  85.  
  86. class MyPanel2 extends JPanel {
  87.  
  88. static int width = 500;
  89. static int height = 500;
  90. static int i = 0;
  91. static int r = 60;
  92. static int x;
  93. static int y;
  94.  
  95. final static Color bc = Color.black;
  96. final static Color dc = Color.green;
  97.  
  98. private ScreenToucher owner;
  99.  
  100. public MyPanel2(ScreenToucher owner) {
  101. setBackground(Color.black);
  102. this.owner = owner;
  103. MouseListener();
  104. repaint();
  105. }
  106.  
  107. void MouseListener() {
  108. addMouseListener(new MouseAdapter() {
  109. public void mousePressed(MouseEvent e) {
  110. double mouseX = e.getX();
  111. double mouseY = e.getY();
  112. if (mouseX > x && mouseX < x + 2 * r) {
  113. if (mouseY > y && mouseY < y + 2 * r) {
  114. repaint();
  115. owner.setCount(Count());
  116. }
  117. }
  118. }
  119. });
  120. }
  121.  
  122. protected void paintComponent(Graphics g) {
  123. super.paintComponent(g);
  124. x = (int) (Math.random() * width);
  125. y = (int) (Math.random() * height) + 30;
  126. if ((x < width - 2 * r) && (y < height - 2 * r)) {
  127. g.setColor(dc);
  128. g.fillOval(x, y, r, r);
  129. } else {
  130. repaint();
  131. }
  132. }
  133.  
  134. int Count() {
  135. i += 100;
  136. return i;
  137. }
  138.  
  139. public void setPanel3(){
  140. MyPanel3 p3 = new MyPanel3();
  141. add(p3);
  142. }
  143.  
  144. }
  145.  
  146. class 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. }
Add Comment
Please, Sign In to add comment