1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217:
============DndTest.java=========== package juhan.exer.dnd; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.KeyEvent; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JToolBar; import javax.swing.SwingUtilities; import javax.swing.UIManager; /** * * @author juhshen */ public class DndTest extends JFrame { private JDesktopPane dp = new JDesktopPane(); private JSequencePanel sequenceDiagram = new JSequencePanel(); public DndTest(){ super("Dnd Test"); init(); } private void init() { setJMenuBar(createDummyMenuBar()); getContentPane().add(createDummyToolBar(), BorderLayout.NORTH); JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, dp, sequenceDiagram); sp.setDividerLocation(120); getContentPane().add(sp); sequenceDiagram.addClass(new Clazz()); } private JToolBar createDummyToolBar() { JToolBar tb = new JToolBar(); JButton b; b = new JButton("New"); b.setRequestFocusEnabled(false); tb.add(b); b = new JButton("Open"); b.setRequestFocusEnabled(false); tb.add(b); b = new JButton("Save"); b.setRequestFocusEnabled(false); tb.add(b); b = new JButton("Print"); b.setRequestFocusEnabled(false); tb.add(b); b = new JButton("Preview"); b.setRequestFocusEnabled(false); tb.add(b); tb.setFloatable(false); return tb; } private JMenuBar createDummyMenuBar() { JMenuBar mb = new JMenuBar(); mb.add(createDummyMenu("File")); mb.add(createDummyMenu("Edit")); mb.add(createDummyMenu("Search")); mb.add(createDummyMenu("View")); mb.add(createDummyMenu("Tools")); mb.add(createDummyMenu("Help")); JMenu demo = new JMenu("Demo"); demo.setMnemonic(KeyEvent.VK_D); mb.add(demo); return mb; } private JMenu createDummyMenu(String str) { JMenu menu = new JMenu(str); JMenuItem item = new JMenuItem("[Empty]"); item.setEnabled(false); menu.add(item); return menu; } public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { //Turn off metal's use of bold fonts UIManager.put("swing.boldMetal", Boolean.FALSE); createAndShowGUI(args); } private void createAndShowGUI(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { } DndTest test = new DndTest(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.setSize(800, 600); test.setLocationRelativeTo(null); test.setVisible(true); } }); } } ===================DndTest.java ends======================== ==================JSequencePanel.java starts================ package juhan.exer.dnd; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.ArrayList; import java.util.List; import javax.swing.JPanel; /** * * @author juhshen */ public class JSequencePanel extends JPanel { private List<Clazz> moduleList = new ArrayList(); public JSequencePanel() { super(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); for(int i=0;i<moduleList.size();i++){ moduleList.get(i).paint(g); } } public void addClass(Clazz clazz){ clazz.setFocusable(true); moduleList.add(clazz); this.add(clazz); } } ================JSequencePanel.java ends============ ==============Clazz.jva starts===================== package juhan.exer.dnd; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import javax.swing.JComponent; /** * * @author juhshen */ class Clazz extends JComponent{ private static final int defaultRectWidth = 30; private static final int defaultRectHeight = 30; private static final int defaultLineLength = 80; private Point center = new Point(50,50); public Clazz(){ super(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Point rectLT =new Point(); Point lineTop = new Point(); Point lineBottom = new Point(); CalculatePoints(rectLT,lineTop,lineBottom,center); Point testX = new Point(120,0); Point testY = new Point(130,130); Graphics2D g2 = (Graphics2D)g; // g2.setPaint(Color.BLUE); // g2.drawLine(testX.x, testX.y, testY.x, testY.y); g2.drawRect(rectLT.x,rectLT.y,defaultRectWidth,defaultRectHeight); g2.drawLine(lineTop.x, lineTop.y, lineBottom.x, lineBottom.y); } private void CalculatePoints(Point rectLT,Point lineTop, Point lineBottom, Point center) { rectLT.x=center.x-defaultRectWidth/2; rectLT.y=center.y-defaultRectHeight/2; lineTop.x=center.x; lineTop.y=center.y+defaultRectHeight/2; lineBottom.x = center.x; lineBottom.y = center.y+defaultRectHeight/2+defaultLineLength; } } ======================Clazz.java ends===================================== =======================Code ends=====================================