部落格專用相簿
[JAVA] 結合套用 UML 模擬(表單操作)
- 修正部分的 class, 去除錯誤的繼承架構。
- 增加 Class Diagram 內部選單操作。
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.*;
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;
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();
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();
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;
}
ClassDiagram() {
Font displayFont = new Font("Serif", Font.BOLD, 18);
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.setBounds(50, 50, 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);
}
});
}
}
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));
} 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);
}
}
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.awt.*;
import javax.swing.*;
public class MainFrame extends JFrame {
private static int WIDTH = 1000, HEIGHT = 600;
MainFrame() {
this.setTitle("AddMenuOper");
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 Class " + 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.*;
public class PaintPanel extends JPanel {
private Component focusPic = null;
PaintPanel() {
this.setBackground(new Color(40, 40, 40));
ShowListener listener = new ShowListener();
this.addMouseListener(listener);
this.addMouseMotionListener(listener);
this.setLayout(null);
this.getDropTarget();
}
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() {
return focusPic;
}
public void deleteFocusOnPic() {
if (focusPic != null) {
this.remove(focusPic);
this.updateUI();
focusPic = 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 {
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);
// 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();
j1.getComponentAt(e.getPoint());
}
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) {
}
}
文章定位: