3 Replies - 29686 Views - Last Post: 18 May 2008 - 12:01 AM Rate Topic: -----

#1 dingo190  Icon User is offline

  • New D.I.C Head

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

draw Line triangle

Post icon  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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 DillonSalsman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#4 dingo190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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~
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1