Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Image won't display

pnorrispnorris Posts: 2Member
I'm trying to add an image to a canvas then add that canvas to a frame but the image isn't displaying. Any ideas on how to fix this?

[code]
Image img1 = Toolkit.getDefaultToolkit ().getImage ("clef.jpg");
g.drawImage(img1, 100, 100, this);
canvas1.update(g);
frame1.add(canvas1);
frame1.pack ();
frame1.show ();
[/code]

Comments

  • programmersingprogrammersing Posts: 3Member
    : I'm trying to add an image to a canvas then add that canvas to a frame but the image isn't displaying. Any ideas on how to fix this?
    :
    : [code]
    : Image img1 = Toolkit.getDefaultToolkit ().getImage ("clef.jpg");
    : g.drawImage(img1, 100, 100, this);
    : canvas1.update(g);
    : frame1.add(canvas1);
    : frame1.pack ();
    : frame1.show ();
    : [/code]
    :





    it depends on the softwarer you are using for the drawing. you can apply clip art in microsoft wod or use coreldraw.
    in case u feel like using coreldraw because it is very fast you just import the image to be added to the object and you then paste it from the file menu.
    bye 4 now
    sing

  • zibadianzibadian Posts: 6,349Member
    : : I'm trying to add an image to a canvas then add that canvas to a frame but the image isn't displaying. Any ideas on how to fix this?
    : :
    : : [code]
    : : Image img1 = Toolkit.getDefaultToolkit ().getImage ("clef.jpg");
    : : g.drawImage(img1, 100, 100, this);
    : : canvas1.update(g);
    : : frame1.add(canvas1);
    : : frame1.pack ();
    : : frame1.show ();
    : : [/code]
    : :
    :
    :
    :
    :
    :
    : it depends on the softwarer you are using for the drawing. you can apply clip art in microsoft wod or use coreldraw.
    : in case u feel like using coreldraw because it is very fast you just import the image to be added to the object and you then paste it from the file menu.
    : bye 4 now
    : sing
    :
    :
    My guess is that he's using java to show the image, since this is a messageboard dedicated to the Java programming langauge.
  • CrimmoCrimmo Posts: 4Member
    : I'm trying to add an image to a canvas then add that canvas to a frame but the image isn't displaying. Any ideas on how to fix this?
    :
    : [code]
    : Image img1 = Toolkit.getDefaultToolkit ().getImage ("clef.jpg");
    : g.drawImage(img1, 100, 100, this);
    : canvas1.update(g);
    : frame1.add(canvas1);
    : frame1.pack ();
    : frame1.show ();
    : [/code]
    :

    I think the problem is with the 'g.drawImage' line. This needs to be inside an overriden paint method, which will then be called by either 'frame1.show();' or 'canvas1.repaint();' to update any changes.

    So, something like:

    [code]

    // simplified overridden paint() method
    public void paint(Graphics g){
    Image img1 = Toolkit.getDefaultToolkit ().getImage ("clef.jpg");
    g.drawImage(img1, 100, 100, this);
    }

    // with this code inside your constructor or other main method
    frame1.add(canvas1);
    frame1.pack();
    frame1.show();
    [/code]

    should be a bit closer to what you're after.
Sign In or Register to comment.