제8장 MVC Model
8.1 MVC 모델 (1/7) MVC (Model, View, Controller) 모델 스윙은 MVC 모델에기초를두고있다. MVC란 Xerox의연구소에서 Smalltalk 언어를바탕으로사용자인터페이스를개발하기위한방법 MVC는 3개의구성요소로구성 Model : 응용프로그램의자료를표현하기위한모델 View : 자료를시각적으로 (GUI 방식으로 ) 표현하는것 Controller: 사용자가 View를통해입력을하면 Model을변경해주는것 MVC Model 2
8.1 MVC 모델 (2/7) MVC 모델 모델, 뷰, 컨트롤러를분리한 MVC 모델은상당히유용하고강력한프로그래밍모델이기때문에사용자인터페이스개발에많이적용 스윙을개발하면서 MVC 모델을그대로적용하기에는실제적인어려움이많아서 MVC를변형한모델을개발. 스윙에서는 MVC의뷰와컨트롤러를합쳐서 "UI delegate" 라는객체를정의. MVC 모델 내부상태, 자료관리 UI delegate 모양결정, 이벤트 MVC Model 3
8.1 MVC 모델 (3/7) 스윙의모든 GUI 컴포넌트들은 UI delegate 부분과 Model 부분으로구성 UI delegate 부분은 GUI 컴포넌트의모양과사용자의입력에따른이벤트를처리 모델의역할 내부상태를알아본다. 내부상태를관리한다. 이벤트리스너를추가 / 삭제한다. 이벤트를발생시킨다. UI delegate 의역할 GUI 컴포넌트를그려준다. GUI 컴포넌트의위치정보를리턴한다. AWT 이벤트를처리한다. MVC Model 4
8.1 MVC 모델 (4/7) 각컴포넌트의모델인터페이스와모델타입 (I) MVC Model 5
8.1 MVC 모델 (5/7) 각컴포넌트의모델인터페이스와모델타입 (II) MVC Model 6
예제 : CountButtonModel.java 8.1 MVC 모델 (6/7) 4 public class CountButtonModel extends DefaultButtonModel { 5 private int count; 6 private JButton btn; 7 8 public CountButtonModel(JButton btn) { 9 this.btn = btn; 10 btn.setmodel(this); 11 } 13 public void setpressed(boolean b) { 14 if(b) { 15 count = ++count % 4; 16 switch(count) { 17 case 0: 18 btn.setbackground(color.lightgray); MVC Model 7
8.1 MVC 모델 (7/7) 예제 : ButtonModelTest.java 8 ImageIcon rai = new ImageIcon("rai.gif"); 9 ok = new JButton("OK", rai); 10 ok.setrollovericon(rai); 11 ok.setpressedicon(rai); 12 CountButtonModel model = new CountButtonModel(ok); 13 14 getcontentpane().add("south", ok); 15 setdefaultcloseoperation(jframe.exit_on_close); 16 setsize(400, 300); 17 setvisible(true); MVC Model 8
8.2 MVC 모델예제 (1/12) 새로운컴포넌트 (ShipComponent) 만들기 MVC Model 9
8.2 MVC 모델예제 (2/12) MVCtest.java ShipModel.java ShipComponent.java ShipComponentUI.java DefaultShipModel.java ImgShipComponent.java MVC Model 10
8.2 MVC 모델예제 (3/12) 예제 : ShipComponent.java 5 public class ShipComponent extends JComponent { 6 private ShipModel model; 7 private ShipComponentUI ui; 8 private ActionListener actionlistener; 9 private boolean pressed; 10 11 public ShipComponent(String s, String d) { 12 enableevents(awtevent.mouse_event_mask); 13 model = new DefaultShipModel(s, d); 14 setui(new ShipComponentUI()); 15 } MVC Model 11
8.2 MVC 모델예제 (4/12) 예제 : ShipComponent.java( 계속 ) 22 public void paint(graphics g) { 23 ui.paint(this, g); 24 } 35 public void setui(shipcomponentui ui) { 36 if(ui!= null && this.ui!= null) { 37 this.ui.uninstallui(this); 38 } 40 if(ui!= null) { 41 this.ui = ui; 42 this.ui.installui(this); 43 } 44 updateshipcomponent(); MVC Model 12
8.2 MVC 모델예제 (5/12) 예제 : ShipComponent.java( 계속 ) 51 public Dimension getpreferredsize() { 52 return ui.getpreferredsize(this); 53 } 67 public boolean ispressed() { 68 return pressed; 69 } 70 71 public void addactionlistener(actionlistener listener) { 72 actionlistener = 73 AWTEventMulticaster.add(actionListener, listener); 74 } MVC Model 13
8.2 MVC 모델예제 (6/12) 예제 : ShipComponent.java( 계속 ) 81 public void processmouseevent(mouseevent e) { 82 Graphics g; 83 switch(e.getid()) { 88 case MouseEvent.MOUSE_RELEASED: 89 if(actionlistener!= null) { 90 actionlistener.actionperformed(new ActionEvent(this, 91 ActionEvent.ACTION_PERFORMED, model.getdata())); 92 } 93 if(pressed == true) { 94 pressed = false; 95 repaint(); MVC Model 14
8.2 MVC 모델예제 (7/12) 예제 : ShipComponentUI.java 5 public class ShipComponentUI extends ComponentUI { 6 private Dimension dim; 32 public void paint(shipcomponent ship, Graphics g) { 37 int xs[] = {2, d.width-2, 2*d.width/3, d.width/3, 2}; 38 int ys[] = { 2, 2, d.height -2, d.height - 2, 2}; 39 Color fg = g.getcolor(); 40 g.setcolor(new Color(200, 200, 200)); 41 g.fillpolygon(xs, ys, xs.length); 42 g.setcolor(fg); 43 if(ship.ispressed()) { 44 g.drawpolygon(xs, ys, xs.length); MVC Model 15
8.2 MVC 모델예제 (8/12) 예제 : ImgShipComponentUI.java 3 public class ImgShipComponentUI extends ShipComponentUI { 4 private Image img; 37 public void paint(shipcomponent ship, Graphics g) { 38 Dimension d = ship.getsize(); 39 ShipModel model = ship.getmodel(); 40 ship.settooltiptext(model.getdata()); 41 if(img!= null) { 42 int x = (d.width - img.getwidth(ship))/2; 43 int y = (d.height - img.getheight(ship))/2; 44 g.drawimage(img, x, y, ship); 45 } 46 if(ship.ispressed()) { 47 g.drawrect(1,1,d.width-2, d.height-2); MVC Model 16
8.2 MVC 모델예제 (9/12) 예제 : ShipModel.java 1 public interface ShipModel { 2 public String getdata(); 3 } MVC Model 17
8.2 MVC 모델예제 (10/12) 예제 : DefaultShipModel.java 1 public class DefaultShipModel implements ShipModel { 2 private String dest; 3 private String start; 4 5 public DefaultShipModel(String s, String d) { 6 start = s; 7 dest = d; 8 } 9 10 public String getdata() { 11 return start + " -> " + dest; MVC Model 18
8.2 MVC 모델예제 (11/12) 예제 : MVCTest.java 4 public class MVCTest extends JFrame implements ActionListener { 6 private ShipComponent tola, totokyo; 12 tola = new ShipComponent("Seoul", "LA"); 13 tola.addactionlistener(this); 14 map.add(tola); 19 totokyo = new ShipComponent("Seoul", "Tokyo"); 20 totokyo.addactionlistener(this); 21 totokyo.setui(new ImgShipComponentUI(this, "ship.jpg")); 22 map.add(totokyo); 23 totokyo.setlocation(250, 130); 24 totokyo.setsize(totokyo.getpreferredsize()); MVC Model 19
8.2 MVC 모델예제 (12/12) 결과 MVC Model 20