java - How to create a single drawingpanel that uses multiple classes to create graphics -


i looking starting point, not answer, more trying understand concept. how windows on 1 panel/window?

creates facebookperson:

public class facebookperson{  private string myname; protected string mymood; protected facebook myfacebook;  public facebookperson(string name){   myname = name;   myfacebook = new facebook(myname);   //system.out.println("facebookperson_graphics's constructor"); }  public facebookperson(){  }  public string getname(){   return myname; }  public void setmood(string newmood){  mymood = newmood;  myfacebook.setcontent(mymood); }  public string getmood(){   return mymood; }  } 

this code creates , edits:

package facebook;  import java.awt.*;  public class facebook{  private string name; private string content; drawingpanel panel; private graphics g;  public facebook(string nm){    content = "undefined";    name = nm;     // create drawing panel    panel =new drawingpanel(200,150);    g = panel.getgraphics();    // display name    g.drawstring(name+"'s mood undefined.", 20, 75);   }  public void setcontent(string newcontent){ content = newcontent;     if(content.equals("happy")){         g.setcolor(color.red);         g.fillrect(0, 0, 200, 150);         g.setcolor(color.black);         // display mood         g.drawstring(name+"'s mood is:"+ "happy", 20, 75);     }     else{         g.setcolor(color.white);         g.fillrect(0, 0, 200, 150);         g.setcolor(color.black);         g.drawstring(name+"'s mood is:"+ content, 20, 75);     }  }        public string getcontent(){         return content;   }    } 

creates user interface implement 2 classes:

 package facebook;   import java.util.*;   public class testfacebook{    public static void main (string[] args){  // prompt user enter number of facebookpresons scanner userinput = new scanner(system.in); system.out.println("enter number of facebookpresons created: "); int nump=0; while(true){     try{         nump = userinput.nextint();         userinput.nextline();         if(nump>0 && nump<=9)  // accept number if within range. here define range 1 5.              break;         else             system.out.println("the number out of range [1, 9]! enter again");     } catch (inputmismatchexception e){         system.out.println("invalid input. enter integer number!");         userinput.nextline();     } }  facebookperson[] fbp = new facebookperson[nump];  //ask user enter name each person, , create persons for(int i=0; i< nump; i++){     system.out.println("enter name person "+ (i+1)+ ":");     string name = userinput.nextline();     fbp[i] = new facebookperson(name); } system.out.println("-------select person , type mood below--------");  //ask user set mood person, , update mood, enter "####" exit while(true){     system.out.println("enter name person (enter #### exit):");     string name = userinput.nextline();     if(name.equals("####"))          system.exit(0);     int personid = -1;     for(int i=0; i< nump; i++){         if(fbp[i].getname().equals(name)){             personid = i;             break;  // break loop         }     }     if(personid!=-1){  // found person, otherwise personid should still -1         system.out.println("enter mood person:");         string mood = userinput.nextline();         fbp[personid].setmood(mood);     }     else         system.out.println("unrecognized name!"); } // end while  } // end main  } 

lets start not doing g = panel.getgraphics();, not how custom painting works. take @ painting in awt , swing , performing custom painting more details how painting should done.

start defining concept of "paintable" or "drawable" (use interface defines entry point can paint, can paint paintable)

public interface drawable {     public void draw(graphics2d g2d, jcomponent parent); // other parameters might useful... }  

anything want painted on "drawable surface" should implement interface.

create "drawable surface" can take list of drawable objects and, well, draw them...

public class drawablepane extends jpanel {     private list<drawable> drawables;      public drawablepane() {         drawables = new arraylist<>(25);     }      public void add(drawable drawable) {         drawables.add(drawable);         repaint();     }      public void remove(drawable drawable) {         drawables.remove(drawable);         repaint();     }      @override     protected void paintcomponent(graphics g) {         (drawable drawable : drawables) {             graphics2d g2d = (graphics2d)g.create();             drawable(g2d, this);             g2d.dispose();         }     } } 

as idea...


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -