need help with design to separate model and gui code?

les say i have 3 classes.

Main.java
BoxGui.java
BoxModel.java



public classBoxModel
{
    BoxGui bg = new BoxGui();

    public void moveBox(){
        int x = bg.getX();
        bg.setX(x+1);
    }
}

public class BoxGui
{
    int x,y,width,height;
    public void paint(Graphics g){
        g.drawRect(x, y, width, height);
    }
}


public class Main{
    BoxModel bm = new BoxModel();
     BoxGui bg = new BoxGui();
    ...
     public void paintComponent(Graphics g) {
         bm.moveBox();
     }
     ...

      public void paint(Graphics g){
          bg.paint(g);
      }
}

I guess i dont understand which class should have which class object. for ex:
in main do we create BoxModel or BoxGui or both obj?
in BoxModel do we need to create BoxGui obj?
in BoxGui do we need to create BoxModel obj?

Typically you would have main create the model then create the GUI, passing a ref to the model into the GUI constructor, so the GUI can use the model's accessor methods.

Model m - new Model(...) // model knows nothing about the GUI
GUI g = new GUI(m, ...)  // ... but the GUI has to use the model

This minimises the copupling between the GUI and the model, reducing it to the absolute minimum needed to make it work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.