简单java程序改错!

本程序要实现的是随着鼠标点击,将在画布上话小方块。importjava.awt.*;importjava.awt.event.*;importjava.util.*;im... 本程序要实现的是随着鼠标点击,将在画布上话小方块。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;

public class MouseTest
{
public static void main(String[] args)
{
MouseFrame frame=new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("MouseTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

MousePanel panel=new MousePanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}

public static final int DEFAULT_WIDTH=300;
public static final int DEFAULT_HEIGHT=200;

}

class MousePanel extends JPanel
{
public MousePanel()
{
squares=new ArrayList();
current=null;

addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D)g;
g2.setPaint(Color.RED);

for(int i=0;i<squares.size();i++)
g2.draw((Rectangle2D)squares.get(i));
}

public Rectangle2D find(Point2D p)
{
for(int i=0;i<squares.size();i++)
{
Rectangle2D r=(Rectangle2D)squares.get(i);
if(r.contains(p)) return r;
}
return null;
}

public void add(Point2D p)
{
double x=p.getX();
double y=p.getY();

current=new Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
squares.add(current);
repaint();
}

public void remove(Rectangle2D s)
{
if(s==null)return;
if(s==current) current=null;
squares.remove(s);
repaint();
}

private static final int SIDELENGTH=10;
private ArrayList squares;
private Rectangle2D current;

private class MouseHandler extends MouseAdapter
{
public void mouseHandler(MouseEvent event)
{
current=find(event.getPoint());
if(current==null)
add(event.getPoint());

}

public void mouseClicked(MouseEvent event)
{
current=find(event.getPoint());
if(current==null&&event.getClickCount()>=2)
remove(current);
}
}

private class MouseMotionHandler implements MouseMotionListener

{
public void mouseMoved(MouseEvent event)
{
if(find(event.getPoint())==null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event)
{
if (current!=null)
{
int x=event.getX();
int y=event.getY();

current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
repaint();
}

}
}

}
展开
 我来答
bjmok
2006-12-05 · 超过23用户采纳过TA的回答
知道答主
回答量:111
采纳率:0%
帮助的人:91.7万
展开全部
这地方是改错的,不是给你实现功能的,哈哈
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式