Steve
Steve

Reputation: 49

Java Triangle Drawing

I am trying to get input from the user of three coordinate points which will be the start and end points of three lines that will form a triangle. I am pretty sure I have the logic figured out but for some reason when I run this, it only draws the points and not the lines connecting them. I will post the Triangle portion of my main method as well as my triangle class. the triangle class isn't finished I only tried to draw one of the lines to test if it worked but it didn't.

     if(input==(4)) {
           System.out.print("Enter starting X value: ");
           x1 = sc1.nextInt();
           System.out.print("Enter starting Y value: ");
           y1 = sc1.nextInt();
           System.out.print("Enter second X value: ");
           x2 = sc1.nextInt();
           System.out.print("Enter second Y value: ");
           y2 = sc1.nextInt();
           System.out.print("Enter final X value: ");
           x3 = sc1.nextInt();
           System.out.print("Enter final Y value: ");
           y3 = sc1.nextInt();
           Triangle myTriangle = new Triangle(x1,y1,x2,y2,x3,y3);

           JFrame frame4 = new JFrame();

           frame4.setSize(400,500);
           frame4.setTitle("ShapeViewer");
           frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           frame4.add(myTriangle);
           frame4.setVisible(true);}

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
public class Triangle extends JComponent{

    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private int x3;
    private int y3;
    private double l1;
    private double l2;
    private double l3;


    public Triangle(int x1, int y1, int x2, int y2, int x3, int y3) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.x3 = x3;
        this.y3 = y3;
    }
    public double getLength1(){
        l1 = Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)));
        return l1;

    }
    public double getLength2() {
        l2 = Math.sqrt(((x3-x2)*(x3-x2)) + ((y3-y2)*(y3-y2)));
        return l2;
    }
    public double getLength3() {
        l3 =  Math.sqrt(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));
        return l3;
    }
    public double getPerimiter() {
        return l1 + l2 + l3;
    }
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        Line2D.Double line1 = new Line2D.Double(x1,x2,y1,y2);
        g2.draw(line1);
        g2.draw(new Line2D.Double (x2, x3, y2, y3));
        g2.draw(new Line2D.Double (x1, x3, y1, y3));
}
}

in the end, I'm also going to have to print the values of each length as well as the area and perimeter which is why those functions are in the triangle class but I know exactly how to do that I'm just having trouble actually drawing the triangle.

Upvotes: 0

Views: 903

Answers (2)

Tamara Koliada
Tamara Koliada

Reputation: 1194

Since Java 7 it possible to use The GeneralPath class which represents a geometric path constructed from straight lines, and quadratic and cubic (Bézier) curves. It can contain multiple subpaths.

 Shape shape = null;
 Paint paint = null;
 Stroke stroke = null;

@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
    Graphics2D g2 = (Graphics2D) g;
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

    paint = Color.RED;
    stroke = new BasicStroke(1.5f,0,0,10.0f,null,0.0f);
    shape = new GeneralPath();
    ((GeneralPath)shape).moveTo(2, 2);
    ((GeneralPath)shape).lineTo(10, 2);
    ((GeneralPath)shape).lineTo(2, 10);
    ((GeneralPath)shape).lineTo(2, 2);
    ((GeneralPath)shape).closePath();
    g2.setPaint(paint);
    g2.setStroke(stroke);
    g2.fill(shape);
    g2.draw(shape);
    g2.dispose();
} 

Don't forget dispose() graphics after use.

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191671

The parameter order for Line2D.Double is

(double x1, double y1, double x2, double y2)

You put (x1, x2, y1, y2)

Sidenote: You can try Math.pow( ... , 2) in your distance functions

Upvotes: 2

Related Questions