Image won't display - Programmers Heaven

Howdy, Stranger!

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

Categories

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.