Reputation: 725
What I want to achieve is something like this:
public void paint(Graphics g) {
Graphics2D ga = (Graphics2D) g;
MyShape c = new MyShape();
ga.draw(c);
}
I want that MyShape
class to contain the info required to draw a circle with a number inside it.
So, I guess I need to create some kind of container/component, and drew what I need (the circle and the number) inside it, and then pass it further to the method I've pasted above.
The problem is I don't know what class to extend ... and the rest of the story.
Upvotes: 0
Views: 2537
Reputation: 205785
You can certainly implement the Shape
interface yourself, but there's no need when you can use an existing subclass, such as Ellipse2D
. Just construct it with the same value for width
and height
. There's an example here that shows how to center an arbitrary glyph in an Ellipse2D.Double
.
Upvotes: 3
Reputation: 691645
A Shape is just that: a shape. A circle is a shape. A rectangle is a shape. But a circle with a number inside it is not a shape. My guess is that you in fact want something like this:
public class CircleWithNumberInside extends JComponent {
@Override
protected void paintComponent(Graphics g) {
// TODO draw a circle, and draw a number inside it.
}
}
Upvotes: 3
Reputation: 923
You have to extend the class Shape, which inside you would have to override the paintComponent so that the Graphics2D object knows what to draw.
Upvotes: 0