Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.22 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4.  
  5. import javax.swing.*;
  6. import javax.swing.table.DefaultTableModel;
  7.  
  8. /*====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
  9. *====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
  10. *====== Imie:
  11. *====== Nazwisko:
  12. *====== Nr indeksu:
  13. *====== Grupa:
  14. *====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
  15. *====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
  16. */
  17. class Kolo implements Comparable<Kolo> {
  18.  
  19. public String getWaluta() {
  20. return waluta;
  21. }
  22.  
  23. public void setWaluta(String waluta) throws Exception {
  24. if(waluta == null || waluta.equals(""))
  25. throw new Exception();
  26. this.waluta = waluta;
  27. }
  28.  
  29. public int getWartosc() {
  30. return wartosc;
  31. }
  32.  
  33. public void setWartosc(int wartosc) throws Exception {
  34. this.wartosc = wartosc;
  35. }
  36.  
  37. public void setWartosc(String wartosc) throws Exception{
  38. if (wartosc == null || wartosc.equals("")) {
  39. setWartosc(0);
  40. return;
  41. }
  42. try {
  43. setWartosc(Integer.parseInt(wartosc));
  44. } catch (NumberFormatException e) {
  45. throw new Exception();
  46. }
  47. }
  48.  
  49. String waluta;
  50. int wartosc;
  51.  
  52. public Kolo(String waluta, int wartosc) {
  53. this.waluta = waluta;
  54. this.wartosc = wartosc;
  55. }
  56.  
  57. @Override
  58. public int hashCode() {
  59. final int prime = 31;
  60. int result = 1;
  61. result = prime * result + ((waluta == null) ? 0 : waluta.hashCode());
  62. result = prime * result + wartosc;
  63. return result;
  64. }
  65.  
  66. @Override
  67. public boolean equals(Object obj) {
  68. if (this == obj)
  69. return true;
  70. if (obj == null)
  71. return false;
  72. if (getClass() != obj.getClass())
  73. return false;
  74. Kolo other = (Kolo) obj;
  75. if (waluta == null) {
  76. if (other.waluta != null)
  77. return false;
  78. } else if (!waluta.equals(other.waluta))
  79. return false;
  80. if (wartosc != other.wartosc)
  81. return false;
  82. return true;
  83. }
  84.  
  85. @Override
  86. public int compareTo(Kolo k) {
  87. return this.waluta.compareTo(k.waluta);
  88. }
  89.  
  90. } // koniec klasy Kolo
  91.  
  92. class Panel extends JPanel implements MouseListener, MouseMotionListener, ActionListener {
  93.  
  94. private static final long serialVersionUID = 1L;
  95.  
  96. JButton autorButton = new JButton("Autor");
  97.  
  98. boolean lewyKlikniety = false;
  99. boolean prawyKlikniety = false;
  100. boolean rysuj = true;
  101.  
  102. int pozycjaX = -200;
  103. int pozycjaY = -200;
  104.  
  105. Panel() {
  106.  
  107. autorButton.addActionListener(this);
  108. addMouseListener(this);
  109. addMouseMotionListener(this);
  110.  
  111. add(autorButton);
  112. }
  113.  
  114. @Override
  115. protected void paintComponent(Graphics g) {
  116. super.paintComponent(g);
  117. if(rysuj == true) g.drawOval(pozycjaX-25, pozycjaY-25, 50, 50);
  118.  
  119. if(prawyKlikniety) {
  120. g.setColor(Color.RED);
  121. g.fillOval(pozycjaX-25, pozycjaY-25, 50, 50);
  122. }
  123. if(lewyKlikniety) {
  124. g.setColor(Color.GREEN);
  125. g.fillOval(pozycjaX-25, pozycjaY-25, 50, 50);
  126. }
  127. if(prawyKlikniety && lewyKlikniety) {
  128. g.setColor(Color.YELLOW);
  129. g.fillOval(pozycjaX-25, pozycjaY-25, 50, 50);
  130. }
  131.  
  132. }
  133. @Override
  134. public void mouseDragged(MouseEvent e) {
  135. pozycjaX = e.getX();
  136. pozycjaY = e.getY();
  137. repaint();
  138. }
  139.  
  140. @Override
  141. public void mouseMoved(MouseEvent e) {
  142. pozycjaX = e.getX();
  143. pozycjaY = e.getY();
  144. repaint();
  145. }
  146.  
  147. @Override
  148. public void mouseClicked(MouseEvent e) {
  149.  
  150. }
  151.  
  152. @Override
  153. public void mouseEntered(MouseEvent e) {
  154. rysuj = true;
  155. repaint();
  156. }
  157.  
  158. @Override
  159. public void mouseExited(MouseEvent e) {
  160. rysuj = false;
  161. repaint();
  162. }
  163.  
  164. @Override
  165. public void mousePressed(MouseEvent e) {
  166. if(e.getButton() == 1) {
  167. lewyKlikniety = true;
  168. }
  169. if(e.getButton() == 3) {
  170. prawyKlikniety = true;
  171. }
  172. repaint();
  173. }
  174.  
  175. @Override
  176. public void mouseReleased(MouseEvent e) {
  177. if(e.getButton() == 1) {
  178. lewyKlikniety = false;
  179. }
  180. if(e.getButton() == 3) {
  181. prawyKlikniety = false;
  182. }
  183. repaint();
  184. }
  185.  
  186. @Override
  187. public void actionPerformed(ActionEvent e) {
  188. Object scr = e.getSource();
  189.  
  190. if(scr == autorButton)
  191. JOptionPane.showMessageDialog(this, "Imie naziwsko \4");
  192.  
  193. }
  194.  
  195. } // koniec klasy Panel
  196.  
  197. class Okno extends JFrame implements ActionListener, Runnable {
  198.  
  199. private static final long serialVersionUID = 1L;
  200.  
  201. public static void main(String[] args) {
  202. new Okno();
  203. }
  204.  
  205. int time = 0;
  206.  
  207. JPanel panel = new Panel();
  208.  
  209. //Menu
  210. JMenuBar menuBar = new JMenuBar();
  211. JMenu menu = new JMenu("Menu");
  212. JMenuItem autorMenuItem = new JMenuItem("Autor");
  213. JMenuItem zakonczMenuItem = new JMenuItem("Zakoncz");
  214.  
  215.  
  216. //Zegar
  217. JButton zerujButton = new JButton("Zeruj");
  218. JLabel sekundyLabel = new JLabel("Sekudny:");
  219. JTextField czasTextField = new JTextField(3);
  220.  
  221. Thread licznik = new Thread(this);
  222.  
  223.  
  224. //Kolo
  225. ArrayList<Kolo> kolekcja = new ArrayList<>();
  226.  
  227. JLabel walutaLabel = new JLabel("Waluta:");
  228. JTextField walutaTextField = new JTextField(10);
  229. JLabel wartoscLabel = new JLabel("Wartość:");
  230. JTextField wartoscTextField = new JTextField(10);
  231.  
  232. JButton dodajKoloButton = new JButton("Dodaj Kolo");
  233. JButton usunKoloButton = new JButton("Usuń Kolo");
  234. JButton usunWszystkieButton = new JButton("Usuń Wszystkie");
  235.  
  236. //Widok kolekcji
  237. WidokKolekcji widok = new WidokKolekcji(kolekcja);
  238.  
  239. //Sortuj
  240. JButton sortujWaluteButton = new JButton("Sortuj wg waluty");
  241. JButton sortujWartoscButton = new JButton("Sortuj wg wartosc");
  242.  
  243.  
  244. Okno() {
  245. setSize(600,350);
  246. setTitle("Imie naziwsko");
  247. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  248.  
  249. autorMenuItem.addActionListener(this);
  250. zakonczMenuItem.addActionListener(this);
  251. zerujButton.addActionListener(this);
  252. dodajKoloButton.addActionListener(this);
  253. usunKoloButton.addActionListener(this);
  254. usunWszystkieButton.addActionListener(this);
  255. sortujWaluteButton.addActionListener(this);
  256. sortujWartoscButton.addActionListener(this);
  257.  
  258. //Menu
  259. menuBar.add(menu);
  260. menu.add(autorMenuItem);
  261. menu.add(zakonczMenuItem);
  262.  
  263. //Licznik
  264. panel.add(sekundyLabel);
  265. panel.add(czasTextField);
  266. czasTextField.setEditable(false);
  267. panel.add(zerujButton);
  268.  
  269. licznik.start();
  270.  
  271. //Kolo
  272. panel.add(walutaLabel);
  273. panel.add(walutaTextField);
  274.  
  275. panel.add(wartoscLabel);
  276. panel.add(wartoscTextField);
  277.  
  278. panel.add(dodajKoloButton);
  279. panel.add(usunKoloButton);
  280. panel.add(usunWszystkieButton);
  281.  
  282. //Widok kolekcji
  283. panel.add(widok);
  284.  
  285. //Sortuj
  286. panel.add(sortujWaluteButton);
  287. panel.add(sortujWartoscButton);
  288.  
  289. setContentPane(panel);
  290. setJMenuBar(menuBar);
  291. setVisible(true);
  292. }
  293.  
  294. @Override
  295. public void actionPerformed(ActionEvent event) {
  296. Object scr = event.getSource();
  297. if(scr == autorMenuItem)
  298. JOptionPane.showMessageDialog(this, "Imie nazwisko \nNumer albumu");
  299. if(scr == zakonczMenuItem)
  300. System.exit(0);
  301. if(scr == zerujButton) {
  302. time = 0;
  303. czasTextField.setText(Integer.toString(time));
  304. }
  305. if(scr == dodajKoloButton)
  306. {
  307. try {
  308. kolekcja.add(new Kolo(walutaTextField.getText(),Integer.parseInt(wartoscTextField.getText())));
  309. widok.refresh();
  310. }
  311. catch(Exception e) {
  312. JOptionPane.showMessageDialog(this, "Podaj poprawne wartości!");
  313. }
  314. }
  315. if(scr == usunKoloButton) {
  316. try {
  317. kolekcja.remove(new Kolo(walutaTextField.getText(),Integer.parseInt(wartoscTextField.getText())));
  318. widok.refresh();
  319. }
  320. catch(Exception e) {
  321. JOptionPane.showMessageDialog(this, "Podaj poprawne wartości!");
  322. }
  323. }
  324. if(scr == usunWszystkieButton)
  325. {
  326. kolekcja.removeAll(kolekcja);
  327. widok.refresh();
  328. }
  329. if(scr == sortujWaluteButton) {
  330. Collections.sort((ArrayList<Kolo>) kolekcja, new Comparator<Kolo>() {
  331.  
  332. @Override
  333. public int compare(Kolo b1, Kolo b2) {
  334. return b1.getWaluta().compareTo(b2.getWaluta());
  335. }
  336. });
  337. widok.refresh();
  338. }
  339. if(scr == sortujWartoscButton) {
  340. Collections.sort((ArrayList<Kolo>) kolekcja, new Comparator<Kolo>() {
  341.  
  342. @Override
  343. public int compare(Kolo b1, Kolo b2) {
  344. if (b1.getWartosc() < b2.getWartosc())
  345. return -1;
  346. if (b1.getWartosc() < b2.getWartosc())
  347. return 1;
  348. return 0;
  349. }
  350. });
  351. widok.refresh();
  352. }
  353. }
  354.  
  355. @Override
  356. public void run() {
  357. while(true) {
  358. czasTextField.setText(Integer.toString(time));
  359. time++;
  360. try {
  361. Thread.sleep(1000);
  362. } catch (InterruptedException e) {
  363. e.printStackTrace();
  364. }
  365.  
  366. }
  367. }
  368.  
  369. } // koniec klasy Okno
  370.  
  371. class WidokKolekcji extends JScrollPane {
  372. private static final long serialVersionUID = 1L;
  373.  
  374. // W TEJ KLASIE NIE TRZEBA NIC MODYFIKOWAĆ !!!
  375.  
  376. private JTable tabela;
  377. private DefaultTableModel modelTabeli;
  378. Collection<Kolo> kolekcja;
  379.  
  380. /*
  381. * Do konstruktora należy przekazać referencję na kolekcję, której zawartość ma
  382. * być wyświetlana na panelu
  383. */
  384.  
  385. WidokKolekcji(Collection<Kolo> kolekcja) {
  386. String[] kolumny = { "Waluta", "Wartość" };
  387. modelTabeli = new DefaultTableModel(kolumny, 0);
  388. tabela = new JTable(modelTabeli);
  389. tabela.setRowSelectionAllowed(false);
  390. this.kolekcja = kolekcja;
  391. setViewportView(tabela);
  392. setPreferredSize(new Dimension(150, 200));
  393. setBorder(BorderFactory.createTitledBorder("Koloy"));
  394. }
  395.  
  396. /*
  397. * Metodę refresh() należy wywołać po każdej modyfikacji zawartości wyświetlanej
  398. * kolejcji
  399. */
  400. void refresh() {
  401. modelTabeli.setRowCount(0);
  402. for (Kolo p : kolekcja) {
  403. String[] s = { p.waluta, "" + p.wartosc };
  404. modelTabeli.addRow(s);
  405. }
  406. }
  407. } // koniec klasy WidokKolekcji
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement