Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 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 niewiemco1;
  7. import javax.swing.*;
  8. import java.util.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.Timer;
  12. import javax.swing.event.*;
  13.  
  14. public class NieWiemCo1 {
  15.  
  16.  
  17. public static void main(String[] args) {
  18. // TODO code application logic here
  19. Okno o = new Okno("Test", 200, 100, 800, 800, Color.gray);
  20. }
  21.  
  22. }
  23.  
  24. class Okno extends JFrame
  25. {
  26. private Container tlo;
  27.  
  28. public Okno(String tyt, int x, int y, int szer, int wys, Color t)
  29. {
  30. //budujemy okno
  31. super(tyt);
  32. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  33. setBounds(x,y,szer,wys);
  34. tlo = getContentPane();
  35. tlo.setBackground(t);
  36. BudujUI(); //buduje user interface
  37. setVisible(true);
  38. }
  39.  
  40.  
  41. private void BudujUI()
  42. {
  43. //tworzymy panel
  44. final MojPanel mp = new MojPanel(Color.yellow, Color.black);
  45. tlo.setLayout(new BorderLayout());
  46. JPanel gorny = new JPanel();
  47. gorny.setBackground(Color.blue);
  48.  
  49. //robimy przyciski i dodajemy listenery
  50. JButton Start = new JButton("Start");
  51. Start.addActionListener(new ActionListener() {
  52. @Override
  53. public void actionPerformed(ActionEvent e)
  54. {
  55. mp.zegarOn();
  56. }
  57. });
  58.  
  59. JButton Stop = new JButton("Stop");
  60. Stop.addActionListener(new ActionListener() {
  61. @Override
  62. public void actionPerformed(ActionEvent e)
  63. {
  64. mp.zegarOff();
  65. }
  66. });
  67.  
  68. JButton Koniec = new JButton("Koniec");
  69. Koniec.addActionListener(new ActionListener() {
  70. @Override
  71. public void actionPerformed(ActionEvent e)
  72. {
  73. mp.zegarOff();
  74. dispose();
  75. }
  76. });
  77.  
  78. JButton Dodaj = new JButton("Dodaj");
  79. Dodaj.addActionListener(new ActionListener() {
  80. @Override
  81. public void actionPerformed(ActionEvent e)
  82. {
  83. mp.addBall();
  84. }
  85. });
  86.  
  87. JButton Usun = new JButton("Usun");
  88. Usun.addActionListener(new ActionListener() {
  89. @Override
  90. public void actionPerformed(ActionEvent e)
  91. {
  92. mp.deleteBall();
  93. }
  94. });
  95.  
  96. JButton disco = new JButton("disco");
  97. disco.addActionListener(new ActionListener() {
  98. @Override
  99. public void actionPerformed(ActionEvent e)
  100. {
  101. mp.discoSwitch();
  102. }
  103. });
  104.  
  105.  
  106.  
  107. class SliderPanel extends JComponent {
  108. JSlider slider;
  109. JLabel label;
  110.  
  111. SliderPanel() {
  112. setLayout(new BorderLayout());
  113.  
  114. slider = new JSlider(0,70,25);
  115. slider.addChangeListener(new ChangeListener() {
  116. @Override
  117. public void stateChanged(ChangeEvent e)
  118. {
  119. mp.changeDelay(slider.getValue());
  120. }
  121. });
  122.  
  123. label = new JLabel("Prędkość");
  124.  
  125. add(label,BorderLayout.CENTER);
  126. add(slider,BorderLayout.SOUTH);
  127.  
  128. }
  129. }
  130.  
  131. SliderPanel sliderPanel = new SliderPanel();
  132.  
  133. gorny.add(sliderPanel);
  134. gorny.add(disco);
  135. gorny.add(Start);
  136. gorny.add(Stop);
  137. gorny.add(Koniec);
  138. gorny.add(Dodaj);
  139. gorny.add(Usun);
  140. //gorny.add(slider);
  141.  
  142. tlo.add(gorny,BorderLayout.NORTH);
  143. tlo.add(mp,BorderLayout.CENTER);
  144.  
  145. }
  146. }
  147.  
  148. class MojPanel extends JPanel
  149. {
  150. class Ball
  151. {
  152. public int x, y, vx, vy, r;
  153. Ball(int ax, int ay, int avx, int avy, int ar)
  154. {
  155. x = ax;
  156. y = ay;
  157. vx = avx;
  158. vy = avy;
  159. r = ar;
  160. }
  161.  
  162. }
  163.  
  164. ArrayList<Ball> ball = new ArrayList<Ball>();
  165. Color[] col = {Color.black, Color.blue, Color.cyan, Color.red, Color.magenta, Color.green, Color.orange, Color.gray};
  166. int ballSize;
  167. protected int red, blue, green, count;
  168. Random rand = new Random();
  169. boolean disco = false;
  170.  
  171. private Timer tim;
  172. int wid, hei;
  173. private Color k_tlo, k_ramka;
  174. public MojPanel(Color ak_tlo, Color ak_ramka)
  175. {
  176. super();
  177. wid=200;
  178. hei=200;
  179. addStartingBalls();
  180.  
  181. k_tlo = ak_tlo;
  182. k_ramka = ak_ramka;
  183. setBackground(k_tlo);
  184. Random rand = new Random();
  185. int r = rand.nextInt(12);
  186. //Timer z akcją co sek
  187. tim = new Timer(10,new ActionListener()
  188. {
  189. @Override
  190. public void actionPerformed(ActionEvent e)
  191. {
  192. //sprawdzenie czy nie ma sciany ani innych obiektow
  193. checkMove();
  194. checkObjects();
  195.  
  196. //rysowanie w innym miejscu
  197. repaint();
  198. }
  199. });
  200.  
  201. }
  202.  
  203. @Override
  204. public void paintComponent(Graphics g)
  205. {
  206. super.paintComponent(g);
  207. wid = getWidth();
  208. hei = getHeight();
  209. ballSize = ball.size();
  210. int j;
  211. //g.setColor(Color.black);
  212. // g.drawString(""+ballSize,100,100);
  213. if (disco) {
  214. count++;
  215. for(int i = 0; i< ballSize; i++) {
  216. j=(i+(count/7))%col.length;
  217. g.setColor(col[j]);
  218. g.fillOval(ball.get(i).x-ball.get(i).r,ball.get(i).y-ball.get(i).r,2*ball.get(i).r,2*ball.get(i).r);
  219. //setBackground(col[j]);
  220. }
  221. }
  222. else {
  223. for(int i = 0; i< ballSize; i++) {
  224. j=(i+(count/7))%col.length;
  225. g.setColor(col[j]);
  226. g.fillOval(ball.get(i).x-ball.get(i).r,ball.get(i).y-ball.get(i).r,2*ball.get(i).r,2*ball.get(i).r);}
  227. }
  228.  
  229.  
  230. }
  231.  
  232. public void addStartingBalls()
  233. {
  234. addBall(150, 150, 1, 1, 30);
  235. addBall(250, 250, 2, 1, 40);
  236. addBall(350, 150, 1, 2, 30);
  237. addBall(150, 350, 2, 2, 50);
  238. addBall(200, 200, 1, 1, 20);
  239. addBall(300, 200, 1, 1, 25);
  240. addBall();
  241. addBall();
  242. addBall();
  243. }
  244.  
  245. public void addBall(int x, int y, int vx, int vy, int r) {
  246. ball.add(new Ball(x, y, vx, vy, r));
  247. }
  248.  
  249. public void addBall() {
  250. if(ball.size()<20){
  251. Random rand = new Random();
  252. int r = rand.nextInt(31)+20;
  253. int x = rand.nextInt(wid-r)+r;
  254. int y = rand.nextInt(hei-r)+r;
  255. int vx = rand.nextInt(2)+1;
  256. int yx = rand.nextInt(2)+1;
  257.  
  258.  
  259. addBall(x,y,vx,yx,r);
  260. int lastBall = ball.size()-1;
  261. if (checkCollision(lastBall)||checkBorderXCollision(lastBall)||checkBorderYCollision(lastBall)) {
  262. deleteBall();
  263. addBall();
  264. }
  265. }
  266. else
  267. JOptionPane.showMessageDialog(null,"Ograniczenie do 20 kulek","Ogranieczenie",JOptionPane.ERROR_MESSAGE);
  268. }
  269.  
  270. public void deleteBall() {
  271. if(ball.size()==0)
  272. JOptionPane.showMessageDialog(null,"Nie ma kulek do usuniecia","Brak kul",JOptionPane.ERROR_MESSAGE);
  273. else
  274. ball.remove(ball.size()-1);
  275. }
  276.  
  277. public void checkMove()
  278. {
  279. //sprawdzanie kolizjii i zmiana wektorow ze scianami
  280. for(int i = 0; i < ball.size(); i++)
  281. {
  282. if(checkBorderXCollision(i))
  283. {
  284. ball.get(i).vx = -ball.get(i).vx;
  285.  
  286. }
  287.  
  288. if(checkBorderYCollision(i))
  289. {
  290. ball.get(i).vy = -ball.get(i).vy;
  291. }
  292. moveBalls(i);
  293. }
  294. }
  295.  
  296. private boolean checkBorderXCollision (int i) {
  297. if ( ball.get(i).x >= (wid - ball.get(i).r) || ball.get(i).x <= ball.get(i).r) return true;
  298. else return false;
  299. }
  300.  
  301. private boolean checkBorderYCollision (int i) {
  302. if( ball.get(i).y >= (hei - ball.get(i).r) || ball.get(i).y <= ball.get(i).r ) return true;
  303. else return false;
  304. }
  305.  
  306. public void checkObjects()
  307. {
  308. //sprawdza kolizje z innymi objektami
  309. for(int j = 0; j < ball.size(); j++)
  310. {
  311. if (checkCollision(j)) {
  312. ball.get(j).vx = -ball.get(j).vx;
  313. ball.get(j).vy = -ball.get(j).vy;
  314. }
  315. }
  316. }
  317.  
  318. public boolean checkCollision(int j) {
  319. for(int k = 0; k < ball.size(); k++)
  320. {
  321. if (k==j) continue;
  322.  
  323. if(pythagoras(ball.get(j).x, ball.get(j).y, ball.get(k).x, ball.get(k).y, ball.get(k).r, ball.get(j).r))
  324. {
  325. return true;
  326. }
  327. }
  328. return false;
  329. }
  330.  
  331. public boolean pythagoras(int ax, int ay, int bx, int by, int ar, int br)
  332. {
  333. //z pitagorasa
  334. if(Math.sqrt((bx-ax)*(bx-ax)+(by-ay)*(by-ay)) <= ar+ br)
  335. {return true;}
  336. else
  337. {return false;}
  338. }
  339.  
  340. private void moveBalls(int i) {
  341. ball.get(i).x += ball.get(i).vx;
  342. ball.get(i).y += ball.get(i).vy;
  343. }
  344.  
  345. public void zegarOn()
  346. {
  347. tim.start();
  348. }
  349. public void zegarOff()
  350. {
  351. tim.stop();
  352. }
  353.  
  354. public void changeDelay(int speed)
  355. {
  356. int delay = 70;
  357. tim.setDelay(delay-speed);
  358. }
  359.  
  360.  
  361. public void discoSwitch() {
  362. disco = !disco;
  363. }
  364.  
  365.  
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement