Hi all. Subj.
I need draw oval with certain bound width. How to set it?
Thanks for advance.
Printable View
Hi all. Subj.
I need draw oval with certain bound width. How to set it?
Thanks for advance.
you'll have to write a method that draws multiple ovals, one inside the other. The other options is to fill an oval with the background color on top of the first oval.
e.g
The above method should work for the first option, but I haven't tested it.Code:public void drawThickOval(Graphics draw, Color col, Point at, int borderWidth, int width, int height){
draw.setColor(col);
for(int i=0; i<borderWidth; i++){
draw.drawOval(at.x+i, at.y+i, width-(i*2), height-(i*2));
}
}
2 Singing Boyo:
That's great .) But is it the only possibility to do solve a problem?
Looks some... In russian that way could be described as "через жопу" (doing it throug the ass).
Generally, my task is to draw oval with certain border width on the JLabel component. I'm not forced to use Graphics class exactly. I just need bold oval drawn over the JLabel.
You can cast the Graphics object to a Graphics2D (Graphics is an abstract class), and set a new Stroke for it using BasicStroke, like this:Alternatively you can create the oval as a Shape, make it a stroked shape using the createStrokedShape method of (Basic)Stroke, then pass it to the Graphics2D.drawShape method to be drawn... I'd use the first way unless you're writing a drawing application.Code:protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(10F)); // set stroke width of 10
g2D.drawOval(200, 100, 100, 50);
...
}
If you're a bit paranoid, you can check that you haven't been passed a DebugGraphics object before casting it to Graphics2D, but it really isn't necessary.
2 dlord:
thank you. it works.
but i use Stroke like a spell ) i should learn about it and about G2d as well.
Hmm... I always forget about Graphics2D... I thought my way was strange as well, but it should have worked.
Yeah, it's not the cleverest part of the JDK... Graphics2D is the instance always used by default in the JDK, but the paint methods take a Graphics parameter which doesn't have the useful Graphics2D methods declared. Bit of a mess, really.
Your method ought to work, but it's easier to use the classes supplied.
It's possible - There is a PrinterGraphics interface that is implemented by the Graphics objects passed to Printables, and it's all very hairy, so I'd be careful around there. But for GUI stuff, I think it's safe. The only other Graphics subclass in the library is DebugGraphics, which is only used in debugging graphics(!). Graphics itself is abstract, so unless there is some undocumented subclass of Graphics being used somewhere and passed to methods in the public API (argh!), it's generally safe to assume Graphics2D.
As I said, a paranoid coder would double-check - and I guess a good coder is a little bit paranoid :D The JDK is inconsistent (as usual :rolleyes:), sometimes the check is done, sometimes not - depending who wrote the code, and how careful they were, I guess.
You're welcome... it's good to share - knowledge is valuable, but experience is precious ;)Quote:
thanks for your great advice, dlord!