Results 1 to 9 of 9
  1. #1
    inc_123 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default How to set line width in Graphics object?

    Hi all. Subj.
    I need draw oval with certain bound width. How to set it?
    Thanks for advance.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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
    Java 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));
         }
    }
    The above method should work for the first option, but I haven't tested it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    inc_123 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    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.
    Last edited by inc_123; 08-15-2009 at 08:07 PM.

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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:
    Java 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);
        ...
    }
    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.

    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.

  5. #5
    inc_123 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    2 dlord:

    thank you. it works.

    but i use Stroke like a spell ) i should learn about it and about G2d as well.

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Hmm... I always forget about Graphics2D... I thought my way was strange as well, but it should have worked.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Singing Boyo View Post
    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dlorde View Post
    ... Graphics2D is the instance always used by default in the JDK, ...
    Don't take this as gospel, but I think that there are exceptions here, especially (again I think) when printing is involved. Please correct me if wrong.

    ...Bit of a mess, really.
    Yep, agree 100%

    As always, thanks for your great advice, dlorde!
    Last edited by Fubarable; 08-15-2009 at 11:10 PM.

  9. #9
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    Don't take this as gospel, but I think that there are exceptions here, especially (again I think) when printing is involved. Please correct me if wrong.
    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.

    thanks for your great advice, dlord!
    You're welcome... it's good to share - knowledge is valuable, but experience is precious ;)

Similar Threads

  1. Replies: 2
    Last Post: 03-01-2011, 02:30 AM
  2. problem with changes of Dynamic input width box
    By synclenovo in forum New To Java
    Replies: 1
    Last Post: 04-09-2009, 01:15 AM
  3. Get Image Height and Width
    By neeraj.singh in forum AWT / Swing
    Replies: 10
    Last Post: 02-18-2009, 02:09 PM
  4. ComboBox Fix Width
    By Azndaddy in forum New To Java
    Replies: 2
    Last Post: 05-29-2008, 05:23 AM
  5. how to retrieve the width of the column
    By katie in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 11:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •