2013-10-14 10:28:41Morris
[JAVA] 結合套用 UML 模擬(初步)
將前幾次的 class 融合再一起,BUG 修正了一點,由於之前寫縮放的向量沒考慮四個角的所有情況。
以及移動基準的判斷限制為邊框,但除了四個角鄰近的位置。
還有 repaint 與 updateUI 的差異。
--> 待修正的功能 - 刪除,無法在選擇某個 PaintPanel 的 Object 偵測到 DELETE 鍵後自我刪除。
package package1;
import java.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static int WIDTH = 1000, HEIGHT = 800;
MainFrame() {
this.setTitle("ButtonGroupObject");
this.setSize(WIDTH, HEIGHT);
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.*;
public class MainPanel extends JPanel {
private static int WIDTH = 1000, HEIGHT = 500;
private DefaultListModel listModel;
private JList sampleJList;
MainPanel() {
this.setLayout(new GridBagLayout());
listModel = new DefaultListModel();
listModel.addElement("Select");
listModel.addElement("Class");
listModel.addElement("Association");
sampleJList = new JList(listModel);
sampleJList.setCellRenderer(new MRenderer());
sampleJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sampleJList.setSelectionBackground(null);
sampleJList.setBackground(new Color(240, 240, 240));
Font displayFont = new Font("Serif", Font.BOLD, 35);
sampleJList.setFont(displayFont);
sampleJList.setSelectedIndex(0);
PaintPanel 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 = 30;
GBC.weighty = 30;
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 = 3;
GBC.gridheight = 3;
GBC.weightx = 300;
GBC.weighty = 150;
this.add(samplePanel, GBC);
}
public String getToolsSelected() {
int idx = sampleJList.getSelectedIndex();
return listModel.get(idx).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.*;
public class PaintPanel extends JPanel {
PaintPanel() {
this.setBackground(new Color(40, 40, 40));
ShowListener listener = new ShowListener();
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;
}
}
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 implements MouseInputListener {
private int gap;
private Color borderColor;
private int rectWidth, rectHeight;
private boolean selected;
public ResizeBorder(Color color, int gap, int w, int h) {
borderColor = color;
this.gap = gap;
rectWidth = w;
rectHeight = h;
/*
* ResizeObjectListener listener = new ResizeObjectListener();
* this.addMouseListener(listener);
* this.addMouseMotionListener(listener);
*/
}
@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)
g2d.setColor(Color.BLUE);
else
g2d.setColor(borderColor);
// 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;
}
/*------------------------------------*/
private Point p = new Point(0, 0);
private boolean usedLarge;
public void mouseMoved(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) {
// System.out.println("dragged");
if (SwingUtilities.isLeftMouseButton(e)) {
if (usedLarge) {
Component jl = (Component) e.getSource();
Point pt = e.getPoint();
int width = (int) jl.getBounds().getWidth();
int height = (int) jl.getBounds().getHeight();
Point[] corner = { new Point(0, 0), new Point(width, 0),
new Point(0, height), new Point(width, height) };
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 = 0;
for (int i = 0; i < corner.length; i++) {
if (Math.pow(pt.x - corner[i].x, 2)
+ Math.pow(pt.y - corner[i].y, 2) < 30 * 30)
cornerIdx = i;
}
Point newP = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
int vx = newP.x - p.x, vy = newP.y - p.y;
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);
p = newP;
}
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
public void mouseReleased(MouseEvent e) {
if (usedLarge)
usedLarge = false;
Component jl = (Component) e.getSource();
((ClassDiagram) jl).updateUI();
jl.repaint();
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
Component jl = (Component) e.getSource();
jl.repaint();
selected = false;
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
Component jl = (Component) e.getSource();
Point pt = e.getPoint();
int width = (int) jl.getBounds().getWidth();
int height = (int) jl.getBounds().getHeight();
Point[] corner = { new Point(0, 0), new Point(width, 0),
new Point(0, height), new Point(width, height) };
boolean inCorner = false;
for (int i = 0; i < corner.length; i++) {
if (Math.pow(pt.x - corner[i].x, 2)
+ Math.pow(pt.y - corner[i].y, 2) < 30 * 30)
inCorner = true;
}
if (inCorner == false)
return;
selected = true;
System.out.println("selected");
return;
}
}
}
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
Component jl = (Component) e.getSource();
Point pt = e.getPoint();
int width = (int) jl.getBounds().getWidth();
int height = (int) jl.getBounds().getHeight();
Point[] corner = { new Point(0, 0), new Point(width, 0),
new Point(0, height), new Point(width, height) };
boolean inCorner = false;
for (int i = 0; i < corner.length; i++) {
if (Math.pow(pt.x - corner[i].x, 2)
+ Math.pow(pt.y - corner[i].y, 2) < 30 * 30)
inCorner = true;
}
if (inCorner == false)
return;
selected = true;
p = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
jl.repaint();
} else {
if (selected == true) {
usedLarge = true;
}
}
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
}
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) {
}
public void mouseReleased(MouseEvent e) {
Component j1 = (Component) e.getSource();//jl = PaintPanel
if(((PaintPanel)j1).getToolsSelected() == "Class") {
ClassDiagram cd = new ClassDiagram();
Point newP = SwingUtilities.convertPoint(j1, e.getPoint(),
j1.getParent().getParent());
cd.setLocation(e.getPoint());
((PaintPanel)j1).add(cd);
((PaintPanel)j1).updateUI();
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
}
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.event.KeyEvent;
import java.awt.event.KeyListener;
public class ClassDiagram extends JPanel implements KeyEventDispatcher {
private DefaultListModel attributeListModel;
private DefaultListModel methodListModel;
private JTextField className;
private JList classAttribute;
private JList classMethod;
private JTable table = null;
private MTableModel model = null;
private JScrollPane s_pan = null;
ClassDiagram() {
Font displayFont = new Font("Serif", Font.BOLD, 18);
attributeListModel = new DefaultListModel();
methodListModel = new DefaultListModel();
attributeListModel.addElement("+");
methodListModel.addElement("-");
className = new JTextField("ClassName");
classAttribute = new JList(attributeListModel);
classMethod = new JList(methodListModel);
className.setBackground(new Color(255, 255, 185));
className.setFont(displayFont);
className.setHorizontalAlignment(JTextField.CENTER);
// classAttribute.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
classAttribute.setBackground(new Color(255, 255, 185));
// classMethod.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
classMethod.setBackground(new Color(255, 255, 185));
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);
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(classAttribute, GBC);
model = new MTableModel(10);
table = new JTable(model);
table.setBackground(new Color(255, 255, 185));
TableColumnModel tcm = table.getColumnModel();
// tcm.getColumn(3).setCellEditor(new DefaultCellEditor(com));
String[] choose = { "+", "#", "-", "~" };
JComboBox combx = new JComboBox(choose);
tcm.getColumn(0).setCellEditor(new DefaultCellEditor(combx));
tcm.getColumn(0).setPreferredWidth(10);
tcm.getColumn(1).setPreferredWidth(290);
model.addRow("toString()");
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(table, GBC);
this.setBounds(50, 50, 250, 250);
this.setBackground(Color.RED);
ResizeBorder rborder = new ResizeBorder(Color.RED, 5, 10, 30);
this.setBorder(rborder);
DragObjectListener listener = new DragObjectListener();
this.addMouseListener(rborder);
this.addMouseMotionListener(rborder);
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
this.setBackground(Color.ORANGE);
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(this);
}
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_RELEASED)
return false;
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
}
return false;
}
}
class MTableModel extends AbstractTableModel {
// private static final long serialVersionUID = -7495940408592595397L;
private Vector content = null;
private String[] title_name = { "ID", "Name" };
public MTableModel() {
content = new Vector();
}
public MTableModel(int count) {
content = new Vector(count);
}
public void addRow(String name) {
Vector v = new Vector(2);
v.add(0, "+");
v.add(1, name);
content.add(v);
}
public void removeRow(int row) {
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) {
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();
Point pt = e.getPoint();
int width = (int) jl.getBounds().getWidth();
int height = (int) jl.getBounds().getHeight();
Point[] corner = { new Point(0, 0), new Point(width, 0),
new Point(0, height), new Point(width, height) };
for (int i = 0; i < corner.length; i++) {
if (Math.pow(pt.x - corner[i].x, 2)
+ Math.pow(pt.y - corner[i].y, 2) < 30 * 30)
return;
}
Point newP = SwingUtilities.convertPoint(jl, e.getPoint(),
jl.getParent());
jl.setLocation(jl.getX() + (newP.x - p.x), jl.getY()
+ (newP.y - p.y));
p = newP;
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
Component source = (Component) e.getSource();
}
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
if (e.getClickCount() == 2) {
Component source = (Component) e.getSource();
}
}
}
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)) {
Component jl = (Component) e.getSource();
Point pt = e.getPoint();
int width = (int) jl.getBounds().getWidth();
int height = (int) jl.getBounds().getHeight();
Point[] corner = { new Point(0, 0), new Point(width, 0),
new Point(0, height), new Point(width, height) };
for (int i = 0; i < corner.length; i++) {
if (Math.pow(pt.x - corner[i].x, 2)
+ Math.pow(pt.y - corner[i].y, 2) < 30 * 30)
return;
}
p = SwingUtilities.convertPoint(jl, e.getPoint(), jl.getParent());
} else if (SwingUtilities.isRightMouseButton(e)) {
} else if (SwingUtilities.isMiddleMouseButton(e)) {
}
}
}
上一篇:[JAVA] 繪圖選單