non-academic guidance for computer science

Information

This article was written on 24 Úno 2011, and is filled under Java, Tutoring.

Generate ASCII image text

 

Náročnost

Začátečník ( co to znamená? )

Zadání

Naprogramovat program, který bude v reálném čase přepisovan psaný text do ASCII formátu.

Nepoužívat návrhové vzory a složitější struktury, GUI.

 Screenshot

Video

Příklad

Začneme kompilační třídou a objekty, metodami, parametry, které budeme používat.

Main

package ascii;

public class Main {
  public static void main( String[] args )
  {
    // kompilacni metoda zde
  }
}

Form

package ascii;

public class Form
{
  // vstupni pole
  // vystupni pole
  Form()
  {
    // konstruktor zde
  }
  // metoda na prevod vstupniho pole na vystupni
  // metody na odposlouchavani stisknuti klaves
}

Render

package ascii;

public class Render
{

  // uloziste vystupu

  Render()
  {
    // konstruktor zde
  }

  // metoda ktera prevede normalni znak na ASCII a vrati ASCII
}

Sheet

package ascii;

class Sheet
{
  // Databaze pro tento priklad idealni HashTable (klic-hodnota)
  Sheet()
  {
    // konstruktor zde
  }
  // iniciace databaze ASCII znaku a relaci
  // nalezeni znaku v ASCII na zaklade znaku vstupniho

}

Následně si postupně doplníme do jednotlivých objektů danné metody

Main

package ascii;

public class Main {
  public static void main( String[] args )
  {
   // Vytvoří nový objekt Form a spustí metodu inicializace
   // která je v našem případě zobrazení gui.
   new Form() . init() ;
  }
}

Form

package ascii;

// uživatelské prostředí bude okno se vstupem a výstupem, musíme tedy našemu
// objektu přidat rodiče okno (neboli JFrame)
  public class Form extends JFrame
{

  // jako vystupni pole si zvolime viceradkovy formular
  private JTextArea _output = new JTextArea(200, 200);
  // jako vstupni pole si zvolime jednoradkove pole
  private JTextField _input = new JTextField();
  Form()
  {

    // nastavime jednoduchy layout (neboli rozlozeni grafickych komponentu)
    setLayout(new FlowLayout(FlowLayout.LEFT,10,10));

    // nastavime rozmery vstupni bunky a pridame ji posluchace stisku klaves
    _input.setPreferredSize(new Dimension(200,20));       
    _input.addKeyListener(new Adapter());

    // nastavime vystupnimu formulari font
    // ktery ma stejnou sirku vsech znaku
    _output.setFont(new Font("courier",Font.PLAIN,8));

    _output.setEditable(false);

    // vlozime do okna vstupni a vystupni bunky
    this.add(_input);
    this.add(_output);

    // nastavime velikost okna a urcime ze po zavreni okna
    // ukoncujeme cely program
    this.setSize(600,600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

  // nas objekt na odposlouchavani stisknutych klaves
  class Adapter extends KeyAdapter
  {
    public void keyReleased(KeyEvent event)
    {
      // zavola metodu prevodu hodnoty ve vstupni bunce na vystupni ASCII
      charToASCII();
    }
  }

  // metoda inicializace
  void init()
  {
    setVisible(true);
  }

  void charToASCII(){

    _output.setText("");  // Vycisti vystup
    String newVal="";    // Docasna promenna pro novy vystup

    // kontrola neprazdneho retezce
    if(_inputArea.getText().length()!=0)
    { 

      // vygeneruje nove pole
      String[] temp = new Render(_inputArea.getText(),"•").getRender();

      // prenese hodnoty z pole do docasne promenne po radcich
      for(String line : temp) newVal+=line+"\n";

  }

  // naplni vystupni formular hodnotou v docasne promenne
  _output.setText( newVal ); 

}

}

Render

package ascii;

public class Render
{

  // uloziste vystupu
  private LinkedList render = new LinkedList();

  // pri dotazu na delku retezce vratime delku retezce v poli
  private int length( ) { return render.size( ); }

  // pri dotazu na obsah retezce vratime obsah pole
  private String[] toArray()
  {
    return (String[]) render.toArray(new String[length()]);
  }

  // Slovnik ktery znak se rovna kteremu ASCII retezci
  private Sheet table = new Sheet();

  Render( String value, String to){

   String[][] array;

  // Potrebujeme zapisovat do pole podle sloupcu, nikoli podle radku
  // z duvodu ze 1 ASCII znak je na vice radku

  // Promenna ktera rika na kterem znaku prave pracuji
  int letterCounter;

  // Hlidac nepreteceni radku
  int j;
// Vystup 1 radku String oneLine; // Nasledny cyklus prochazi vsechny znaky ze vstupu // a hleda k nim ekvivalentni serii znaku pro vystup // na zaklade hodnoty na jakem radku pracujeme for(int i=0; i<5; i++) { letterCounter=0; array = getRepresentation(value.substring(0,1)); oneLine=""; for(j=0; j<5; j++){ oneLine+=array[i][j].replace("*", to); if(j==4 && letterCounter<value.length()-1) { j=-1; letterCounter++; array = getRepresentation( value.substring(letterCounter, letterCounter+1) ); } } // Do naseho vystupu pridame jeden radek render . add( oneLine ); } protected String[] getRender() { return toArray(); } protected String[][] getRepresentation(String key) { // ve slovniku mame pouze mala pismena // prevedeme tedy vstup na mala pismena key=key.toLowerCase(); // jako navratovou hodnotu urcime ekvivalentni // ASCII znak z naseho sesitu ASCII znaku return table . get( key ) ; } }

Sheet

package ascii;

class Sheet
{
  // Nase tabulka v syntaxi ZNAK/(ASCII pole)
  Hashtable<Object,String[][]> table = new Hashtable<Object,String[][]>();
  Sheet()
  {
    // konstruktor zde
  }

  // vlozeni noveho znaku do tabulky
  public void put(Object key, String[][] value)
  {
    table.put(key,value);
  }

  // ziskani ASCII znaku z tabulky
  public String[][] get(Object key)
  {
    return table.get(key);
  }
  // nalezeni znaku v ASCII na zaklade znaku vstupniho

}

// rucni napsani vytvoreni tabulky v tomto postupu
// vytvorim si docasnou promennou kterou je ctvercova
// matice (pole a*a) a do tohoto pole nadefinuji
// plne znaky jako "*" a prazdne znaky jako " "
// toto pole pak vlozim do tabulky a priradim
// mu jemu-ekvivalentni klic

// Aktualni program ma znaky a-z a 0-9
// je mozne data ukladat do souboru, pripadne
// pridat specialni znaky. To je uz na vas.
public void createTable(){

String[][] a = {
{" "," ","*"," ","  "},
{" ","*"," ","*","  "},
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "}
};

put("a",a);

String[][] b = {
{"*","*","*","*","  "},
{"*"," "," "," ","* "},
{"*","*","*","*","  "},
{"*"," "," "," ","* "},
{"*","*","*","*","  "}
};

put("b",b);

String[][] c = {
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{"*"," "," "," ","  "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("c",c);

String[][] d = {
{"*","*","*","*","  "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*","*","*","*","  "}
};

put("d",d);

String[][] e = {
{"*","*","*","*","* "},
{"*"," "," "," ","  "},
{"*","*","*","*","  "},
{"*"," "," "," ","  "},
{"*","*","*","*","* "}
};

put("e",e);

String[][] f = {
{"*","*","*","*","* "},
{"*"," "," "," ","  "},
{"*","*","*"," ","  "},
{"*"," "," "," ","  "},
{"*"," "," "," ","  "}
};

put("f",f);

String[][] g = {
{" ","*","*","*","  "},
{"*"," "," "," ","  "},
{"*"," ","*","*","* "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("g",g);

String[][] h = {
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*","*","*","*","* "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "}
};

put("h",h);

String[][] i = {
{" ","*","*","*","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "},
{" ","*","*","*","  "}
};

put("i",i);

String[][] j = {
{"*","*","*","*","* "},
{" "," "," "," ","* "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("j",j);

String[][] k = {
{"*"," "," ","*","  "},
{"*"," ","*"," ","  "},
{"*","*"," "," ","  "},
{"*"," ","*"," ","  "},
{"*"," "," ","*","  "}
};

put("k",k);

String[][] l = {
{"*"," "," "," ","  "},
{"*"," "," "," ","  "},
{"*"," "," "," ","  "},
{"*"," "," "," ","  "},
{"*","*","*","*","* "}
};

put("l",l);

String[][] m = {
{"*"," "," "," ","* "},
{"*","*"," ","*","* "},
{"*"," ","*"," ","* "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "}
};

put("m",m);

String[][] n = {
{"*"," "," "," ","* "},
{"*","*"," "," ","* "},
{"*"," ","*"," ","* "},
{"*"," "," ","*","* "},
{"*"," "," "," ","* "}
};

put("n",n);

String[][] o = {
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("o",o);

String[][] p = {
{"*","*","*","*","  "},
{"*"," "," "," ","* "},
{"*","*","*","*","  "},
{"*"," "," "," ","  "},
{"*"," "," "," ","  "}
};

put("p",p);

String[][] q = {
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*"," "," ","*","  "},
{" ","*","*"," ","* "}
};

put("q",q);

String[][] r = {
{"*","*","*","*","  "},
{"*"," "," "," ","* "},
{"*","*","*","*","  "},
{"*"," ","*"," ","  "},
{"*"," "," ","*","  "}
};

put("r",r);

String[][] s = {
{" ","*","*","*","* "},
{"*"," "," "," ","  "},
{" ","*","*","*","  "},
{" "," "," "," ","* "},
{"*","*","*","*","  "}
};

put("s",s);

String[][] t = {
{"*","*","*","*","* "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "}
};

put("t",t);

String[][] u = {
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("u",u);

String[][] v = {
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{" ","*"," ","*","  "},
{" ","*"," ","*","  "},
{" "," ","*"," ","  "}
};

put("v",v);

String[][] w = {
{"*"," "," "," ","* "},
{"*"," "," "," ","* "},
{"*"," ","*"," ","* "},
{"*"," ","*"," ","* "},
{" ","*"," ","*","  "}
};

put("w",w);

String[][] x = {
{"*"," "," "," ","* "},
{" ","*"," ","*","  "},
{" "," ","*"," ","  "},
{" ","*"," ","*","  "},
{"*"," "," "," ","* "}
};

put("x",x);

String[][] y = {
{"*"," "," "," ","* "},
{" ","*"," ","*","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "}
};

put("y",y);

String[][] z = {
{"*","*","*","*","* "},
{" "," "," ","*","  "},
{" "," ","*"," ","  "},
{" ","*"," "," ","  "},
{"*","*","*","*","* "}
};

put("z",z);

String[][] nula = {
{" ","*","*","*","  "},
{"*","*"," "," ","* "},
{"*"," ","*"," ","* "},
{"*"," "," ","*","* "},
{" ","*","*","*","  "}
};

put("0",nula);

String[][] jedna = {
{" "," ","*"," ","  "},
{" ","*","*"," ","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "},
{" ","*","*","*","  "}
};

put("1",jedna);

String[][] dve = {
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{" "," "," ","*","  "},
{" ","*","*"," ","  "},
{"*","*","*","*","* "}
};

put("2",dve);

String[][] tri = {
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{" "," ","*","*","  "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("3",tri);

String[][] ctyri = {
{"*"," "," "," ","  "},
{"*"," ","*"," ","  "},
{"*","*","*","*","  "},
{" "," ","*"," ","  "},
{" "," ","*"," ","  "}
};

put("4",ctyri);

String[][] pet = {
{"*","*","*","*","* "},
{"*"," "," "," ","  "},
{"*","*","*","*","  "},
{" "," "," "," ","* "},
{"*","*","*","*","  "}
};

put("5",pet);

String[][] sest = {
{" ","*","*","*","  "},
{" "," "," "," ","* "},
{" ","*","*","*","* "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("6",sest);

String[][] sedm = {
{"*","*","*","*","  "},
{" "," "," ","*","  "},
{" "," ","*","*","* "},
{" "," "," ","*","  "},
{" "," "," ","*","  "}
};

put("7",sedm);

String[][] osm = {
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{" ","*","*","*","  "}
};

put("8",osm);

String[][] devet = {
{" ","*","*","*","  "},
{"*"," "," "," ","* "},
{" ","*","*","*","* "},
{" "," "," "," ","* "},
{" "," "," "," ","* "}
};

put("9",devet);

String[][] space = {
{" "," "," "," ","  "},
{" "," "," "," ","  "},
{" "," "," "," ","  "},
{" "," "," "," ","  "},
{" "," "," "," ","  "}
};

put(" ",space);
}

Komentáře: 28

  1. how to get abs
    6 Zář ’11

    Thumb up 0 Thumb down 0

    The heading of your post – Generate ASCII image text – caught my eye, so I came by to check it out. Glad I did. FYI – I also bookmarked this page – http://jiny-svet.cz/word/2011/02/generator-asci-obrazku-z-textu/ – on StumbleUpon so others can find it too.

    • jancajthaml
      15 Zář ’11

      Thumb up 0 Thumb down 0

      I’m up to rewrite this post more user friendly.

  2. Andrew Johnson
    24 Zář ’11

    Thumb up 0 Thumb down 0

    To begin with Off, allow me commend your clearness on this matter. I’m not an expert on this subject matter, but soon after reading through your report, my understanding has produced substantially. Remember to allow me to grab your rss feed to remain in contact with any forthcoming updates. Positive task and will offer it on to acquaintances and my readers.I would want to thank you to the efforts you have produced in composing this text. I’m heading for the same greatest function from you sooner or later also. The truth is your fanciful composing abilities has prompted me to get started on my own blog site now. Truly the running a blog is spreading its wings speedily. Your publish up is usually a good example of it.

  3. Chicken Coop Plans
    24 Zář ’11

    Thumb up 0 Thumb down 0

    Love It…

    Awesome Site much thanks for making time to post this info here please keep up the great work thanks be back soon…

    • jancajthaml
      21 Lis ’11

      Thumb up 0 Thumb down 0

      Will do :)

  4. teeter inversion tables
    28 Zář ’11

    Thumb up 0 Thumb down 0

    I would love to thanks with the efforts you’ve made in composing this post. I am heading for your exact same best do the job from you in the future also. Actually your fanciful producing talents has prompted me to begin my own weblog now. Essentially the blogging is spreading its wings quickly. Your publish up can be a great example of it.

  5. Fansedge Coupon
    6 Říj ’11

    Thumb up 0 Thumb down 0

    I believe other website proprietors should take this website as an model, very clean and excellent user genial style and design .

  6. lens adapter
    10 Říj ’11

    Thumb up 0 Thumb down 0

    Hey, Wonderful article, I’ve bookmarked this page and have a feeling I’ll be returning to it regularly.

  7. dtg printer
    16 Lis ’11

    Thumb up 0 Thumb down 0

    you could have an incredible weblog here! would you like to make some invite posts on my weblog?

  8. Sally
    17 Lis ’11

    Thumb up 0 Thumb down 0

    The core of your writing whilst appearing agreeable in the beginning, did not sit very well with me personally after some time. Someplace within the paragraphs you actually managed to make me a believer but only for a short while. I however have got a problem with your leaps in assumptions and one would do well to help fill in all those breaks. In the event that you actually can accomplish that, I would definitely be impressed.

  9. Herbert Pavlik
    17 Lis ’11

    Thumb up 0 Thumb down 0

    good publish but your site loading very gradual :-( .

    • jancajthaml
      21 Lis ’11

      Thumb up 0 Thumb down 0

      Working on it…

  10. dcf kids
    17 Lis ’11

    Thumb up 0 Thumb down 0

    wonderful weblog, thanks for the info

  11. Brigette Stemarie
    18 Lis ’11

    Thumb up 0 Thumb down 0

    I do not even know how I ended up here, but I thought this post was good. I do not know who you are but certainly you’re going to a famous blogger if you aren’t already ;) Cheers!

  12. Lisette Salaz
    18 Lis ’11

    Thumb up 0 Thumb down 0

    I am not sure where you’re getting your information, but good topic. I needs to spend some time learning more or understanding more. Thanks for fantastic information I was looking for this information for my mission. Fantastic beat ! I wish to apprentice while you amend your website, how could i subscribe for a blog site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear concept

    Best regards

    • jancajthaml
      21 Lis ’11

      Thumb up 0 Thumb down 0

      By time I decided to write „learn-how-to“ site where anyone can learn anything in few steps (the start kick).
      Its just a lack of time why a few of my posts are uncomplete. In the furute there will be a lot of desgin/developing HOWTOs and templates.
      Just a lack of time.
      Nowadays I am focusing on Java, because its the simpliest useful programming language (Pearl is next, then Python and C# … but Java is a begining to understand HOWTO write good codes ).
      Where do I get my information? Lifetime experience.

  13. Five Panel Drug Test
    20 Lis ’11

    Thumb up 0 Thumb down 0

    While nodding in agreement, I find myself thinking, „Without a struggle there can be no progress..“.

  14. Marcy Rizzuti
    20 Lis ’11

    Thumb up 0 Thumb down 0

    Hello are using WordPress for your blog platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you need any html coding expertise to make your own blog? Any help would be greatly appreciated!

    • jancajthaml
      21 Lis ’11

      Thumb up 0 Thumb down 0

      WordPress is just a packet for bloging. You don’t need any experiense and your webblog will be still up. You have some experiense and you can modify something in your „kernel“ of wordpress. You have a serious experiense and you can write your own plugins and or modify anything. WordPress is just a package of „stuff for blogging“

  15. 關鍵字優化
    21 Lis ’11

    Thumb up 0 Thumb down 0

    I truly like your method of blogging. I bookmarked it to my bookmark website listing and might be checking back soon.

  16. 茶葉禮盒
    21 Lis ’11

    Thumb up 0 Thumb down 0

    Useful content and awesome design you got here! I want to thank you for sharing your solutions and taking the time into the stuff you publish! Sublime work!

  17. Thumb up 0 Thumb down 0

    Our deeds determine us as much as we determine our deeds.

  18. Diet To Go Coupon Code
    22 Lis ’11

    Thumb up 0 Thumb down 0

    You have observed very interesting points ! ps decent site.

  19. hale metalice suceava
    24 Lis ’11

    Thumb up 0 Thumb down 0

    2 Minutes well spent, im gonna come back to this blog

  20. Jam Tangan Original
    1 Led ’12

    Thumb up 0 Thumb down 0

    Its such as you learn my mind! You appear to know so much about this, such as you wrote the ebook in it or something. I believe that you simply could do with a few % to power the message home a bit, however instead of that, this is magnificent blog. An excellent read. I’ll certainly be back.

  21. Engine Oil Wholesale
    17 Úno ’12

    Thumb up 0 Thumb down 0

    Digg…

    While checking out DIGG today I noticed this…

  22. Thumb up 0 Thumb down 0

    Wikia…

    Wika linked to this website…

  23. Thumb up 0 Thumb down 0

    Great post, I conceive people should learn a lot from this web blog its really user friendly .

Napsat komentář