Results 1 to 7 of 7
  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Readinng resources from JAR

    Hi!

    I have my resources located inside my jar. How would i load one? getResource is what google says but i cant understand a word...

    something like
    Java Code:
    imageLabel.setIcon(new ImageIcon(getClass().getResourceAsStream("Resources\\Lost.png")));

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Readinng resources from JAR

    So Google says getResource() and you're calling getResourceAsStream().

    You might want to use:

    Java Code:
    imageLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("Resources/Lost.png")));
    And are you sure the resources folder/package is actually named 'Resources' with a capital?
    Last edited by gimbal2; 08-28-2013 at 06:44 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Readinng resources from JAR

    Cannot make a static reference to the non-static method getClass() from the type Object...

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Readinng resources from JAR

    That's another problem. Post more code you have.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Readinng resources from JAR

    Oh....

    Java Code:
    package xyexs.snake.src;
    
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    import java.util.ArrayList;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    
    public class Main {
    	// Variables
    	public static final int EASY_GROW_TICK = 48;
    	public static final int EASY_MOVE_TICK = 4;
    	public static final int MEDIUM_GROW_TICK = 32;
    	public static final int MEDIUM_MOVE_TICK = 3;
    	public static final int HARD_GROW_TICK = 28;
    	public static final int HARD_MOVE_TICK = 2;
    	public static final int EXTREME_GROW_TICK = 24;
    	public static final int EXTREME_MOVE_TICK = 1;
    	public static int growTick = MEDIUM_GROW_TICK;
    	public static int moveTick = MEDIUM_MOVE_TICK;
    	static int direction = 0;
    	static Panel panel = new Panel();
    	static Score scoreboard = new Score();
    	static int score = 0;
    	public static ArrayList<Point> snake = new ArrayList<Point>();
    	static JFrame window = new JFrame(Reference.PROGRAM_NAME + " " + Reference.VERSION);
    	static Boolean doGrow = false;
    	static Boolean retry = false;
    
    	// Main
    	public static void main(String[] args) {
    		
    		while (true) {
    			JLabel imageLabel = new JLabel();
    			imageLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("resources/Lost.png")));
    			imageLabel.setVisible(true);
    			score = 0;
    			direction = 0;
    			snake.removeAll(snake);
    			snake.add(new Point(500, 250));
    			snake.add(new Point(500, 300));
    			snake.add(new Point(500, 350));
    			window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			window.setResizable(false);
    			window.add(panel, BorderLayout.CENTER);
    			window.add(scoreboard, BorderLayout.SOUTH);
    			window.setSize(Reference.WINDOW_X, Reference.WINDOW_Y);
    			window.pack();
    			window.setVisible(true);
    			int growCounter = 0;
    			int moveCounter = 0;
    			final JDialog select = new JDialog(window, "Choose a difficulty", JDialog.DEFAULT_MODALITY_TYPE);
    			select.setLayout(new FlowLayout(FlowLayout.LEADING));
    			select.setSize(320, 70);
    			select.setResizable(false);
    			JButton easyButton = new JButton("Easy");
    			JButton mediumButton = new JButton("Medium");
    			JButton hardButton = new JButton("Hard");
    			JButton extremeButton = new JButton("Extreme");
    
    			easyButton.addActionListener(new ActionListener() {
    
    				public void actionPerformed(ActionEvent e) {
    					growTick = EASY_GROW_TICK;
    					moveTick = EASY_MOVE_TICK;
    					select.dispose();
    				}
    			});
    
    			mediumButton.addActionListener(new ActionListener() {
    
    				public void actionPerformed(ActionEvent e) {
    					growTick = MEDIUM_GROW_TICK;
    					moveTick = MEDIUM_MOVE_TICK;
    					select.dispose();
    				}
    			});
    
    			hardButton.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					growTick = HARD_GROW_TICK;
    					moveTick = HARD_MOVE_TICK;
    					select.dispose();
    				}
    			});
    			
    			extremeButton.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					growTick = EXTREME_GROW_TICK;
    					moveTick = EXTREME_MOVE_TICK;
    					select.dispose();
    				}
    			});
    		
    			select.add(easyButton);
    			select.add(mediumButton);
    			select.add(hardButton);
    			select.add(extremeButton);
    			select.setVisible(true);
    			
    			while (true) {
    				try {
    					Thread.sleep(Reference.TICK_TIME);
    				}
    				catch (InterruptedException e) {
    					System.err.println("Sleep interrupted ( " + e + " )");
    				}
    				++growCounter;
    				++moveCounter;
    				if (growCounter == growTick) {
    					growCounter = 0;
    					doGrow = true;
    				}
    				if (moveCounter == moveTick) {
    					Point coord = snake.get(0);
    					Boolean isByTop = (coord.y == 0);
    					Boolean isByBot = (coord.y + Reference.PIXEL_SIZE == Reference.WINDOW_Y);
    					Boolean isByLeft = (coord.x == 0);
    					Boolean isByRight = (coord.x + Reference.PIXEL_SIZE == Reference.WINDOW_X);
    					Boolean facingSnake = false;
    					if (snake.lastIndexOf(snake.get(0)) > 0) {
    						facingSnake = true;
    					}
    					if (facingSnake == true) {
    						break;
    					}
    					if (isByTop == true && direction == 0) {
    						break;
    					}
    					else if (isByBot == true && direction == 2) {
    						break;
    					}
    					else if (isByRight == true && direction == 1) {
    						break;
    					}
    					else if (isByLeft == true && direction == 3) {
    						break;
    					}
    					else {
    						if (direction == 0) {
    							snake.add(0, new Point(coord.x, coord.y - Reference.PIXEL_SIZE));
    						}
    						else if (direction == 1) {
    							snake.add(0, new Point(coord.x + Reference.PIXEL_SIZE, coord.y));
    						}
    						else if (direction == 2) {
    							snake.add(0, new Point(coord.x, coord.y + Reference.PIXEL_SIZE));
    						}
    						else if (direction == 3) {
    							snake.add(0, new Point(coord.x - Reference.PIXEL_SIZE, coord.y));
    						}
    						if (doGrow == false) {
    							snake.remove(snake.size() - 1);
    						}
    						else {
    							doGrow = false;
    						}
    						panel.repaint();
    					}
    					moveCounter = 0;
    				}
    				score = score + Reference.SCORE_INCREMENT;
    				scoreboard.setText("Made by Xyexs        Score: " + score + "   Length: " + snake.size());
    			}
    			
    			window.remove(panel);
    			window.add(imageLabel,BorderLayout.CENTER);
    			window.pack();
    
    			final JDialog restart = new JDialog(window, "Retry?", JDialog.DEFAULT_MODALITY_TYPE);
    			JButton yes = new JButton("Sure!");
    			JButton no = new JButton("Nahh...");
    			restart.setLayout(new FlowLayout(FlowLayout.LEADING));
    			restart.setSize(165, 65);
    			restart.setResizable(false);
    			
    			retry = false;
    			yes.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					retry = true;
    					restart.dispose();
    				}
    			});
    			no.addActionListener(new ActionListener() {
    				public void actionPerformed(ActionEvent e) {
    					restart.dispose();
    					retry = false;
    				}
    			});
    			restart.add(yes);
    			restart.add(no);
    			restart.setVisible(true);
    			if (retry == false) {
    				window.dispose();
    				break;
    			}
    		}
    	}
    
    	// Public Functions
    	public static int getDir() {
    		return direction;
    	}
    
    	public static void setDir(int dir) {
    		direction = dir;
    	}
    }

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Readinng resources from JAR

    There's a pretty good explanation in the tutorial: Loading Images Using getResource

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

  7. #7
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Readinng resources from JAR

    imageLabel.setIcon(new ImageIcon(Main.class.getResource("/resources/Lost.png")))

    That worked... ish... but i had to change my sturcture in a way that i dont like. I wanna learn this for the future.
    my structure (the way i want it):
    Java Code:
    Snake ( project)
        src (source folder)
            xyexs.snake.src (package)
                 class names
        resources (normal folder)
            lost.png
    What i had to do:
    Java Code:
    Snake ( project)
        src (source folder)
            xyexs.snake.src (package)
                 class names
            resources (package)
                lost.png

Similar Threads

  1. problems with resources
    By homka in forum Advanced Java
    Replies: 2
    Last Post: 10-12-2011, 03:39 PM
  2. SCWCD Resources
    By coyne20 in forum Java Certification
    Replies: 4
    Last Post: 06-08-2010, 07:02 AM
  3. Struts Resources
    By chizbox in forum Web Frameworks
    Replies: 0
    Last Post: 03-03-2010, 10:46 AM
  4. Resources
    By zzpprk in forum Advanced Java
    Replies: 6
    Last Post: 08-20-2009, 04:11 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
  •