Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to draw triangle in paintcomponent  RSS feed

 
Raghu ram
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody help me out.

For the Rectangle thr is a method called

g.drawRect(x,y,width,height);
But for the triangle there is no method.
Somebody told that we can draw using the method
g.drawLine(x,y,x1,y1);

Plss reply as soon as possible
 
Kerry Shetline
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you probably want is java.awt.Polygon.

To avoid nasty looking "jaggies", you might want to look into Graphics2D.setRenderingHint(...) to handle antialiasing. Here are a couple of utility methods I wrote for getting the current antialiasing state (in case you want to save and restore it) and for setting the state on and off:

 
Raghu ram
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt get your code. I want the code for drawing triangle
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "raghu1ram"-

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, a triangle is a polygon with 3 line segments. So after defining the polygon, a call to drawPolygon will draw it.

Alternatively, you can draw it using 3 drawLine calls.

What do you have so far?
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now that's not that hard, is it?

Of course you can also use ints instead of Points if you already have those.
 
Raghu ram
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime Thanks for giving the solution.......
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!