1

I wanna upload my project, a card game to Github, and it has a picture from my hard drive as the game card table. Is there a way to compile/run and upload it without the code having the entire file path hard coded into it? As of now, its:

public static void main(String[] args){
    JFrame frame = new JFrame(); 
    JLabel panel = new JLabel(new ImageIcon("C:\\Users\\MyName\\Documents\\javaprojects\\Cardgame\\cardgameProject\\cardgameTableCanvas.jpg"));  
    frame.setSize(WIDTH,HEIGHT);
    panel.setSize(WIDTH,HEIGHT);
    frame.add(panel);  
    frame.setTitle("Test Canvas");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    CardgameTable sampleTable = new CardgameTable();
    frame.add(sampleTable);
    frame.setVisible(true);
}

I also don't want issues if this is pulled from Git by someone else, and I think that file path could do it.

Derry
  • 371
  • 1
  • 13
  • Where is the java code located in relation to the image? Are they in the same directory? – cosinepenguin Jun 29 '17 at 00:43
  • yes they are are the same directory, and in fact, the same folder – Derry Jun 29 '17 at 00:51
  • Does `JLabel panel = new JLabel(new ImageIcon("cardgameTableCanvas.jpg")); ` not work for you then? – cosinepenguin Jun 29 '17 at 00:52
  • OK it does work with just the name and the extension. thanks! – Derry Jun 29 '17 at 01:01
  • For all reasonable applications, the image should go into the resources folder so that it will eventually be packed into a JAR. You should hardly *ever* refer to a real image file from within your code, but always to resources. – Marco13 Jun 29 '17 at 01:48

1 Answers1

1

This really depends on where your java code is located! There are several tools that allow you to create a URL object of a local file path, and these can be fed into ImageIcon creation!

If the image is in the same directory (folder) as you java code, then the following should work to ensure the file is referenced even on other machines:

URL cardgameCanvas = new File("cardgameTableCanvas.jpg").toURI().toURL();
JLabel panel = new JLabel(new ImageIcon(cardgameCanvas));  

(For java 7+: Paths.get("cardgameTableCanvas.jpg").toUri().toURL())

This should allow you to reference the image by first creating a URL object that links to it, and passing that URL object to the new ImageIcon object!

Hope this helps!

Additional Source

Edit

You can also just do

JLabel panel = new JLabel(new ImageIcon("cardgameTableCanvas.jpg"));

;)

Edit 2

Your main would look something like this (if you wanted to go the "hard way"):

public static void main(String[] args){
    JFrame frame = new JFrame(); 
    URL cardgameCanvas = new File("cardgameTableCanvas.jpg").toURI().toURL();
    JLabel panel = new JLabel(new ImageIcon(cardgameCanvas));   
    frame.setSize(WIDTH,HEIGHT);
    panel.setSize(WIDTH,HEIGHT);
    frame.add(panel);  
    frame.setTitle("Test Canvas");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    CardgameTable sampleTable = new CardgameTable();
    frame.add(sampleTable);
    frame.setVisible(true);
}

But since it works for you I would definetaly recommend just referencing the local document itself like this:

public static void main(String[] args){
    JFrame frame = new JFrame(); 
    JLabel panel = new JLabel(new ImageIcon("cardgameTableCanvas.jpg")); 
    frame.setSize(WIDTH,HEIGHT);
    panel.setSize(WIDTH,HEIGHT);
    frame.add(panel);  
    frame.setTitle("Test Canvas");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    CardgameTable sampleTable = new CardgameTable();
    frame.add(sampleTable);
    frame.setVisible(true);
}
cosinepenguin
  • 1,545
  • 1
  • 12
  • 21
  • which library to I have to import for this? can you show me what the main of this class would look like? thanks! – Derry Jun 29 '17 at 01:09
  • I added both possible mains, but since it works for you, I would recommend just doing `JLabel panel = new JLabel(new ImageIcon("cardgameTableCanvas.jpg"));`! This is much cleaner than what I originally proposed! – cosinepenguin Jun 29 '17 at 01:14