Results 1 to 12 of 12
  1. #1
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Getting iconimages to show up

    I'm working on a program that takes in an image and then draws on top of the image based off of a data file. Unfortunately the initial image is not showing up, all I get is a blank gray rectangle. Any thoughts as to what I'm missing? Relevant code parts below:

    Java Code:
    	static Dimension size = new Dimension(1000, 700); //size of the maps 
    	
    	
    	public static void main(String args[]) throws FileNotFoundException{
    		
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setTitle("Time Map Interface");
    		frame.setVisible(true);
    		frame.setLayout(new BorderLayout());
    		frame.setMinimumSize(size);
    		
    		
    		
    		JFileChooser chooser = new JFileChooser();
    	        FileNameExtensionFilter filter = new FileNameExtensionFilter(
    	        "logfile", "txt");
    	        chooser.setFileFilter(filter);
    	        int returnVal = chooser.showOpenDialog(frame);
    	        if(returnVal == JFileChooser.APPROVE_OPTION) {
    	    	    parse(new File(chooser.getSelectedFile().getName()));
    	    	    drawTimeMap(frame);
    	        }
    
    ...
    
    	public static void drawTimeMap(JFrame frame){
    		
    		BufferedImage mapPic = null;
    		try {
    			mapPic = ImageIO.read(new File("basemap.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	        JLabel mapPanel = new JLabel(new ImageIcon(mapPic));
    	        frame.add(mapPanel, BorderLayout.CENTER);
    	        mapPanel.setMinimumSize(size);
    	        mapPanel.setVisible(true);
    Last edited by 011121; 04-30-2011 at 10:24 PM. Reason: code tags added

  2. #2
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    You could try drawing the BufferedImage to the JPanel. For example something like this should work:
    Java Code:
    BufferedImage pic;
    // load pic
    JPanel panel = new JPanel();
    Graphics2D g = (Graphics2D)panel.getGraphics();
    g.drawImage(0,0,pic);
    g.dispose();

  3. #3
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Hrrm, also does not seem to be working. I changed the drawMap method to the following:

    Java Code:
    	public static void drawTimeMap(JFrame frame){
    		
    		BufferedImage mapPic = null;
    		try {
    			mapPic = ImageIO.read(new File("basemap.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    //	    JLabel mapPanel = new JLabel(new ImageIcon(mapPic));
    	  	JPanel mapPanel = new JPanel();
    	  	mapPanel.setMinimumSize(size);
    	  	frame.add(mapPanel, BorderLayout.CENTER);
    		Graphics2D g2 = (Graphics2D)mapPanel.getGraphics();
    		g2.drawImage(mapPic, null, 0,0);
    		g2.dispose();
    		mapPanel.setVisible(true);
    eclipse complained about drawImage using (int, int, bufferedImage) for arguments. The closest I could find was (BufferedImage, BufferedImageOp, int, int). Same behavior as before just left with a blank gray window. I just triple checked that "basemap.png" is not blank gray :)
    Any other ideas what I should try?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Zman3359 View Post
    You could try drawing the BufferedImage to the JPanel. For example something like this should work:
    Java Code:
    BufferedImage pic;
    // load pic
    JPanel panel = new JPanel();
    Graphics2D g = (Graphics2D)panel.getGraphics();
    g.drawImage(0,0,pic);
    g.dispose();
    Original poster: Please disregard zman's advice as this is not the way to do graphics in Java. The Graphics image obtained will be short lived and you will find yourself running up against NullPointerExceptions at unexpected times. Better to place an ImageIcon in a JLabel or draw in a JPanel's paintComponent method, but don't get a Graphics object from a Swing component via getGraphics and expect it to work.

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

    Default

    Zman3359 and 011121, please see this recent thread
    Passing a variable problem.

    db

  6. #6
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Original poster: Please disregard zman's advice as this is not the way to do graphics in Java. The Graphics image obtained will be short lived and you will find yourself running up against NullPointerExceptions at unexpected times. Better to place an ImageIcon in a JLabel or draw in a JPanel's paintComponent method, but don't get a Graphics object from a Swing component via getGraphics and expect it to work.
    Oops my bad, I had no idea that there was problems with this. I usually draw on a Canvas by getting a graphics object from the Canvas's buffer strategy. Is there anything wrong with that? I read the article that 011121 linked to and it suggests extending a JPanel and drawing there. Should I do this instead? Or maybe extend a Canvas?

  7. #7
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Zman3359 and 011121, please see this recent thread
    Passing a variable problem.

    db
    It would be helpful to explain why using getGraphics() is a bad thing to do instead of simply stating it. Otherwise I really have no way of A) learning and B) evaluating the different opinions I hear (and yes I do hear other people say getGraphics is absolutely fine to do).

  8. #8
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Quote Originally Posted by 011121 View Post
    It would be helpful to explain why using getGraphics() is a bad thing to do instead of simply stating it. Otherwise I really have no way of A) learning and B) evaluating the different opinions I hear (and yes I do hear other people say getGraphics is absolutely fine to do).
    Fubarable explained it a little here:

    Quote Originally Posted by Fubarable
    The Graphics image obtained will be short lived and you will find yourself running up against NullPointerExceptions at unexpected times.

  9. #9
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Any other suggestions on the original issue?

  10. #10
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    aha!
    Apparently the issue was the lack of a frame.pack() command. I hadn't thouht it was necessary because I thought it only dealt with sizing the frame correctly and I had already set a size I wanted it to use, but I added that in and now it works.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    I guess you find it fun to drive on the wrong side of the road too.

    db

  12. #12
    011121 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    See, that's just not helpful in understanding your point, which only makes it more likely people will ignore your point. You are essentially saying "do it this way because I said so." But I don't know you, I have no idea if you know what you are talking about and so an appeal to authority is not very compelling.

Similar Threads

  1. How to Show Off?
    By maknib in forum New To Java
    Replies: 6
    Last Post: 11-05-2010, 10:26 AM
  2. my JButtons won't show up
    By gib65 in forum AWT / Swing
    Replies: 4
    Last Post: 09-25-2010, 04:09 AM
  3. Replies: 3
    Last Post: 05-27-2010, 10:54 AM
  4. the component does not show
    By shruti in forum AWT / Swing
    Replies: 8
    Last Post: 07-08-2009, 12:21 PM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 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
  •