Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

implementing paint

1 view
Skip to first unread message

Carramba

unread,
Mar 21, 2007, 9:43:33 AM3/21/07
to
Hi!
I have extended JButton, and know I wonder how I can implement public
void paint(Graphics g)
What I want to do is to draw a triangle in the background, that is would
by in right down corner like 40 % of the length and hight of the button,
and with 3% distance from bounds.

class MiniCalDay extends JButton {

private Color c;

MiniCalDay(String name, Color color) {
super(name);
this.c=color;
String thisObjectName = this.getClass().getCanonicalName();
setSize(settings.Size.CELL_SIZE);
setToolTipText(name);
setVerticalTextPosition(SwingConstants.CENTER);
setHorizontalTextPosition(SwingConstants.LEADING);
setBorderPainted(settings.getBorder(thisObjectName));
setBackground(settings.getBackround(thisObjectName));
setForeground(settings.getForeground(thisObjectName));
setFont(settings.getFont(thisObjectName));
}

@Override
public void paint(Graphics g) {
//draw triangle with color c ???
//how?
}
}

Thanx in advance!

Andrew Thompson

unread,
Mar 21, 2007, 10:10:17 AM3/21/07
to
On Mar 22, 12:43 am, Carramba <u...@example.net> wrote:

> public void paint(Graphics g) {

Should be paintComponent(), for Swing classes

> //draw triangle with color c ???
> //how?

<http://java.sun.com/docs/books/tutorial/uiswing/painting/
practice.html>

> }
>
> }

Andrew T.

Carramba

unread,
Mar 21, 2007, 2:26:34 PM3/21/07
to

Thanx that helped a bit;
but now the problem is in this

public void paint(Graphics g) {
// TODO Auto-generated method stub
Graphics2D g2d = (Graphics2D)g;
super.paint(g);
Polygon p = new Polygon();
System.out.println(getBounds());
p.addPoint(getBounds().x+3, getBounds().y+3);
p.addPoint(getBounds().x+10, getBounds().y+3);
p.addPoint(getBounds().x+3, getBounds().x+10);
g2d.fillPolygon(p);
g2d.drawPolygon(p);
}

/* (non-Javadoc)
* @see java.awt.Component#getBounds()
*/
@Override
public Rectangle getBounds() {
// TODO Auto-generated method stub
return super.getBounds();
}
get bounds returns absolute position, but I would like to have relative
to by able to draw triangle..

>> }
>>
>> }
>
> Andrew T.
>

Oliver Wong

unread,
Mar 21, 2007, 3:23:23 PM3/21/07
to

"Carramba" <us...@example.net> wrote in message
news:46017533$0$488$cc7c...@news.luth.se...

>
> but now the problem is in this
>
> public void paint(Graphics g) {
> // TODO Auto-generated method stub
> Graphics2D g2d = (Graphics2D)g;
> super.paint(g);
> Polygon p = new Polygon();
> System.out.println(getBounds());
> p.addPoint(getBounds().x+3, getBounds().y+3);
> p.addPoint(getBounds().x+10, getBounds().y+3);
> p.addPoint(getBounds().x+3, getBounds().x+10);
> g2d.fillPolygon(p);
> g2d.drawPolygon(p);
> }
>
> /* (non-Javadoc)
> * @see java.awt.Component#getBounds()
> */
> @Override
> public Rectangle getBounds() {
> // TODO Auto-generated method stub
> return super.getBounds();
> }
> get bounds returns absolute position, but I would like to have relative
> to by able to draw triangle..

The relative origin position is (0,0). I.e. call addPoint(3,3), and
not addPoint(getBounds().x+3,getBounds().y+3).

- Oliver


0 new messages