谁帮忙了,感激不尽!!
修改一下以下程序MouseDraw.java,使其克服以下缺陷:
1当鼠标在绘图区域单击时,可用黑色圆点实时显示光标当前位置。
2当绘制的两条线段交叉时,不会因为一条线段的移动而导致另一条线段被擦除。
3所有绘制工作完成后,当对绘图窗口执行以下几种操作时,不会导致原有绘制好的线段丢失。
(1) 改变绘图窗口大小
(2) 最小化绘图窗口后再恢复成窗口大小
(3) 用其他窗口全部或部分覆盖绘图窗口,然后再露出绘图窗口
//MouseDraw.java
import java.awt.*;
import java.awt.event.*;
public class MouseDraw
{
public static void main(String[] args)
{
MouseFrame frame=new MouseFrame();
frame.addWindowListener(new Wadapt());
frame.setVisible(true);
}
}
class MouseFrame extends Frame
{
public MouseFrame()
{
setTitle("利用鼠标绘图");
setSize(300,300);
//将panel加入到frame
MousePanel panel=new MousePanel();
add(panel);
}
}
class MousePanel extends Panel implements MouseListener,MouseMotionListener
{
private int x,y,start_X,start_Y,old_X,old_Y;
private boolean draw;
public MousePanel()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mousePressed(MouseEvent evt)
{
start_X=evt.getX();
start_Y=evt.getY();
draw=false;
}
public void mouseReleased(MouseEvent evt){}
public void mouseClicked(MouseEvent evt){}
public void mouseEntered(MouseEvent evt)
{
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent evt)
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void mouseDragged(MouseEvent evt)
{
x=evt.getX();
y=evt.getY();
repaint();
}
public void mouseMoved(MouseEvent evt){}
/*public void update(Graphics g)
{
paint(g);
}
*/
public void update(Graphics g)
{
if(!draw)
{
old_X=x;
old_Y=y;
draw=true;
}
else
{
g.setColor(getBackground());
g.drawLine(start_X,start_Y,old_X,old_Y);
old_X=x;
old_Y=y;
}
g.setColor(getForeground());
g.drawLine(start_X,start_Y,x,y);
}
}
class Wadapt extends WindowAdapter
{
public void windowClosing(WindowEvent evt)
{
Frame frm=(Frame)evt.getSource();
frm.setVisible(false);
frm.dispose();
System.exit(0);
}
}
----------------解决方案--------------------------------------------------------
把画的线存起来
----------------解决方案--------------------------------------------------------
怎么存啊,可不可以说详细一点啊
----------------解决方案--------------------------------------------------------