修改java源文件,使其克服以下缺陷:
1.当鼠标在绘图区域单击时,可用黑色圆点实时显示光标当前位置。
2.当绘制的两条线段交*时,不会因为一条线段的移动而导致另一条线段被擦除。
3.所有绘制工作完成后,当对绘图窗口执行以下几种操作时,不会导致原有绘制好的线段丢失。
(1)改变绘图窗口大小
(2)最小化绘图窗口后再恢复成窗口大小
(3)用其他窗口全部或部分覆盖绘图窗口,然后再露出绘图窗口
附代码:
//MouseDraw.java
package chapter7.MouseDraw;
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 paint(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);
}
}
重大事件,进来看一下
请帮我看看下面的题目吧,我找了好多资料还是不知道怎么做,谢谢了!!!!!