只是出于好奇,我做了这样的事情。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class RectanglePanel extends JPanel{
private Point anchorPoint = null;
private Point intermediatePoint = null;
private Point finalPoint = null;
public RectanglePanel(){
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me){
if(anchorPoint == null){
// first click, set anchor point
anchorPoint = me.getPoint();
}else if(finalPoint == null){
// second click, set final point
finalPoint = me.getPoint();
}else{
// third click, reset clicks, anchor point, intermediate point and final point
anchorPoint = null;
finalPoint = null;
intermediatePoint = null;
}
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent me){
if(anchorPoint != null && finalPoint == null){
// mouse moved
// set intermediate point if anchor point is set and final point is not set yet
intermediatePoint = me.getPoint();
repaint();
}
}
});
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(anchorPoint != null){
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
Point p = finalPoint != null ? finalPoint : intermediatePoint;
if(p != null && !p.equals(anchorPoint)){
// final point or intermediate point is set, and is not same as anchor point
// draw square
// calculate angle to rotate canvas
double angle = -Math.toRadians(45) + Math.atan2(p.y - anchorPoint.y, p.x - anchorPoint.x);
// width of square, calculated using distance formaula and pythagorus theorem
// distance formula: distance = sqrt((x1-x2)^2 + (y1-y2)^2)
// pythagorus for right angled triangle: c^2 = a^2 + b^2
double width = Math.sqrt(((p.x - anchorPoint.x) * (p.x - anchorPoint.x) + (p.y - anchorPoint.y) * (p.y - anchorPoint.y)) / 2.0);
// set origin to anchorpoint
g2d.translate(anchorPoint.x, anchorPoint.y);
// rotate canvas
g2d.rotate(angle);
Rectangle2D rectangle2D = new Rectangle2D.Double(0, 0, width, width);
// draw square
g2d.draw(rectangle2D);
// rotate back canvas
g2d.rotate(-angle);
// reset back origin
g2d.translate(-anchorPoint.x, -anchorPoint.y);
}else{
g2d.drawRect(anchorPoint.x, anchorPoint.y, 1, 1);
}
}
}
public static void main(String [] args){
final JFrame frame = new JFrame("Rectangle Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.getContentPane().add(new RectanglePanel());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});
}
}
您可以像这样实施,以解决您的问题。让我知道这是否是您要找的?
脚步:
1) 计算正方形的宽度。你有点,代表正方形的对角。这两点之间的距离是对角线的长度。因此,考虑两个点(x1, y1)
和(x2, y2)
,使用距离公式,对角线的长度由下式给出:
diagonal_length * diagonal_length = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)
正方形和对角线的两侧将形成直角三角形。正方形的边长相等,设正方形的边为side
,然后使用毕达哥拉斯定理:
side * side + side * side = diagonal_length * diagonal_length
求解以上两个方程,
side = Math.sqrt(((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) / 2.0);
2)计算旋转画布的角度,所以第二点与x轴成45度角,以第一点为原点。
3)制作第一点原点。
4)旋转画布,使第二个点与x轴成45度角,第一个点是原点。这将使正方形的两侧落在轴上,而另外两侧则与轴平行,因此您可以使用图形的绘制方法来绘制矩形/正方形。
5) 从原点画正方形,边长如上计算。
6) 将画布向反方向旋转,使其与旋转前一样。
7) 将原点复位为原点设置前的原点。
完毕!