Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- import javax.swing.border.Border;
- import javax.swing.filechooser.FileFilter;
- import javax.swing.filechooser.FileNameExtensionFilter;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class Main extends JFrame {
- private JPanel västra;
- private JPanel norra;
- private JRadioButton named;
- private JRadioButton describes;
- private JList catList;
- private JButton newButton;
- MusLyss muslyss = new MusLyss();
- private JTextArea display = new JTextArea();
- KartPanel bp = null;
- JScrollPane scroll = null;
- JFileChooser jfc = new JFileChooser(".");
- ArrayList<Coordinates> allCoordinates = new ArrayList<Coordinates>();
- ArrayList<Plats> allaPlatser = new ArrayList<Plats>();
- String[] categories = {"Bus", "Underground", "Train"};
- private void showErrorMessage() {
- JOptionPane.showMessageDialog(Main.this,
- "Platsen finns inte.");
- }
- private void searchCoordinates(int x, int y) {
- CoordinatesFormulär f = new CoordinatesFormulär();
- int svar = JOptionPane.showConfirmDialog(Main.this, f, "Ny", JOptionPane.OK_CANCEL_OPTION);
- if (svar != JOptionPane.OK_OPTION)
- return;
- for (Coordinates c : allCoordinates) {
- if (c.getX_cord() == x && c.getY_cord() == y) {
- for (Plats p : allaPlatser) {
- //p.makeVisible;
- //p.mark;
- }
- } else {
- showErrorMessage();
- }
- }
- }
- public Main() {
- super("program");
- setLayout(new BorderLayout());
- JMenuBar mBar = new JMenuBar();
- setJMenuBar(mBar);
- JMenu archiveMenu = new JMenu("Archive");
- mBar.add(archiveMenu);
- JMenuItem newMapItem = new JMenuItem("New Map");
- archiveMenu.add(newMapItem);
- newMapItem.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JFileChooser jfc = new JFileChooser();
- int svar = jfc.showOpenDialog(Main.this);
- if (svar != JFileChooser.APPROVE_OPTION)
- return;
- File file = jfc.getSelectedFile();
- String fileName = file.getAbsolutePath();
- if (scroll != null)
- remove(scroll);
- bp = new KartPanel(fileName);
- scroll = new JScrollPane(bp);
- add(scroll, BorderLayout.CENTER);
- validate();
- repaint();
- }
- });
- JMenuItem loadItem = new JMenuItem("Load Places");
- archiveMenu.add(loadItem);
- //Här ska en actionlistener in
- JMenuItem saveItem = new JMenuItem("Save");
- archiveMenu.add(saveItem);
- JMenuItem exitItem = new JMenuItem("Exit");
- archiveMenu.add(exitItem);
- FileFilter ff = new FileNameExtensionFilter("Bilder", "jpg", "gif", "png");
- jfc.setFileFilter(ff);
- //WEST
- västra = new KartPanel("jarvafaltet.png");
- add(västra);
- västra.setLayout(null);
- //NORRA
- norra = new JPanel();
- add(norra, BorderLayout.NORTH);
- newButton = new JButton("New");
- norra.add(newButton);
- newButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- västra.addMouseListener(muslyss);
- newButton.setEnabled(false);
- västra.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- }
- });
- named = new JRadioButton("Named", true);
- norra.add(named);
- named.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- });
- describes = new JRadioButton("Describes", false);
- norra.add(describes);
- describes.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- });
- ButtonGroup bg = new ButtonGroup();
- bg.add(named);
- bg.add(describes);
- JTextField searchbar = new JTextField("Search");
- norra.add(searchbar);
- searchbar.setPreferredSize(new Dimension(130, 25));
- JButton searchButton = new JButton("Search");
- norra.add(searchButton);
- searchButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- });
- JButton hideButton = new JButton("Hide");
- norra.add(hideButton);
- hideButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- });
- JButton removeButton = new JButton("Remove");
- norra.add(removeButton);
- removeButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- }
- });
- JButton coordinatesButton = new JButton("Coordinates");
- norra.add(coordinatesButton);
- coordinatesButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- //enterCoordinates();
- }
- });
- //högra
- JPanel högra = new JPanel();
- add(högra, BorderLayout.EAST);
- catList = new JList(categories);
- catList.setVisibleRowCount(20);
- catList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- catList.setFixedCellWidth(100);
- högra.add(new JScrollPane(catList));
- JPanel södra = new JPanel();
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setSize(800, 400);
- setLocationRelativeTo(null);
- setVisible(true);
- }
- public static void main(String[] args) {
- new Main();
- }
- class MusLyss extends MouseAdapter {
- @Override
- public void mouseClicked(MouseEvent mev) {
- int x = mev.getX();
- int y = mev.getY();
- if (named.isSelected()) {
- String namn = JOptionPane.showInputDialog("namn?");
- int listIndex = catList.getSelectedIndex();
- String kategori;
- switch (listIndex) {
- case 0:
- kategori = "bus";
- break;
- case 1:
- kategori = "underground";
- break;
- case 2:
- kategori = "train";
- break;
- default:
- kategori = "";
- }
- NamedPlats p = new NamedPlats(x,y,kategori, namn);
- allaPlatser.add(p);
- västra.add(p);
- västra.validate();
- västra.repaint();
- västra.removeMouseListener(muslyss);
- newButton.setEnabled(true);
- västra.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
- System.out.println(p);
- }
- }
- }
- }
- NEW CLASS
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- public class KartaPanel extends JPanel {
- private ImageIcon bild;
- KartPanel(String filnamn){
- bild = new ImageIcon(filnamn);
- int w = bild.getIconWidth();
- int h = bild.getIconHeight();
- setPreferredSize(new Dimension(w, h));
- setMaximumSize(new Dimension(w, h));
- setMinimumSize(new Dimension(w, h));
- }
- protected void paintComponent(Graphics g){
- super.paintComponent(g);
- g.drawImage(bild.getImage(), 0, 0, this );
- }
- }
- NEW CLASS
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public abstract class Plats extends JPanel {
- private Coordinates position;
- private Color color;
- private String kategori;
- private JPanel balk = new JPanel();
- private JTextArea text = new JTextArea();
- public Plats(int xCord, int yCord, String kategori) {
- this.position = new Coordinates(xCord, yCord);
- this.kategori = kategori;
- if (kategori.equals("bus"))
- this.color = Color.RED;
- else if (kategori.equals("underground"))
- this.color = Color.BLUE;
- else if (kategori.equals("train"))
- this.color = Color.GREEN;
- else
- this.color = Color.BLACK;
- setBounds(xCord, yCord, 200, 150);
- setLayout(new BorderLayout());
- add(balk, BorderLayout.NORTH);
- add(text, BorderLayout.CENTER);
- balk.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- balk.setPreferredSize(new Dimension(200, 30));
- balk.setBackground(color);
- text.setBackground(color);
- FlyttLyss flyttLyss = new FlyttLyss();
- addMouseMotionListener(flyttLyss);
- addMouseListener(flyttLyss);
- addKeyListener(new PilLyss());
- }
- class FlyttLyss extends MouseAdapter {
- private int startX, startY;
- @Override
- public void mousePressed(MouseEvent mev) {
- startX = mev.getX();
- startY = mev.getY();
- requestFocusInWindow();
- }
- public void mouseDragged(MouseEvent mev) {
- int x = mev.getX();
- int y = mev.getY();
- setLocation(getX() + x - startX, getY() + y - startY);
- }
- }
- class PilLyss extends KeyAdapter {
- @Override
- public void keyPressed(KeyEvent kev) {
- int x = getX();
- int y = getY();
- switch (kev.getKeyCode()) {
- case KeyEvent.VK_LEFT:
- x--;
- break;
- case KeyEvent.VK_RIGHT:
- x++;
- break;
- case KeyEvent.VK_UP:
- y--;
- break;
- case KeyEvent.VK_DOWN:
- y++;
- break;
- }
- setLocation(x, y);
- }
- }
- @Override
- public String toString() {
- return "Plats{" +
- "position=" + position +
- ", color=" + color +
- ", kategori='" + kategori + '\'' +
- ", balk=" + balk +
- ", text=" + text +
- '}';
- }
- }
- NEW CLASS
- package com.company;
- public class NamedPlats extends Plats{
- private String namn;
- public NamedPlats(int xCord, int yCord, String kategori, String namn) {
- super(xCord, yCord, kategori);
- this.namn = namn;
- }
- }
- NEW CLASS
- package com.company;
- public class DescribedPlace extends Plats{
- private String desc;
- public DescribedPlace(int xCord, int yCord, String kategori, String desc) {
- super(xCord, yCord, kategori);
- this.desc = desc;
- }
- }
- NEW CLASS
- package com.company;
- import java.awt.*;
- import java.util.UUID;
- public class Coordinates {
- private int x_cord;
- private int y_cord;
- public Coordinates(int x_cord, int y_cord) {
- this.x_cord = x_cord;
- this.y_cord = y_cord;
- }
- public int hashCode(){
- return x_cord + y_cord;
- }
- public boolean equals(Object obj){
- if (obj instanceof Coordinates){
- Coordinates pp = (Coordinates) obj;
- return (pp.x_cord == this.x_cord && pp.y_cord == this.y_cord);
- } else {
- return false;
- }
- }
- public int getX_cord() {
- return x_cord;
- }
- public void setX_cord(int x_cord) {
- this.x_cord = x_cord;
- }
- public int getY_cord() {
- return y_cord;
- }
- public void setY_cord(int y_cord) {
- this.y_cord = y_cord;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement