============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=====================================
|