问题已在代码中用红色标出
/**
* @version 1.20 25 Mar 1998
* @author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class MousePanel extends JPanel
implements MouseMotionListener
{ public MousePanel()
{ addMouseListener(new MouseAdapter()
{ public void mousePressed(MouseEvent evt)
{ int x = evt.getX();
int y = evt.getY();
//如果返回的不是-1则说是被选中一个矩形,而不是进行绘图
//也就不会去执行下的add(x,y)方法
current = find(x, y);
if (current < 0) // not inside a square
add(x, y);
} public void mouseClicked(MouseEvent evt)
{ int x = evt.getX();
int y = evt.getY(); if (evt.getClickCount() >= 2)
{ remove(current);
}
}
});
//鼠标移动侦听器
addMouseMotionListener(this);
} public void paintComponent(Graphics g)
{ //调试时要一点应用程序就会调用这里
super.paintComponent(g);
for (int i = 0; i < nsquares; i++)
draw(g, i);
}
public int find(int x, int y)
{ for (int i = 0; i < nsquares; i++)
//当第二次的鼠标与上一次鼠标重合时下面的if语句会成立
if (squares[i].x - SQUARELENGTH / 2 <= x &&
x <= squares[i].x + SQUARELENGTH / 2
&& squares[i].y - SQUARELENGTH / 2 <= y
&& y <= squares[i].y + SQUARELENGTH / 2)
//if中的条件也就是用鼠标选中哪个矩形的条件
//返回的i是被选中的那个矩形
return i;
return -1;
} public void draw(Graphics g, int i)
{ //这里- SQUARELENGTH / 2的原因是:让鼠标指针放在矩形的中心,
//如果- SQUARELENGTH / 2鼠标指针会放在矩形的左上角
//squares[i].x是鼠标的在面板上的x轴
g.drawRect(squares[i].x - SQUARELENGTH / 2,
squares[i].y - SQUARELENGTH / 2,
SQUARELENGTH,
SQUARELENGTH);
} public void add(int x, int y)
{ if (nsquares < MAXNSQUARES)
//把这次的坐标点存入点对象数组中
{ squares[nsquares] = new Point(x, y);
current = nsquares;
nsquares++;
repaint();
}
} public void remove(int n)
{
//在什么条件下n<0,n>=nsquares会成立
//这里的nsquares应该是n的下一个
if (n < 0 || n >= nsquares) return; nsquares--;
//这是重新定义数组,把刚才选择的那个矩形从数组中去掉
squares[n] = squares[nsquares];
//为什么还要把curren置为-1
if (current == n) current = -1; repaint();
} public void mouseMoved(MouseEvent evt)
{ int x = evt.getX();
int y = evt.getY(); if (find(x, y) >= 0)
//setCursor()设置光标
//得到具有有指定预定义类型的光标对象
//Cursor.CROSSHAIR_CURSOR返回十字光标类型
setCursor(Cursor.getPredefinedCursor
(Cursor.CROSSHAIR_CURSOR));
else
//返回系统默认的光标
setCursor(Cursor.getDefaultCursor());
} public void mouseDragged(MouseEvent evt)
{ int x = evt.getX();
int y = evt.getY(); if (current >= 0)
{ Graphics g = getGraphics();
g.setXORMode(getBackground());
draw(g, current);
squares[current].x = x;
squares[current].y = y;
draw(g, current);
g.dispose();
}
} private static final int SQUARELENGTH = 10;
private static final int MAXNSQUARES = 100;
private Point[] squares = new Point[MAXNSQUARES];
private int nsquares = 0;
private int current = -1;
}class MouseFrame extends JFrame
{ public MouseFrame()
{ setTitle("MouseTest");
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} ); Container contentPane = getContentPane();
contentPane.add(new MousePanel());
}
}public class MouseTest
{ public static void main(String[] args)
{ JFrame frame = new MouseFrame();
frame.show();
}
}
解决方案 »
- 用C/S模式做的JAVA QQ,S端导出JAR包后连不上数据库,连不上1433端口,请高手们支下招吧。。。
- 兄弟们,帮我注释下这段代码,关于TextArea
- jndi data source sybase
- HTTP Status 404 - Servlet action is not available
- 为什么我在做java包的时候会出错....请大虾指教....
- 请高手帮我理解一下这个错误!
- 如何获得编辑光标的当前像素位置?在jEditorPane上
- e文不好^_^!JDK1.3.1 Winhelp Documentation中的这段关于DataInputStream的意思是什么?
- 关于JB5下调试小应用程序的问题
- 为什么在静态方法里不能用this ?
- 关于用java开发的软件的发布问题
- java控制台输出颜色怎么设定!
那么长的代码,确实不怎么愿意看
好运