我假设有两个 Path2D,一个包含另一个。当我从第一个形状移动一个点时,第二个形状移动相同,但是由于角度的变化,形状之间的距离也发生了变化(最终结果......)。
到目前为止,我有这个,第二个三角形(innerTriangle)的硬编码点:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Path2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
class DragTest extends JPanel {
private final class MouseDrag extends MouseAdapter {
private boolean dragging = false;
private Point last;
@Override
public void mousePressed(MouseEvent m) {
if(dRect.contains(m.getPoint())){
last = m.getPoint();
dragging = isInsideRect(dRect, last);
if (!dragging) {
x = last.x;
y = last.y;
width = 0;
height = 0;
}
}
repaint();
}
@Override
public void mouseReleased(MouseEvent m) {
last = null;
dragging = false;
repaint();
}
@Override
public void mouseDragged(MouseEvent m) {
if(dRect.contains(m.getPoint())){
int dx = m.getX() - last.x;
int dy = m.getY() - last.y;
if (dragging) {
x += dx;
y += dy;
} else {
width += dx;
height += dy;
}
last = m.getPoint();
}
repaint();
}
}
private int x;
private int y;
private int width;
private int height;
private Rectangle2D.Float dRect ;
private Path2D triangle = new Path2D.Float();
private Path2D innerTriangle = new Path2D.Float();
private Point p1;
private Point p2 = new Point(5,200);
private Point p3 = new Point(400,200);
private MouseDrag mouseDrag;
public DragTest() {
setBackground(Color.WHITE);
dRect = new Rectangle2D.Float(x, y, 10+width, 10+height);
mouseDrag = new MouseDrag();
addMouseListener(mouseDrag);
addMouseMotionListener(mouseDrag);
}
public boolean isInsideRect(Shape s, Point point) {
return s.contains(point);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
dRect = new Rectangle2D.Float(x, y , 10, 10);
g2.draw(dRect);
triangle.reset();
triangle.moveTo(dRect.getCenterX(), dRect.getCenterY());
p1 = new Point((int)dRect.getCenterX(), (int)dRect.getCenterY());
triangle.lineTo(p2.x, p2.y);
triangle.lineTo(p3.x, p3.y);
triangle.closePath();
innerTriangle.reset();
innerTriangle.moveTo(p1.x+10, p1.y+17);
innerTriangle.lineTo(p2.x+10, p2.y-10);
innerTriangle.lineTo(p3.x-47, p3.y-10);
innerTriangle.closePath();
g2.draw(triangle);
g2.draw(innerTriangle);
g2.dispose();
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setSize(800, 600);
jFrame.add(new DragTest());
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
如何以编程方式在周围保持相同的距离(比如 10 像素)?
外三角形和红色(内)三角形之间的距离应该在所有方向上始终相同。任何想法?
目前我正在做一些事情:
// Calculate distance from point to line
public double pointToLineDistance(Point A, Point B, Point P) {
double normalLength = Math.sqrt((B.x-A.x)*(B.x-A.x)+(B.y-A.y)*(B.y-A.y));
return Math.abs((P.x-A.x)*(B.y-A.y)-(P.y-A.y)*(B.x-A.x))/normalLength;
}
// initially set :
// pi1.x = p1.x;
// pi1.y = p1.y;
// -------------------- Then ------------------------------
while(pointToLineDistance(p1, p3, pi1) == 10 && pointToLineDistance(p1, p2, pi1) == 10){
pi1.y++;
pi1.x++;
pi1.setLocation(pi1.x, pi1.y);
}
...但不起作用。帮助!!!:)
谢谢你!