java - Drawing text on envelope -


hy guys!

i need little assist on because cant figure out i'm doing wrong. i'm working on envelope writing program. program have 4 jtextfield. name, city, addres, postal code. use keylistener on name jtextfield recognize enter button, , add new name jtextfield. (basicly, sets 2nd name jtextfield visible). total have 4 name jtextfield , others (city, address, postal code) total 7. has print jbutton, jtable viewing written envelopes waiting printing on. when use g.drawstring() use predefined x,y value changing dynamicly according names field's text length measured font metrics' stringwidth() method , calculating right side margin value.. whatever. main problem here:

if (name.length() > address.length()) {              g.drawstring(name,                 (250 - namecord_x) + 46, 214);       //this working             g.drawstring(city, (250 - namecord_x) + 46, 226);             g.drawstring(address, (250 - namecord_x) + 46, 238);             g.drawstring(postal, (250 - namecord_x + 46, 250);         } else {             if (address.length() > name.length()                 && name2.isempty())             { // working                 g.drawstring(name, (250 - addresscord_x) + 46, 214);                 g.drawstring(city, (250 - addresscord_x) + 46, 226);                 g.drawstring(address, (250 - addresscord_x) + 46, 238);                 g.drawstring(postal, (250 - addresscord_x + 46, 250);              } }                   if (name2.length() > name.length()                     && name2.length() > address.length()                     && name2.isvisible())                 {// working                      g.drawstring(name, (250 - name2cord_x) + 46, 202);                     g.drawstring(name2, (250 - name2cord_x) + 46, 214);                     g.drawstring(city, (250 - name2cord_x) + 46, 226);                     g.drawstring(address, (250 - name2cord_x) + 46, 238);                     g.drawstring(postal, (250 - name2cord_x) + 46, 250);                  }       //this part doesnt working. prints out 5 lines       // 2nd line same first line,       // , real 2nd line value printed over. // example name: lion name2:lion (altough typed // example horse name2 field, , program draw // horse string on 2nd line contains lion.)   //altough prints out 5 row, messing //the second row :-/                        if( (name.length() > name2.length()                     && name.length() > address.length()                     && name2.isvisible()))                 {                     g.drawstring(name, (250 - namecord_x) + 46, 202);                     g.drawstring(name2, (250 - namecord_x) + 46, 214);                     g.drawstring(city, (250 - namecord_x) + 46, 226);                     g.drawstring(address, (250 - namecord_x) + 46, 238);                     g.drawstring(postal, (250 - namecord_x) + 46, 250);                    } 

you have complicated if blocks there. not clear types of variables are. seems string e.g. call isvisible() on name2.

i understand using namecord_x, addresscord_x, name2cord_x alignment purposes. want strings left aligned , start drawing @ point allow largest string correctly fit not exceed right side of envelope. correct?

if yes, suggest first find maximum of above lengths. should take each 1 account if has meaningful value. can following:

int max = ... // maximum of above values - not empty values should taken account int dy = 12;  // seems leave 12 pixels between lines int y = ...;  // value topmost y. seems use 202 when both names present 214 when name2 not given int x = 250 - max + 46;  string[] labels = new string[] {name, name2, city, address, postal}; (string label : labels) {     if (label == null || label.length() == 0) {         continue; // ignore null or empty values     }     g.drawstring(name, x, y);     y += dy; }     

i hope helps


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -