Custom shape in Java
-
04-12-2019 - |
题
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 have to extend the class Shape, which inside you would have to override the paintComponent so that the Graphics2D object knows what to draw.