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.

有帮助吗?

解决方案

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.
    }
}

其他提示

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.

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top