public void paint(Graphics g, JComponent c) { //若是當前被設計的組件不爲空,則畫出該組件來 if ((selected_components != null) &&!selected_components.isEmpty()) {佈局 //若是當前選中的組件不爲空,則畫出他們的resizing邊框來 if (selection_bounds != null) {url //若是當前正在選擇組件,畫出選擇框來 private void paintResizing(Graphics g, JComponent c) {設計 //當前這些選中的組件是不是能夠resize的component //帶有佈局管理器的容器組件內的組件不該該能resizeablehtm //另外若是所選擇的組件跨了幾個容器,它們也不該該能resizeable for (Component comp : selected_components) { Rectangle bounds = getRelativeBounds(comp); g.setColor(SELECTION_COLOR); int x = bounds.x + LEFT_PAD; //畫出被選擇組件的高亮邊框 g.drawRect(x, y, w, h); if (resizable) { //若是是resizable,要畫出其拖拽點矩形 drawResizingThumbs(g, x, y, w, h); private void paintDesignedComponent(Graphics g, JComponent c) { //這兒和之前講過的同樣,再使用Renderer技術時,要保證渲染組件是非緩衝的 disableBuffer(component, dbcomponents); int width = component.getWidth(); //建立相對根組件的剪切窗口圖形對象 //使用跟組件渲染 component.paint(clipg); int x = LEFT_PAD - BORDER_THICK; //畫出外邊框來 areaBorder.paintBorder(component, g, x, y, w, h); |
時間很少了,今天先寫到這兒吧。明天再詳細講述如何給界面設計工具加上選擇、鍵盤、編輯等功能。這兒有初步的代碼下載。已經能進行組件選擇、拖放、添加、刪除、複製、粘貼等簡單的編輯動做了。下面是個截圖:
更新:
這兩天作了進一步重構,添加了一些新的功能和註釋。如今的設計界面工具能夠進行佈局管理,添加了添加提示、添加、刪除、複製、剪切、粘帖、對齊等功能,把組件選擇工具條作了修改,能夠添加容器類等等。下面是幾個截圖: