Advertisement
pojler

Untitled

Dec 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package testquakea;
  7.  
  8. import java.awt.*;
  9. import java.awt.event.ActionEvent;
  10. import javax.swing.Timer;
  11. import java.awt.event.ActionListener;
  12. import java.util.ArrayList;
  13. import javax.swing.*;
  14. import java.util.*;
  15.  
  16. /**
  17. *
  18. * @author Student
  19. */
  20. public class Testquakea {
  21.  
  22. /**
  23. * @param args the command line arguments
  24. */
  25. public static void main(String[] args) {
  26. Okno a1 = new Okno("Quake", 200, 200, 800, 600);
  27. }
  28. }
  29. //----------------------------------
  30. class Okno extends JFrame {
  31. private Container tlo;
  32. private JButton start;
  33. private JButton stop;
  34. private JButton koniec;
  35. private MojPanel cent;
  36. private Timer t;
  37.  
  38. public Okno(String atyt, int ax0, int ay0, int aszer, int awys) {
  39. super(atyt);
  40. setBounds(ax0, ay0, aszer, awys);
  41. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42. tlo = getContentPane();
  43. budujUI();
  44. setVisible(true);
  45. }
  46. private void budujUI() {
  47. tlo.setLayout(new BorderLayout());
  48. JPanel gorny = new JPanel();
  49.  
  50. gorny.setBackground(Color.RED);
  51. gorny.setLayout(new FlowLayout(FlowLayout.CENTER));
  52. cent = new MojPanel();
  53. cent.setLayout(new FlowLayout(FlowLayout.CENTER));
  54.  
  55. tlo.add(BorderLayout.NORTH, gorny);
  56. tlo.add(cent);
  57.  
  58. koniec = new JButton("Koniec");
  59. start = new JButton("Start");
  60. stop = new JButton("Stop");
  61. koniec.addActionListener(new ActionListener() {
  62.  
  63. @Override
  64. public void actionPerformed(ActionEvent ae) {
  65. t.stop();
  66. dispose();
  67. }
  68. });
  69.  
  70. start.addActionListener(new ActionListener()
  71. {
  72. @Override
  73. public void actionPerformed(ActionEvent ae) {
  74. t.start();
  75. }
  76. });
  77.  
  78. stop.addActionListener(new ActionListener()
  79. {
  80. @Override
  81. public void actionPerformed(ActionEvent ae) {
  82. t.stop();
  83. }
  84. });
  85.  
  86.  
  87.  
  88.  
  89. gorny.add(koniec);
  90. gorny.add(start);
  91. gorny.add(stop);
  92.  
  93.  
  94.  
  95. t = new Timer(10, new ActionListener() {
  96.  
  97. @Override
  98. public void actionPerformed(ActionEvent ae) {
  99. cent.poruszkulki();
  100. }
  101. });
  102. }
  103. }
  104. //------------------------------------------------------------------------------
  105. class MojPanel extends JPanel {
  106. ArrayList<Kulka> lista1=new ArrayList<>();
  107.  
  108.  
  109. public MojPanel() {
  110. super();
  111. setBackground(Color.BLACK);
  112. Random r = new Random();
  113.  
  114. for(int i=0;i<50;i++)
  115. {
  116. lista1.add( new Kulka( new Color( r.nextInt(256), r.nextInt(256), r.nextInt(256)),
  117. 100, 100, r.nextInt(30),
  118. r.nextInt(11) - 5, r.nextInt(11) - 5));
  119. }
  120. }
  121. @Override
  122. public void paintComponent(Graphics g) {
  123. super.paintComponent(g);
  124. for(Kulka x:lista1)
  125. x.rysuj(g);
  126.  
  127. /*kulka1.rysuj(g);
  128. kulka2.rysuj(g);
  129. kulka3.rysuj(g);
  130. kulka4.rysuj(g);*/
  131. }
  132.  
  133. public void poruszkulki() {
  134. for(Kulka x:lista1)
  135. {
  136. x.porusz(getWidth(),getHeight());
  137. }
  138. repaint();
  139. /*kulka1.porusz(getWidth(),getHeight());
  140. kulka2.porusz(getWidth(),getHeight());
  141. kulka3.porusz(getWidth(),getHeight());
  142. kulka4.porusz(getWidth(),getHeight());
  143. repaint();*/
  144.  
  145. }
  146. }
  147. //------------------------------------------------------------------------------
  148. class Kulka {
  149. private int x, y, r, vx, vy;
  150. Color a, b;
  151.  
  152. public Kulka(Color aa, int ax, int ay, int ar, int avx, int avy) {
  153. a = aa;
  154. x=ax;
  155. y=ay;
  156. r=ar;
  157. vx=avx;
  158. vy=avy;
  159. }
  160.  
  161.  
  162. public void rysuj(Graphics g) {
  163. g.setColor(a);
  164. g.fillOval(x-r, y-r, 2*r, 2*r);
  165. }
  166.  
  167. public void porusz(int szer, int wys) {
  168. if(x >= szer-r || x <= r) {
  169. vx=-vx;
  170. }
  171. if(y >= wys-r || y <= r) {
  172. vy=-vy;
  173. }
  174. x+=vx;
  175. y+=vy;
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement