Results 1 to 20 of 27
- 03-07-2011, 04:10 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
Extracting program, custom graphics goes away..
Hi
Every time when i'm making a java program with custom graphics, maybe a space ship for a certain game or whatever. But when i extract the app/program as a Runnable Jar File the graphics just dissapear, they don't show up when you play the game/app/program. How to fix this? I tried to putting the images in the src file of the project..
- 03-07-2011, 05:07 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Do you get any exceptions thrown?
If not, you're not eating exceptions are you? That is, catching them and not doing anything at all.
How are you loading the images? Since they're int he jar file they need to be loaded using getResourceAsStream().
- 03-07-2011, 05:19 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
I'm simple using
:/PHP Code:ImageIcon CUBE; CUBE = new ImageIcon("src/Cube2.png");
- 03-07-2011, 05:29 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Which won't work, and will throw an exception, which you are probably hiding.
Well, unless the graphic happens to be in a 'src' directory below where your jar file happens to be located.
You need to use the method I mention above to get the InputStream which you can then pass to ImageIO.read() which will give you a BufferedImage that you can then give to the ImageIcon constructor. Or:
Note, this is from memory so some of it may be slightly wrong. Always check the API.Java Code:CUBE = new ImageIcon(ImageIO.read(this.class.getResourceAsStream("<path to your image in the jar>")));
- 03-07-2011, 07:09 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
I don't get this to work, i've got problems on how to do the code, maybe i'm just understanding your description wrong or i simply don't get it. Anyway, could you describe abit more detailed on how to do this? I've already been googling around so don't recommend google :/
- 03-08-2011, 08:18 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Show us your code and tell us what errors you're getting.
- 03-08-2011, 02:36 PM #7
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
Okey here's my code, the part of the code that's involved in the problem, graphics and jframe.
PHP Code:public class Cube extends JPanel implements ActionListener,KeyListener{ public Cube(){ setLayout(null); fails = new JLabel(""); fails.setBounds(10, 10, 300, 40); fails.setForeground(Color.WHITE); fails.setFont(new Font("Habbo", Font.PLAIN, 21)); add(fails); begin = new Timer(5, this); begin.start(); ////////////////////////////////////////////////////////////// if(startscreen){ LevelSelect = new JButton("Level Select"); LevelSelect.addActionListener(this); LevelSelect.setBounds(320, 240, 300 ,60); add(LevelSelect); Howto = new JButton("Shapes & Controls"); Howto.addActionListener(this); Howto.setBounds(320, 320, 300 ,60); add(Howto); } level1 = new JButton("Level 1"); level1.addActionListener(this); level1.setBounds(100, 320, 200 ,50); level2 = new JButton("Level 2"); level2.addActionListener(this); level2.setBounds(320, 320, 200 ,50); if(!lvl1done){ level2.setEnabled(false); } else if(lvl1done){ level2.setEnabled(true); } level3 = new JButton("Level 3"); level3.addActionListener(this); level3.setBounds(540, 320, 200 ,50); if(!lvl1done){ level3.setEnabled(false); } else if(lvl1done){ level2.setEnabled(true); } setVisible(true); setSize(900, 500); setBackground(Color.BLACK); setLocation(500, 250); addKeyListener(this); setFocusable(true); } public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; /////////////////////////////////////////////////// if(startscreen){ LevelSelect.setVisible(true); Howto.setVisible(true); } //////////////////////////////////////////////////// if(!levelscreen){ remove(level1); remove(level2); remove(level3); }if(levelscreen){ Howto.setVisible(false); LevelSelect.setVisible(false); add(level1); add(level2); add(level3); //////////////////////////////////////////////////////// if(startscreen){ fails.setText(""); SSbg = new ImageIcon("src/TheCubeLogoevilcubes2.png"); SSbg.paintIcon(this, g, 150, -50);
The problem is simply that i grahpics isn't showing up, and i guess to fix that you use getResourceAsStream, but i don't know how to make the coding for that. I don't get any errors or so when running my code.PHP Code:public class Starter extends JFrame{ public static void main(String[]args){ new Starter(); } int x,y; public Starter(){ super("The Cube"); Cube cu = new Cube(); addKeyListener(cu); add(cu); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setLocation(500, 250); setResizable(false); setSize(900, 500); } public Starter(int startX, int startY){ x = startX; y = startY; }
- 03-08-2011, 03:20 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
And I said to change this:
so it gets the image from within the jar file.Java Code:SSbg = new ImageIcon("src/TheCubeLogoevilcubes2.png");
Which you haven't done.
- 03-08-2011, 03:22 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
I did do that, but when it came to the getResource and so, i didn't quite get the coding, so i made it back to it was before so i wouldn't mess up the code :/ But what to do after i've added "SSbg = new ImageIcon("src/TheCubeLogoevilcubes2.png");" how to do the getResourceAtStream code lines?
- 03-08-2011, 03:24 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
So, show us what you wrote with the bits in (or your interpretation of them) that I suggested.
There's no point saying something doesn't work and then not showing us the problem...
- 03-08-2011, 03:33 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
I tried this
Which underlines "class" as wrong, and i didn't know if you ment the name of my class, but i tried that aswell and it didn't work, it says "Identifier expected", and i'm abit confused with all the terms such as variable and so on, so i don't know what an identfier is..for the moment..PHP Code:SSbg = new ImageIcon(ImageIO.read(this.class.getResourceAsStream("src/TheCubeLogoEvilcubes2.png")));
Then i did this:
and then i didn't know what to do nextPHP Code:InputStream is = getClass( ).getResourceAsStream("src/TheCubeLogoevilcubes2.png);Last edited by AndroidAppNewbie; 03-08-2011 at 03:35 PM.
- 03-08-2011, 03:48 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
To quote myself:
So I'm not surprised I got something wrong, but you still managed to get an InputStream, which is what I was doing. Which is a Good Thing.Note, this is from memory so some of it may be slightly wrong. Always check the API.
That call to getClass() is the thing I intended with "this.class"...so you should be able to figure out what to do next.
- 03-08-2011, 04:03 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
Right.. :| i did this...so you should be able to figure out what to do next.
And then it was a big error, which went away if i made a try/catch around it, but when i made that try/catch the whole program won't even start(Note, i'm wrting the stuff in the paintComponent, method), so.. am i simply bad or am i doing some obvious misstake?PHP Code:SSbg = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("src/TheCubeLogoEvilcubes2.png")));
- 03-08-2011, 04:05 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Well, I would store the image myself as an attribute (or static even?), rather than loading it everytime a paint is called.
The try/catch is presumably for a FileNotFound, or IOException. Make sure you print the stack trace in the catch.
- 03-08-2011, 04:12 PM #15
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
How do i store an image as an attribute then? Isn't there a simple way to make graphics stay, and by simple i mean max 5 lines of code.
- 03-08-2011, 04:16 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Then initialise it in the constructor...Java Code:ImageIcon startScreen;
- 03-08-2011, 04:20 PM #17
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
Okey, i've done all that now, but what to do with the crash causing try/catch containing : SSbg = new ImageIcon(ImageIO.read(getClass().getResourceAsStr eam("src/TheCubeLogoEvilcubes2.png")));
- 03-08-2011, 04:22 PM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Well, what does the exception stack trace say?
- 03-08-2011, 04:34 PM #19
Member
- Join Date
- Sep 2010
- Posts
- 47
- Rep Power
- 0
This is causing my console logg to spam tons of errors and makes the app unstartable.PHP Code:try { SSbg = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("src/TheCubeLogoEvilcubes2.png"))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- 03-08-2011, 04:41 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Custom exception exits program, while NumberFormatException only shows error
By DerekRaimann in forum New To JavaReplies: 6Last Post: 12-09-2010, 08:44 AM -
Custom graphics on dialogs, etc.
By trader5050 in forum New To JavaReplies: 1Last Post: 11-24-2010, 07:29 AM -
Graphics program
By ccie007 in forum New To JavaReplies: 6Last Post: 10-27-2010, 08:08 PM -
custom graphics, buttons & drop boxes
By jaytee in forum New To JavaReplies: 1Last Post: 03-14-2010, 12:27 AM -
program help: Extracting words from a string
By toad in forum New To JavaReplies: 1Last Post: 11-04-2007, 06:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks