home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / asciishopsource / asciiShop / mainFrame.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  11.9 KB  |  393 lines

  1. /** 
  2.  * mainFrame.java
  3.  *
  4.  * Title:            ASCII Shop
  5.  * Description:        The goal of this program is to provide many functionalities of photoshop, with ASCII art.
  6.  * @author            Expotech
  7.  * @version            
  8.  */
  9.  
  10. package asciiShop;
  11.  
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.io.*;
  15.  
  16. public class mainFrame extends java.awt.Frame {
  17.  
  18. char[][] artBuffer2;
  19. int useLength, length1, length2;
  20. int globalCount = 0;
  21. PrintWriter writeToFile;
  22. String bigString;
  23. int i, j, k;
  24. int countForOpenFile, countForOpenFileSave;
  25. FileDialog newFile;
  26. String pathToFile;
  27. String file;
  28. BufferedReader openFile;
  29. String[] artBuffer; 
  30. String[][] layerBuffer;
  31.  
  32.  
  33. // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
  34. // every time the form is saved. All manual changes will be overwritten.
  35. // BEGIN GENERATED CODE
  36.     // member declarations
  37.     java.awt.TextArea fileBuffer = new java.awt.TextArea();
  38.     java.awt.List layers = new java.awt.List();
  39.     java.awt.MenuBar menuBar1 = new java.awt.MenuBar();
  40.     java.awt.Menu menuFile = new java.awt.Menu();
  41.     java.awt.MenuItem menuFileOpen = new java.awt.MenuItem();
  42.     java.awt.MenuItem menuFileWriteOut = new java.awt.MenuItem();
  43.     java.awt.MenuItem menuFileQuit = new java.awt.MenuItem();
  44.     java.awt.MenuItem menuFileRefreshArt = new java.awt.MenuItem();
  45.     java.awt.MenuItem menuFileReplaceCharacters = new java.awt.MenuItem();
  46.     java.awt.Menu menuLayers = new java.awt.Menu();
  47.     java.awt.MenuItem menuLayersAddLayer = new java.awt.MenuItem();
  48.     java.awt.MenuItem menuLayersRemoveLayer = new java.awt.MenuItem();
  49.     java.awt.MenuItem menuLayersRedrawLayers = new java.awt.MenuItem();
  50.     java.awt.Menu menumirror = new java.awt.Menu();
  51.     java.awt.MenuItem menumirrorSideWays = new java.awt.MenuItem();
  52.     java.awt.MenuItem menumirrorFlipVerticle = new java.awt.MenuItem();
  53.     java.awt.Menu menuLineTools = new java.awt.Menu();
  54.     java.awt.MenuItem menuLineToolsVerticleLine = new java.awt.MenuItem();
  55.     java.awt.MenuItem menuLineToolsHorizantalLine = new java.awt.MenuItem();
  56. // END GENERATED CODE
  57.  
  58.     public mainFrame() {
  59.     }
  60.  
  61.     public void initComponents() throws Exception {
  62. // IMPORTANT: Source code between BEGIN/END comment pair will be regenerated
  63. // every time the form is saved. All manual changes will be overwritten.
  64. // BEGIN GENERATED CODE
  65.         // the following code sets the frame's initial state
  66.  
  67.         fileBuffer.setLocation(new java.awt.Point(60, 70));
  68.         fileBuffer.setVisible(true);
  69.         fileBuffer.setSize(new java.awt.Dimension(480, 360));
  70.  
  71.         layers.setLocation(new java.awt.Point(570, 270));
  72.         layers.setVisible(true);
  73.         layers.setSize(new java.awt.Dimension(130, 160));
  74.  
  75.         menuBar1.add(menuFile);
  76.         menuBar1.add(menuLayers);
  77.         menuBar1.add(menumirror);
  78.         menuBar1.add(menuLineTools);
  79.  
  80.         menuFile.setLabel("File");
  81.         menuFile.add(menuFileOpen);
  82.         menuFile.add(menuFileWriteOut);
  83.         menuFile.add(menuFileQuit);
  84.         menuFile.add(menuFileRefreshArt);
  85.         menuFile.add(menuFileReplaceCharacters);
  86.  
  87.         menuFileOpen.setLabel("Open");
  88.  
  89.         menuFileWriteOut.setLabel("Write Out");
  90.  
  91.         menuFileQuit.setLabel("Quit");
  92.  
  93.         menuFileRefreshArt.setLabel("Refresh Art");
  94.  
  95.         menuFileReplaceCharacters.setLabel("Replace Characters");
  96.  
  97.         menuLayers.setLabel("Layers");
  98.         menuLayers.add(menuLayersAddLayer);
  99.         menuLayers.add(menuLayersRemoveLayer);
  100.         menuLayers.add(menuLayersRedrawLayers);
  101.  
  102.         menuLayersAddLayer.setLabel("Add Layer");
  103.  
  104.         menuLayersRemoveLayer.setLabel("Remove Layer");
  105.  
  106.         menuLayersRedrawLayers.setLabel("Redraw Layers");
  107.  
  108.         menumirror.setLabel("mirror");
  109.         menumirror.add(menumirrorSideWays);
  110.         menumirror.add(menumirrorFlipVerticle);
  111.  
  112.         menumirrorSideWays.setLabel("invert");
  113.  
  114.         menumirrorFlipVerticle.setLabel("Flip Characters Verticle");
  115.  
  116.         menuLineTools.setLabel("Line Tools");
  117.         menuLineTools.add(menuLineToolsVerticleLine);
  118.         menuLineTools.add(menuLineToolsHorizantalLine);
  119.  
  120.         menuLineToolsVerticleLine.setLabel("Verticle Line");
  121.  
  122.         menuLineToolsHorizantalLine.setLabel("Horizantal Line");
  123.  
  124.         setLocation(new java.awt.Point(5, 40));
  125.         setTitle("asciiShop.mainFrame");
  126.         setLayout(null);
  127.         setMenuBar(menuBar1);
  128.         setSize(new java.awt.Dimension(736, 520));
  129.         add(fileBuffer);
  130.         add(layers);
  131.  
  132.  
  133.         fileBuffer.addTextListener(new java.awt.event.TextListener() {
  134.             public void textValueChanged(java.awt.event.TextEvent e) {
  135.                 fileBufferTextValueChanged(e);
  136.             }
  137.         });
  138.         layers.addItemListener(new java.awt.event.ItemListener() {
  139.             public void itemStateChanged(java.awt.event.ItemEvent e) {
  140.                 layersItemStateChanged(e);
  141.             }
  142.         });
  143.         menuFileOpen.addActionListener(new java.awt.event.ActionListener() {
  144.             public void actionPerformed(java.awt.event.ActionEvent e) {
  145.                 menuFileOpenActionPerformed(e);
  146.             }
  147.         });
  148.         menuFileWriteOut.addActionListener(new java.awt.event.ActionListener() {
  149.             public void actionPerformed(java.awt.event.ActionEvent e) {
  150.                 menuFileWriteOutActionPerformed(e);
  151.             }
  152.         });
  153.         menuFileRefreshArt.addActionListener(new java.awt.event.ActionListener() {
  154.             public void actionPerformed(java.awt.event.ActionEvent e) {
  155.                 menuFileRefreshArtActionPerformed(e);
  156.             }
  157.         });
  158.         menuFileReplaceCharacters.addActionListener(new java.awt.event.ActionListener() {
  159.             public void actionPerformed(java.awt.event.ActionEvent e) {
  160.                 menuFileReplaceCharactersActionPerformed(e);
  161.             }
  162.         });
  163.         menuLayersAddLayer.addActionListener(new java.awt.event.ActionListener() {
  164.             public void actionPerformed(java.awt.event.ActionEvent e) {
  165.                 menuLayersAddLayerActionPerformed(e);
  166.             }
  167.         });
  168.         menumirrorSideWays.addActionListener(new java.awt.event.ActionListener() {
  169.             public void actionPerformed(java.awt.event.ActionEvent e) {
  170.                 menumirrorSideWaysActionPerformed(e);
  171.             }
  172.         });
  173.         menumirrorFlipVerticle.addActionListener(new java.awt.event.ActionListener() {
  174.             public void actionPerformed(java.awt.event.ActionEvent e) {
  175.                 menumirrorFlipVerticleActionPerformed(e);
  176.             }
  177.         });
  178.         menuLineToolsVerticleLine.addActionListener(new java.awt.event.ActionListener() {
  179.             public void actionPerformed(java.awt.event.ActionEvent e) {
  180.                 menuLineToolsVerticleLineActionPerformed(e);
  181.             }
  182.         });
  183.         addWindowListener(new java.awt.event.WindowAdapter() {
  184.             public void windowClosing(java.awt.event.WindowEvent e) {
  185.                 thisWindowClosing(e);
  186.             }
  187.         });
  188.  
  189. // END GENERATED CODE
  190.     }
  191.   
  192.       private boolean mShown = false;
  193.       
  194.     public void addNotify() {
  195.         super.addNotify();
  196.         
  197.         if (mShown)
  198.             return;
  199.             
  200.         // move components to account for insets
  201.         Insets insets = getInsets();
  202.         Component[] components = getComponents();
  203.         for (int i = 0; i < components.length; i++) {
  204.             Point location = components[i].getLocation();
  205.             location.move(location.x, location.y + insets.top);
  206.             components[i].setLocation(location);
  207.         }
  208.  
  209.         mShown = true;
  210.     }
  211.  
  212.     // Close the window when the close box is clicked
  213.     void thisWindowClosing(java.awt.event.WindowEvent e) {
  214.         setVisible(false);
  215.         dispose();
  216.         System.exit(0);
  217.     }
  218.     
  219.     public void list1ItemStateChanged(java.awt.event.ItemEvent e) {
  220.     }
  221.     
  222.     public void layersItemStateChanged(java.awt.event.ItemEvent e) {
  223.     }
  224.     
  225.     public void fileBufferTextValueChanged(java.awt.event.TextEvent e) {
  226.     }
  227.     
  228.     public void menuFileOpenActionPerformed(java.awt.event.ActionEvent e) {
  229.         bigString = new String("");
  230.         countForOpenFile = 0; // To get the number of lines to use with artBuffer
  231.         newFile = new FileDialog(this, "Open File", FileDialog.LOAD); // Opens and open file dialog box
  232.         newFile.setVisible(true);
  233.         pathToFile = newFile.getDirectory();
  234.         file = newFile.getFile();
  235.         artBuffer = new String[200];
  236.         try {
  237.             System.out.println(pathToFile + file);
  238.             openFile = new BufferedReader(new FileReader(pathToFile + file));
  239.         } catch(FileNotFoundException badStuff) {
  240.             System.out.println("The file could not be found... DAMN MICROSOFT!!!");
  241.         }
  242.             try {
  243.                 while( (artBuffer[countForOpenFile] = openFile.readLine()).equals("***end***") == false)
  244.                 {
  245.                         countForOpenFile++;
  246.                 }
  247.             } catch(IOException badStuff) {
  248.                 System.out.println("HOW THE HELL DiD YOU GET THIS ERROR!?");
  249.             }
  250.         System.out.println("How many lines in the art: " + countForOpenFile);
  251.         for(i = 0; i < countForOpenFile; i++) {
  252.             bigString = bigString + artBuffer[i] + "\n";
  253.         }
  254.         fileBuffer.setText(bigString);
  255.         countForOpenFileSave = countForOpenFile;
  256.     }
  257.     
  258.     public void menuFileWriteOutActionPerformed(java.awt.event.ActionEvent e) {
  259.         newFile = new FileDialog(this, "Save File", FileDialog.SAVE);
  260.         newFile.setVisible(true);
  261.         pathToFile = newFile.getDirectory();
  262.         file = newFile.getFile();
  263.         try {
  264.         writeToFile = new PrintWriter(new FileWriter(pathToFile + file), true);
  265.         System.out.println("Wrote to file: " + pathToFile + file);
  266.         } catch(IOException badStuff) {
  267.             System.out.println("GODDAMN HOW THE HELL DID YOU MANAGE TO GET THIS ERROR!?");
  268.         }
  269.         bigString = fileBuffer.getText();
  270.         writeToFile.println(bigString + "\n***end***");
  271.         System.out.println("Wrote out to file: " + pathToFile + file + "\n" + bigString + "***end***");
  272.         return;
  273.     }
  274.     
  275.     public void menuLayersAddLayerActionPerformed(java.awt.event.ActionEvent e) {
  276.         {
  277.             String [][] temp;
  278.             
  279.             temp = layerBuffer;
  280.             layerBuffer = new String[globalCount+1][200];
  281.             for(int i=0; i < globalCount; i++)
  282.                 layerBuffer[i] = temp[i];
  283.         }
  284.         countForOpenFile = 0; // To get the number of lines to use with artBuffer
  285.         newFile = new FileDialog(this, "Add Layer", FileDialog.LOAD); // Opens and open file dialog box
  286.         newFile.setVisible(true);
  287.         pathToFile = newFile.getDirectory();
  288.         file = newFile.getFile();
  289.         layerBuffer[globalCount] = new String[200];
  290.             System.out.println("Trying to apply layer: " + pathToFile + file);
  291.             try {
  292.                 openFile = new BufferedReader(new FileReader(pathToFile + file));
  293.             } catch(IOException badStuff) {
  294.                 System.out.println("HOW THE HELL DID YOU GET THIS ERROR!? - 3");
  295.             }
  296.             try {
  297.                 while( (layerBuffer[globalCount][countForOpenFile] = openFile.readLine()).equals("***end***") == false)
  298.                 {
  299.                         countForOpenFile++;
  300.                 }
  301.             } catch(IOException badStuff) {
  302.                 System.out.println("HOW THE HELL DID YOU GET THIS ERROR!? -2");
  303.             }
  304.             length1 = artBuffer[0].length();
  305.             length2 = layerBuffer[globalCount][0].length();
  306.             if(length1 < length2) {
  307.                 useLength = length2;
  308.             }
  309.             else {
  310.                 useLength = length1;
  311.             }
  312.             
  313.             artBuffer2 = new char[200][200];
  314.             
  315.             for(i = 0; i < countForOpenFileSave; i++) {
  316.                 for(k = 0; k < useLength; k++) {
  317.                     artBuffer2[i][k] = artBuffer[i].charAt(k);
  318.                 }
  319.                 for(j = 0; j < useLength; j++) {
  320.                     if(layerBuffer[i][j].charAt(j) != ' ') {
  321.                         artBuffer2[i][j] = layerBuffer[i][j].charAt(j);
  322.                     }
  323.                     else {
  324.                         artBuffer2[i][j] = ' ';
  325.                     }
  326.                 }    
  327.             }
  328.             for(i = 0; i < countForOpenFileSave; i++) {
  329.                 for(k = 0; k < useLength; k++) {
  330.                     System.out.println(artBuffer2[i][k]);
  331.                 }
  332.             }
  333.             for(i = 0; i < countForOpenFileSave; i++) {
  334.                 System.out.println(artBuffer[i]);
  335.             }
  336.             globalCount++;
  337.     }
  338.     
  339.     public void menumirrorSideWaysActionPerformed(java.awt.event.ActionEvent e) {
  340.         asciiLibs foo = new asciiLibs(artBuffer);
  341.         foo.invert(countForOpenFile);
  342.         refresh();
  343.     }
  344.     
  345.     public void menuFileRefreshArtActionPerformed(java.awt.event.ActionEvent e) {
  346.         bigString = "";
  347.         //fileBuffer.setText("");
  348.         for(i = 0; i < countForOpenFile; i++) {
  349.             bigString = bigString + artBuffer[i] + "\n";
  350.         }
  351.         fileBuffer.setText(bigString);
  352.         //fileBuffer.replaceText(bigString, 0, 40000);
  353.     }
  354.     public void refresh() {
  355.         bigString = "";
  356.         for(i = 0; i < countForOpenFile; i++) {
  357.             bigString = bigString + artBuffer[i] + "\n";
  358.         }
  359.         fileBuffer.setText(bigString);
  360.     }
  361.     
  362.     public void menumirrorFlipVerticleActionPerformed(java.awt.event.ActionEvent e) {
  363.         asciiLibs foo = new asciiLibs(artBuffer);
  364.         foo.invertVert(countForOpenFile);
  365.         refresh();
  366.     }
  367.     
  368.     public void menuLineToolsVerticleLineActionPerformed(java.awt.event.ActionEvent e) {
  369.             asciiLibs foo = new asciiLibs(artBuffer);
  370.             foo.horizontalLine(countForOpenFile);
  371.             refresh();
  372.     }
  373.     
  374.     public void menuFileReplaceCharactersActionPerformed(java.awt.event.ActionEvent e) {
  375.         try {
  376.             asciiLibs foo = new asciiLibs(artBuffer);
  377.             foo.replaceChar(countForOpenFileSave);
  378.             refresh();
  379.         } catch (IOException badStuff) {
  380.             System.out.println("Bad stuff has occurred -1");
  381.         }
  382.     }
  383.     
  384.     
  385.     
  386.     
  387.     
  388.     
  389.     /*layerBuffer = new String[globalCount][];
  390.     layerBuffer[2] = new String[2]{"blah","soo"};
  391.     system.out.println(layerBuffer[2][1])*/
  392. }
  393.