Draw a picture on a component

hi,
does anyone know how i can draw a picture on a JPanel or a component that is inside a frame?
thanks

Read this section from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html]Custom Painting.
Here is a simple example:
import java.awt.*;
import javax.swing.*;
public class FaceComponent
    public static void main(String args[])
        JPanel swing = new JPanel()
            public void paintComponent(Graphics g)
                 super.paintComponent(g);
                g.drawArc( 100, 45, 80, 80, 0, 360);
                g.setColor( Color.blue );
                g.drawArc( 120, 70, 10, 10, 0, 360);
                g.setColor( Color.blue );
                g.drawArc( 150, 70, 10, 10, 0, 360);
                g.setColor( Color.magenta );
                g.drawLine ( 140, 85, 140, 100 );
                g.setColor( Color.red );
                g.drawArc ( 110, 55, 60, 60, 0, -180 );
        swing.setPreferredSize( new Dimension(200, 200) );
        JFrame frame = new JFrame();
        frame.getContentPane().add( swing );
        frame.pack();
        frame.setVisible( true );
}

Similar Messages

  • Draw a picture from a XML file

    Hi,
    the problem I have is the following.
    We have a UI (JNET) where you can create a data model which later on will be analyzied for data conistency. All details of the data model are stored in an XML file. To document what we did we need to create a word report. This word report should also contain a pricure of the data model which should be included automatically. The creation of the report itself is not a problem but I have no idea how I can include the picture of the data model.
    Is there a way of drawing/ a picture (jpg, gif, png, etc.) from a XML file using ABAP?? 
    I attached an example of the data model. It is also the target which should be displayed from the XML file.
    Thanks for your support.
    Best regards,
    Niklas

    Hi,
    Thanks for the reply.I got the solution for my problem.Actually i wanted to delete only a particular element.
    The solution to this is after using
    Element element = (Element)document.getElementsByTagName("job").item(r-2);
    element.getParentNode().removeChild(element);
    writeXmlFile(document,"xmlfilename");.

  • How to draw the picture in labview 8.6 and how to simulate the same

    Hi
    Iam using Labview8.6 and we need to draw the picture or import the autocad drawing and we need to simulate the same.
    If anybody knows please reply me.

    Hi MikeS81,
    In detail,I have one overall layout of my project in a cad format consisting of Shell,Boom etc.. Simulation means when I give a run command to Shell motor,my Shell in the picture also should be rotate.i.e.,user has to understand what is going on in the field.
    Regards
    vbk123

  • Putting more than one picture on a component?

    Hey
    im creating an AI agent that wanders around a world and eat and drinks and all that in java. Each position in the world is represented by a JButton(at the mo). My problem is that i need to be able to show two or more pictures on the one JButton at a time on top of each other, i.e. a position in the world could have the agent and a food source at the same time. Does anyone know if i can do this with a JButton or should i change to a panel or something different -
    Thanks

    Hello, I think what tjacobs01 means is to overwrite the paintComponent method. In doing this, you do not have to worry about the Graphics g Object . This is passed to the method automatically.
    The img1 and img 2 references would be the two images you wanted to place with the button, ie, a world location and a food location. (S)He has placd them as img1 and img2 as generic references, perhaps putting in img1 is worldImage and img2 as foodImage it may have been more understandable. I am not trying to bag tjacobs01, just trying to show you what (s)he may have meant..
    tjacobso1's code is below
    public void paintComponent(Graphics g) {
    g.drawImage(img1, ...)
    g.drawImage(img2, ...)
    }The code I am replying to was,
    public Graphics paintComponent(Graphics g)
    g.drawImage(img1, ...)
    g.drawImage(img2, ...)
    return g;
    JButton jb = new JButton();
    jb.setIcon(paintComponent(new Graphics));You could try something like
    // you image does not have to be an ImageIcon, it can be whatever you would like
    ImageIcon worldImage = new ImageIcon("yourWorldImage.gif");
    ImageIcon canOfDrinkImage = new ImageIcon("yourCanOfDrinkImage.gif");
    ImageIcon hotdogImage = new ImageIcon("yourHotdogImage");
    // as many images as you would like
    // it would be best to have boolean values for saying whether to display an image or not, or flags of some sort to determine which images to display. Then in the paintComponents method below, you can easily choose which image to paint.
    JButton jb = new JButton() {
            // this is called an anonymous inner class, as it is an inner class without a name.
            public void paintComponent(Graphics g) {
                // make some ints to hold the positions of the next image to draw, update these after every image added below.
                int x = 0;
                int y = 0;
                int distanceBetweenImages = 3;
                int imageWidth = 32; // this size should be known by you or you can find out from the ImageIcon.getIconWidth();
                int imageHeight = 32; // same as above comment
                // use the booleans that i mentioned previously here, in a manner similar to ...
                if (showWorldImage) {
                     * since the g.drawImage has a lot of overloaded methods, meaning you can call it with different parameters, it is worth looking at the api code for the java.awt.Graphics class.
                     * This seems like the most basic method for your use
                     * drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
                     * I have not talked about the ImageObserver at all, but you should be able to find details about it also in the API.
                   g.drawImage(worldImage, x, y, imageWidth, imageHeight, observer);
                   x += imageWidth + distanceBetweenImages;
                if (showCanOfDrinkImage) {
                    // include same as showWorldImage
                   g.drawImage(worldImage, x, y, imageWidth, imageHeight, observer);
                   x += imageWidth + distanceBetweenImages;
    };Then when the JButton needs to be repainted it will call that method and repaint according to what images you have.
    I have not icluded the code to paint any text string, if you wanted to display text..
    If you think this is too much work, and you would prefer to use a JPanel, that is up to you. With a JPanel you will not have to worry about spacing between the icons etc, and I would recommend you use JLabels for the Images.
    Jack573

  • Drawing on top of a component

    Hi there.
    if I have JPanel that contains a JLabel that contains is a 600x800 image, how can i draw a shape like circle on top of the image? I would like to be able to see the image with the circle on it. How can i make this possible?
    Right now i can draw the circle on its own JPanel, but that is not what i want.
    Thanks

    Notably, that you have a inner class referencing
    image variables defined in the class it's nested in.Inner classes can access all members of the declaring class,
    even private members. In fact, the inner class itself is said to be
    a member of the class; therefore, following the rules of
    object-oriented engineering, it should have access to all
    members of the class.
    The inner class does nothing to set it's own size,
    , since as far as that JLabel instance knows, it
    contains nothing.
    But this is not corrected, it's
    only relying on the BorderLayout to fill the frame
    with the label.
    While your sample works fine as is, it's not very
    portable. As the OP posted after, he attempted to
    use your code adding a JPanel in between the content
    pane and the label, and it wouldn't work because of
    the layout issues. To solve that, one might tend
    towards defining a null layout and setting the bounds
    explicitly, or one might add a setMinimumSize or
    setPreferredSize call to get a size.
    But that's adding more code to fix a problem that has
    a much simpler solution. Which is to just give the
    label the image directly, let the label size itself
    automatically based on that image, let the label draw
    the image itself and only override paint to get the
    custom drawing done on top.
    Myself, I understand what you were saying in your
    code. But clearly the OP did not, and I suspect was
    heading to starting patching what wasn't working by
    applying more code when one could more cleanly use
    less code and take advantage of what JLabel will
    already do for you.Agreed. But this problem can also be easily solved with "setPreferredSize(new Dimension(200,200));" in the constructor of "Picture".

  • Can anyone tell me how to draw these pictures

    i dunno how to draw the lower two circuits ...can anyone draw them for me ?
    Attachments:
    Picture 001.jpg ‏821 KB

    If your drawing for cct 3 is correct (with a node/junction @ the center of the X), then it will be like mostafa_cct3-1. This is equivalent to the next cct that I have drawn, i.e. mostafa_cct3-2.
    It's your drawing, only you can tell if it is drawn correctly (or ask your lecturer?). Calculating the current is simple in this case - just a matter of series/parallel resistors.
    Attachments:
    mostafa_cct3-1.jpg ‏61 KB
    mostafa_cct3-2.jpg ‏58 KB

  • Draw with picture control using even structure

    what i want to do is use the event structure and picture function vi to draw a figure on the displayed file
    Attachments:
    picturecntrl.vi ‏25 KB

    Maybe something like that? (LV 8.5).
    (You still need to add support for the zoomfactor, etc.)
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    picturecntrlMOD.vi ‏32 KB

  • Drawing a picture within JFrame when I've already created a JFrame

    Hi everyone,
    I was wondering if you could help me out with this problem I'm getting. I'm creating a program to simulate an 'elevator.' When first creating the project I created a brand new 'Java GUI Form' : 'JFrame Form'. From here I went on to drag-and-drop various command buttons and labels to create the user interface (the 'inside' of an elevator). Here is where my question comes in. On the very left of my interface I want to have the program draw a simulation of a box (the elevator) moving up and down with the 'floor numbers' the user pushes. However i don't know how to access the coding of the jFrame object that I created through the design editor so i can input the coding.
    First Question: What do i have to 'write' that would force that box to print into the current JFrame?
    Second Question: What Code (and where do i put it) essentially 'locks' the JFrame from being resized?

    Here, maybe by putting this it might help you recognize what i'm trying to say. The code i'm trying to put in is:
    JFrame window = new JFrame ("Drawing");
            window.setBounds(200,200,700,500);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setVisible(true);
            Container contentPane = window.getContentPane();
            contentPane.setBackground(new Color(125, 125, 125));
            Graphics g = contentPane.getGraphics();* This is a portion of the code
    When run, this program creates a NEW JFRAME WINDOWS, i'm trying to make it run inside an already existing JFrame, whose variable Name i dont know how to find.

  • Need Help with drawing in Photoshop CC

    I am drawing a picture and want to select and copy an element but don't know how.  Can someone help me please?

    If element is on it's own layer, from Layers panel right click and select duplicate layer.
    Nancy O.

  • Component Video Cable and Bumper Case

    Just a head's up. If you have an iPhone 4 and a bumper case, the Component video cable will not fit properly when the bumper case is on. It inserts, but the cutout that fits the standard USB cable is too small for the plastic part of the cable that is larger on the Component Video cable.
    When I tried to plug it in, I did not notice this the first time and got the "This accessory is not compatible" message. I also got B&W video. When I took the case off and the cable seated securely, I was able to get 420p video from the video I took with the camera. NOTE - The Component cable only supports 420p when playing on an NTSC TV. I was also able to see YouTube at 420p. I did not try anything else.
    Hope this helps some of you avoid frustration

    Yes I do live in Europe. But who says that Component Video Cable is a standart from USA or Japan. I have been using Component Video Cables to connect my DVD Player with my video projector since 2002. Component Video Cables are wisely used in Europe. And its the ONLY analogue way of transferring HD-Video (Plus DVI and HDMI)
    What disturbs me alot is that on www.toshiba-europe.com they explain that my computer has:
    Interfaces 1 x headphone
    1 x DC-in
    1 x line-in
    1 x external monitor
    1 x RJ-11
    1 x RJ-45
    1 x i.LINK (IEEE 1394)
    4 x USB 2.0
    1 x Monitor-in (line-in port)
    1 x TV-antenna
    1 x S-Video-in (Composite/Line-in)
    1 x TV-out (s-video, Component-out))
    1 x 5-in-1 Bridge Media slot (supports SD Card, Memory Stick, Memory Stick Pro, MultiMedia Card, xD-Picture Card)
    Where is the Component-out ???Promise is a promise. Why do they say it if they dont give it?
    Instruction manual also says that : D-Video port lets you transfer 525i(480i),525p(480p),1125i(1080i) or 750p(720p) data to external devices.
    How can you transfer 720p with a Scart Cable?? It is impossible.... The only way that Qosmio can transfer 1280x720 picture is a Component Video cable.(Because it has no DVI port) How much does this cable cost anyway?? But the problem is NOBODY is producing it in Europe. I am writing to Toshiba-Japan. Since they are saying that my computer has Component-out so they must get me the cable....

  • Want a Simple Way to Insert a Simple Picture

    I'm having trouble inserting a simple picture into a pages document. I don't care about anything fancy, the document is an academic paper and I just want a simple diagram with a few boxes and arrows. No colors, animation, bitmaps, etc. I created the diagram in Keynote first. Then I grouped all the boxes and arrows into one object and copied the object. Then I pasted the object into the Pages document. What I expected is the text would wrap around it and i might have to tweak that a bit. My preference is just to have text above and below the picture, no fancy text wrapping, the picture is wide enough it takes up the width of most of the page anyway. But the text and image seem to blur together.
    Just to be clear, what I want should have the following layout shown in quotes below:
    "Blah, blah, blah...as we can see in the diagram 1 below:
    BOXES AND ARROW PICTURE GOES HERE
    As we can see in diagram 1 above..."
    I've tried anchoring the text with "below:" or "As we can see" but I can't figure out how to change the anchor. I position the cursor where I want the image to be (in the blank space that says BOXES above) but it seems to want to place it higher up (the little blue anchor thing isn't where I expect but further up in the paragraph) and I can't figure out how to change it and I've even -- and this just shows how desperate I am -- looked at the documentation and still can't figure it out.
    I also tried various hacks like inserting a bunch of line breaks or even a new page all on it's own and the picture still ends up getting displayed strange. The picture takes up about .75 the width and .25 the height of a single page.
    I know I must be doing something stupid and am probably missing something very basic. Any help would be greatly appreciated.

    Yes, Pages 5.2.
    Thanks I'll try that.
    Regarding where I draw the picture, are you saying it would be easier with Pages or should the behavior be pretty much equivalent either way?
    I've always preferred using some simple graphics tool like Powerpoint or now Keynote for diagrams because I usually have lots of diagrams and I like to have them in a separate tool, sort of a repository for the pictures. Also, as in this case, the picture started out as part of a presentation and I think will migrate back to a presentation. For consistency and because at least with previous tools you could do more with graphics in a presentation tool I always have done it that way. But if the drawing tool in Pages works better I could use that.

  • Pictures in Table problem

    hi :
    i want the follwoing thing ,,
    im using MySQL database connected as datasource in the Creator,,
    not i have the follwing table:
    CREATE TABLE `uploads` (
      `std_id` int(11) NOT NULL default '0',
      `file_name` varchar(255) NOT NULL default '',
      `content` longblob NOT NULL
    ) TYPE=MyISAM;it has the id , name and the content of the picture
    now i want to know how to store a picture in this table useing upload component in the creator ,,and how to preview this picture in table component to preview the pictuires stored in the table
    it is urgent plz ,,,
    thanks in advance

    hi Yesenia
    thanks for ur answer
    First i did what i asked for,, storing picture in database :)
    but the problem is that ,, even i have the picture in the database or if i have these pictures outside the database,,, i need to know how can i set the image in datatable with these picture ????
    this is my new Question,,,
    thanks for ur answer.....

  • Best way to include drawing in an email?

    When writing emails, I often need/want to draw something to illustrate what I am talking about. Lately it was a rough plan of my house to show the rooms. Sometimes it could be an article of clothing. Nothing professional or precise.
    I find that drawing with a mouse has a lot to be desired and would much prefer drawing with a stylus right on the screen, but failing that, I wouldn't mind using my fingertips. I do not want to use some  other gizmo "in addition to" in order to draw something for the email or notes.
    To do this, is the iPad my best bet? I can see myself ending up sending emails on my iPad if this is true.
    Thank you for any enlightenment on this.
    -L

    Meg, I have thought about it overnight and am leaning towards getting the iPad2 with the hope that it might have some apps that let me incorporate my drawings, notes, and audio files into Apple Mail.
    I have been watching some iPad2 videos on the Apple site and  it gives one the impression that the person is writing words and drawing pictures with his fingertips right onto the iPad2 screen. Am I wrong in thinking this?
    My desire is to be writing an email to someone and illustrating whatever I am trying to describe right on top of the email. Is that possible? Or would I have to do this in some other way? Would I have to draw a picture of, for example, a house plan or a new gizmo, and then attach it to my Apple Mail?
    I downloaded AudioNoteLITE for my iMac last night and it is great, but it does not allow for drawing. It does allow for typing and recording sound. Then one can put the file into an email.
    So are there any other solutions in addition to Bamboo Paper??
    -L

  • Implementing a Text Component

    Hi all, I have to implement a custom text component for a
    project im working on. I was hoping someone could give me
    some direction on how to implement the text storage.
    I wont need more than the regular 256 ascii characters and
    text sizes could reach 4+ megs.
    1 - an array list of bytes
    2 - a stringbuffer
    3 - a LONG string
    I was thinking about doing every line as an arraylist of bytes
    and then putting the line objects into an arraylist - as opposed
    to a contiguous list of bytes.
    that way moving through the data will be easier.
    As well, im most interested in knowing how they implement the
    actual component. I was going to draw the characters (im using
    custom fonts) onto a bufferedimage and then display them onto
    a custom component that will be a JPanel with a drawn on scrollbar
    that will control the drawing bounds.
    i was hoping someone with a knowledge of how Java implements
    text components could give a brief comment on how
    text is stored in memory and drawn on to the screen in a
    text component - id be forever grateful!
    i hear a lot of people say they read the Java source code but i
    wouldnt even know where to begin in such a process, lol.
    thanks!

    Secondly, I don't really understand what you are doing or
    why you feel you need to create a custom component to display textIm working on a mathematics project.
    There is an equation editor that has to be integrated with
    graphics routines (graphs, models etc).
    This JPanel will have keypresses mapped to symbols and
    special functions with the "text" formatted around the embedded
    graphics.
    The implementation will be easy - BUT it did get me curious
    on how the JVM implements a text component.
    From the internal representation (maybe stringbuffers?) to
    how it converts each Unicode char and then paints it into
    the component.
    After all looking up each char and then decoding the TrueType
    format (which i read uses a Virtual Machine with instruction sets)
    and drawing it (directly to the component or to an image?) seems
    like it would be intensive - but text components always seem
    very fast.

  • Draw rectangle in plot

    How can i draw rectangle in graph
    with Component works++?

    Annotations were introduced to the C++/ActiveX graph in Measurement Studio 6.0. This is the best way to draw a rectangle.
    Details:
    CNiAnnotation:hape is a property of type CNiShape. CNiShape::Type is an enumeration, one value of which is CNiShape::Rectangle. Use CNiShape::XCoordinates and CNiShape::YCoordinates to specify the location and size of the rectangle. Use CNiAnnotation::CoordinateType to specify whether the coordinates of the rectangle are in axis units or in pixels relative to plot area or screen area.
    If you cannot upgrade to Measurement Studio 6.0, you could consider using 2 cursors as a workaround.
    David Rohacek
    National Instruments

Maybe you are looking for

  • ITunes quitting when I try to update iPod

    Hi! First post here ever. Been a Mac user for about 4 years. I own an 5th Gen iPod Video of 30 GB. I'm getting around iTunes 7 and even though there are things I'm still getting used to; this is close to freaking me out. I installed iTunes 7.0.1. Whe

  • Adobe Download Assistant not working on Lion

    I successfully downloaded and installed the trial of Design Premium a couple days ago (but not without having to try to install download assistant a couple of times) and I'm now trying to download AfterEffects but I'm having no luck. I either get an

  • File size from save to spreadsheet

    This may be more of a fundamental programming/computer science question (I'm a Mechanical Engineer) but I want to know just a general correlation between the number of data points created by the Save to Spreadsheet subVI and the size of the resulting

  • Multi-touch and launchpad bugs.

    Hello. I have got a MacBook Pro 13' 2010. After Mavericks installation i found some bugs with launchpad: Icons staying in this position for all time. And if I want to scroll them i must do many actions on my trackpad instead of one. Sometimes Launchp

  • Time on battery check

    Is there a way I can see how long the laptop has been running on battery? This is different than seeing an estimate how long the laptop can run on the battery.