Now my trouble is only happening when my code is being run from a .jar
Look under getURL(filename)
- Code: Select All Code
import java.awt.Color;
import java.awt.GridLayout;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.io.Console;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* @category class
* @
* @author Jordan C.D
* @author Blue Light
* {@link http://www.comweb.nl/java/Console/Console.html}
*/
@SuppressWarnings({ "serial", "unused" })
public class mapPanel extends JPanel{
GridLayout layout = new GridLayout(3,0);
static private char[][] map;
static private JLabel[][] tileMap;
URL[] gameURL = {
getURL("DefualtTileBackup.gif"),
getURL("Wall.gif"),
getURL("Floor.gif"),
getURL("Character.gif"),
getURL("Monster.gif")
};
/**
* @param args
*/
public static void main(String[] args) {
//System.console();
char[][] charTest = {
{'@','F','M','S','$'},
{'M','@','F','S','$'},
{'$','@','F','M','S'}
};
JFrame frame = new JFrame();
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mapPanel panel = new mapPanel(charTest);
panel.setLayout(new GridLayout(3,5));
frame.add(panel);
frame.pack();
frame.setVisible(true);
//String Ur = new String(""+ClassLoader.getSystemResource("mapPanel.class"));
//System.out.println(Ur);
//Ur.replace("test%202.jar!/mapPanel.class", "...test"); //if jar use this
//System.out.println(Ur);
//Ur.replace("Jar:", "");
//System.out.println(Ur);
}
mapPanel(char[][] map){
super();
setMap(map);
displayMap();
for(int Y = 0; Y < mapPanel.map.length; Y = Y+1 ){
for(int X = 0; X < mapPanel.map[Y].length; X = X+1 ){
this.add(mapPanel.tileMap[Y][X]);
}
}
}
private static URL getURL(String filename) {
String Ur = new String(//"jar:file:/C:/Java/Eclipse/FallingStar/Jar/Tile%20test/Tile%20test%202.jar!/mapPanel.class");
""+ClassLoader.getSystemResource("mapPanel.class"));
System.out.println(Ur);
URL url = null;
try {
System.out.println("URL was " + url);
// This if statement check to see if Jar: is in front and if true it will assume this is a Jar and remove Jar elements to the URL
//if(!"jar".equals(Ur.substring(0,3))){
System.out.println("JAR is running");
Ur.replace("/mapPanel.class", " BLA!"); //if jar use this
//Ur.replace("jar:", "");
System.out.println(Ur);
url = new URL(Ur);
Ur.replace(//"test%202.jar!
"/mapPanel.class", filename);
System.out.println(Ur + " I hope this worked");
//}
/*else{
//
System.out.println("CLASS is running");
System.out.println(Ur);
url = new URL(Ur.replace("bin/mapPanel.class", filename)); //if class use this
} */
}
catch (Exception e) { }
return url;
}
private void displayMap() {
for(int Y=0; Y<map.length;Y = Y+1){
for(int X=0; X<map[Y].length; X = X+1){
JLabel tempLabel = new JLabel();
try {
switch(map[Y][X]){
case('$'):
System.out.println("Wall, $");
tempLabel= new JLabel(new ImageIcon(gameURL[1]));
break;
case('F'):
System.out.println("Floor, #");
tempLabel = new JLabel(new ImageIcon(gameURL[2]));
break;
case('@'):
System.out.println("Character, @");
tempLabel = new JLabel(new ImageIcon(gameURL[3]));
break;
case('M'):
System.out.println("Monster, !");
tempLabel = new JLabel(new ImageIcon(gameURL[4]));
break;
default:
System.out.println("defualt,?");
tempLabel = new JLabel(new ImageIcon(gameURL[0]));
break;// end default
}// end switch
mapPanel.tileMap[Y][X] = tempLabel;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
void setMap(char[][] map){
mapPanel.map = map;
mapPanel.tileMap = new JLabel[mapPanel.map.length][mapPanel.map[0].length];
//displayMap();
}
char[][] getMap(){
return mapPanel.map;
}
}
So I've been trying to get these two strings to work.
"jar:file:/C:/Java/Eclipse/FallingStar/Jar/Tile%20test/Tile%20test%202.jar!/mapPanel.class"
and what you get when you call this.
""+ClassLoader.getSystemResource("mapPanel.class"));
In theory for me they are the same thing but I can't get either of them to edit.
Here is my system.out.println() information.
- Code: Select All Code
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
URL was null
JAR is running
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class
jar:file:/C:/Java/Eclipse/FallingStar/Jar/test.jar!/mapPanel.class I hope this worked
Character, @
Floor, #
Monster, !
defualt,?
Wall, $
Monster, !
Character, @
Floor, #
defualt,?
Wall, $
Wall, $
Character, @
Floor, #
Monster, !
defualt,?
at "I hope this worked" we should have at least removed the "/mapPanel.class"
Sorry but i have to goto class now. Post and edit any typos later.