Java Swing在鼠标拖放中绘制矩形
问题描述:
我正在创建一个矩形绘图程序.只有当程序拖到底部时才会绘制正方形.即使向另一个方向拖动,我也想确保正确绘制正方形.我该如何解决?请帮帮我.
I am creating a rectangular drawing program.The square is drawn only if the program is dragging to the bottom side. Even if dragging in the other direction, I want to ensure that squares are drawn correctly. How can I fix it? Please help me.
**DrawRect.java**
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawRect extends JPanel {
int x, y, w, h;
public static void main(String [] args) {
JFrame f = new JFrame("Draw Box Mouse 2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new DrawRect());
f.setSize(300, 300); f.setVisible(true);
}
DrawRect() {
x = y = w = h = 0; //
MyMouseListener listener = new MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
}
public void setStartPoint(int x, int y) {
this.x = x; this.y = y;
}
public void setEndPoint(int x, int y) {
w = Math.abs(this.x - x);
h = Math.abs(this.y - y);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
setStartPoint(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e) {
setEndPoint(e.getX(), e.getY()); repaint();
}
public void mouseReleased(MouseEvent e) {
setEndPoint(e.getX(), e.getY()); repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
Please help me.
g.drawRect(x, y, w, h);
}
}
答
尝试这样的事情.您必须仔细确定起点.起点是第一个和最后一个鼠标坐标的最小 x 和 y 点.
Try something like this. you have to determine start point carefully.start point is the min x and y points of 1st and last mouse coordinates.
这里是解决这个问题的步骤
here is the steps to solve this problem
- 鼠标点击 x,y 时取第一个坐标
- 鼠标拖动 x2,y2 时取最后一个坐标
- 以最小 x 和 y 坐标作为 drawRect 的起点
Math.min(x,x2);
- 使用坐标差的绝对值来计算矩形的高度和宽度.
Math.abs(x-x2);
代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawRect extends JPanel {
int x, y, x2, y2;
public static void main(String[] args) {
JFrame f = new JFrame("Draw Box Mouse 2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new DrawRect());
f.setSize(300, 300);
f.setVisible(true);
}
DrawRect() {
x = y = x2 = y2 = 0; //
MyMouseListener listener = new MyMouseListener();
addMouseListener(listener);
addMouseMotionListener(listener);
}
public void setStartPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setEndPoint(int x, int y) {
x2 = (x);
y2 = (y);
}
public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) {
int px = Math.min(x,x2);
int py = Math.min(y,y2);
int pw=Math.abs(x-x2);
int ph=Math.abs(y-y2);
g.drawRect(px, py, pw, ph);
}
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
setStartPoint(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
public void mouseReleased(MouseEvent e) {
setEndPoint(e.getX(), e.getY());
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
drawPerfectRect(g, x, y, x2, y2);
}
}