Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Question 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..

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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().

  3. #3
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    I'm simple using

    PHP Code:
    ImageIcon CUBE;
    
    CUBE = new ImageIcon("src/Cube2.png");
    :/

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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:
    Java Code:
    CUBE = new ImageIcon(ImageIO.read(this.class.getResourceAsStream("<path to your image in the jar>")));
    Note, this is from memory so some of it may be slightly wrong. Always check the API.

  5. #5
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Question

    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 :/

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Show us your code and tell us what errors you're getting.

  7. #7
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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);

    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;
    	}
    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    And I said to change this:
    Java Code:
    SSbg = new ImageIcon("src/TheCubeLogoevilcubes2.png");
    so it gets the image from within the jar file.

    Which you haven't done.

  9. #9
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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...

  11. #11
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    I tried this
    PHP Code:
    SSbg = new ImageIcon(ImageIO.read(this.class.getResourceAsStream("src/TheCubeLogoEvilcubes2.png")));
    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..

    Then i did this:
    PHP Code:
    InputStream is = getClass( ).getResourceAsStream("src/TheCubeLogoevilcubes2.png);
    and then i didn't know what to do next
    Last edited by AndroidAppNewbie; 03-08-2011 at 03:35 PM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Quote Originally Posted by AndroidAppNewbie View Post
    I tried this
    PHP Code:
    SSbg = new ImageIcon(ImageIO.read(this.class.getResourceAsStream("src/TheCubeLogoEvilcubes2.png")));
    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..

    Then i did this:
    PHP Code:
    InputStream is = getClass( ).getResourceAsStream("src/TheCubeLogoevilcubes2.png);
    and then i didn't know what to do next
    To quote myself:
    Note, this is from memory so some of it may be slightly wrong. Always check the API.
    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.

    That call to getClass() is the thing I intended with "this.class"...so you should be able to figure out what to do next.

  13. #13
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    ...so you should be able to figure out what to do next.
    Right.. :| i did this
    PHP Code:
    SSbg = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("src/TheCubeLogoEvilcubes2.png")));
    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?

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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.

  15. #15
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Java Code:
    ImageIcon startScreen;
    Then initialise it in the constructor...

  17. #17
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    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")));

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Well, what does the exception stack trace say?

  19. #19
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    PHP Code:
    try {
    		SSbg = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("src/TheCubeLogoEvilcubes2.png")));
    		
    	} catch (IOException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    This is causing my console logg to spam tons of errors and makes the app unstartable.

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Which is because you are reading this in on every paint call.
    So move that out of the paint call (as suggested) and do it in the constructor.
    Only allow the constructor to complete if it finds this image.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 6
    Last Post: 12-09-2010, 08:44 AM
  2. Custom graphics on dialogs, etc.
    By trader5050 in forum New To Java
    Replies: 1
    Last Post: 11-24-2010, 07:29 AM
  3. Graphics program
    By ccie007 in forum New To Java
    Replies: 6
    Last Post: 10-27-2010, 08:08 PM
  4. custom graphics, buttons & drop boxes
    By jaytee in forum New To Java
    Replies: 1
    Last Post: 03-14-2010, 12:27 AM
  5. program help: Extracting words from a string
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 06:39 PM

Posting Permissions

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