部落格專用相簿
[JAVA] 結合套用 UML 模擬 關係線
- 增加關係線的設置(初步)
- 修正部分設計顏色
package package1;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.*;
import java.util.*;
public class ClassDiagram extends JPanel {
private JTextField className;
private JTable attributeTable = null;
private JTable methodTable = null;
private MTableModel attributeModel = null;
private MTableModel methodModel = null;
private JScrollPane s_pan = null;
private int mouseState = 0;
private JToolBar nameMenu;
private JToolBar tableMenu;
private ArrayList<Point> attachPoint;
private ArrayList<Point> relativePoint;
private ArrayList<Object> sourcePoint;
ClassDiagram() {
Font displayFont = new Font("Serif", Font.BOLD, 18);
attachPoint = new ArrayList<Point>();
relativePoint = new ArrayList<Point>();
sourcePoint = new ArrayList<Object>();
className = new JTextField("Morris");
className.setBackground(new Color(255, 255, 185));
className.setFont(displayFont);
className.setHorizontalAlignment(JTextField.CENTER);
nameMenu = createNameMenu();
tableMenu = createTableMenu();
this.setLayout(new GridBagLayout());
GridBagConstraints GBC = new GridBagConstraints();
GBC.insets = new Insets(1, 1, 1, 1);
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor = GridBagConstraints.NORTHEAST;
GBC.gridx = 0;
GBC.gridy = 0;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.weightx = 30;
GBC.weighty = 30;
this.add(className, GBC);
TableColumnModel tcm;
String[] choose = { "+", "#", "-", "~" };
JComboBox combx = new JComboBox(choose);
attributeModel = new MTableModel();
attributeTable = new JTable(attributeModel);
attributeTable.setBackground(new Color(255, 255, 185));
tcm = attributeTable.getColumnModel();
tcm.getColumn(0).setCellEditor(new DefaultCellEditor(combx));
tcm.getColumn(0).setPreferredWidth(10);
tcm.getColumn(1).setPreferredWidth(290);
GBC = new GridBagConstraints();
GBC.insets = new Insets(1, 1, 1, 1);
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor = GridBagConstraints.NORTHEAST;
GBC.gridx = 0;
GBC.gridy = 1;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.weightx = 30;
GBC.weighty = 30;
this.add(attributeTable, GBC);
methodModel = new MTableModel();
methodTable = new JTable(methodModel);
methodTable.setBackground(new Color(255, 255, 185));
tcm = methodTable.getColumnModel();
tcm.getColumn(0).setCellEditor(new DefaultCellEditor(combx));
tcm.getColumn(0).setPreferredWidth(10);
tcm.getColumn(1).setPreferredWidth(290);
GBC = new GridBagConstraints();
GBC.insets = new Insets(1, 1, 1, 1);
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor = GridBagConstraints.NORTHEAST;
GBC.gridx = 0;
GBC.gridy = 2;
GBC.gridwidth = 1;
GBC.gridheight = 5;
GBC.weightx = 150;
GBC.weighty = 150;
this.add(methodTable, GBC);
this.setSize(280, 280);
ResizeBorder rborder = new ResizeBorder(Color.RED, 5, 10, 30);
DragObjectListener listener = new DragObjectListener();
this.setBorder(rborder);
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
this.setBackground(Color.ORANGE);
GBC = new GridBagConstraints();
GBC.insets = new Insets(1, 1, 1, 1);
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor = GridBagConstraints.NORTHEAST;
GBC.gridx = 0;
GBC.gridy = 7;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.weightx = 20;
GBC.weighty = 20;
this.add(tableMenu, GBC);
GBC = new GridBagConstraints();
GBC.insets = new Insets(1, 1, 1, 1);
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor = GridBagConstraints.NORTHEAST;
GBC.gridx = 0;
GBC.gridy = 8;
GBC.gridwidth = 1;
GBC.gridheight = 1;
GBC.weightx = 20;
GBC.weighty = 20;
this.add(nameMenu, GBC);
attributeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
attributeTable.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
methodTable.clearSelection();
if (attributeTable.getSelectedRow() >= 0)
tableMenu.setVisible(true);
}
public void focusLost(FocusEvent e) {
tableMenu.setVisible(false);
}
});
methodTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
methodTable.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
attributeTable.clearSelection();
if (methodTable.getSelectedRow() >= 0)
tableMenu.setVisible(true);
tableMenu.setVisible(true);
}
public void focusLost(FocusEvent e) {
tableMenu.setVisible(false);
}
});
}
public void addAttachPoint(Point ap, Point rp, Object obj) {
/*
* for (int i = 0; i < attachPoint.size(); i++) { Object o =
* sourcePoint.get(i); if (o == obj) { relativePoint.remove(i);
* relativePoint.add(i, rp); return; } }
*/
attachPoint.add(ap);
relativePoint.add(rp);
sourcePoint.add(obj);
System.out.println(attachPoint.size());
}
public void clearAttachPoint() {
for (int i = 0; i < attachPoint.size(); i++) {
Object o = sourcePoint.get(i);
if (o instanceof LinkSegment) {
((LinkSegment) o).clearAttachObject(this);
}
}
}
public void removeAttachPoint(Object obj) {
for (int i = 0; i < attachPoint.size(); i++) {
Object o = sourcePoint.get(i);
if (o == obj) {
attachPoint.remove(i);
relativePoint.remove(i);
sourcePoint.remove(i);
}
}
}
public void adjustAttachPoint() {
for (int i = 0; i < attachPoint.size(); i++) {
Point ap = attachPoint.get(i);
Point rp = relativePoint.get(i);
rp.x = Math.min(rp.x, this.getWidth());
rp.y = Math.min(rp.y, this.getHeight());
ap.x = rp.x + this.getX();
ap.y = rp.y + this.getY();
}
((PaintPanel) this.getParent()).updateUI();
}
public String toString() {
return className.getText();
}
void setMouseState(int val) {
mouseState = val;
if (mouseState != 0) {
((PaintPanel) (this.getParent())).setFocusPic(this);
} else {
((PaintPanel) (this.getParent())).setFocusPic(null);
}
}
int getMouseState() {
return mouseState;
}
void setToolBar(boolean val) {
nameMenu.setVisible(val);
}
public String mouseOnWhere(Point e) {
int width = (int) getBounds().getWidth();
int height = (int) getBounds().getHeight();
Point[] corner = { new Point(0, 0), new Point(width, 0),
new Point(0, height), new Point(width, height) };
int cornerIdx = -1;
for (int i = 0; i < corner.length; i++) {
if (Math.pow(e.x - corner[i].x, 2) + Math.pow(e.y - corner[i].y, 2) < 30 * 30)
cornerIdx = i;
}
if (cornerIdx == -1)
return "border";
else
return "corner" + cornerIdx;
}
private JToolBar createNameMenu() {
JToolBar ret = new JToolBar();
ImageIcon newAttrib = new ImageIcon("NewAttrib.png");
ImageIcon newMethod = new ImageIcon("NewMethod.png");
ImageIcon del = new ImageIcon("Del.png");
ImageIcon clr = new ImageIcon("Clear.png");
JButton btnAttrib = new JButton(newAttrib);
JButton btnMethod = new JButton(newMethod);
JButton btnDel = new JButton(del);
JButton btnClr = new JButton(clr);
btnAttrib.setBackground(null);
btnAttrib.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
attributeTable.clearSelection();
attributeModel.addRow("+", "AttributeName");
methodTable.clearSelection();
updateUI();
}
});
btnMethod.setBackground(null);
btnMethod.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
methodTable.clearSelection();
methodModel.addRow("+", "MethodName()");
attributeTable.clearSelection();
updateUI();
}
});
btnDel.setBackground(null);
btnDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int delIdx;
delIdx = methodTable.getSelectedRow();
methodModel.removeRow(delIdx);
delIdx = attributeTable.getSelectedRow();
attributeModel.removeRow(delIdx);
updateUI();
}
});
btnClr.setBackground(null);
btnClr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
methodTable.clearSelection();
attributeTable.clearSelection();
updateUI();
}
});
ret.add(btnAttrib);
ret.add(btnMethod);
ret.add(btnDel);
ret.add(btnClr);
ret.setBackground(null);
ret.setFloatable(false);
ret.setVisible(false);
return ret;
}
private JToolBar createTableMenu() {
JToolBar ret = new JToolBar();
ImageIcon publicImg = new ImageIcon("public.png");
ImageIcon protectImg = new ImageIcon("protected.png");
ImageIcon privateImg = new ImageIcon("private.png");
ImageIcon packageImg = new ImageIcon("package.png");
ImageIcon upImg = new ImageIcon("upitem.png");
ImageIcon downImg = new ImageIcon("downitem.png");
JButton btnPublic = new JButton(publicImg);
JButton btnProtect = new JButton(protectImg);
JButton btnPrivate = new JButton(privateImg);
JButton btnPackage = new JButton(packageImg);
JButton btnUp = new JButton(upImg);
JButton btnDown = new JButton(downImg);
btnPublic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (attributeTable.getSelectedRow() >= 0) {
int rowIdx = attributeTable.getSelectedRow();
attributeModel.setValueAt("+", rowIdx, 0);
}
if (methodTable.getSelectedRow() >= 0) {
int rowIdx = methodTable.getSelectedRow();
methodModel.setValueAt("+", rowIdx, 0);
}
}
});
btnPublic.setBackground(null);
btnProtect.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (attributeTable.getSelectedRow() >= 0) {
int rowIdx = attributeTable.getSelectedRow();
attributeModel.setValueAt("#", rowIdx, 0);
}
if (methodTable.getSelectedRow() >= 0) {
int rowIdx = methodTable.getSelectedRow();
methodModel.setValueAt("#", rowIdx, 0);
}
}
});
btnProtect.setBackground(null);
btnPrivate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (attributeTable.getSelectedRow() >= 0) {
int rowIdx = attributeTable.getSelectedRow();
attributeModel.setValueAt("-", rowIdx, 0);
}
if (methodTable.getSelectedRow() >= 0) {
int rowIdx = methodTable.getSelectedRow();
methodModel.setValueAt("-", rowIdx, 0);
}
}
});
btnPrivate.setBackground(null);
btnPackage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (attributeTable.getSelectedRow() >= 0) {
int rowIdx = attributeTable.getSelectedRow();
attributeModel.setValueAt("~", rowIdx, 0);
}
if (methodTable.getSelectedRow() >= 0) {
int rowIdx = methodTable.getSelectedRow();
methodModel.setValueAt("~", rowIdx, 0);
}
}
});
btnPackage.setBackground(null);
btnUp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (attributeTable.getSelectedRow() > 0) {
int rowIdx = attributeTable.getSelectedRow();
Object obj0, obj1;
obj0 = attributeModel.getValueAt(rowIdx, 0);
obj1 = attributeModel.getValueAt(rowIdx, 1);
attributeModel.removeRow(rowIdx);
attributeModel.insertRow(rowIdx - 1, obj0, obj1);
attributeTable.setRowSelectionInterval(rowIdx - 1,
rowIdx - 1);
}
if (methodTable.getSelectedRow() > 0) {
int rowIdx = methodTable.getSelectedRow();
Object obj0, obj1;
obj0 = methodModel.getValueAt(rowIdx, 0);
obj1 = methodModel.getValueAt(rowIdx, 1);
methodModel.removeRow(rowIdx);
methodModel.insertRow(rowIdx - 1, obj0, obj1);
methodTable.setRowSelectionInterval(rowIdx - 1, rowIdx - 1);
}
updateUI();
}
});
btnUp.setBackground(null);
btnDown.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (attributeTable.getSelectedRow() < attributeTable
.getRowCount() - 1) {
int rowIdx = attributeTable.getSelectedRow();
if (rowIdx >= 0) {
Object obj0, obj1;
obj0 = attributeModel.getValueAt(rowIdx, 0);
obj1 = attributeModel.getValueAt(rowIdx, 1);
attributeModel.removeRow(rowIdx);
attributeModel.insertRow(rowIdx + 1, obj0, obj1);
attributeTable.setRowSelectionInterval(rowIdx + 1,
rowIdx + 1);
}
}
if (methodTable.getSelectedRow() < methodTable.getRowCount() - 1) {
int rowIdx = methodTable.getSelectedRow();
if (rowIdx >= 0) {
Object obj0, obj1;
obj0 = methodModel.getValueAt(rowIdx, 0);
obj1 = methodModel.getValueAt(rowIdx, 1);
methodModel.removeRow(rowIdx);
methodModel.insertRow(rowIdx + 1, obj0, obj1);
methodTable.setRowSelectionInterval(rowIdx + 1,
rowIdx + 1);
}
}
updateUI();
}
});
btnDown.setBackground(null);
ret.add(btnPublic);
ret.add(btnProtect);
ret.add(btnPrivate);
ret.add(btnPackage);
ret.add(btnUp);
ret.add(btnDown);
ret.setBackground(null);
ret.setFloatable(false);
ret.setVisible(false);
return ret;
}
}
class MTableModel extends AbstractTableModel {
// private static final long serialVersionUID = -7495940408592595397L;
private Vector content = null;
private String[] title_name = { "TYPE", "NAME" };
public MTableModel() {
content = new Vector();
}
public MTableModel(int count) {
content = new Vector(count);
}
public void insertRow(int row, Object obj0, Object obj1) {
Vector v = new Vector(2);
v.add(0, obj0);
v.add(1, obj1);
content.add(row, v);
}
public void addRow(Object obj0, Object obj1) {
Vector v = new Vector(2);
v.add(0, obj0);
v.add(1, obj1);
content.add(v);
}
public void removeRow(int row) {
if (row < 0)
return;
content.remove(row);
}
public void removeRows(int row, int count) {
for (int i = 0; i < count; i++) {
if (content.size() > row) {
content.remove(row);
}
}
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if (columnIndex == 0)
return false;
return true;
}
public void setValueAt(Object value, int row, int col) {
((Vector) content.get(row)).remove(col);
((Vector) content.get(row)).add(col, value);
this.fireTableCellUpdated(row, col);
}
public String getColumnName(int col) {
return title_name[col];
}
public int getColumnCount() {
return title_name.length;
}
public int getRowCount() {
return content.size();
}
public Object getValueAt(int row, int col) {
return ((Vector) content.get(row)).get(col);
}
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
}
package package1;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;
/**
* more explain by Chinese language : http://javapub.iteye.com/blog/739146 &
* http://hi.baidu.com/wwwpeng584/item/668f6ef7289020db6225d24a
*
* @author owner
*
*/
public class DragObjectListener implements MouseInputListener {
Point p = new Point(0, 0);
public DragObjectListener() {
}
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
Component jl = (Component) e.getSource();
String pos = ((ClassDiagram) jl).mouseOnWhere(e.getPoint());
Point newP = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
if (pos.equals("border")) {
jl.setLocation(jl.getX() + (newP.x - p.x), jl.getY()
+ (newP.y - p.y));
((ClassDiagram) jl).adjustAttachPoint();
} else {
if (((ClassDiagram) jl).getMouseState() == 3) {
Point[] XYweight = { new Point(1, 1), new Point(0, 1),
new Point(1, 0), new Point(0, 0) };
Point[] WHweight = { new Point(-1, -1), new Point(1, -1),
new Point(-1, 1), new Point(1, 1) };
int cornerIdx = Integer.parseInt(pos.substring(6));
int vx = newP.x - p.x, vy = newP.y - p.y;
System.out.printf("%d %d\n", vx, vy);
jl.setBounds(jl.getX() + vx * XYweight[cornerIdx].x,
jl.getY() + vy * XYweight[cornerIdx].y,
jl.getWidth() + vx * WHweight[cornerIdx].x,
jl.getHeight() + vy * WHweight[cornerIdx].y);
((ClassDiagram) jl).adjustAttachPoint();
}
}
p = newP;
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
public void mouseReleased(MouseEvent e) {
Component jl = (Component) e.getSource();
if (((ClassDiagram) jl).getMouseState() == 3) {
((ClassDiagram) jl).setMouseState(0);
((ClassDiagram) jl).updateUI();
}
}
public void mouseEntered(MouseEvent e) {
Component jl = (Component) e.getSource();
((ClassDiagram) (jl)).setToolBar(true);
}
public void mouseExited(MouseEvent e) {
Component jl = (Component) e.getSource();
if (!jl.contains(e.getPoint()))
((ClassDiagram) (jl)).setToolBar(false);
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
Component jl = (Component) e.getSource();
String pos = ((ClassDiagram) jl).mouseOnWhere(e.getPoint());
if (pos.equals("border")) {
} else {
((ClassDiagram) jl).setMouseState(1);
p = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
jl.repaint();
}
}
}
}
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
Component jl = (Component) e.getSource();
String pos = ((ClassDiagram) jl).mouseOnWhere(e.getPoint());
p = SwingUtilities.convertPoint(jl, e.getPoint(), jl.getParent());
if (!pos.equals("border")) {
if (((ClassDiagram) jl).getMouseState() == 1)
((ClassDiagram) jl).setMouseState(3);
} else {
if (e.getClickCount() == 2) {
((ClassDiagram) jl).setMouseState(1);
}
}
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
}
package package1;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class LinkSegment {
private ArrayList<Point> segment;
private boolean selected;
private Object startPoint = null;
private Object endPoint = null;
LinkSegment() {
segment = new ArrayList<Point>();
segment.add(new Point(0, 0));
segment.add(new Point(100, 100));
}
LinkSegment(int bx, int by) {
segment = new ArrayList<Point>();
segment.add(new Point(bx, by));
segment.add(new Point(bx + 50, by + 50));
segment.add(new Point(bx + 100, by + 100));
}
public void clearAttachObject(Object o) {
if (startPoint == o)
startPoint = null;
if (endPoint == o)
endPoint = null;
}
public void setAttachObject(int index, Object o) {
if(index == 0) {
if(startPoint != null) {
if(startPoint instanceof ClassDiagram) {
((ClassDiagram)startPoint).removeAttachPoint(this);
}
}
startPoint = o;
} else {
if(endPoint != null) {
if(endPoint instanceof ClassDiagram) {
((ClassDiagram)endPoint).removeAttachPoint(this);
}
}
endPoint = o;
}
}
public int getSize() {
return segment.size();
}
public String mouseOnWhere(Point e) {
for (int i = 0; i < segment.size(); i++) {
Point pt = segment.get(i);
if ((pt.x - e.x) * (pt.x - e.x) + (pt.y - e.y) * (pt.y - e.y) < 20 * 20)
return "point" + i;
}
for (int i = 0; i < segment.size(); i++) {
Point pt = segment.get(i);
Point nt = null;
if (i + 1 < segment.size())
nt = segment.get(i + 1);
if (nt != null) {
int area = Math.abs((pt.x - e.x) * (nt.y - e.y) - (pt.y - e.y)
* (nt.x - e.x)) / 2;
double a = Math.hypot(pt.x - nt.x, pt.y - nt.y);
double b = Math.hypot(pt.x - e.x, pt.y - e.y);
double c = Math.hypot(e.x - nt.x, e.y - nt.y);
double h = 2 * area / a;
if (h < 10 && b * b < a * a + c * c && c * c < b * b + a * a) {
// System.out.println("line choose");
return "line" + i;
}
}
}
return "nop";
}
public void setPoint(int index, Point pt) {
segment.get(index).x = pt.x;
segment.get(index).y = pt.y;
}
public Point getPointAddr(int index) {
return segment.get(index);
}
public boolean getSelected() {
return selected;
}
public void setSelected(boolean val) {
selected = val;
}
public void drawOnGraph(Graphics g) {
Color color = new Color(253, 247, 217);
g.setColor(color);
for (int i = 0; i < segment.size(); i++) {
Point pt = segment.get(i);
Point nt = null;
if (i + 1 < segment.size())
nt = segment.get(i + 1);
if (selected) {
for (int r = 5; r < 10; r++)
g.drawOval(pt.x - r, pt.y - r, 2 * r, 2 * r);
}
if (nt != null) {
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(2.0f));
for (int r = 0; r < 5; r++)
g2d.drawLine(pt.x, pt.y, nt.x, nt.y);
}
}
}
}
package package1;
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static int WIDTH = 1000, HEIGHT = 600;
MainFrame() {
this.setTitle("LinkObject");
WIDTH = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()*4/5;
HEIGHT = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()*4/5;
this.setSize(WIDTH, HEIGHT);
//this.setExtendedState(JFrame.MAXIMIZED_BOTH);
//this.setUndecorated(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new MainPanel());
this.setVisible(true);
}
public static void main(String[] args) {
new MainFrame();
}
}
package package1;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class MainPanel extends JPanel {
private static int WIDTH = 1000, HEIGHT = 600;
private DefaultListModel listModel;
private JList sampleJList;
private PaintPanel samplePanel;
MainPanel() {
Font displayFont = new Font("Serif", Font.BOLD, 28);
this.setLayout(new GridBagLayout());
listModel = new DefaultListModel();
listModel.addElement("Select");
listModel.addElement("Class");
listModel.addElement("Link");
listModel.addElement("Delete");
sampleJList = new JList(listModel);
sampleJList.setCellRenderer(new MRenderer());
sampleJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sampleJList.setSelectionBackground(null);
sampleJList.setBackground(new Color(240, 240, 240));
sampleJList.setFont(displayFont);
sampleJList.setSelectedIndex(0);
sampleJList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent listEvent) {
boolean adjust = listEvent.getValueIsAdjusting();
if (adjust == false)
return;
JList list = (JList) listEvent.getSource();
String selected = list.getSelectedValue().toString();
if (selected.equals("Delete")) {
if (samplePanel.getFocusPic() != null) {
String delObj = samplePanel.getFocusPic().toString();
int cmd = JOptionPane.showConfirmDialog(null,
"Delete " + delObj + " ?",
"Delete Check", JOptionPane.YES_NO_OPTION);
if (cmd == 0) {
samplePanel.deleteFocusOnPic();
}
}
list.setSelectedIndex(0);
}
}
});
this.setLayout(new GridBagLayout());
samplePanel = new PaintPanel();
GridBagConstraints GBC = new GridBagConstraints();
GBC.insets = new Insets(10, 10, 10, 10);
GBC.fill = GridBagConstraints.HORIZONTAL;
GBC.anchor = GridBagConstraints.NORTHEAST;
GBC.gridx = 0;
GBC.gridy = 0;
GBC.gridwidth = 1;
GBC.gridheight = 3;
GBC.weightx = 10;
GBC.weighty = 10;
this.add(sampleJList, GBC);
GBC = new GridBagConstraints();
GBC.insets = new Insets(10, 10, 10, 10);
GBC.fill = GridBagConstraints.BOTH;
GBC.anchor = GridBagConstraints.NORTHEAST;
GBC.gridx = 1;
GBC.gridy = 1;
GBC.gridwidth = 5;
GBC.gridheight = 3;
GBC.weightx = 300;
GBC.weighty = 10;
this.add(samplePanel, GBC);
}
public String getToolsSelected() {
return sampleJList.getSelectedValue().toString();
}
}
package package1;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class MRenderer extends DefaultListCellRenderer {
private Hashtable iconTable = new Hashtable();
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list,
value.toString(),// display non-text.
index, isSelected, false);
ImageIcon icon = (ImageIcon) iconTable.get(value.toString()
+ isSelected);
if (icon == null) {
String fileName = value.toString();
if (isSelected == true)
fileName += "1.png";
else
fileName += "0.png";
icon = new ImageIcon(fileName);
iconTable.put(value.toString() + isSelected, icon);
}
label.setIcon(icon);
return label;
}
}
package package1;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class PaintPanel extends JPanel {
private Component focusPic = null;
private ArrayList<LinkSegment> linkSegment;
PaintPanel() {
linkSegment = new ArrayList<LinkSegment>();
ShowListener listener = new ShowListener();
this.setBackground(new Color(40, 40, 40));
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
this.setLayout(null);
}
public String getToolsSelected() {
Component j1 = this.getParent();
if (j1 instanceof MainPanel)
return ((MainPanel) j1).getToolsSelected();
else
return null;
}
void setFocusPic(Object obj) {
if (obj != null)
focusPic = (Component) obj;
else
focusPic = null;
}
Object getFocusPic() {
String ret = "";
if(focusPic != null)
ret += "Class " + focusPic.toString();
for (int i = 0; i < linkSegment.size(); i++) {
if (linkSegment.get(i).getSelected()) {
ret += " & Link";
break;
}
}
return ret;
}
public void deleteFocusOnPic() {
if (focusPic != null) {
if (focusPic instanceof ClassDiagram) {
((ClassDiagram) focusPic).clearAttachPoint();
}
this.remove(focusPic);
focusPic = null;
}
for (int i = 0; i < linkSegment.size(); i++) {
if (linkSegment.get(i).getSelected()) {
linkSegment.remove(i);
break;
}
}
this.updateUI();
}
public void addLinkImg(LinkSegment ls) {
linkSegment.add(ls);
}
public void setSelectedObject(Point pt) {
boolean hasSelected = false;
for (int i = 0; i < linkSegment.size(); i++) {
if (hasSelected) {
linkSegment.get(i).setSelected(false);
continue;
}
String pos = linkSegment.get(i).mouseOnWhere(pt);
if (!pos.equals("nop"))
hasSelected = true;
linkSegment.get(i).setSelected(!pos.equals("nop"));
}
this.repaint();
}
public void moveSelectedObject(Point pt) {
for (int i = 0; i < linkSegment.size(); i++) {
if (linkSegment.get(i).getSelected()) {
String pos = linkSegment.get(i).mouseOnWhere(pt);
if (pos.indexOf("point") >= 0) {
int index = Integer.parseInt(pos.substring(5));
// System.out.println(index);
linkSegment.get(i).setPoint(index, pt);
if (index == 0 || index == linkSegment.get(i).getSize() - 1) {
Component jl = this.getComponentAt(pt);
if (jl != null) {
if (jl instanceof ClassDiagram) {
((ClassDiagram) jl).addAttachPoint(linkSegment
.get(i).getPointAddr(index), new Point(
pt.x - jl.getX(), pt.y - jl.getY()),
linkSegment.get(i));
linkSegment.get(i).setAttachObject(index, jl);
linkSegment.get(i).setSelected(false);
}
}
}
this.repaint();
}
}
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < linkSegment.size(); i++) {
linkSegment.get(i).drawOnGraph(g);
}
}
}
package package1;
import javax.swing.border.*;
import javax.swing.event.MouseInputListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
public class ResizeBorder extends AbstractBorder {
private int gap;
private Color borderColor;
private int rectWidth, rectHeight;
private boolean selected, usedLarge;
private Point p = new Point(0, 0);
public ResizeBorder(Color color, int gap, int w, int h) {
borderColor = color;
this.gap = gap;
rectWidth = w;
rectHeight = h;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
super.paintBorder(c, g, x, y, width, height);
if (g instanceof Graphics2D) {
Graphics2D g2d = (Graphics2D) g;
if (selected || usedLarge
|| ((ClassDiagram) c).getMouseState() != 0)
g2d.setColor(Color.BLUE);
else {
g2d.setColor(borderColor);
return;
}
// left-up corner.
g2d.fillRect(x, y, rectWidth, rectHeight);
g2d.fillRect(x, y, rectHeight, rectWidth);
// right-up corner.
g2d.fillRect(width - rectWidth, y, rectWidth, rectHeight);
g2d.fillRect(width - rectHeight, y, rectHeight, rectWidth);
// left-bottom corner.
g2d.fillRect(x, height - rectHeight, rectWidth, rectHeight);
g2d.fillRect(x, height - rectWidth, rectHeight, rectWidth);
// right-bottom corner.
g2d.fillRect(width - rectWidth, height - rectHeight, rectWidth,
rectHeight);
g2d.fillRect(width - rectHeight, height - rectWidth, rectHeight,
rectWidth);
}
}
@Override
public Insets getBorderInsets(Component c) {
return getBorderInsets(c, new Insets(gap, gap, gap, gap));
}
@Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.right = insets.bottom = insets.top = gap;
return insets;
}
@Override
public boolean isBorderOpaque() {
return true;
}
}
package package1;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;
public class ShowListener implements MouseInputListener {
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
/*
* e.getPoint(); Component j1 = (Component) e.getSource();
* System.out.println(j1.getComponentAt(e.getPoint()));
*/
if (SwingUtilities.isLeftMouseButton(e)) {
Component j1 = (Component) e.getSource();
((PaintPanel)j1).moveSelectedObject(e.getPoint());
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
public void mouseReleased(MouseEvent e) {
Component j1 = (Component) e.getSource();// jl = PaintPanel
if (((PaintPanel) j1).getToolsSelected() == "Class") {
ClassDiagram cd = new ClassDiagram();
cd.setLocation(e.getPoint());
((PaintPanel) j1).add(cd);
((PaintPanel) j1).updateUI();
}
if (((PaintPanel) j1).getToolsSelected() == "Link") {
LinkSegment ls = new LinkSegment(e.getX(), e.getY());
((PaintPanel) j1).addLinkImg(ls);
((PaintPanel) j1).updateUI();
/*
* LinkPanel lp = new LinkPanel(); lp.setLocation(e.getPoint());
* ((PaintPanel) j1).add(lp); ((PaintPanel) j1).updateUI();
*/
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if(e.getClickCount() == 2) {
Component j1 = (Component) e.getSource();
((PaintPanel)j1).setSelectedObject(e.getPoint());
}
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
public void mousePressed(MouseEvent e) {
}
}
文章定位: