Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Arrow Button

833176Jan 21 2011 — edited Jan 22 2011
Hello all,

I am trying create a button in the shape of an arrow with rounded edges, I have managed to create the rounded edges by using a RoundedRectangle as the shape of the button. However I am unable to attach a tringle at the end of the rounded rectangle to make it look like an arrow. The approximate shape I am trying to achieve is shown below:

________
|```````` \
|________ /


The code fragment I have written so far to create this shape is below. What I have done is subclassed a JButton, in the constructer set the shape of the button, followed by overriding the paintComponent() to paint the custom shape.

The width and heights used is worked out based on the font and the text the button will display.
The m_areaFill object is what is used by the paintComponent().
protected void setShape() 
{
	// The rounded rectangle
	rectMain = new RoundRectangle2D.Double(0d, 0d, m_dWidthFill, m_dHeightFill, 10, 10);
	areaMain = new Area(rectMain);
		
	rect = new RoundRectangle2D.Double(0, 0, m_dWidthFill - 30, m_dHeightFill, 10, 10);
	areaMain.add(new Area(rect));

	Point p1 = new Point(((int)m_dWidthFill - 30), 0);
	Point p2 = new Point(((int)m_dWidthFill - 30), (int)m_dWidthFill);
	Point p3 = new Point((int)m_dWidthFill, ((int)m_dWidthFill/2));

	int[] xs = { p1.x, p2.x, p3.x };
	int[] ys = { p1.y, p2.y, p3.y };
	Polygon triangle = new Polygon(xs, ys, xs.length);
	areaMain.add(new Area(triangle));
}

protected void paintComponent(Graphics g) 
{
	Graphics2D g2 = (Graphics2D) g;

	RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, enderingHints.VALUE_ANTIALIAS_ON);
	g2.setRenderingHints(hints);

	if (getModel().isArmed())
	{
		g2.setColor(Color.gray);
	}
	else
	{
		g2.setColor(Color.lightGray);
	}

	g2.fill(areaMain);

	super.paintComponent(g2);
}
Firstly what I would like to ask is am I taking the correct approach to create the arrow button, or is there another approach I should be taking. Also if this is a good enough approach how would I add triangle (the arrow head) to the rounded rectangle.

Kind Regards Mark
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 19 2011
Added on Jan 21 2011
3 comments
1,584 views