Results 1 to 12 of 12
Thread: Getting iconimages to show up
- 04-30-2011, 07:05 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
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
- 05-01-2011, 02:38 AM #2
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();
- 05-01-2011, 02:57 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Hrrm, also does not seem to be working. I changed the drawMap method to the following:
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 :)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);
Any other ideas what I should try?
-
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.
- 05-01-2011, 03:37 AM #5
Zman3359 and 011121, please see this recent thread
Passing a variable problem.
db
- 05-01-2011, 03:47 AM #6
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?
- 05-01-2011, 04:01 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
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).
- 05-01-2011, 04:04 AM #8
- 05-02-2011, 02:30 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
Any other suggestions on the original issue?
- 05-02-2011, 02:57 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
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.
- 05-02-2011, 05:57 AM #11
I guess you find it fun to drive on the wrong side of the road too.
db
- 05-02-2011, 09:22 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 9
- Rep Power
- 0
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
-
How to Show Off?
By maknib in forum New To JavaReplies: 6Last Post: 11-05-2010, 10:26 AM -
my JButtons won't show up
By gib65 in forum AWT / SwingReplies: 4Last Post: 09-25-2010, 04:09 AM -
how to show web browser in mobile app or is it possible to show it in textArea
By Basit781 in forum CLDC and MIDPReplies: 3Last Post: 05-27-2010, 10:54 AM -
the component does not show
By shruti in forum AWT / SwingReplies: 8Last Post: 07-08-2009, 12:21 PM -
netbeans 6.0 not show commpunent or show blank page
By fahimaamir in forum NetBeansReplies: 1Last Post: 01-26-2008, 06:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks