Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.74 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import javax.swing.filechooser.*;
  8. import javax.swing.filechooser.FileFilter;
  9.  
  10. public class inlupp2 extends JFrame {
  11.  
  12.     private JComboBox<String> boxen;
  13.     private String[] meny = { "Named", "Described" };
  14.     private String[] kategoriVal = { "Buss", "Tunnelbana", "Tåg" };
  15.     private JTextField sökText;
  16.     private JFileChooser jfc = new JFileChooser("Desktop");
  17.     private BildPanel bp = null;
  18.     private JScrollPane scroll = null;
  19.     private platsLyssna platsLyssna = new platsLyssna();
  20.     private MusLyssna musLyssna = new MusLyssna();
  21.     private JList<String> kategori = new JList<String>(kategoriVal);
  22.     private Map<Position, Plats> platser = new HashMap<>();
  23.     private Plats tempPlats = null;
  24.     private ArrayList<Plats> positioner = new ArrayList<>();
  25.  
  26.     public inlupp2() {
  27.         super("inlupp 2");
  28.  
  29.         FileFilter bildFilter = new FileNameExtensionFilter("Bilder", "jpg", "gif", "png", "txt");
  30.         jfc.setFileFilter(bildFilter);
  31.  
  32.         // menyfält
  33.         JMenuBar jmb = new JMenuBar();
  34.         setJMenuBar(jmb);
  35.         JMenu archive = new JMenu("Archive");
  36.         jmb.add(archive);
  37.         JMenuItem newMap = new JMenuItem("New Map");
  38.         archive.add(newMap);
  39.         newMap.addActionListener(new öppnaLyssna());
  40.         JMenuItem loadPlaces = new JMenuItem("Load Places");
  41.         archive.add(loadPlaces);
  42.         loadPlaces.addActionListener(new laddaLyssna());
  43.         JMenuItem save = new JMenuItem("Save");
  44.         archive.add(save);
  45.         JMenuItem exit = new JMenuItem("Exit");
  46.         archive.add(exit);
  47.  
  48.         // archive.addActionListener();
  49.  
  50.         // norra panelen
  51.         JPanel norra = new JPanel();
  52.         add(norra, BorderLayout.NORTH);
  53.         JLabel ny = new JLabel("New:");
  54.         norra.add(ny);
  55.         boxen = new JComboBox<String>(meny);
  56.         boxen.setSelectedIndex(0);
  57.         norra.add(boxen);
  58.         boxen.addActionListener(new nyLyssna());
  59.  
  60.         sökText = new JTextField(10);
  61.         norra.add(sökText);
  62.         JButton sök = new JButton("Sök");
  63.         norra.add(sök);
  64.         sök.addActionListener(new sökLyssna());
  65.         JButton göm = new JButton("Hide");
  66.         norra.add(göm);
  67.         göm.addActionListener(new gömLyssna());
  68.         JButton taBort = new JButton("Remove");
  69.         norra.add(taBort);
  70.         taBort.addActionListener(new taBortLyssna());
  71.         JButton whatIsHere = new JButton("What is here");
  72.         norra.add(whatIsHere);
  73.         whatIsHere.addActionListener(new whatIsHereLyssna());
  74.  
  75.         // högra panelen
  76.         JPanel högra = new JPanel();
  77.         högra.setLayout(new BoxLayout(högra, BoxLayout.Y_AXIS));
  78.         add(högra, BorderLayout.EAST);
  79.         högra.add(new JLabel("Categories"));
  80.         kategori.setFixedCellWidth(50);
  81.         kategori.setVisibleRowCount(10);
  82.         kategori.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  83.         JScrollPane scroll = new JScrollPane(kategori);
  84.         högra.add(scroll);
  85.         kategori.addListSelectionListener(new kategorierLyssna());
  86.         JButton gömKategorier = new JButton("Hide category");
  87.         högra.add(gömKategorier);
  88.         gömKategorier.addActionListener(new gömKategorierLyssna());
  89.  
  90.         // GUI
  91.         setSize(600, 300);
  92.         pack();
  93.         setLocationRelativeTo(null);
  94.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  95.         setVisible(true);
  96.     }
  97.  
  98.     class MusLyssna extends MouseAdapter {
  99.         public void mouseClicked(MouseEvent mev) {
  100.             int x = mev.getX();
  101.             int y = mev.getY();
  102.             System.out.println(x + " " + y);
  103.             registrera(x, y);
  104.         }
  105.  
  106.         private void registrera(int x, int y) {
  107.             Format f = new Format();
  108.             String val = (String) (boxen.getSelectedItem());
  109.             switch (val) {
  110.             case "Named":
  111.                 f = new Format(null);
  112.                 break;
  113.             case "Described":
  114.                 f = new Format("");
  115.                 break;
  116.             }
  117.             int svar = JOptionPane.showConfirmDialog(inlupp2.this, f, "Registrera", JOptionPane.OK_CANCEL_OPTION);
  118.             if (svar != JOptionPane.OK_OPTION) {
  119.                 avbryt();
  120.                 return;
  121.             }
  122.             if (f.getNamn().equals("")) {
  123.                 JOptionPane.showMessageDialog(inlupp2.this, "Inget namn!", "Fel!", JOptionPane.ERROR_MESSAGE);
  124.                 avbryt();
  125.                 return;
  126.             }
  127.             if (!platser.containsKey(f.getNamn())) {
  128.                 if (val.equals("Named")) {
  129.                     if (kategori.getSelectedValue() == null) {
  130.                         Plats plats = new Plats(f.getNamn(), x, y, "");
  131.                         regAvslut(plats);
  132.                     } else {
  133.                         Plats plats = new Plats(f.getNamn(), x, y, kategori.getSelectedValue());
  134.                         regAvslut(plats);
  135.                     }
  136.                 }
  137.                 if (val.equals("Described")) {
  138.                     if (kategori.getSelectedValue() == null) {
  139.                         BeskrivnaPlatser plats = new BeskrivnaPlatser(f.getNamn(), x, y, "", f.getBeskrivningsFält());
  140.                         regAvslut(plats);
  141.                     } else {
  142.                         BeskrivnaPlatser plats = new BeskrivnaPlatser(f.getNamn(), x, y, kategori.getSelectedValue(),
  143.                                 f.getBeskrivningsFält());
  144.                         regAvslut(plats);
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.  
  150.         private void regAvslut(Plats plats) {
  151.             Position position = plats.getPosition();
  152.             plats.addMouseListener(new platsLyssna());
  153.             platser.put(position, plats);
  154.             System.out.println(platser.get(position));
  155.             bp.add(plats);
  156.             bp.validate();
  157.             bp.repaint();
  158.             avbryt();
  159.         }
  160.  
  161.         private void avbryt() {
  162.             bp.removeMouseListener(musLyssna);
  163.             bp.setCursor(Cursor.getDefaultCursor());
  164.             boxen.setEnabled(true);
  165.         }
  166.  
  167.     }
  168.  
  169.     class nyLyssna implements ActionListener {
  170.         public void actionPerformed(ActionEvent ave) {
  171.             if (!(bp == null)) {
  172.                 bp.addMouseListener(musLyssna);
  173.                 bp.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  174.                 boxen.setEnabled(false);
  175.                 return;
  176.             }
  177.         }
  178.     }
  179.  
  180.     class öppnaLyssna implements ActionListener {
  181.         public void actionPerformed(ActionEvent e) {
  182.             int svar = jfc.showOpenDialog(inlupp2.this);
  183.             if (svar != JFileChooser.APPROVE_OPTION)
  184.                 return;
  185.             File fil = jfc.getSelectedFile();
  186.             String filnamn = fil.getAbsolutePath();
  187.             if (scroll != null)
  188.                 remove(scroll);
  189.             bp = new BildPanel(filnamn);
  190.             bp.setLayout(null);
  191.             scroll = new JScrollPane(bp);
  192.             add(scroll);
  193.             pack();
  194.             validate();
  195.             repaint();
  196.         }
  197.     }
  198.  
  199.     class laddaLyssna implements ActionListener {
  200.         public void actionPerformed(ActionEvent ave) {
  201.             FileFilter bildFilter = new FileNameExtensionFilter("png", "txt");
  202.             jfc.setFileFilter(bildFilter);
  203.             int svar = jfc.showOpenDialog(inlupp2.this);
  204.             if (svar != JFileChooser.APPROVE_OPTION)
  205.                 return;
  206.             File fil = jfc.getSelectedFile();
  207.             String filnamn = fil.getAbsolutePath();
  208.             try {
  209.                 FileReader infil = new FileReader(filnamn);
  210.                 BufferedReader in = new BufferedReader(infil);
  211.                 String line;
  212.                 while ((line = in.readLine()) != null) {
  213.                     String[] tokens = line.split(",");
  214.                     String kategori = tokens[1];
  215.                     int x = Integer.parseInt(tokens[2]);
  216.                     int y = Integer.parseInt(tokens[3]);
  217.                     String namn = tokens[4];
  218.                     Plats plats = new Plats(namn, x, y, kategori);
  219.                     Position pos = plats.getPosition();
  220.                     platser.put(pos, plats);
  221.                     System.out.println(platser.get(pos));
  222.                     plats.addMouseListener(new platsLyssna());
  223.                     bp.add(plats);
  224.                     bp.repaint();
  225.                 }
  226.                 in.close();
  227.                 infil.close();
  228.             } catch (FileNotFoundException e) {
  229.                 System.err.println("kan inte öppna ");
  230.             } catch (IOException e) {
  231.                 System.err.println("Fel: " + e.getMessage());
  232.             }
  233.         }
  234.     }
  235.  
  236.     class SparaLyssna implements ActionListener {
  237.         public void actionPerformed(ActionEvent ave) {
  238.             try {
  239.                 FileOutputStream utfil = new FileOutputStream("test.txt");
  240.                 ObjectOutputStream oos = new ObjectOutputStream(utfil);
  241.                 oos.writeObject(platser);
  242.  
  243.                 // FileWriter utfil = new FileWriter("test.reg");
  244.                 // PrintWriter out = new PrintWriter(utfil);
  245.                 // for(Position p: platser.values())
  246.                 // out.println(p.getName()+","+p.getX()+","+p.getY()+","+p.getKategori()+","+p.getBeskrivning());
  247.                 // out.close();
  248.                 // utfil.close();
  249.             } catch (FileNotFoundException e) {
  250.                 JOptionPane.showMessageDialog(null, "Kan inte öppna filen");
  251.             } catch (IOException e) {
  252.                 JOptionPane.showMessageDialog(null, "Fel: " + e.getMessage());
  253.             }
  254.         }
  255.     }
  256.  
  257.     class BildPanel extends JPanel {
  258.         private ImageIcon bild;
  259.  
  260.         public BildPanel(String filnamn) {
  261.             bild = new ImageIcon(filnamn);
  262.             int w = bild.getIconWidth();
  263.             int h = bild.getIconHeight();
  264.             setPreferredSize(new Dimension(w, h));
  265.         }
  266.  
  267.         protected void paintComponent(Graphics g) {
  268.             super.paintComponent(g);
  269.             g.drawImage(bild.getImage(), 0, 0, this);
  270.         }
  271.     }
  272.  
  273.     class sökLyssna implements ActionListener {
  274.         public void actionPerformed(ActionEvent arg0) {
  275.             String namn = sökText.getText();
  276.             for (Plats plats : platser.values()) {
  277.                 plats.setVald(false);
  278.                 positioner.clear();
  279.             }
  280.             for (Plats plats : platser.values()) {
  281.                 if (plats.getNamn().equals(namn)) {
  282.                     System.out.println(plats);
  283.                     tempPlats = plats;
  284.                     positioner.add(tempPlats);
  285.                     tempPlats.setVald(true);
  286.                     tempPlats.repaint();
  287.                     if (!bp.equals(tempPlats)) {
  288.                         bp.add(tempPlats);
  289.                         bp.repaint();
  290.                     }
  291.                 }
  292.             }
  293.         }// sök
  294.     }
  295.  
  296.     class gömLyssna implements ActionListener {
  297.         public void actionPerformed(ActionEvent arg0) {
  298.             System.out.println(platser.get(tempPlats) + " har tagits bort från kartan");
  299.             for (Plats p : positioner) {
  300.                 bp.remove(p);
  301.                 bp.repaint();
  302.             }
  303.             positioner.clear();
  304.         }
  305.     }
  306.  
  307.     class taBortLyssna implements ActionListener {
  308.         public void actionPerformed(ActionEvent arg0) {
  309.             System.out.println(platser.get(tempPlats) + " Borttaget");
  310.             for (Plats p : positioner) {
  311.                 bp.remove(p);
  312.                 p.removeMouseListener(platsLyssna);
  313.                 platser.remove(p);
  314.                 bp.validate();
  315.                 bp.repaint();
  316.             }
  317.             positioner.clear();
  318.         }
  319.     }
  320.  
  321.     class whatIsHereLyssna implements ActionListener {
  322.         public void actionPerformed(ActionEvent ave) {
  323.             System.out.println("Du har tryckt på What is here?");
  324.             // temp
  325.  
  326.         }
  327.  
  328.     }
  329.  
  330.     class kategorierLyssna implements ListSelectionListener {
  331.         public void valueChanged(ListSelectionEvent lse) {
  332.             if (!lse.getValueIsAdjusting()) {
  333.                 String val = kategori.getSelectedValue();
  334.                 System.out.println(val);
  335.                 for (Plats plats : platser.values()) {
  336.                     plats.setVald(false);
  337.                     positioner.clear();
  338.                 }
  339.                 for (Plats plats : platser.values()) {
  340.                     if (plats.getKategori().equals(val)) {
  341.                         tempPlats = plats;
  342.                         positioner.add(tempPlats);
  343.                         tempPlats.setVald(true);
  344.                         tempPlats.repaint();
  345.                         if (!bp.equals(tempPlats)) {
  346.                             bp.add(tempPlats);
  347.                             bp.repaint();
  348.                         }
  349.                     }
  350.                 }
  351.             }
  352.         }
  353.     }
  354.  
  355.     class platsLyssna extends MouseAdapter {
  356.         public void mouseClicked(MouseEvent mev) {
  357.             mev.getSource().toString();
  358.             tempPlats = (Plats) mev.getSource();
  359.             if (mev.getButton() == MouseEvent.BUTTON1) {
  360.                 while (true) {
  361.                     if (!positioner.contains(tempPlats)) {
  362.                         tempPlats.setVald(true);
  363.                         positioner.add(tempPlats);
  364.                         tempPlats.repaint();
  365.                         break;
  366.                     }
  367.                     if (positioner.contains(tempPlats)) {
  368.                         positioner.remove(tempPlats);
  369.                         tempPlats.setVald(false);
  370.                         tempPlats.repaint();
  371.                         break;
  372.                     }
  373.                 }
  374.             }
  375.             if (mev.getButton() == MouseEvent.BUTTON3) {
  376.                 while (true) {
  377.                     if (!tempPlats.getUtvik()) {
  378.                         tempPlats.setUtvik(true);
  379.                         tempPlats.repaint();
  380.                         System.out.println("Utvik: " + tempPlats.getUtvik());
  381.                         break;
  382.                     }
  383.                     if (tempPlats.getUtvik()) {
  384.                         tempPlats.setUtvik(false);
  385.                         tempPlats.repaint();
  386.                         System.out.println("Utvik: " + tempPlats.getUtvik());
  387.                         break;
  388.                     }
  389.                 }
  390.             }
  391.         }
  392.     }
  393.  
  394.     class gömKategorierLyssna implements ActionListener {
  395.         public void actionPerformed(ActionEvent arg0) {
  396.             for (Plats plats : platser.values()) {
  397.                 platser.get(plats.getPosition());
  398.                 if (kategori.getSelectedValue().equals(plats.getKategori())) {
  399.                     System.out.println(plats.getPosition() + " har gömts");
  400.                     bp.remove(plats);
  401.                     bp.repaint();
  402.                 }
  403.             }
  404.         }// gömKategorier
  405.     }
  406.  
  407.     public static void main(String[] args) {
  408.         new inlupp2();
  409.     }
  410.  
  411. }
  412.  
  413. import java.awt.Color;
  414. import java.awt.Graphics;
  415. import java.awt.Polygon;
  416. import java.sql.PseudoColumnUsage;
  417.  
  418. import javax.swing.*;
  419.  
  420. public class Plats extends JComponent {
  421.    
  422.     private String kategori;
  423.     private String namn;
  424.     private Position position;
  425.     private boolean vald;
  426.     private boolean utvik;
  427.     private int[] xes = { 0, 15, 30 };
  428.     private int[] yes = { 0, 30, 0 };
  429.     private Polygon triangle = new Polygon(xes, yes, 3);
  430.    
  431.     public Plats(String namn, int x, int y, String kategori) {
  432.         position = new Position(x,y);
  433.         this.kategori = kategori;
  434.         this.namn = namn;
  435.         utvik = false;
  436.         vald = false;
  437.         setBounds(x - 15, y - 30, 30, 30);
  438.         setOpaque(false);
  439.     }
  440.    
  441.     public Position getPosition() {
  442.         return position;
  443.     }
  444.    
  445.     public String getNamn() {
  446.         return namn;
  447.     }
  448.    
  449.     public String getKategori(){
  450.         return kategori;
  451.     }
  452.    
  453.     public String toString() {
  454.         return namn +" "+ kategori + " x:" + position.getX() +" y:"+ position.getY();
  455.     }
  456.    
  457.     protected void paintComponent(Graphics g) {
  458.         super.paintComponent(g);
  459.         if (utvik == false) {
  460.             g.setColor(färg(kategori));
  461.             g.fillPolygon(triangle);
  462.         }
  463.         if (utvik == true) {
  464.             g.setColor(Color.BLACK);
  465.             g.drawString("TEST", position.getX(), position.getY()-20);
  466.         }
  467.         if (vald == true) {
  468.             g.setColor(Color.CYAN);
  469.             g.drawPolygon(triangle);
  470.         }
  471.     }
  472.  
  473.     private Color färg(String typ) {
  474.         switch (typ) {
  475.         case "Tunnelbana":
  476.             return Color.BLUE;
  477.         case "Tåg":
  478.             return Color.GREEN;
  479.         case "Buss":
  480.             return Color.RED;
  481.         case "":
  482.             return Color.BLACK;
  483.         }
  484.         return null;
  485.     }
  486.    
  487.     public boolean getUtvik() {
  488.         return utvik;
  489.     }
  490.  
  491.     public void setVald(boolean vald) {
  492.         this.vald = vald;
  493.     }
  494.    
  495.     public void setUtvik(boolean utvik) {
  496.         this.utvik = utvik;
  497.     }
  498.    
  499. }
  500.  
  501. import java.awt.*;
  502. import javax.swing.*;
  503.  
  504. public class Position {
  505.  
  506.     private int x;
  507.     private int y;
  508.  
  509.     public Position(int x, int y) {
  510.         this.x =x;
  511.         this.y = y;
  512.     }
  513.  
  514.     public int getX() {
  515.         return x;
  516.     }
  517.    
  518.     public int getY() {
  519.         return y;
  520.     }
  521.  
  522.    
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement