java - ActionPerformed & update(Model,Object) in mvc and GridBagLayout -
i have severall question , excuse denglish! problems:
- my actionperformed in controller dont react
- and dont know how use update(model,object) method in view.
- the next problem gridbag. looks chaotic although code orientated on oracle example.
- no last one. in view line 41 npe result of class initialised in model. dont no why. allready testes if class constructed , yes is.
the programm should greate juliaset. classes without theire imports controller:
public class juliacontroller extends windowadapter implements actionlistener { private juliaview view; private juliamodel model; private string linkbuffer; public juliacontroller(){ model = new juliamodel(); system.out.println("true!"); view = new juliaview("juliamenge"); view.makeview(); } @override public void actionperformed(actionevent arg0) { string action = arg0.getactioncommand(); if(action.equals(view.action_clear)) { //clear graphics view.setbtclear(); } if(action.equals(view.action_complex)) { string input = view.getcomplex(); view.settfcomplex(); //zurücksetzen des felds! model.juliaberechnung(input); view.getbtpaint(); //setzt den button auf anklickbar //paint button anklickbar machen } if(action.equals(view.action_ende)) { view.release(); system.exit(0); } if(action.equals(view.action_link)) { string inlink = view.gettflink(); view.settflink(); try { model.juliaberechnung(datainlist(inlink)); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } view.getbtpaint(); } if(action.equals(view.action_paint)) { //erzeuge graphics! view.getbtpaint(); //button nicht mehr anklickbarkeit } } public string datainlist(string link)throws ioexception { string temp=""; bufferedreader inbuffer = null; try { inbuffer = new bufferedreader(new filereader(new file(link))); while((temp=inbuffer.readline())!=null) { //system.out.println("buffer : "+temp); linkbuffer += temp; } } catch (filenotfoundexception e) { e.printstacktrace(); } int dateilength = linkbuffer.length(); inbuffer.close(); char c; int index=0; for(int = index;index<dateilength;index++){ c = linkbuffer.charat(index); if((c=='+')||(c=='-'))break; } temp = ""; for(int i=index;i<index+8;i++) { temp+=linkbuffer.charat(i); } return temp; //keine anhang in der datei } public void release() { // todo auto-generated method stub model = null; view = null; } public void windowclosing( windowevent we) { view.release(); }
}
view:
public class juliaview extends jframe implements observer{ //:::: actions public final string action_ende = "ende"; public final string action_paint = "paint"; public final string action_complex = "+a.x+b.x"; public final string action_link = "link"; public final string action_clear = "clear"; //:::: components private jbutton btende; private jbutton btpaint; private jbutton btclear; public juliapanel drawarea; //bereich für paint //private jbutton btautopaint; private jtextfield tfcomplex; private jtextfield tflink; //:::: observer private juliamodel model; private juliacontroller controller; private juliabild map; public juliaview(string titel){ super(titel); this.model = model; //this.map = model.getmap(); //??????//iterationsarray einbinden //this.model.addobserver(this); //controller = makecontroller(); initform(); //makeview(); } void makeview() { resetview(); // fenster addwindowlistener( controller); pack(); setvisible( true); } /*private juliacontroller makecontroller() { ????????????????? return new juliacontroller(); }*/ /** * anordnen der komponenten im gridbag */ private void initform(){ this.setdefaultcloseoperation(jframe.exit_on_close); this.setlayout(new gridbaglayout()); this.setbounds(200, 200, 800, 600); gridbagconstraints c = new gridbagconstraints(); c.insets = new insets(15,15,15,15); //this.add(btautopaint,c); // textfield für kompleze zahl tfcomplex = new jtextfield(action_complex,8); tfcomplex.addactionlistener(controller); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 0; c.gridwidth = 2; this.getcontentpane().add(tfcomplex,c); // link tflink = new jtextfield(action_link); tflink.addactionlistener(controller); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 1; c.gridwidth = 2; this.getcontentpane().add(tflink,c); // paint button btpaint = new jbutton(action_paint); btpaint.setenabled(false); btpaint.addactionlistener(controller); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 2; c.gridwidth = 2; c.ipady = 40; this.getcontentpane().add(btpaint,c); //cls button btclear = new jbutton(action_clear); btclear.addactionlistener(controller); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 4; c.gridwidth = 2; this.getcontentpane().add(btclear,c); //exit button btende = new jbutton(action_ende); btende.addactionlistener(controller); c.fill = gridbagconstraints.horizontal; c.gridx = 1; c.gridy = 5; c.gridwidth = 1; this.getcontentpane().add(btende,c); //draw area drawarea = new juliapanel(); //place juliaset c.fill = gridbagconstraints.horizontal; c.gridx = 2; c.gridy = 0; c.gridwidth = 6; this.getcontentpane().add(drawarea,c); setvisible( true); pack(); } public void resetview(){ this.tfcomplex.settext("+a.x+b.x"); this.tflink.settext("c:/..."); //drawarea.paint(g); } @override public void update(observable m, object o) { // todo auto-generated method stub if(model == m) setbtpaint(); //??? } public void setbtpaint() { btpaint.settext(action_paint); btpaint.requestfocus(); btpaint.setenabled(true); drawarea.setkoordinaten(map); //zeichnen //drawarea.repaint(); drawarea.print(getgraphics()); //drawarea.paint(getgraphics()); } public void getbtpaint() { btpaint.settext(action_paint); drawarea.createimage(); btpaint.requestfocus(); btpaint.setenabled(false); //anklickbarkeit } public void setbtclear() { btclear.settext(action_clear); resetview(); drawarea.clearimage(); btclear.requestfocus(); } public void settfcomplex() { tfcomplex.settext(action_complex); tfcomplex.setcaretposition(tfcomplex.gettext().length()); tfcomplex.requestfocus(); } public void settflink() { tflink.settext(action_link); tflink.setcaretposition(tflink.gettext().length()); tflink.requestfocus(); } public void getbtende() { btende.settext(action_ende); btende.requestfocus(); } public string getcomplex() { string temp = ""; temp = tfcomplex.gettext(); return temp; } public string gettflink() { string temp = tflink.gettext(); return temp; } public void release(){ dispose(); // controller controller.release(); controller = null; // model model.deleteobserver( this); model = null; }
} model:
public class juliamodel extends observable{ //private complex startwert; private complex konst; private string pfad; private juliabild map; public juliamodel() { //startwert = new complex(re, im); konst = new complex(1.0,1.0); pfad = ""; makebild(); } private void makebild() { map = new juliabild(600,600); system.out.println("bild true"); } public juliamodel(double rek, double imk) { map = new juliabild(600,600); //startwert = new complex(re, im); konst = new complex(rek,imk); pfad = ""; } private int grundalgoit(double x,double y) { complex temp = new complex(x,y); double xtemp=0; for(int n=0;n<256;n++) { if(temp.abs()<4){return n;} xtemp = math.pow(temp.getre(), 2)-math.pow(temp.getim(), 2) + konst.getre(); temp.setim((2*temp.getim()*temp.getre())+konst.getim());//y= temp.setre(xtemp); } return 0; } void juliaberechnung(string input) { double xmin = -2.0;//fx double ymin = -2.0;//fy double width = 4.0;//fw //weg im koordinatensystem double height = 4.0;//fh double schrittweite = 4.0/600; stringtokonst(input); //complex temp = new complex(xmin-schrittweite,1); double xtemp,ytemp; xtemp = xmin-schrittweite; //durch die pixel gehen for(int j=0;j<600;j++) { xtemp = xtemp+schrittweite; ytemp = ymin; for(int i=0;i<600;i++) { ytemp = ytemp + schrittweite; map.setbild(j,i,grundalgoit(xtemp,ytemp)); } } } private void stringtokonst(string in) { char cbuffer; string sbuffer=""; double im = 0; double re = 0; for(int a=0;a<=4;a+=4) for(int i=0;i<4;i++) { cbuffer = in.charat(i+a); sbuffer+=cbuffer; if(a==0&&i==4) { re = double.parsedouble(sbuffer); } if(a==4&&i==4) { im = double.parsedouble(sbuffer); } } konst.setnumber(re, im); } public juliabild getmap() { return map; }}
thanks help!
my actionperformed in controller dont react
this because never register controller capable of producing actionevent
and dont know how use update(model,object) method in view.
the update
method called on behalf of model when it's state changed. should update view based on new state of model.
if model supports multiple properties, propertychangelistener
might better suited. if model has different states, might better defining own listener, defines states model might trigger.
but, again, never called, never add observer
it
the next problem gridbag. looks chaotic although code orientated on oracle example.
yep, that's gridbaglayout
you
no last one. in view line 41 npe result of class initialised in model. dont no why. allready testes if class constructed , yes is
which one's line 41? what's stack trace like?
general advice
- mvc not easy thing implement within swing, swing uses form of vc-m of it's own, kind of end doubling effort. it's not can't done, need think differently
- in forms of mvc, controller knows model , view, model , view don't know each other , communication between model , view done via controller. kinda of swing implementation deviates, should aware how it's more commonly implemented
start defining contract between view , controller. define actions controller can perform on view event listeners view willing accept.
generally, avoid attaching listeners directly view controls controller , instead define events view can generate, in way, decouple contract between 2 , allow different implementations of view , controller work together.
for example, if have button suppose signify user read process input, instead of allowing controller directly add actionlistener
button, define useracceptedinput
event in 1 of listeners view supports. way, implementation use other mechanism trigger event, controller doesn't care.
for more discussions on subject have at:
Comments
Post a Comment