在监听器Mouse类中修改代码

//创建图形类数组,每个单元存放一个图形
Shape[] s = new Shape[10];
//下标
int index=0;

public void mouseReleased(MouseEvent e){
x2 = e.getX();
y2 = e.getY();

if("直线".equals(text))
{
g.drawLine(x1, y1, x2, y2);
//创造shape类对象,将原图形的数据记录在缓存中
Shape h = new Shape();
h.x1 = x1;
h.x2 = x2;
h.y1 = y1;
h.y2 = y2;
h.type = "直线";
s[index++] = h;
}
if("矩形".equals(text))
{
g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2));
//创造shape类对象,将原图形的数据记录在缓存中
Shape h = new Shape(http://www.amjmh.com/v/BIBRGZ_558768/);
h.x1 = x1;
h.x2 = x2;
h.y1 = y1;
h.y2 = y2;
h.type = "矩形";
s[index++] = h;
}
}
---------------------

原文地址:https://www.cnblogs.com/hyhy904/p/11342599.html