如何使用swing在Java中绘制一个倒置的矩形?
使用'g.drawRect(x,y,width,height)'方法, 通过鼠标拖动创建矩形是成功的,但是有一些错误。
如果拖动到比第一个点(x,y)大的点(x2,y2 | x2> x1 && y2> y1),它将正常运行。
但是,在相反的情况下,如果拖动点的坐标小于第一次单击点的坐标,则会沿相反的方向而不是拖动点的方向进行绘制。
即使我试图通过if()将其反转,我也不知道该怎么做。
我想要的方向就像Window中的拖动框,但是对我来说有点难。 请给我一些提示,以克服这一艰难的旅程。
↓这是我的代码
class Rect {
int x, y, w, h;
}
public class Rectangle extends JPanel{
int i = 0;
int x, y = 0;
Rect [] ary = new Rect[100];
public Rectangle() {
addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if(i>100) return;
ary[i] = new Rect();
ary[i].x = e.getX(); ary[i].y = e.getY();
x= ary[i].x; y = ary[i].y;
}
@Override
public void mouseReleased(MouseEvent e) {
ary[i].w = Math.abs(ary[i].x-e.getX());
ary[i].h = Math.abs(ary[i].y- e.getY());
i++;
repaint();
}
});
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
ary[i].w = Math.abs(ary[i].x-e.getX());
ary[i].h = Math.abs(ary[i].y- e.getY());
repaint();
}
public void mouseMoved(MouseEvent e) {
}
});
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Rect r:ary){
if(r !=null) {
g.setColor(Color.BLACK);
g.drawRect(r.x, r.y, r.w, r.h);
}
}
}
}
请帮助我
答案 0 :(得分:0)
因为ary[i].x
和ary[i].y
必须是按下和释放坐标中的最小值:
@Override
public void mouseReleased(MouseEvent e) {
ary[i].w = Math.abs(ary[i].x-e.getX());
ary[i].h = Math.abs(ary[i].y- e.getY());
// upper left point
ary[i].x = Math.min(ary[i].x,e.getX());
ary[i].y = Math.min(ary[i].y,e.getY());
i++;
repaint();
}
答案 1 :(得分:0)
首先,不要使用数组存储要绘制的Rectangle对象。使用ArrayList,当您要绘制更多矩形时,它将动态增长。
当前代码的问题是,您试图通过3种单独的方法更新x / y / width / height值。这只能在mouseDragged
方法中完成。
基本步骤是:
mousePressed
中保存初始鼠标点。创建一个空的Rectangle对象以绘制矩形mouseDragged
中,将初始鼠标点与当前鼠标点进行比较,并确定最小x / y值。然后,您分别获得x和y值的绝对值,因此您知道矩形的宽度/高度。更新Rectangle对象的x / y / width / height值并重新绘制Rectangle mouseReleased
中,将Rectangle对象添加到ArrayList 有关实现上述建议的有效示例,请参见Custom Painting Approaches中的DrawOnComponent
示例。
以上示例中的MouseInputAdapter实现如下。它显示了大多数逻辑在mouseDragged方法中:
class MyMouseListener extends MouseInputAdapter
{
private Point startPoint;
public void mousePressed(MouseEvent e)
{
startPoint = e.getPoint();
shape = new Rectangle();
}
public void mouseDragged(MouseEvent e)
{
int x = Math.min(startPoint.x, e.getX());
int y = Math.min(startPoint.y, e.getY());
int width = Math.abs(startPoint.x - e.getX());
int height = Math.abs(startPoint.y - e.getY());
shape.setBounds(x, y, width, height);
repaint();
}
public void mouseReleased(MouseEvent e)
{
if (shape.width != 0 || shape.height != 0)
{
addRectangle(shape, e.getComponent().getForeground());
}
shape = null;
}
}