Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.85 KB | None | 0 0
  1. package kulki;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import javax.swing.Timer;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10. import javax.swing.*;
  11. public class JavaApplication38
  12. {
  13. public static void main(String[] args)
  14. {
  15. Okno a1 = new Okno("KOT", 200, 200, 800, 600);
  16.  
  17. }
  18. }
  19. //---------------------------------
  20. class Okno extends JFrame
  21. {
  22. private Container tlo;
  23. private JButton start;
  24. private JButton stop;
  25. private JButton koniec;
  26. private MojPanel cent = new MojPanel();
  27.  
  28. public Okno(String atyt, int ax0, int ay0, int aszer, int awys) {
  29. super(atyt);
  30. setBounds(ax0, ay0, aszer, awys);
  31. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. tlo = getContentPane();
  33. budujUI();
  34. setVisible(true);
  35. }
  36. private void budujUI() {
  37. tlo.setLayout(new BorderLayout());
  38. JPanel gorny = new JPanel();
  39. gorny.setBackground(Color.GRAY);
  40. gorny.setLayout(new FlowLayout(FlowLayout.CENTER));
  41. tlo.add(BorderLayout.NORTH, gorny);
  42.  
  43. // cent.setLayout(new FlowLayout(FlowLayout.CENTER));
  44. cent.setFocusable(true);
  45. cent.setRequestFocusEnabled(true);
  46. cent.addKeyListener(new KeyListener()
  47. {
  48.  
  49. @Override
  50. public void keyTyped(KeyEvent ke)
  51. {
  52. }
  53. @Override
  54. public void keyPressed(KeyEvent ke)
  55. {
  56. int k = ke.getKeyCode();
  57. if(k == KeyEvent.VK_LEFT)
  58. {
  59. cent.p1.wLewo();
  60. cent.repaint();
  61. }
  62.  
  63. if(k == KeyEvent.VK_RIGHT)
  64. {
  65. cent.p1.wPrawo();
  66. cent.repaint();
  67. }
  68. }
  69.  
  70. @Override
  71. public void keyReleased(KeyEvent ke)
  72. {
  73. }
  74. });
  75. tlo.add(cent);
  76.  
  77. koniec = new JButton("END");
  78. start = new JButton("START");
  79. stop = new JButton("STOP");
  80. koniec.addActionListener(new KlikKoniec());
  81. start.addActionListener(new KlikStart());
  82. stop.addActionListener(new KlikStop());
  83.  
  84. gorny.add(koniec);
  85. gorny.add(start);
  86. gorny.add(stop);
  87.  
  88.  
  89. }
  90. class KlikKoniec implements ActionListener
  91. {
  92. @Override
  93. public void actionPerformed(ActionEvent ae) {
  94. cent.zegarStop();
  95. dispose();
  96. }
  97. }
  98. class KlikStart implements ActionListener
  99. {
  100. @Override
  101. public void actionPerformed(ActionEvent ae) {
  102. cent.zegarStart();
  103. cent.requestFocus();
  104. }
  105. }
  106. class KlikStop implements ActionListener
  107. {
  108. @Override
  109. public void actionPerformed(ActionEvent ae) {
  110. cent.zegarStop();
  111. }
  112. }
  113. }
  114.  
  115. //------------------------------------------------------------------------------
  116. class MojPanel extends JPanel
  117. {
  118. private ArrayList <Figura> figury = new ArrayList <Figura>();
  119. private Figura fi = new Figura( Color.RED, Color.GREEN, 200, 300, 50, -1, 2) {};
  120. public Paletka p1 = new Paletka(400, 100, 10, Color.black);
  121. private Timer t;
  122.  
  123. public MojPanel()
  124. {
  125. super();
  126.  
  127.  
  128. Random r = new Random();
  129. for(int i=0; i<5; i++)
  130. {
  131. figury.add(new Kulka(Color.RED,Color.WHITE,r.nextInt(400) + 100,r.nextInt(300) + 100,r.nextInt(50)+10,r.nextInt(21)-10,r.nextInt(21)-10));
  132. figury.add(new Kwadrat(Color.GREEN,Color.WHITE,r.nextInt(400) + 100,r.nextInt(300) + 100,r.nextInt(50)+10,r.nextInt(21)-10,r.nextInt(21)-10));
  133. // figury.add(new Prostokat(Color.BLUE,Color.WHITE,r.nextInt(400) + 100,r.nextInt(300) + 100,r.nextInt(50)+10,r.nextInt(21)-10,r.nextInt(21)-10));
  134.  
  135.  
  136. }
  137.  
  138. t=new Timer(20,new ActionListener()
  139. {
  140. @Override
  141. public void actionPerformed(ActionEvent ae) {
  142. int szer = getWidth(), wys = getHeight();
  143. for(Figura f: figury)
  144. f.porusz( szer, wys);
  145. fi.porusz(szer, wys);
  146. repaint();
  147. }
  148. });
  149. }
  150. @Override
  151. public void paintComponent(Graphics g) {
  152. super.paintComponent(g);
  153. for(Figura f: figury)
  154. f.rysuj(g);
  155. fi.rysuj(g);
  156. p1.rysuj(g, getHeight());
  157. repaint();
  158. }
  159. public void zegarStart()
  160. {
  161. t.start();
  162. }
  163. public void zegarStop()
  164. {
  165. t.stop();
  166. }
  167. }
  168. //------------------------------------------------------------------------------
  169. abstract class Figura
  170. {
  171. Color a, b;
  172. int x, y, r, vx, vy;
  173. public Figura(Color aa, Color ab, int ax, int ay, int ar, int avx, int avy)
  174. {
  175. a = aa;
  176. b = ab;
  177. x=ax;
  178. y=ay;
  179. r=ar;
  180. vx=avx;
  181. vy=avy;
  182. }
  183.  
  184. public void rysuj(Graphics g)
  185. {
  186. g.setColor(a);
  187. g.fillOval(x-r, y-r, 2*r, 2*r);
  188. g.setColor(b);
  189. g.drawOval(x-r, y-r, 2*r, 2*r);
  190. }
  191.  
  192. public void porusz(int szer, int wys)
  193. {
  194. if(x >= szer-r || x <= r)
  195. {
  196. vx=-vx;
  197. }
  198. if(y >= wys-r || y <= r)
  199. {
  200. vy=-vy;
  201. }
  202. x+=vx;
  203. y+=vy;
  204. }
  205.  
  206.  
  207. }
  208. class Kulka extends Figura
  209. {
  210.  
  211. public Kulka(Color aa, Color ab, int ax, int ay, int ar, int avx, int avy) {
  212. super(aa, ab, ax, ay, ar, avx, avy);
  213. }
  214.  
  215. @Override
  216. public void rysuj(Graphics g)
  217. {
  218. g.setColor(a);
  219. g.fillOval(x-r, y-r, 2*r, 2*r);
  220. g.setColor(b);
  221. g.drawOval(x-r, y-r, 2*r, 2*r);
  222. }
  223.  
  224. }
  225.  
  226.  
  227. class Kwadrat extends Figura
  228. {
  229.  
  230. public Kwadrat(Color aa, Color ab, int ax, int ay, int ar, int avx, int avy) {
  231. super(aa, ab, ax, ay, ar, avx, avy);
  232. }
  233.  
  234. @Override
  235. public void rysuj(Graphics g)
  236. {
  237. g.setColor(a);
  238. g.fillRect(x-r, y-r, 2*r, 2*r);
  239. g.setColor(b);
  240. g.drawRect(x-r, y-r, 2*r, 2*r);
  241. }
  242.  
  243. }
  244.  
  245. class Paletka
  246. {
  247. private int x, szer, wys, dx = 10;
  248. private Color k;
  249. public Paletka(int ax, int aszer, int awys, Color ak)
  250. {
  251. x= ax;
  252. szer = aszer;
  253. wys= awys;
  254. k=ak;
  255. }
  256.  
  257.  
  258. public void rysuj(Graphics g, int wysOkna)
  259. {
  260. g.setColor(k);
  261. g.fillRect(x - szer/2, wysOkna - wys, szer, wys);
  262.  
  263. }
  264. public void wLewo()
  265. {
  266. x-=dx;
  267. }
  268. public void wPrawo()
  269. {
  270. x+=dx;
  271. }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement