# draw Line triangle

Page 1 of 1

## 3 Replies - 74415 Views - Last Post: 18 May 2008 - 12:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=52352&amp;s=6a6cae404d2a8b452a19ea8860ceb6f3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dingo190

Reputation: 0
• Posts: 6
• Joined: 26-February 08

# draw Line triangle

Posted 17 May 2008 - 08:43 AM

hw do i find 3 side of triangle ... in simple code.... i been crunking my head...
i did find some web and try ... but still can't nid some help wif it ~.... using drawLine

```import java.awt.*;

public class MyTriangle implements Myshape
{
protected int height;
protected int width;
protected Color color;
public MyTriangle(int width, int height, Color color)
{
this.height = height;
this.width= width;
this.color= color;

}
public void drawMe(Graphics g, Point location)
{
g.setColor(color);
g.drawLine((int) location.getX(), (int) location.getY(), width, height);

}
}

```

Is This A Good Question/Topic? 0

## Replies To: draw Line triangle

### #2 DillonSalsman

Reputation: 13
• Posts: 144
• Joined: 30-October 07

## Re: draw Line triangle

Posted 17 May 2008 - 10:56 AM

First off, getX() and getY() both return doubles, so just use location.x and location.y : )

For a triangle with a flat horizontal base and the 3rd point being above the middle of the base line, your 3 points are going to be:
```		location (this will be your first point);
Point point2 = new Point(location.x+width,location.y);
Point point3 = new Point(location.x+(width/2),location.y -height);

```

Just connect the points to each other and you have a triangle.
```		Point point2 = new Point(location.x+width,location.y);
Point point3 = new Point(location.x+(width/2),location.y - height);
g.drawLine(location.x,location.y,point2.x,point2.y);
g.drawLine(location.x,location.y,point3.x,point3.y);
g.drawLine(point2.x,point2.y,point3.x,point3.y);

```

That should work
I don't have any experience with Java2D and havn't tested that code but thats just how I'd go about it

So to add that into your code I do believe it would be:
```import java.awt.*;

public class MyTriangle
{
protected int height;
protected int width;
protected Color color;
public MyTriangle(int width, int height, Color color)
{
this.height = height;
this.width= width;
this.color= color;

}
public void drawMe(Graphics g, Point location)
{
g.setColor(color);
Point point2 = new Point(location.x+width,location.y);
Point point3 = new Point(location.x+(width/2),location.y - height);
g.drawLine(location.x,location.y,point2.x,point2.y);
g.drawLine(location.x,location.y,point3.x,point3.y);
g.drawLine(point2.x,point2.y,point3.x,point3.y);

}
}
```

This post has been edited by DillonSalsman: 17 May 2008 - 11:34 AM

### #3 DillonSalsman

Reputation: 13
• Posts: 144
• Joined: 30-October 07

## Re: draw Line triangle

Posted 17 May 2008 - 11:15 AM

This is the best part of answering questions on here, LEARNING and TEACHING at the same time : )

Alright, and so now to actually make use of that code
```import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class TriangleApplet extends Applet
{
MyTriangle mt = new MyTriangle(50,70,Color.ORANGE);
public void paint (Graphics g)
{
mt.drawMe(g, new Point(100,140));
}
}

```

This post has been edited by DillonSalsman: 17 May 2008 - 11:32 AM

### #4 dingo190

Reputation: 0
• Posts: 6
• Joined: 26-February 08

## Re: draw Line triangle

Posted 18 May 2008 - 12:01 AM

ok thx guys i go try it out ~.... thx for the help~