Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.table.DefaultTableModel;
- /*====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
- *====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
- *====== Imie:
- *====== Nazwisko:
- *====== Nr indeksu:
- *====== Grupa:
- *====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
- *====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
- */
- class Kolo implements Comparable<Kolo> {
- public String getWaluta() {
- return waluta;
- }
- public void setWaluta(String waluta) throws Exception {
- if(waluta == null || waluta.equals(""))
- throw new Exception();
- this.waluta = waluta;
- }
- public int getWartosc() {
- return wartosc;
- }
- public void setWartosc(int wartosc) throws Exception {
- this.wartosc = wartosc;
- }
- public void setWartosc(String wartosc) throws Exception{
- if (wartosc == null || wartosc.equals("")) {
- setWartosc(0);
- return;
- }
- try {
- setWartosc(Integer.parseInt(wartosc));
- } catch (NumberFormatException e) {
- throw new Exception();
- }
- }
- String waluta;
- int wartosc;
- public Kolo(String waluta, int wartosc) {
- this.waluta = waluta;
- this.wartosc = wartosc;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((waluta == null) ? 0 : waluta.hashCode());
- result = prime * result + wartosc;
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Kolo other = (Kolo) obj;
- if (waluta == null) {
- if (other.waluta != null)
- return false;
- } else if (!waluta.equals(other.waluta))
- return false;
- if (wartosc != other.wartosc)
- return false;
- return true;
- }
- @Override
- public int compareTo(Kolo k) {
- return this.waluta.compareTo(k.waluta);
- }
- } // koniec klasy Kolo
- class Panel extends JPanel implements MouseListener, MouseMotionListener, ActionListener {
- private static final long serialVersionUID = 1L;
- JButton autorButton = new JButton("Autor");
- boolean lewyKlikniety = false;
- boolean prawyKlikniety = false;
- boolean rysuj = true;
- int pozycjaX = -200;
- int pozycjaY = -200;
- Panel() {
- autorButton.addActionListener(this);
- addMouseListener(this);
- addMouseMotionListener(this);
- add(autorButton);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if(rysuj == true) g.drawOval(pozycjaX-25, pozycjaY-25, 50, 50);
- if(prawyKlikniety) {
- g.setColor(Color.RED);
- g.fillOval(pozycjaX-25, pozycjaY-25, 50, 50);
- }
- if(lewyKlikniety) {
- g.setColor(Color.GREEN);
- g.fillOval(pozycjaX-25, pozycjaY-25, 50, 50);
- }
- if(prawyKlikniety && lewyKlikniety) {
- g.setColor(Color.YELLOW);
- g.fillOval(pozycjaX-25, pozycjaY-25, 50, 50);
- }
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- pozycjaX = e.getX();
- pozycjaY = e.getY();
- repaint();
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- pozycjaX = e.getX();
- pozycjaY = e.getY();
- repaint();
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- rysuj = true;
- repaint();
- }
- @Override
- public void mouseExited(MouseEvent e) {
- rysuj = false;
- repaint();
- }
- @Override
- public void mousePressed(MouseEvent e) {
- if(e.getButton() == 1) {
- lewyKlikniety = true;
- }
- if(e.getButton() == 3) {
- prawyKlikniety = true;
- }
- repaint();
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- if(e.getButton() == 1) {
- lewyKlikniety = false;
- }
- if(e.getButton() == 3) {
- prawyKlikniety = false;
- }
- repaint();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- Object scr = e.getSource();
- if(scr == autorButton)
- JOptionPane.showMessageDialog(this, "Imie naziwsko \4");
- }
- } // koniec klasy Panel
- class Okno extends JFrame implements ActionListener, Runnable {
- private static final long serialVersionUID = 1L;
- public static void main(String[] args) {
- new Okno();
- }
- int time = 0;
- JPanel panel = new Panel();
- //Menu
- JMenuBar menuBar = new JMenuBar();
- JMenu menu = new JMenu("Menu");
- JMenuItem autorMenuItem = new JMenuItem("Autor");
- JMenuItem zakonczMenuItem = new JMenuItem("Zakoncz");
- //Zegar
- JButton zerujButton = new JButton("Zeruj");
- JLabel sekundyLabel = new JLabel("Sekudny:");
- JTextField czasTextField = new JTextField(3);
- Thread licznik = new Thread(this);
- //Kolo
- ArrayList<Kolo> kolekcja = new ArrayList<>();
- JLabel walutaLabel = new JLabel("Waluta:");
- JTextField walutaTextField = new JTextField(10);
- JLabel wartoscLabel = new JLabel("Wartość:");
- JTextField wartoscTextField = new JTextField(10);
- JButton dodajKoloButton = new JButton("Dodaj Kolo");
- JButton usunKoloButton = new JButton("Usuń Kolo");
- JButton usunWszystkieButton = new JButton("Usuń Wszystkie");
- //Widok kolekcji
- WidokKolekcji widok = new WidokKolekcji(kolekcja);
- //Sortuj
- JButton sortujWaluteButton = new JButton("Sortuj wg waluty");
- JButton sortujWartoscButton = new JButton("Sortuj wg wartosc");
- Okno() {
- setSize(600,350);
- setTitle("Imie naziwsko");
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- autorMenuItem.addActionListener(this);
- zakonczMenuItem.addActionListener(this);
- zerujButton.addActionListener(this);
- dodajKoloButton.addActionListener(this);
- usunKoloButton.addActionListener(this);
- usunWszystkieButton.addActionListener(this);
- sortujWaluteButton.addActionListener(this);
- sortujWartoscButton.addActionListener(this);
- //Menu
- menuBar.add(menu);
- menu.add(autorMenuItem);
- menu.add(zakonczMenuItem);
- //Licznik
- panel.add(sekundyLabel);
- panel.add(czasTextField);
- czasTextField.setEditable(false);
- panel.add(zerujButton);
- licznik.start();
- //Kolo
- panel.add(walutaLabel);
- panel.add(walutaTextField);
- panel.add(wartoscLabel);
- panel.add(wartoscTextField);
- panel.add(dodajKoloButton);
- panel.add(usunKoloButton);
- panel.add(usunWszystkieButton);
- //Widok kolekcji
- panel.add(widok);
- //Sortuj
- panel.add(sortujWaluteButton);
- panel.add(sortujWartoscButton);
- setContentPane(panel);
- setJMenuBar(menuBar);
- setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent event) {
- Object scr = event.getSource();
- if(scr == autorMenuItem)
- JOptionPane.showMessageDialog(this, "Imie nazwisko \nNumer albumu");
- if(scr == zakonczMenuItem)
- System.exit(0);
- if(scr == zerujButton) {
- time = 0;
- czasTextField.setText(Integer.toString(time));
- }
- if(scr == dodajKoloButton)
- {
- try {
- kolekcja.add(new Kolo(walutaTextField.getText(),Integer.parseInt(wartoscTextField.getText())));
- widok.refresh();
- }
- catch(Exception e) {
- JOptionPane.showMessageDialog(this, "Podaj poprawne wartości!");
- }
- }
- if(scr == usunKoloButton) {
- try {
- kolekcja.remove(new Kolo(walutaTextField.getText(),Integer.parseInt(wartoscTextField.getText())));
- widok.refresh();
- }
- catch(Exception e) {
- JOptionPane.showMessageDialog(this, "Podaj poprawne wartości!");
- }
- }
- if(scr == usunWszystkieButton)
- {
- kolekcja.removeAll(kolekcja);
- widok.refresh();
- }
- if(scr == sortujWaluteButton) {
- Collections.sort((ArrayList<Kolo>) kolekcja, new Comparator<Kolo>() {
- @Override
- public int compare(Kolo b1, Kolo b2) {
- return b1.getWaluta().compareTo(b2.getWaluta());
- }
- });
- widok.refresh();
- }
- if(scr == sortujWartoscButton) {
- Collections.sort((ArrayList<Kolo>) kolekcja, new Comparator<Kolo>() {
- @Override
- public int compare(Kolo b1, Kolo b2) {
- if (b1.getWartosc() < b2.getWartosc())
- return -1;
- if (b1.getWartosc() < b2.getWartosc())
- return 1;
- return 0;
- }
- });
- widok.refresh();
- }
- }
- @Override
- public void run() {
- while(true) {
- czasTextField.setText(Integer.toString(time));
- time++;
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- } // koniec klasy Okno
- class WidokKolekcji extends JScrollPane {
- private static final long serialVersionUID = 1L;
- // W TEJ KLASIE NIE TRZEBA NIC MODYFIKOWAĆ !!!
- private JTable tabela;
- private DefaultTableModel modelTabeli;
- Collection<Kolo> kolekcja;
- /*
- * Do konstruktora należy przekazać referencję na kolekcję, której zawartość ma
- * być wyświetlana na panelu
- */
- WidokKolekcji(Collection<Kolo> kolekcja) {
- String[] kolumny = { "Waluta", "Wartość" };
- modelTabeli = new DefaultTableModel(kolumny, 0);
- tabela = new JTable(modelTabeli);
- tabela.setRowSelectionAllowed(false);
- this.kolekcja = kolekcja;
- setViewportView(tabela);
- setPreferredSize(new Dimension(150, 200));
- setBorder(BorderFactory.createTitledBorder("Koloy"));
- }
- /*
- * Metodę refresh() należy wywołać po każdej modyfikacji zawartości wyświetlanej
- * kolejcji
- */
- void refresh() {
- modelTabeli.setRowCount(0);
- for (Kolo p : kolekcja) {
- String[] s = { p.waluta, "" + p.wartosc };
- modelTabeli.addRow(s);
- }
- }
- } // koniec klasy WidokKolekcji
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement