Advertisement
Guest User

Untitled

a guest
May 21st, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.45 KB | None | 0 0
  1. package com.company;
  2. import javax.swing.*;
  3. import javax.swing.border.Border;
  4. import javax.swing.filechooser.FileFilter;
  5. import javax.swing.filechooser.FileNameExtensionFilter;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.io.File;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14.  
  15. public class Main extends JFrame {
  16.  
  17.  
  18.     private JPanel västra;
  19.     private JPanel norra;
  20.     private JRadioButton named;
  21.     private JRadioButton describes;
  22.     private JList catList;
  23.     private JButton newButton;
  24.  
  25.     MusLyss muslyss = new MusLyss();
  26.  
  27.  
  28.     private JTextArea display = new JTextArea();
  29.     KartPanel bp = null;
  30.     JScrollPane scroll = null;
  31.     JFileChooser jfc = new JFileChooser(".");
  32.  
  33.     ArrayList<Coordinates> allCoordinates = new ArrayList<Coordinates>();
  34.     ArrayList<Plats> allaPlatser = new ArrayList<Plats>();
  35.     String[] categories = {"Bus", "Underground", "Train"};
  36.  
  37.     private void showErrorMessage() {
  38.         JOptionPane.showMessageDialog(Main.this,
  39.                 "Platsen finns inte.");
  40.     }
  41.  
  42.     private void searchCoordinates(int x, int y) {
  43.         CoordinatesFormulär f = new CoordinatesFormulär();
  44.         int svar = JOptionPane.showConfirmDialog(Main.this, f, "Ny", JOptionPane.OK_CANCEL_OPTION);
  45.         if (svar != JOptionPane.OK_OPTION)
  46.             return;
  47.         for (Coordinates c : allCoordinates) {
  48.             if (c.getX_cord() == x && c.getY_cord() == y) {
  49.                 for (Plats p : allaPlatser) {
  50.                     //p.makeVisible;
  51.                     //p.mark;
  52.                 }
  53.             } else {
  54.                 showErrorMessage();
  55.  
  56.             }
  57.         }
  58.  
  59.     }
  60.  
  61.  
  62.     public Main() {
  63.         super("program");
  64.         setLayout(new BorderLayout());
  65.         JMenuBar mBar = new JMenuBar();
  66.         setJMenuBar(mBar);
  67.  
  68.         JMenu archiveMenu = new JMenu("Archive");
  69.         mBar.add(archiveMenu);
  70.         JMenuItem newMapItem = new JMenuItem("New Map");
  71.         archiveMenu.add(newMapItem);
  72.         newMapItem.addActionListener(new ActionListener() {
  73.             @Override
  74.             public void actionPerformed(ActionEvent e) {
  75.                 JFileChooser jfc = new JFileChooser();
  76.                 int svar = jfc.showOpenDialog(Main.this);
  77.  
  78.                 if (svar != JFileChooser.APPROVE_OPTION)
  79.                     return;
  80.  
  81.                 File file = jfc.getSelectedFile();
  82.                 String fileName = file.getAbsolutePath();
  83.                 if (scroll != null)
  84.                     remove(scroll);
  85.                 bp = new KartPanel(fileName);
  86.                 scroll = new JScrollPane(bp);
  87.                 add(scroll, BorderLayout.CENTER);
  88.                 validate();
  89.                 repaint();
  90.  
  91.  
  92.             }
  93.         });
  94.  
  95.  
  96.         JMenuItem loadItem = new JMenuItem("Load Places");
  97.         archiveMenu.add(loadItem);
  98.         //Här ska en actionlistener in
  99.  
  100.         JMenuItem saveItem = new JMenuItem("Save");
  101.         archiveMenu.add(saveItem);
  102.  
  103.         JMenuItem exitItem = new JMenuItem("Exit");
  104.         archiveMenu.add(exitItem);
  105.  
  106.  
  107.         FileFilter ff = new FileNameExtensionFilter("Bilder", "jpg", "gif", "png");
  108.         jfc.setFileFilter(ff);
  109.  
  110.         //WEST
  111.         västra = new KartPanel("jarvafaltet.png");
  112.         add(västra);
  113.         västra.setLayout(null);
  114.  
  115.  
  116.         //NORRA
  117.         norra = new JPanel();
  118.         add(norra, BorderLayout.NORTH);
  119.         newButton = new JButton("New");
  120.         norra.add(newButton);
  121.  
  122.  
  123.         newButton.addActionListener(new ActionListener() {
  124.             @Override
  125.             public void actionPerformed(ActionEvent e) {
  126.                 västra.addMouseListener(muslyss);
  127.                 newButton.setEnabled(false);
  128.                 västra.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  129.  
  130.  
  131.             }
  132.         });
  133.  
  134.  
  135.         named = new JRadioButton("Named", true);
  136.         norra.add(named);
  137.  
  138.         named.addActionListener(new ActionListener() {
  139.             @Override
  140.             public void actionPerformed(ActionEvent e) {
  141.  
  142.             }
  143.         });
  144.         describes = new JRadioButton("Describes", false);
  145.         norra.add(describes);
  146.  
  147.         describes.addActionListener(new ActionListener() {
  148.             @Override
  149.             public void actionPerformed(ActionEvent e) {
  150.  
  151.             }
  152.         });
  153.  
  154.         ButtonGroup bg = new ButtonGroup();
  155.         bg.add(named);
  156.         bg.add(describes);
  157.  
  158.  
  159.         JTextField searchbar = new JTextField("Search");
  160.         norra.add(searchbar);
  161.         searchbar.setPreferredSize(new Dimension(130, 25));
  162.  
  163.  
  164.         JButton searchButton = new JButton("Search");
  165.         norra.add(searchButton);
  166.  
  167.         searchButton.addActionListener(new ActionListener() {
  168.             @Override
  169.             public void actionPerformed(ActionEvent e) {
  170.  
  171.             }
  172.         });
  173.  
  174.         JButton hideButton = new JButton("Hide");
  175.         norra.add(hideButton);
  176.  
  177.         hideButton.addActionListener(new ActionListener() {
  178.             @Override
  179.             public void actionPerformed(ActionEvent e) {
  180.  
  181.             }
  182.         });
  183.  
  184.         JButton removeButton = new JButton("Remove");
  185.         norra.add(removeButton);
  186.  
  187.         removeButton.addActionListener(new ActionListener() {
  188.             @Override
  189.             public void actionPerformed(ActionEvent e) {
  190.  
  191.             }
  192.         });
  193.  
  194.         JButton coordinatesButton = new JButton("Coordinates");
  195.         norra.add(coordinatesButton);
  196.  
  197.         coordinatesButton.addActionListener(new ActionListener() {
  198.             @Override
  199.             public void actionPerformed(ActionEvent e) {
  200.                 //enterCoordinates();
  201.  
  202.  
  203.             }
  204.         });
  205.  
  206.  
  207.         //högra
  208.         JPanel högra = new JPanel();
  209.         add(högra, BorderLayout.EAST);
  210.         catList = new JList(categories);
  211.         catList.setVisibleRowCount(20);
  212.         catList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  213.         catList.setFixedCellWidth(100);
  214.         högra.add(new JScrollPane(catList));
  215.  
  216.  
  217.         JPanel södra = new JPanel();
  218.  
  219.  
  220.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  221.         setSize(800, 400);
  222.         setLocationRelativeTo(null);
  223.         setVisible(true);
  224.  
  225.     }
  226.  
  227.  
  228.     public static void main(String[] args) {
  229.         new Main();
  230.     }
  231.  
  232.  
  233.     class MusLyss extends MouseAdapter {
  234.         @Override
  235.         public void mouseClicked(MouseEvent mev) {
  236.             int x = mev.getX();
  237.             int y = mev.getY();
  238.  
  239.             if (named.isSelected()) {
  240.                 String namn = JOptionPane.showInputDialog("namn?");
  241.  
  242.                 int listIndex = catList.getSelectedIndex();
  243.                 String kategori;
  244.  
  245.                 switch (listIndex) {
  246.                     case 0:
  247.                         kategori = "bus";
  248.                         break;
  249.                     case 1:
  250.                         kategori = "underground";
  251.                         break;
  252.                     case 2:
  253.                         kategori = "train";
  254.                         break;
  255.                     default:
  256.                         kategori = "";
  257.  
  258.                 }
  259.  
  260.  
  261.                 NamedPlats p = new NamedPlats(x,y,kategori, namn);
  262.                 allaPlatser.add(p);
  263.                 västra.add(p);
  264.                 västra.validate();
  265.                 västra.repaint();
  266.                 västra.removeMouseListener(muslyss);
  267.                 newButton.setEnabled(true);
  268.                 västra.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  269.                 System.out.println(p);
  270.  
  271.             }
  272.  
  273.  
  274.         }
  275.     }
  276.  
  277. }
  278.  
  279. NEW CLASS
  280.  
  281. package com.company;
  282.  
  283. import javax.swing.*;
  284. import java.awt.*;
  285.  
  286.  
  287. public class KartaPanel extends JPanel {
  288.     private ImageIcon bild;
  289.  
  290.     KartPanel(String filnamn){
  291.         bild = new ImageIcon(filnamn);
  292.         int w = bild.getIconWidth();
  293.         int h = bild.getIconHeight();
  294.         setPreferredSize(new Dimension(w, h));
  295.         setMaximumSize(new Dimension(w, h));
  296.         setMinimumSize(new Dimension(w, h));
  297.  
  298.  
  299.  
  300.  
  301.     }
  302.  
  303.     protected void paintComponent(Graphics g){
  304.         super.paintComponent(g);
  305.         g.drawImage(bild.getImage(), 0, 0, this );
  306.  
  307.     }
  308.  
  309.  
  310. }
  311.  
  312. NEW CLASS
  313.  
  314. package com.company;
  315.  
  316. import javax.swing.*;
  317. import java.awt.*;
  318. import java.awt.event.*;
  319.  
  320.  
  321. public abstract class Plats extends JPanel {
  322.  
  323.  
  324.     private Coordinates position;
  325.     private Color color;
  326.     private String kategori;
  327.     private JPanel balk = new JPanel();
  328.     private JTextArea text = new JTextArea();
  329.  
  330.     public Plats(int xCord, int yCord, String kategori) {
  331.  
  332.         this.position = new Coordinates(xCord, yCord);
  333.         this.kategori = kategori;
  334.         if (kategori.equals("bus"))
  335.             this.color = Color.RED;
  336.         else if (kategori.equals("underground"))
  337.             this.color = Color.BLUE;
  338.         else if (kategori.equals("train"))
  339.             this.color = Color.GREEN;
  340.         else
  341.             this.color = Color.BLACK;
  342.  
  343.         setBounds(xCord, yCord, 200, 150);
  344.         setLayout(new BorderLayout());
  345.  
  346.         add(balk, BorderLayout.NORTH);
  347.         add(text, BorderLayout.CENTER);
  348.         balk.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  349.         balk.setPreferredSize(new Dimension(200, 30));
  350.         balk.setBackground(color);
  351.         text.setBackground(color);
  352.         FlyttLyss flyttLyss = new FlyttLyss();
  353.         addMouseMotionListener(flyttLyss);
  354.         addMouseListener(flyttLyss);
  355.         addKeyListener(new PilLyss());
  356.  
  357.  
  358.  
  359.     }
  360.  
  361.     class FlyttLyss extends MouseAdapter {
  362.         private int startX, startY;
  363.  
  364.         @Override
  365.         public void mousePressed(MouseEvent mev) {
  366.             startX = mev.getX();
  367.             startY = mev.getY();
  368.             requestFocusInWindow();
  369.  
  370.  
  371.         }
  372.  
  373.         public void mouseDragged(MouseEvent mev) {
  374.             int x = mev.getX();
  375.             int y = mev.getY();
  376.             setLocation(getX() + x - startX, getY() + y - startY);
  377.         }
  378.  
  379.  
  380.     }
  381.  
  382.  
  383.     class PilLyss extends KeyAdapter {
  384.         @Override
  385.         public void keyPressed(KeyEvent kev) {
  386.             int x = getX();
  387.             int y = getY();
  388.             switch (kev.getKeyCode()) {
  389.                 case KeyEvent.VK_LEFT:
  390.                     x--;
  391.                     break;
  392.                 case KeyEvent.VK_RIGHT:
  393.                     x++;
  394.                     break;
  395.                 case KeyEvent.VK_UP:
  396.                     y--;
  397.                     break;
  398.                 case KeyEvent.VK_DOWN:
  399.                     y++;
  400.                     break;
  401.  
  402.             }
  403.             setLocation(x, y);
  404.  
  405.  
  406.         }
  407.  
  408.  
  409.     }
  410.  
  411.  
  412.  
  413.     @Override
  414.     public String toString() {
  415.         return "Plats{" +
  416.                 "position=" + position +
  417.                 ", color=" + color +
  418.                 ", kategori='" + kategori + '\'' +
  419.                 ", balk=" + balk +
  420.                 ", text=" + text +
  421.                 '}';
  422.     }
  423. }
  424.  
  425. NEW CLASS
  426.  
  427. package com.company;
  428.  
  429. public class NamedPlats extends Plats{
  430.     private String namn;
  431.  
  432.  
  433.     public NamedPlats(int xCord, int yCord, String kategori, String namn) {
  434.         super(xCord, yCord, kategori);
  435.         this.namn = namn;
  436.     }
  437. }
  438.  
  439. NEW CLASS
  440.  
  441. package com.company;
  442.  
  443. public class DescribedPlace extends Plats{
  444.     private String desc;
  445.  
  446.  
  447.     public DescribedPlace(int xCord, int yCord, String kategori, String desc) {
  448.         super(xCord, yCord, kategori);
  449.         this.desc = desc;
  450.     }
  451. }
  452.  
  453. NEW CLASS
  454.  
  455. package com.company;
  456.  
  457. import java.awt.*;
  458. import java.util.UUID;
  459.  
  460. public class Coordinates {
  461.     private int x_cord;
  462.     private int y_cord;
  463.  
  464.     public Coordinates(int x_cord, int y_cord) {
  465.         this.x_cord = x_cord;
  466.         this.y_cord = y_cord;
  467.  
  468.     }
  469.  
  470.     public int hashCode(){
  471.         return  x_cord + y_cord;
  472.  
  473.     }
  474.  
  475.     public boolean equals(Object obj){
  476.         if (obj instanceof Coordinates){
  477.             Coordinates pp = (Coordinates) obj;
  478.             return (pp.x_cord == this.x_cord && pp.y_cord == this.y_cord);
  479.         } else {
  480.             return false;
  481.         }
  482.     }
  483.  
  484.  
  485.  
  486.     public int getX_cord() {
  487.         return x_cord;
  488.     }
  489.  
  490.     public void setX_cord(int x_cord) {
  491.         this.x_cord = x_cord;
  492.     }
  493.  
  494.     public int getY_cord() {
  495.         return y_cord;
  496.     }
  497.  
  498.     public void setY_cord(int y_cord) {
  499.         this.y_cord = y_cord;
  500.     }
  501. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement