1) choose the triangle coordinate using Pythagora's theorem or Al-Kashi's theorem (no Java, maths only)
2) code : put the coordinates found in a GeneralPath (must be closed after last point), then draw this shape !
alternative :
You may think using a GeneralPath for 3 points is too expensive. Simply make it width 3x Graphics.drawLine()....
import java.awt.*;
import javax.swing.*;
public class DrawingATriangle extends JPanel {
Polygon[] triangles;
public DrawingATriangle() {
triangles = new Polygon[3];
triangles[0] = new Polygon();
triangles[0].addPoint(50,25);
triangles[0].addPoint(50,150);
triangles[0].addPoint(150,150);
triangles[1] = getPolygon(75, 250, 100, 200);
triangles[2] = getPolygon(300, 175, -150, -75);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(int j = 0; j < triangles.length; j++)
g2.draw(triangles[j]);
}
private Polygon getPolygon(int x, int y, int s1, int s2) {
int sides = 3;
int[] xpoints = new int[sides];
int[] ypoints = new int[sides];
xpoints[0] = x;
ypoints[0] = y + s1;
xpoints[1] = x;
ypoints[1] = y;
xpoints[2] = x + s2;
ypoints[2] = y;
return new Polygon(xpoints, ypoints, sides);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new DrawingATriangle());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class FormingATriangle extends JPanel {
Line2D.Double line;
Point2D.Double[] vertices;
public FormingATriangle() {
line = new Line2D.Double(100,100,300,300);
vertices = getVertices(line);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(line);
g2.setPaint(Color.blue);
for(int j = 0; j < vertices.length; j++) {
g2.draw(new Line2D.Double(vertices[j], line.getP1()));
g2.draw(new Line2D.Double(vertices[j], line.getP2()));
}
g2.setPaint(Color.red);
for(int j = 0; j < vertices.length; j++)
mark(vertices[j], g2);
}
private Point2D.Double[] getVertices(Line2D.Double line) {
Point2D.Double[] p = new Point2D.Double[2];
Point2D p1 = line.getP1();
Point2D p2 = line.getP2();
// Try horizontal line at p1 and vertical line at p2
p[0] = new Point2D.Double(p2.getX(), p1.getY());
//System.out.printf("p[0] = [%.1f, %.1f]%n", p[0].x, p[0].y);
// Try vertical line at p1 and horizontal line at p2
p[1] = new Point2D.Double(p1.getX(), p2.getY());
//System.out.printf("p[1] = [%.1f, %.1f]%n", p[1].x, p[1].y);
return p;
}
private void mark(Point2D.Double p, Graphics2D g2) {
g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new FormingATriangle());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}