3. clearRect很不好控制,使用这个方法需要自己处理图元相交,与这个rect相交的所有图元都需要重新绘制,Swing默认没有提供这方面的库,得自己实现或者每用一次都要和所有的图元判断一下,如Qt就提供了QGraphicsView和QGraphicsScene框架,使用Binary Space Partition进行碰撞检测,不需要检测所有对象。
public class MyGUI {
public static void main (String[] args){
FlowChart f = new FlowChart();
}
}
class FlowChart extends JComponent implements MouseListener,MouseMotionListener{
final ArrayList<String> rects = new ArrayList<String>();
final ArrayList<String> lines = new ArrayList<String>();
Point[] textpoints = new Point[50];
Point[] linepoints = new Point[50];
int[] width = new int[50];
int[] height = new int[50];
Graphics g;
int x=10, y=10;
int clickx, clicky;
int movex, movey;
int pressx,pressy;
int draggx,draggy;
int releasex=50,releasey=50;
int item, choice;
JFrame frame = new JFrame();
JPanel menu = new JPanel (new GridLayout(1,4));
JPanel p = new JPanel();
JPanel gp = new JPanel();
JTextArea[] TA = new JTextArea[50];
JTextArea T = new JTextArea(8,40);
JButton Savebutton = new JButton("Save");
JButton Loadbutton = new JButton("Load");
JButton TextArea = new JButton("Text");
JButton Line = new JButton("Connecting Lion");
public FlowChart(){
for(int i =0; i<50;i++)
{
width[i] = 50 ;
height[i] = 50 ;
TA[i] = new JTextArea(8,40);
textpoints[i] = new Point(50,100*(i+1));