public DistributedTextEditor() { area1.setFont(new Font("Monospaced", Font.PLAIN, 12)); area2.setFont(new Font("Monospaced", Font.PLAIN, 12)); ((AbstractDocument) area1.getDocument()).setDocumentFilter(dec); area2.setEditable(false); Container content = getContentPane(); content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); JScrollPane scroll1 = new JScrollPane( area1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); content.add(scroll1, BorderLayout.CENTER); JScrollPane scroll2 = new JScrollPane( area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); content.add(scroll2, BorderLayout.CENTER); content.add(ipaddress, BorderLayout.CENTER); content.add(portNumber, BorderLayout.CENTER); JMenuBar JMB = new JMenuBar(); setJMenuBar(JMB); JMenu file = new JMenu("File"); JMenu edit = new JMenu("Edit"); JMB.add(file); JMB.add(edit); file.add(Listen); file.add(Connect); file.add(Disconnect); file.addSeparator(); file.add(Save); file.add(SaveAs); file.add(Quit); edit.add(Copy); edit.add(Paste); edit.getItem(0).setText("Copy"); edit.getItem(1).setText("Paste"); Save.setEnabled(false); SaveAs.setEnabled(false); Disconnect.setEnabled(false); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); area1.addKeyListener(k1); area1.addMouseListener(m1); setTitle("Disconnected"); setVisible(true); area1.insert("Welcome to Hjortehandlerne's distributed text editor. \n", 0); this.addWindowListener(w1); }
public void actionPerformed(ActionEvent e) { saveOld(); area.setText(""); currentFile = "Untitled"; setTitle(currentFile + " - CoreyTextEditor"); changed = false; Save.setEnabled(false); SaveAs.setEnabled(false); }
/** * The disconnect method will stop the eventreplayer, and send null to the other peers, to stop * their reading from their streams. The GUI-menu is updated appropriately. */ public void disconnect() { setTitle("Disconnected"); active = false; if (connected == true) { er.stopStreamToQueue(); ert.interrupt(); setDocumentFilter(null); connected = false; setLocked(false); } deregisterOnPort(); Disconnect.setEnabled(false); Connect.setEnabled(true); Listen.setEnabled(true); Save.setEnabled(true); SaveAs.setEnabled(true); }
// public void createTimers() // { // timers = new javax.swing.Timer[Wave.numMinions]; // javax.swing.Timer temp = new javax.swing.Timer(0, new Listener()); // for (int x = 0; x < Wave.numMinions; x++) // { // timers[x] = temp; // timers[x].setDelay(multiplier*10); // timers[x].start(); // multiplier++; // } // multiplier = 0; // } public void define() { room = new Room(); save = new Save(); store = new Store(); for (int i = 0; i < tileset_ground.length; i++) { tileset_ground[i] = new ImageIcon("tileset_ground.png").getImage(); tileset_ground[i] = createImage( new FilteredImageSource( tileset_ground[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32))); } for (int i = 0; i < tileset_air.length; i++) { tileset_air[i] = new ImageIcon("tileset_air.png").getImage(); tileset_air[i] = createImage( new FilteredImageSource( tileset_air[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32))); } save.loadSave(new File("save/mission.ajay")); }
// action listeners public void actionPerformed(ActionEvent e) { // doge click button gives doge if (e.getSource() == dogeClick) { cps = cps + 1; Sounds.run("wow"); // increases size of button temporarily if (animation == 0) { dogeClick.setBounds(460, 110, 80, 80); dogeClick.setIcon(new ImageIcon("Images/dogeopen.jpg")); animation = 1; } else if (animation == 1) { dogeClick.setBounds(450, 100, 100, 100); dogeClick.setIcon(new ImageIcon("Images/doge.jpg")); animation = 0; } // adds doge accordingly and updates JLabel doge = doge + ((clickUpgrade) * clickMultiply) * multiplier; dogeCount.setText("Doge: " + doge); // randomize text flavourClick.setText(flavourText[(int) (Math.random() * 49)]); flavourClick.setBounds( (int) (Math.random() * (800)), (int) ((Math.random() * (401)) + 50), getWidth(), 50); flavourClick.setFont( new Font("Comic Sans MS", Font.BOLD, (int) ((Math.random() * (15)) + 15))); Color colour = Random.getRandomColour(); flavourClick.setForeground(colour); flavourClick.setVisible(true); } // for loop for all buttons for (int i = 0; i < MAX_UPGRADES; i++) { // updates button stats and count if (e.getSource() == producers[i] && doge >= producerStats[i].getCost()) { doge = doge - producerStats[i].getCost(); producers[i].setIcon(new ImageIcon("Images//bought.PNG")); producerStats[i].increaseCount(); producerStats[i].increaseCost(); producers[i].setToolTipText( "Your " + producerStats[i].getButtonName() + " gives " + producerStats[i].getDogeProduction() * producerStats[i].getCount() + " doge per second"); dps = dps + producerStats[i].getDogeProduction(); buyProducers[i].setText( "Buy " + producerStats[i].getButtonName() + " for " + producerStats[i].getCost() + " doge"); buyDetails[i].setText( "You have: " + producerStats[i].getCount() + " " + producerStats[i].getButtonName()); } } // updates click stats and count for (int i = 0; i < MAX_CLICK; i++) { if (e.getSource() == clickers[i] && doge >= clickerStats[i].getCost()) { doge = doge - clickerStats[i].getCost(); clickerStats[i].increaseCount(); clickUpgrade = clickUpgrade + clickerStats[i].getClickBonus(); clickMultiply = clickMultiply * clickerStats[i].getClickMultiplier(); dogeClick.setToolTipText( "Each click gives you " + (clickUpgrade) * clickMultiply + " doge. wow"); clickers[i].setVisible(false); buyClickers[i].setVisible(false); } } // secret developer button in corner if (e.getSource() == devButton) { doge = doge * 2; // plays Sandstorm by Darude Sounds.run("sandstorm"); } if (e.getSource() == options) { // opens options gui Options options = new Options(); } // saves current progress into save file if (e.getSource() == save) { // opens JOtionPane Sounds.run("save"); Save temp = new Save(); String name = JOptionPane.showInputDialog("What is the name of your save file?"); temp.createOutputFile("Save//" + name + ".txt"); String producerCount = ""; String clickCount = ""; String achievementCount = ""; // adds line of code for the amount of producers for (int i = 0; i < MAX_UPGRADES; i++) { producerCount = producerCount + producerStats[i].getCount() + "|"; } // adds line of code for the amount of clickers for (int i = 0; i < MAX_CLICK; i++) { // if bought write true if (clickerStats[i].getCount() != 0) { clickCount = clickCount + "t|"; } else { // if not bought write false clickCount = clickCount + "f|"; } } // adds a line of code for achievements for (int i = 0; i < MAX_ACHIEVEMENTS; i++) { // if possess write true if (achievementStats[i].getCount() != 0) { achievementCount = achievementCount + "t|"; } else { // if do not have write f achievementCount = achievementCount + "f|"; } } // add all lines to file temp.addInfo("" + doge); temp.addInfo("" + producerCount); temp.addInfo("" + clickCount); temp.addInfo("" + achievementCount); temp.closeOutputFile(); } // opens existing save file if (e.getSource() == open) { Save temp = new Save(); String name = JOptionPane.showInputDialog("What is the name of your save file?"); temp.openInputFile("Save//" + name + ".txt"); try { // counters to open save file int add = 0; String data = ""; int producerCount = 0; int clickCount = 0; int achievementCount = 0; // turn each line into char array doge = Long.parseLong(temp.getInfo()); char producerSave[] = temp.getInfo().toCharArray(); char clickSave[] = temp.getInfo().toCharArray(); char achievementSave[] = temp.getInfo().toCharArray(); // looks at producer line and adjusts values and resets Jlabel // text for (int i = 0; i < producerSave.length; i++) { if (producerSave[i] != '|') { data = data + Character.getNumericValue(producerSave[i]); } else { // updates data in producers add = Integer.parseInt(data); producerStats[producerCount].setCount(add); producerStats[producerCount].setCost( (int) (producerStats[producerCount].getCost() * (add * producerStats[producerCount].getCostIncrease()))); buyProducers[producerCount].setText( "Buy " + producerStats[producerCount].getButtonName() + " for " + producerStats[producerCount].getCost() + " doge"); buyDetails[producerCount].setText( "You have: " + producerStats[producerCount].getCount() + " " + producerStats[producerCount].getButtonName()); producers[producerCount].setToolTipText( "Your " + producerStats[producerCount].getButtonName() + " gives " + producerStats[producerCount].getDogeProduction() * producerStats[producerCount].getCount() + " doge per second"); dps = dps + (producerStats[producerCount].getDogeProduction() * producerStats[producerCount].getCount()); data = ""; producerCount++; add = 0; } } // reads clicker upgrades saves for (int i = 0; i < MAX_CLICK * 2; i++) { if (clickSave[i] == 't') { // updates data in clickers clickerStats[clickCount].setCount(1); clickUpgrade = (clickUpgrade + clickerStats[clickCount].getClickBonus()) * clickerStats[clickCount].getClickMultiplier(); dogeClick.setToolTipText("Each click gives you " + clickUpgrade + " doge. wow"); clickers[clickCount].setVisible(false); buyClickers[clickCount].setVisible(false); clickCount++; } else if (clickSave[i] == 'f') { clickCount++; } } // reads achievement lines for (int i = 0; i < MAX_ACHIEVEMENTS * 2; i++) { if (achievementSave[i] == 't') { // updates achievements achievementStats[achievementCount].setCount(1); achievements[achievementCount].setVisible(true); achievementCount++; } else if (achievementSave[i] == 'f') { achievementCount++; } } dogeCount.setText("Doge: " + doge); } catch (IOException e1) { // access invalid file e1.printStackTrace(); System.out.println("Invalid file!"); } // closes input file try { temp.closeInputFile(); } catch (IOException e1) { // access invalid file e1.printStackTrace(); System.out.println("Invalid file!"); } } }
public void actionPerformed(ActionEvent e) { saveOld(); area1.setText(""); resetArea2(); try { clientSocket = new Socket(ipaddress.getText(), Integer.parseInt(portNumber.getText())); Random r = new Random(); serverport = 10000 + r.nextInt(8999); // random port :D serverSocket = new ServerSocket(serverport); active = true; editor.setTitleToListen(); connected = true; ObjectOutputStream output = new ObjectOutputStream(clientSocket.getOutputStream()); ObjectInputStream input = new ObjectInputStream(clientSocket.getInputStream()); output.writeObject(new JoinNetworkRequest(serverport)); ConnectionData data = getConnectionData(clientSocket, input); lc = new LamportClock(data.getId()); lc.setMaxTime(data.getTs()); dec = new DocumentEventCapturer(lc, editor); er = new EventReplayer(editor, dec, lc); ert = new Thread(er); ert.start(); Peer peer = new Peer( editor, er, data.getHostId(), clientSocket, output, input, lc, clientSocket.getInetAddress().getHostAddress(), data.getPort()); dec.addPeer(peer); Thread thread = new Thread(peer); thread.start(); er.setAcknowledgements(data.getAcknowledgements()); er.setEventHistory(data.getEventHistory()); er.setCarets(data.getCarets()); er.addCaretPos(lc.getID(), 0); for (PeerWrapper p : data.getPeers()) { Socket socket; try { socket = connectToPeer(p.getIP(), p.getPort()); ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); outputStream.writeObject( new NewPeerDataRequest(lc.getID(), serverSocket.getLocalPort(), 0)); Peer newPeer = new Peer( editor, er, p.getId(), socket, outputStream, inputStream, lc, p.getIP(), p.getPort()); dec.addPeer(newPeer); Thread t = new Thread(newPeer); t.start(); } catch (IOException ex) { continue; } } Thread t1 = new Thread( new Runnable() { @Override public void run() { waitForConnection(); } }); t1.start(); area1.setText(data.getTextField()); area1.setCaretPosition(0); setDocumentFilter(dec); dec.sendObjectToAllPeers(new UnlockRequest(lc.getTimeStamp())); changed = false; Connect.setEnabled(false); Disconnect.setEnabled(true); Listen.setEnabled(false); Save.setEnabled(false); SaveAs.setEnabled(false); } catch (NumberFormatException | IOException e1) { setTitle("Unable to connect"); } }
public void actionPerformed(ActionEvent e) { saveOld(); final InetAddress local; active = true; try { local = InetAddress.getLocalHost(); Runnable server = new Runnable() { public void run() { serverport = Integer.parseInt(portNumber.getText()); int myID = getNewId(); registerOnPort(); editor.setTitleToListen(); clientSocket = waitForConnectionFromClient(); lc = new LamportClock(myID); area1.setText(""); resetArea2(); if (clientSocket != null) { listen = true; connected = true; dec = new DocumentEventCapturer(lc, editor); setDocumentFilter(dec); er = new EventReplayer(editor, dec, lc); er.updateCaretPos(myID, 0); ert = new Thread(er); ert.start(); try { ObjectOutputStream output = new ObjectOutputStream(clientSocket.getOutputStream()); ObjectInputStream input = new ObjectInputStream(clientSocket.getInputStream()); JoinNetworkRequest request = (JoinNetworkRequest) input.readObject(); int id = getNewId(); Peer peer = new Peer( editor, er, id, clientSocket, output, input, lc, clientSocket.getInetAddress().getHostAddress(), request.getPort()); ConnectionData cd = new ConnectionData( er.getEventHistory(), er.getAcknowledgements(), er.getCarets(), id, area1.getText(), lc.getTimeStamp(), lc.getID(), dec.getPeers(), serverSocket.getLocalPort()); dec.addPeer(peer); er.addCaretPos(id, 0); Thread t = new Thread(peer); t.start(); peer.writeObjectToStream(cd); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } waitForConnection(); } }; Thread serverThread = new Thread(server); serverThread.start(); } catch (UnknownHostException e1) { e1.printStackTrace(); } changed = false; Disconnect.setEnabled(true); Listen.setEnabled(false); Connect.setEnabled(false); Save.setEnabled(false); SaveAs.setEnabled(false); }
public void keyPressed(KeyEvent e) { changed = true; Save.setEnabled(true); SaveAs.setEnabled(true); }
/** Constructor to create the frame and its components */ public CoreyTextEditor() { // Create a scroll pane area.setFont(new Font("Monospaced", Font.PLAIN, 12)); JScrollPane scroll = new JScrollPane( area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); add(scroll, BorderLayout.CENTER); // Adds the system default look and feel try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) { e.printStackTrace(); } // Create a menu bar JMenuBar JMB = new JMenuBar(); setJMenuBar(JMB); JMenu file = new JMenu("File"); JMenu edit = new JMenu("Edit"); JMB.add(file); JMB.add(edit); // Finishing our menu bar file.add(New); file.add(Open); file.add(Save); file.add(SaveAs); file.addSeparator(); file.add(Quit); edit.add(Cut); edit.add(Copy); edit.add(Paste); edit.getItem(0).setText("Cut"); edit.getItem(0).setIcon(new ImageIcon("cut.gif")); edit.getItem(1).setText("Copy"); edit.getItem(1).setIcon(new ImageIcon("copy.gif")); edit.getItem(2).setText("Paste"); edit.getItem(2).setIcon(new ImageIcon("paste.gif")); // Time to make a toolbar! JToolBar tool = new JToolBar(); add(tool, BorderLayout.NORTH); tool.add(New); tool.add(Open); tool.add(Save); tool.addSeparator(); JButton cut = tool.add(Cut); JButton cop = tool.add(Copy); JButton pas = tool.add(Paste); cut.setText(null); cut.setIcon(new ImageIcon("cut.gif")); cop.setText(null); cop.setIcon(new ImageIcon("copy.gif")); pas.setText(null); pas.setIcon(new ImageIcon("paste.gif")); Save.setEnabled(false); SaveAs.setEnabled(false); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); pack(); /* KeyListener to change Save and SaveAs */ KeyListener k1 = new KeyAdapter() { public void keyPressed(KeyEvent e) { changed = true; Save.setEnabled(true); SaveAs.setEnabled(true); } }; area.addKeyListener(k1); setTitle(currentFile + " - CoreyTextEditor"); setVisible(true); }