Drawing a circle

Hi Friends,
I have the following requirement. I am relatively new to Java
1. Draw a circle
2. Draw a rectange at the center of the circle
3. Draw lines from the top edge of the circle to various points in the circle.
4. Convert this graphic into a gif or a png image.
Can this be achieved using Java 2D. If yes, can you pls provide me some sample code if possible.
Thanks a lot for your help.

You can also use the TextLayout class for displaying text.
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class GraphicsTest
    public static void main(String[] args)
        GraphicsPanel panel = new GraphicsPanel();
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel.getUIPanel(), "North");
        f.getContentPane().add(panel);
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
class GraphicsPanel extends JPanel
    Font font;
    String text;
    public GraphicsPanel()
        font = new Font("lucida bright regular", Font.PLAIN, 22);
        text = "centered text";
        setBackground(Color.white);
    protected void paintComponent(Graphics g)
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth();
        int h = getHeight();
        int cx = w/2;
        int cy = h/2;
        int dia = Math.min(w,h)*7/8;
        g2.setPaint(Color.blue);
        g2.draw(new Ellipse2D.Double(cx - dia/2, cy - dia/2, dia, dia));
        g2.setPaint(Color.orange);
        g2.draw(new Rectangle2D.Double(cx - dia/4, cy - dia/4, dia/2, dia/2));
        g2.setPaint(Color.blue);
        double theta = -Math.PI/2;
        double
            x1 = cx,
            y1 = cy + (dia/2) * Math.sin(theta),
            x2,
            y2 = cy + (dia/4) * Math.sin(theta);
        g2.draw(new Line2D.Double(x1, y1, x1, y2));
        theta = -Math.PI*3/4;
        x1 = cx + (dia/2) * Math.cos(theta);
        y1 = cy + (dia/2) * Math.sin(theta);
        x2 = cx + (dia/4) * Math.sqrt(2) * Math.cos(-Math.PI*5/4);
        y2 = cy + (dia/4) * Math.sqrt(2) * Math.sin(-Math.PI*5/4);
        g2.draw(new Line2D.Double(x1, y1, x2, y2));
        theta = -Math.PI/4;
        x1 = cx + (dia/2) * Math.cos(theta);
        y1 = cy + (dia/2) * Math.sin(theta);
        x2 = cx + (dia/4) * Math.sqrt(2) * Math.cos(Math.PI/4);
        y2 = cy + (dia/4) * Math.sqrt(2) * Math.sin(Math.PI/4);
        g2.draw(new Line2D.Double(x1, y1, x2, y2));
        g2.setFont(font);
        FontRenderContext frc = g2.getFontRenderContext();
        float textWidth = (float)font.getStringBounds(text, frc).getWidth();
        LineMetrics lm = font.getLineMetrics(text, frc);
        float x = (w - textWidth)/2;
        float y = (h + lm.getHeight())/2 - lm.getDescent();
        g2.drawString(text, x, y);
    public JPanel getUIPanel()
        JButton save = new JButton("save");
        save.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent e)
                saveImage();
        JPanel panel = new JPanel();
        panel.add(save);
        return panel;
    private void saveImage()
        int w = getWidth();
        int h = getHeight();
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        paint(g2);
        g2.dispose();
        try
            ImageIO.write(bi, "png", new File("test.png"));
        catch(IOException ioe)
            System.out.println("IOE: " + ioe.getMessage());
}

Similar Messages

  • How do I draw a circle in MUSE?

    How do I draw a circle in MUSE? Holding shift&rectangle tool (as in InDesign) isn't working.

    obcomm - Use the Rectangle tool to draw a square. Click the corner options in the Control toolbar, then increase the size until you have a circle.
    David

  • Draw animated circle with brush tool in Flash

    Hello all,
    I need to make a circle with a brush tool so that it looks like a child draws it. If I make a dot with the brush tool on the first frame and a circle on the 15th frame and then create shape tween between those frames it looks stupid, like the circle grows itself.
    Is there any possibility of making such a thing except for drawing it per frame?
    Thanks
    Julia  

    I'm just asking what is the method to draw a circle with a brush that you can see it animated - I mean the process of drawing. For now the only thing I know is to draw every small piece of it frame by frame but I suppose there is a better method.

  • Draw graphics, circles

    Hi Guys,
    I ahve a question in ABAP using graphics.
    can we draw a circle, rectangle(a box) , arrows in ABAP.
    My requierment is, we have certain products which we need to link with arrows and there are group of products which I need to put into circle or rectangle.
    plz suggest..
    regards,
    Nazeer

    Hi Nazeer,
    use given below code & try to understand. Test run on your server.
    DATA: BEGIN OF TAB OCCURS 5,
          CLASS(10) TYPE C,
          VAL1(2) TYPE I,
          VAL2(2) TYPE I,
          VAL3(2) TYPE I,
          VAL4(2) TYPE I,
          VAL5(2) TYPE I,
          VAL6(2) TYPE I,
          END OF TAB.
    DATA: BEGIN OF OPTTAB OCCURS 1,
            C(20),
          END OF OPTTAB.
    MOVE: 'anik' TO TAB-CLASS,
          6 TO TAB-VAL1, 7 TO TAB-VAL2, 8 TO TAB-VAL3, 3 TO TAB-VAL4,  5 TO TAB-VAL5, 6 TO TAB-VAL6.
    APPEND TAB.
    CLEAR TAB.
    MOVE: 'narendra' TO TAB-CLASS,
          15 TO TAB-VAL1, 10 TO TAB-VAL2, 18 TO TAB-VAL3, 3 TO TAB-VAL4,  5 TO TAB-VAL5, 6 TO TAB-VAL6 .
    APPEND TAB.
    CLEAR TAB.
    MOVE: 'gaurav' TO TAB-CLASS,
          17 TO TAB-VAL1, 11 TO TAB-VAL2, 20 TO TAB-VAL3, 3 TO TAB-VAL4,  5 TO TAB-VAL5, 6 TO TAB-VAL6.
    APPEND TAB.
    CLEAR TAB.
    MOVE: 'Kushagra' TO TAB-CLASS,
          10 TO TAB-VAL1, 12 TO TAB-VAL2, 15 TO TAB-VAL3, 3 TO TAB-VAL4,  5 TO TAB-VAL5, 6 TO TAB-VAL6.
    APPEND TAB.
    OPTTAB = 'FIFRST = 3D'.     APPEND OPTTAB.
    OPTTAB = 'P2TYPE = TO'.     APPEND OPTTAB.
    OPTTAB = 'P3TYPE = PY'.     APPEND OPTTAB.
    OPTTAB = 'P3CTYP = RO'.     APPEND OPTTAB.
    OPTTAB = 'TISIZE = 5'.      APPEND OPTTAB.
    OPTTAB = 'CLBACK = X'.      APPEND OPTTAB.
    *CALL FUNCTION 'GRAPH_MATRIX_3D'
        EXPORTING*
             COL1        = 'monday'*
             COL2        = 'tuesday'*
             COL3        = 'wednesday'*
             DIM2        = 'employees'*
             DIM1        = 'months'*
             TITL        = 'Comparison of Employee attendace in different months.'*
        TABLES*
             DATA        = TAB*
             OPTS        = OPTTAB*
        EXCEPTIONS*
             OTHERS      = 1.*
    CALL FUNCTION 'GRAPH_MATRIX_3D'
      EXPORTING
        COL1 = 'monday'
        COL2 = 'tuesday'
        COL3 = 'wednesday'
        COL4 = 'thursday'
        COL5 = 'friday'
        COL6 = 'saturday'
        DIM1 = 'week days'
        DIM2 = 'employees'
      TABLES
        DATA = TAB
        OPTS = OPTTAB.
    LEAVE PROGRAM.
    I hope it will help u a lot.
    Regards,
    Narendra

  • Req: Draw Concentric Circles - help please!

    Hi all!
    Newbie question:
    I'm trying to draw a series of concentric circles in Flash 5
    The circle tool always starts to draw the circle from a
    corner - this makes it tricky to draw a set of concentric circles
    I'm sure there's a way of setting defaults so that Flash will
    draw circles (or other shapes like rectangles) about their centres
    instead of their corners, but I can't find the setting.
    Sorry for the dumb question!
    Thanks in anticipation
    Dave

    Thanks, but it's still not quite what I'm after.
    If you scale the circles serially as you suggest, the gap
    between circles is not equal; as the circles get smaller, the gap
    between them also gets narrower, rather like ripples on a pond. I
    wanted the circles to be concentric, but each one to be a fixed
    distance from the next, rather than a geometric progression.
    Also, I'd still like to be able to constrain the "draw
    rectangle" and "draw circle" commands to draw from the centre
    rather than the corner....
    Any more suggestions, or is upgrading to Flash 8 the only
    solution ? ;-)
    Thanks
    Dave

  • Where can I find the VI "draw a circle by radius" (it's used in NI demo) ?

    I work wiht LabVIEW 6.0.2 full version
    And I'd like to use some "picture functions". But I didn't find those functions... So I went to NI site to find some help but it seems that some commands and functions are missing on my computer because I can't find the library "picture.llb" and the VI "draw a circle by radius" whereas with NI example it is written that no add-on is required...

    Take attached zip file
    Attachments:
    picture.zip ‏204 KB

  • How Do I Draw a Circle / Line Through An Image In Pages?

    Hi guys,
    I never do anything very involved in Pages but today ...
    I have an image (jpeg file) and I would like to draw a circle around it and a line through it -- you know, turn it into one of those International "Don't Do This" signs.
    Is there an easy way?
    Thanks
    I'm using Pages '09 version 4.1

    Insert a round Shape and give it a very thick border with equally thick line (also in Shapes) diagonally across the circle.
    To add the image just drag it onto the shape and when you positioned it the way you like it, group the two.
    Peter

  • Where can I find the Ellipse Tool in PE 12 in order to draw a circle ?

    Where can I find the Ellipse Tool in PE 12 in order to draw a circle ?

    ReaderTortune
    Besides the article to which GautamBahl referred....
    You may also want to review the following for working with the Titler's Shapes in Premiere Elements 12.
    http://www.atr935.blogspot.com/2013/08/pe11-titler-shapes-for-highlighting.html
    Thanks.
    ATR

  • Draw a circle on a jFrame

    Hi! How can i draw a circle over a jFrame?

    In general trying to draw directly on a JFrame is not a good idea and can be problematic. You should think about doing your drawing in a JPanel and adding that to the frame
    Here are a number of things to look at in drawing a circle
    Graphics.drawOval(...)
    Ellipse2D, Ellipse2D.Double
    Area - you can create an area from any Shape, and it offers a lot of advantages to dealing with Shape
    Graphics2D.draw/fill(Shape)

  • How to draw the circle diagram of induction motor using labview

    hi
      i am trying to model the  electrical machines using labview..
    i want to know that is it possible to draw the circle diagram of induction motor using labview..........
    if its possible then please suggest me the possible ways.
                                                                                         thanks

    There may be better ways, but the method I used was to convert amplitude & angle to rectangular coords, then create an XY plot from (0,0) to those coords.  (Or 1 plot for Voltage, 1 for Current, etc.)  I would then just keep updating this plot and feeding it to the graph in a loop.  The result was a slightly choppy looking but useable animation of the phase vectors.
    -Kevin P.

  • Drawing smooth circles in labview

    Hi 
    I am using the 'Draw Circle by Radius' VI to draw a pie chart inside a picture box. Its all fine, except that the circle edges are not exactly smooth (see the attached pic). 
    I tried looking for it on the net, but couldn't find anything of much use. 
    Any ideas on how to make the edges nice and smooth? 
    Solved!
    Go to Solution.
    Attachments:
    Circle Jagged edges.png ‏26 KB

    Hi
    The circle is not smooth as no anti-aliasing is done with the picture drawing VIs. To get it to appear smooth you would need to implement an anti-aliasing algorithm in the picture control. This is not something I have ever seen done. You could download MSChart from Microsoft, it is just a free .NET component and code that you can download. Thoric has done a great example here https://decibel.ni.com/content/docs/DOC-9946 that you could start with to draw the pie chart for you.
    Mike

  • This is basic but it's killing me, draw perfect circles inside each other

    Hello,
    I'm trying to draw a perfect circle in PS, I know you hold shift and drag the eclipse tool, to create a 2nd circle, I contracted it, added a new layer and filled it, I continued but I noticed as I went to the 3rd circle, it stopped being a perfect circle, what is the perfect way to do this? I even tried using the Ellipse vector tool but no luck. Here's what I did so far.
    Thanks in advance.

    Edit: Actually same method as post #1 but with a stroke style applied.
    Here I have used a grid so I could easily line up the center cross hairs when I started the shape. I used a layer style on my shapes. In this case, it's stroke with the fill set to 0%.

  • How to animate drawing of circle outline from first point to completed circle?

    How can one animate the drawing of a circle outline in Edge Animate from the first point to the complete circle? Would a better approach be to create a gif and import that into the Edge Animate project? Thank you for your time and expertise.

    This first image is the file in ID with High Quality Display on
    This is with Overprint Preview and that what I aming for, roughly, for the final. [I'm not sure what is the purpose of overprint preview in ID].
    This is the 3 plates of Black and two pantones
    This is the plate of Black plate
    This is the Pantone 812 c plate
    and finally this is the Pantone 8201 c plate
    Hope this can help to get the best solution

  • I have to be missing something badly: How do I draw a circle?

    I'm frustrated; please forgive it that comes through. I've spent an hour trying everything I can think of and searching here and I still don't see how to do this.
    In all the other editors I've used, to do this you pick the circle tool, form the circle by clicking and dragging, and then you can select the circle and edit it's line weight, the line color, and size.
    I'm using PSE9. I have an object (pic) on the background layer and there is an area on the object I want to circle to point out a feature. I don't want to blow it up in size or make some kind of inlay or overlay; I just need to circle it to direct attention to it just like you would circle something with a pencil or pen.
    I have literally tried anything and everything I can find and think of to get a circle in place with no success. I can get a circle, but it's filled and blocks the area I'm trying to focus attention to. I'm sorry to appear so dumb over such a simple thing, but I am stumped.
    Help!
    Thanks,
    Ron

    Hmmm, thanks - it's not quite as straight forward as I'd hoped for. If I understand this correctly, everytime you go too far with the width, you have to either go back with the history or delete the layer.
    Update:
    I went to Window Undo history and it brought up a history list in the lower right hand window but I didn't see how this could undo a Stroke width???
    Well actually you can click on Stroke and delete that history and it reverts back to the previous width. The trick to that is knowing which Stroke(s) you need to delete and keep.
    Update 2:
    I deleted a Stroke and both layer 1 and layer 2 disappeared. I had one circle on each layer. UNDO nor REDO would restore the action and the entire histroy listing went away. It seems once this happens, all work on layers is lost. Not only was that work lost, my lightening and darkening enhancements disappeared. This seems to be a very risky action.
    This sounds awfully awkward for a simple task of putting a circle around something???
    Well, you can use UNDO to go back also. Just seems a little work-aroundish, but I guess that's just the way it is.
    The down side of any of this is that if a circle has been drawn say at 10 px and after some other editing one needs to change the Stroke to a lesser value, it appears that the layer will have to be deleted, a new layer created, and a new circle drawn. Am I on the right track here?
    Thanks,
    Ron

  • How to draw a circle with fill

    This is probably the easiest question you'll get today, so enjoy!!!
    I'm trying to create a simple circle with fill (yeah, I know. Easy). So I used the Ellipse Tool and set the fill to blue. (below). The problem is the fill doesn't fill all the way to the border and there is a gray border around it.
    I really, really just want a nice blue circle...... is that too much to ask... ? (*sniff*)
    Thanks in advance!

    Hi,
    That looks like your seeing the path of the ellipse.
    If you go to the paths panel you can click below the path in the panel to hide the path.
    What version of photoshop are you using?
    Path visible
    path hidden

Maybe you are looking for