I have let's say two Path2D, one contains the other. When I move a point from first shape, the second shape move the same, but because of the angle changing, the distance between shapes changes too (and the final result...).
I have this so far, with hard coded points for the second triangle(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);
}
}
How can I maintain the same distance all around (say 10px), programmatically?
The distance between the outer triangle and the red (inner) triangle should be the same always, in all directions. Any idea?
Currently I'm working on something:
// 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);
}
... but doesn't work. HELP!!! :)
Thank You!