Results 1 to 5 of 5
Like Tree3Likes
  • 1 Post By Norm
  • 2 Post By DarrylBurke

Thread: Why doesn't this image show in the frame?

  1. #1
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Why doesn't this image show in the frame?

    This simple program attempts to display an 8x8 grid of 64 pixel squares. Every other square is supposed to be 64x64 image. So its like a chess board, but all of one colour is a picture, the other colour (white) is the background colour. You'll see commented out sections from the previous version of this program that simply showed a two-colour checkerboard pattern. This time I'm trying to replace one colour with an image.

    But the image doesn't show, I get an empty window. Maybe I used the drawImage() method incorrectly?

    Anyway here it is:

    Java Code:
    import java.awt.AWTEvent;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.WindowEvent;
    
    
    public class PictureCheckerboard extends Frame {
    	
    	private static String filename = "C:\butterfly.png";
    	
    	public static void main(String[] args) {
    		new PictureCheckerboard();
    	}
    	
    	public static String getFilename() {
    		return filename;
    	}
    
    	public void setFilename(String filename) {
    		this.filename = filename;
    	}
    
    	PictureCheckerboard() {
    		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    		Checker checker = new Checker();
    		add(checker);
    		pack();
    		show();
    	}
    	
    	public void processWindowEvent(WindowEvent event) {
    		if (event.getID() == WindowEvent.WINDOW_CLOSING) {
    			System.exit(0);
    		}
    	}
    }
    
    class Checker extends Canvas {
    	
    	int height = 512;
    	int width = 512;
    //	int rHeight = height/4;
    //	int rWidth = width/4;
    	
    	Checker() {
    		setSize(width, height);
    		setBackground(Color.white);
    	}
    	
    	public void paint(Graphics g) {
    		
    		Image image;
    		ImageLoader il = new ImageLoader();
    		image = il.loadImageFromFile(PictureCheckerboard.getFilename());
    		
    		for (int i = 0; i < 8; i++) {
    			for (int j = 0; j < 8; j++) {
    				if(((i+j) % 2) == 0) {
    					int x = i*64;
    					int y = j*64;
    					g.drawImage(image, x, y, x+64, y+64, 0, 0, 0, 0, this);
    				}
    			}
    			
    		}
    		
    //		// Blue squares.
    //		g.setColor(Color.blue);
    //		g.fillRect(0, 0, rWidth, rHeight);
    //		g.fillRect(400, 0, rWidth, rHeight);
    //		g.fillRect(200, 200, rWidth, rHeight);
    //		g.fillRect(600, 200, rWidth, rHeight);
    //		g.fillRect(400, 400, rWidth, rHeight);
    //		g.fillRect(0, 400, rWidth, rHeight);
    //		g.fillRect(200, 600, rWidth, rHeight);
    //		g.fillRect(600, 600, rWidth, rHeight);
    		
    		// Red squares.
    //		g.setColor(Color.red);
    //		g.fillRect(200, 0, rWidth, rHeight);
    //		g.fillRect(600, 0, rWidth, rHeight);
    //		g.fillRect(0, 200, rWidth, rHeight);
    //		g.fillRect(400, 200, rWidth, rHeight);
    //		g.fillRect(200, 400, rWidth, rHeight);
    //		g.fillRect(600, 400, rWidth, rHeight);
    //		g.fillRect(0, 600, rWidth, rHeight);
    //		g.fillRect(400, 600, rWidth, rHeight);
    	}
    }
    Java Code:
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.awt.Toolkit;
    import java.net.URL;
    
    public class ImageLoader extends Component {
    
    	public Image loadImageFromFile(String filename) {
    		Image image = null;
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		image = tk.getImage(filename);
    		waitForImage(image);
    		return(image);
    	}
    
    	private void waitForImage(Image image) {
    		MediaTracker mt = new MediaTracker(this);
    		mt.addImage(image, 1);
    		try {
    			mt.waitForAll();
    		} catch(Exception e) {
    			e.printStackTrace();
    			System.exit(0);
    		}
    	}
    	
    	public Image loadImageFromInternet(String urlString) {
    		Image image = null;
    		URL url = null;
    		try {
    			url = new URL(urlString);
    		} catch(Exception e) {
    			e.printStackTrace();
    			System.exit(0);
    		}
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		image = tk.getImage(url);
    		waitForImage(image);
    		return(image);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Why doesn't this image show in the frame?

    You might have better luck with the code if you were to use more modern classes from swing instead of AWT.
    For example the ImageIO class makes reading images much easier. Trying to fix this old code would be a waste of time.

    I suggest you start over with swing classes.
    DarrylBurke likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Why doesn't this image show in the frame?

    Also, AWT or Swing regardless, don't load resources or do any other I/O in a painting method override.

    db
    Norm and Fubarable like this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: Why doesn't this image show in the frame?

    Understood, I didn't realise I was learning old stuff.

    Thanks

    PS, my Eclipse is set to use Java 1.6. I do have the latest version installed though. Could someone tell me how to set Eclipse to use 1.7?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Why doesn't this image show in the frame?

    That's a question to ask in the Eclipse section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Subreport doesn't show in the PDF
    By sandy1991tje in forum Advanced Java
    Replies: 0
    Last Post: 06-10-2011, 02:45 PM
  2. JList doesn't show the elements
    By mrbeast87 in forum AWT / Swing
    Replies: 2
    Last Post: 04-20-2011, 06:21 PM
  3. jTextPane doesn't show HTML
    By indra-uolles in forum AWT / Swing
    Replies: 5
    Last Post: 08-31-2010, 02:29 PM
  4. Animation doesn't show when used as action?
    By ryuzog in forum New To Java
    Replies: 1
    Last Post: 01-04-2010, 01:38 AM
  5. Frame doesn't appear
    By SarahB in forum AWT / Swing
    Replies: 3
    Last Post: 12-27-2009, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •