Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.util.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.filechooser.*;
- import javax.swing.filechooser.FileFilter;
- public class inlupp2 extends JFrame {
- private JComboBox<String> boxen;
- private String[] meny = { "Named", "Described" };
- private String[] kategoriVal = { "Buss", "Tunnelbana", "Tåg" };
- private JTextField sökText;
- private JFileChooser jfc = new JFileChooser("Desktop");
- private BildPanel bp = null;
- private JScrollPane scroll = null;
- private platsLyssna platsLyssna = new platsLyssna();
- private MusLyssna musLyssna = new MusLyssna();
- private JList<String> kategori = new JList<String>(kategoriVal);
- private Map<Position, Plats> platser = new HashMap<>();
- private Plats tempPlats = null;
- private ArrayList<Plats> positioner = new ArrayList<>();
- public inlupp2() {
- super("inlupp 2");
- FileFilter bildFilter = new FileNameExtensionFilter("Bilder", "jpg", "gif", "png", "txt");
- jfc.setFileFilter(bildFilter);
- // menyfält
- JMenuBar jmb = new JMenuBar();
- setJMenuBar(jmb);
- JMenu archive = new JMenu("Archive");
- jmb.add(archive);
- JMenuItem newMap = new JMenuItem("New Map");
- archive.add(newMap);
- newMap.addActionListener(new öppnaLyssna());
- JMenuItem loadPlaces = new JMenuItem("Load Places");
- archive.add(loadPlaces);
- loadPlaces.addActionListener(new laddaLyssna());
- JMenuItem save = new JMenuItem("Save");
- archive.add(save);
- JMenuItem exit = new JMenuItem("Exit");
- archive.add(exit);
- // archive.addActionListener();
- // norra panelen
- JPanel norra = new JPanel();
- add(norra, BorderLayout.NORTH);
- JLabel ny = new JLabel("New:");
- norra.add(ny);
- boxen = new JComboBox<String>(meny);
- boxen.setSelectedIndex(0);
- norra.add(boxen);
- boxen.addActionListener(new nyLyssna());
- sökText = new JTextField(10);
- norra.add(sökText);
- JButton sök = new JButton("Sök");
- norra.add(sök);
- sök.addActionListener(new sökLyssna());
- JButton göm = new JButton("Hide");
- norra.add(göm);
- göm.addActionListener(new gömLyssna());
- JButton taBort = new JButton("Remove");
- norra.add(taBort);
- taBort.addActionListener(new taBortLyssna());
- JButton whatIsHere = new JButton("What is here");
- norra.add(whatIsHere);
- whatIsHere.addActionListener(new whatIsHereLyssna());
- // högra panelen
- JPanel högra = new JPanel();
- högra.setLayout(new BoxLayout(högra, BoxLayout.Y_AXIS));
- add(högra, BorderLayout.EAST);
- högra.add(new JLabel("Categories"));
- kategori.setFixedCellWidth(50);
- kategori.setVisibleRowCount(10);
- kategori.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
- JScrollPane scroll = new JScrollPane(kategori);
- högra.add(scroll);
- kategori.addListSelectionListener(new kategorierLyssna());
- JButton gömKategorier = new JButton("Hide category");
- högra.add(gömKategorier);
- gömKategorier.addActionListener(new gömKategorierLyssna());
- // GUI
- setSize(600, 300);
- pack();
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- }
- class MusLyssna extends MouseAdapter {
- public void mouseClicked(MouseEvent mev) {
- int x = mev.getX();
- int y = mev.getY();
- System.out.println(x + " " + y);
- registrera(x, y);
- }
- private void registrera(int x, int y) {
- Format f = new Format();
- String val = (String) (boxen.getSelectedItem());
- switch (val) {
- case "Named":
- f = new Format(null);
- break;
- case "Described":
- f = new Format("");
- break;
- }
- int svar = JOptionPane.showConfirmDialog(inlupp2.this, f, "Registrera", JOptionPane.OK_CANCEL_OPTION);
- if (svar != JOptionPane.OK_OPTION) {
- avbryt();
- return;
- }
- if (f.getNamn().equals("")) {
- JOptionPane.showMessageDialog(inlupp2.this, "Inget namn!", "Fel!", JOptionPane.ERROR_MESSAGE);
- avbryt();
- return;
- }
- if (!platser.containsKey(f.getNamn())) {
- if (val.equals("Named")) {
- if (kategori.getSelectedValue() == null) {
- Plats plats = new Plats(f.getNamn(), x, y, "");
- regAvslut(plats);
- } else {
- Plats plats = new Plats(f.getNamn(), x, y, kategori.getSelectedValue());
- regAvslut(plats);
- }
- }
- if (val.equals("Described")) {
- if (kategori.getSelectedValue() == null) {
- BeskrivnaPlatser plats = new BeskrivnaPlatser(f.getNamn(), x, y, "", f.getBeskrivningsFält());
- regAvslut(plats);
- } else {
- BeskrivnaPlatser plats = new BeskrivnaPlatser(f.getNamn(), x, y, kategori.getSelectedValue(),
- f.getBeskrivningsFält());
- regAvslut(plats);
- }
- }
- }
- }
- private void regAvslut(Plats plats) {
- Position position = plats.getPosition();
- plats.addMouseListener(new platsLyssna());
- platser.put(position, plats);
- System.out.println(platser.get(position));
- bp.add(plats);
- bp.validate();
- bp.repaint();
- avbryt();
- }
- private void avbryt() {
- bp.removeMouseListener(musLyssna);
- bp.setCursor(Cursor.getDefaultCursor());
- boxen.setEnabled(true);
- }
- }
- class nyLyssna implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- if (!(bp == null)) {
- bp.addMouseListener(musLyssna);
- bp.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
- boxen.setEnabled(false);
- return;
- }
- }
- }
- class öppnaLyssna implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- int svar = jfc.showOpenDialog(inlupp2.this);
- if (svar != JFileChooser.APPROVE_OPTION)
- return;
- File fil = jfc.getSelectedFile();
- String filnamn = fil.getAbsolutePath();
- if (scroll != null)
- remove(scroll);
- bp = new BildPanel(filnamn);
- bp.setLayout(null);
- scroll = new JScrollPane(bp);
- add(scroll);
- pack();
- validate();
- repaint();
- }
- }
- class laddaLyssna implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- FileFilter bildFilter = new FileNameExtensionFilter("png", "txt");
- jfc.setFileFilter(bildFilter);
- int svar = jfc.showOpenDialog(inlupp2.this);
- if (svar != JFileChooser.APPROVE_OPTION)
- return;
- File fil = jfc.getSelectedFile();
- String filnamn = fil.getAbsolutePath();
- try {
- FileReader infil = new FileReader(filnamn);
- BufferedReader in = new BufferedReader(infil);
- String line;
- while ((line = in.readLine()) != null) {
- String[] tokens = line.split(",");
- String kategori = tokens[1];
- int x = Integer.parseInt(tokens[2]);
- int y = Integer.parseInt(tokens[3]);
- String namn = tokens[4];
- Plats plats = new Plats(namn, x, y, kategori);
- Position pos = plats.getPosition();
- platser.put(pos, plats);
- System.out.println(platser.get(pos));
- plats.addMouseListener(new platsLyssna());
- bp.add(plats);
- bp.repaint();
- }
- in.close();
- infil.close();
- } catch (FileNotFoundException e) {
- System.err.println("kan inte öppna ");
- } catch (IOException e) {
- System.err.println("Fel: " + e.getMessage());
- }
- }
- }
- class SparaLyssna implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- try {
- FileOutputStream utfil = new FileOutputStream("test.txt");
- ObjectOutputStream oos = new ObjectOutputStream(utfil);
- oos.writeObject(platser);
- // FileWriter utfil = new FileWriter("test.reg");
- // PrintWriter out = new PrintWriter(utfil);
- // for(Position p: platser.values())
- // out.println(p.getName()+","+p.getX()+","+p.getY()+","+p.getKategori()+","+p.getBeskrivning());
- // out.close();
- // utfil.close();
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null, "Kan inte öppna filen");
- } catch (IOException e) {
- JOptionPane.showMessageDialog(null, "Fel: " + e.getMessage());
- }
- }
- }
- class BildPanel extends JPanel {
- private ImageIcon bild;
- public BildPanel(String filnamn) {
- bild = new ImageIcon(filnamn);
- int w = bild.getIconWidth();
- int h = bild.getIconHeight();
- setPreferredSize(new Dimension(w, h));
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- g.drawImage(bild.getImage(), 0, 0, this);
- }
- }
- class sökLyssna implements ActionListener {
- public void actionPerformed(ActionEvent arg0) {
- String namn = sökText.getText();
- for (Plats plats : platser.values()) {
- plats.setVald(false);
- positioner.clear();
- }
- for (Plats plats : platser.values()) {
- if (plats.getNamn().equals(namn)) {
- System.out.println(plats);
- tempPlats = plats;
- positioner.add(tempPlats);
- tempPlats.setVald(true);
- tempPlats.repaint();
- if (!bp.equals(tempPlats)) {
- bp.add(tempPlats);
- bp.repaint();
- }
- }
- }
- }// sök
- }
- class gömLyssna implements ActionListener {
- public void actionPerformed(ActionEvent arg0) {
- System.out.println(platser.get(tempPlats) + " har tagits bort från kartan");
- for (Plats p : positioner) {
- bp.remove(p);
- bp.repaint();
- }
- positioner.clear();
- }
- }
- class taBortLyssna implements ActionListener {
- public void actionPerformed(ActionEvent arg0) {
- System.out.println(platser.get(tempPlats) + " Borttaget");
- for (Plats p : positioner) {
- bp.remove(p);
- p.removeMouseListener(platsLyssna);
- platser.remove(p);
- bp.validate();
- bp.repaint();
- }
- positioner.clear();
- }
- }
- class whatIsHereLyssna implements ActionListener {
- public void actionPerformed(ActionEvent ave) {
- System.out.println("Du har tryckt på What is here?");
- // temp
- }
- }
- class kategorierLyssna implements ListSelectionListener {
- public void valueChanged(ListSelectionEvent lse) {
- if (!lse.getValueIsAdjusting()) {
- String val = kategori.getSelectedValue();
- System.out.println(val);
- for (Plats plats : platser.values()) {
- plats.setVald(false);
- positioner.clear();
- }
- for (Plats plats : platser.values()) {
- if (plats.getKategori().equals(val)) {
- tempPlats = plats;
- positioner.add(tempPlats);
- tempPlats.setVald(true);
- tempPlats.repaint();
- if (!bp.equals(tempPlats)) {
- bp.add(tempPlats);
- bp.repaint();
- }
- }
- }
- }
- }
- }
- class platsLyssna extends MouseAdapter {
- public void mouseClicked(MouseEvent mev) {
- mev.getSource().toString();
- tempPlats = (Plats) mev.getSource();
- if (mev.getButton() == MouseEvent.BUTTON1) {
- while (true) {
- if (!positioner.contains(tempPlats)) {
- tempPlats.setVald(true);
- positioner.add(tempPlats);
- tempPlats.repaint();
- break;
- }
- if (positioner.contains(tempPlats)) {
- positioner.remove(tempPlats);
- tempPlats.setVald(false);
- tempPlats.repaint();
- break;
- }
- }
- }
- if (mev.getButton() == MouseEvent.BUTTON3) {
- while (true) {
- if (!tempPlats.getUtvik()) {
- tempPlats.setUtvik(true);
- tempPlats.repaint();
- System.out.println("Utvik: " + tempPlats.getUtvik());
- break;
- }
- if (tempPlats.getUtvik()) {
- tempPlats.setUtvik(false);
- tempPlats.repaint();
- System.out.println("Utvik: " + tempPlats.getUtvik());
- break;
- }
- }
- }
- }
- }
- class gömKategorierLyssna implements ActionListener {
- public void actionPerformed(ActionEvent arg0) {
- for (Plats plats : platser.values()) {
- platser.get(plats.getPosition());
- if (kategori.getSelectedValue().equals(plats.getKategori())) {
- System.out.println(plats.getPosition() + " har gömts");
- bp.remove(plats);
- bp.repaint();
- }
- }
- }// gömKategorier
- }
- public static void main(String[] args) {
- new inlupp2();
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Polygon;
- import java.sql.PseudoColumnUsage;
- import javax.swing.*;
- public class Plats extends JComponent {
- private String kategori;
- private String namn;
- private Position position;
- private boolean vald;
- private boolean utvik;
- private int[] xes = { 0, 15, 30 };
- private int[] yes = { 0, 30, 0 };
- private Polygon triangle = new Polygon(xes, yes, 3);
- public Plats(String namn, int x, int y, String kategori) {
- position = new Position(x,y);
- this.kategori = kategori;
- this.namn = namn;
- utvik = false;
- vald = false;
- setBounds(x - 15, y - 30, 30, 30);
- setOpaque(false);
- }
- public Position getPosition() {
- return position;
- }
- public String getNamn() {
- return namn;
- }
- public String getKategori(){
- return kategori;
- }
- public String toString() {
- return namn +" "+ kategori + " x:" + position.getX() +" y:"+ position.getY();
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- if (utvik == false) {
- g.setColor(färg(kategori));
- g.fillPolygon(triangle);
- }
- if (utvik == true) {
- g.setColor(Color.BLACK);
- g.drawString("TEST", position.getX(), position.getY()-20);
- }
- if (vald == true) {
- g.setColor(Color.CYAN);
- g.drawPolygon(triangle);
- }
- }
- private Color färg(String typ) {
- switch (typ) {
- case "Tunnelbana":
- return Color.BLUE;
- case "Tåg":
- return Color.GREEN;
- case "Buss":
- return Color.RED;
- case "":
- return Color.BLACK;
- }
- return null;
- }
- public boolean getUtvik() {
- return utvik;
- }
- public void setVald(boolean vald) {
- this.vald = vald;
- }
- public void setUtvik(boolean utvik) {
- this.utvik = utvik;
- }
- }
- import java.awt.*;
- import javax.swing.*;
- public class Position {
- private int x;
- private int y;
- public Position(int x, int y) {
- this.x =x;
- this.y = y;
- }
- public int getX() {
- return x;
- }
- public int getY() {
- return y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement