The Artima Developer Community
Sponsored Link

Java Answers Forum
Problem adding a canvas with an image

4 replies on 1 page. Most recent reply: May 27, 2003 5:58 PM by Leandro

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Leandro

Posts: 3
Nickname: leo2
Registered: May, 2003

Problem adding a canvas with an image Posted: May 26, 2003 6:21 AM
Reply to this message Reply
Advertisement
Hi all,
I'm having a problem adding a canvas to the frame in my program. The images get loaded by the canvas objects themselves, but when the canvas is added by the frame to itself, the image doesn't appear on the canvas until the code has been run through completely.
Is this a problem with the way the image is loaded by the canvas? I'm using the getDefaultToolkit() method in an abstract class to load the image onto the canvas.
Otherwise, is there someway to refresh the contents of the frame/canvas to reload the image? I have tried using the doLayout() and repaint() methods to no avail.

Thanks for any help you can give.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Problem adding a canvas with an image Posted: May 26, 2003 8:28 AM
Reply to this message Reply
suggest using the Container method validate()
after adding or modifying components.

Leandro

Posts: 3
Nickname: leo2
Registered: May, 2003

Re: Problem adding a canvas with an image Posted: May 26, 2003 3:57 PM
Reply to this message Reply
Yep, tried using the validate() method after adding the canvas to the frame but it didn't work.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Problem adding a canvas with an image Posted: May 27, 2003 12:09 PM
Reply to this message Reply
The following demonstrates a working Frame and Canvas application that displays an image file.
Substitute your own image file name, and frame size.

import java.awt.*;
import java.awt.event.*;
 
public class ImageViewer extends Frame{
    
    private String imageFileName = "T2.gif";
    
    public ImageViewer(){
        super("ImageViewer");
        init();
    }
    
    public static void main(String[] args){
        new ImageViewer();
    }
    
    public void init(){
        addWindowListener(new FrameListener());
        add(new ImageCanvas(imageFileName));
        setSize(200,200);
        show();
    }
    
    class FrameListener extends WindowAdapter{
        public void windowClosing(WindowEvent we){
            System.exit(0);
        }
    }
    
    class ImageCanvas extends Canvas{
        
        Image image;
        
        ImageCanvas(String filename){
            image = Toolkit.getDefaultToolkit().getImage(filename);
        }
        
        public void paint(Graphics g){
            g.drawImage(image, 0,0, this);
        }
        
    }
 
}

Leandro

Posts: 3
Nickname: leo2
Registered: May, 2003

Re: Problem adding a canvas with an image Posted: May 27, 2003 5:58 PM
Reply to this message Reply
Ok, thanks for the reply.
I'll give that a shot and see if it works.

Flat View: This topic has 4 replies on 1 page
Topic: Urgent: JTree distored on adding the nodes by thread Previous Topic   Next Topic Topic: yahoo wont let me play

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use