import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; public class Game extends JPanel { // Custom Objects Player player; // Three enemies Enemy e1; Enemy e2; Enemy e3; // Arrays about the cards BufferedImage[] cardImages; String[] cardImagePaths; public Game() { // Inits our shit init(); } public void init() { // Inits path to our pictures initPaths(); System.out.println("Ran initPaths - Game.java"); // Inits pictures initPictures(); System.out.println("Ran initPictures - Game.java"); } public void paint(Graphics g) { super.paintComponents(g); } public void initPictures() { // Will init all the .png files to the BufferedImage array - using the paths from cardImagePaths[] // As soon as one BufferedImage isn't initialized correctly the loops stops and there will be no other tries. for(int i = 0; i < cardImagePaths.length; i++) { try { cardImages[i] = ImageIO.read(new File(cardImagePaths[i])); } catch (IOException e) { System.out.println("Something went wrong when trying to init " + cardImagePaths[i]); break; } System.out.println("Succesfully init " + cardImagePaths[i] + " to cardImages[" + i + "]"); // Aight } } public void initPaths() { // Strings used for paths String spades = ".../Cards/Spades/"; String hearts = ".../Cards/Hearts/"; String diamonds = ".../Cards/Diamonds/"; String clubs = ".../Cards/Clubs/"; String png = ".png"; // Init of array cardImagePaths = new String[52]; // //// // //// // Init of indexes ( Starts with Spades, Hearts, Diamonds and then Clubs. From Ace till King // SPADES cardImagePaths[0] = spades + "Ace" + png; cardImagePaths[1] = spades + "Two" + png; cardImagePaths[2] = spades + "Three" + png; cardImagePaths[3] = spades + "Four" + png; cardImagePaths[4] = spades + "Five" + png; cardImagePaths[5] = spades + "Six" + png; cardImagePaths[6] = spades + "Seven" + png; cardImagePaths[7] = spades + "Eight" + png; cardImagePaths[8] = spades + "Nine" + png; cardImagePaths[9] = spades + "Ten" + png; cardImagePaths[10] = spades + "B" + png; cardImagePaths[11] = spades + "D" + png; cardImagePaths[12] = spades + "K" + png; // HEARTS cardImagePaths[13] = hearts + "Ace" + png; cardImagePaths[14] = hearts + "Two" + png; cardImagePaths[15] = hearts + "Three" + png; cardImagePaths[16] = hearts + "Four" + png; cardImagePaths[17] = hearts + "Five" + png; cardImagePaths[18] = hearts + "Six" + png; cardImagePaths[19] = hearts + "Seven" + png; cardImagePaths[20] = hearts + "Eight" + png; cardImagePaths[21] = hearts + "Nine" + png; cardImagePaths[22] = hearts + "Ten" + png; cardImagePaths[23] = hearts + "B" + png; cardImagePaths[24] = hearts + "D" + png; cardImagePaths[25] = hearts + "King" + png; // DIAMONDS cardImagePaths[26] = diamonds + "Ace" + png; cardImagePaths[27] = diamonds + "Two" + png; cardImagePaths[28] = diamonds + "Three" + png; cardImagePaths[29] = diamonds + "Four" + png; cardImagePaths[30] = diamonds + "Five" + png; cardImagePaths[31] = diamonds + "Six" + png; cardImagePaths[32] = diamonds + "Seven" + png; cardImagePaths[33] = diamonds + "Eight" + png; cardImagePaths[34] = diamonds + "Nine" + png; cardImagePaths[35] = diamonds + "Ten" + png; cardImagePaths[36] = diamonds + "B" + png; cardImagePaths[37] = diamonds + "D" + png; cardImagePaths[38] = diamonds + "King" + png; // CLUBS cardImagePaths[39] = clubs + "Ace" + png; cardImagePaths[40] = clubs + "Two" + png; cardImagePaths[41] = clubs + "Three" + png; cardImagePaths[42] = clubs + "Four" + png; cardImagePaths[43] = clubs + "Five" + png; cardImagePaths[44] = clubs + "Six" + png; cardImagePaths[45] = clubs + "Seven" + png; cardImagePaths[46] = clubs + "Eight" + png; cardImagePaths[47] = clubs + "Nine" + png; cardImagePaths[48] = clubs + "Ten" + png; cardImagePaths[49] = clubs + "B" + png; cardImagePaths[50] = clubs + "D" + png; cardImagePaths[51] = clubs + "King" + png; } }
Ran initPaths - Game.java Something went wrong when trying to init .../Cards/Spades/Ace.png Ran initPictures - Game.java
Der kan man bare se, man bliver da klogere og klogere hele tiden. Hvad så hvis man vil til "grandparent directory"?
C:\Documents and Settings\Robert Larsen\Desktop>type Test.java import java.io.File; public class Test { public static void main(String args[]) { File f = new File(args[0]); for (File file : f.listFiles()) { System.out.println(file); } } } C:\Documents and Settings\Robert Larsen\Desktop>java Test "/Documents and Settings" \Documents and Settings\All Users \Documents and Settings\Default User \Documents and Settings\LocalService \Documents and Settings\NetworkService \Documents and Settings\Robert Larsen C:\Documents and Settings\Robert Larsen\Desktop>
robert-crazy-laptop:~ $ cat Test.java import java.io.File; public class Test { public static void main(String[] args) { File f = new File(args[0]); for (File file : f.listFiles()) { System.out.println(file); } } } robert-crazy-laptop:~ $ java Test /home /home/.ecryptfs /home/robert robert-crazy-laptop:~ $
Måske du kan prøve og lægge dine billeder ind i din java applikations resource classpath. Dette gøres vist nok ved at lave en mappe struktur der hedder src/main/resources (Dine java filer er vist i strukturen der hedder src/main/java), hvorefter at du skulle kunne tilgå filen ved at skrive "classpath:minfil.filendelse".Jeg har dog aldrig selv helt leget med dette andet end i java web applikationer, men princippet er vist det samme i almindelige applikationer også.
Er det en java applet eller en java applikation der skal kører på skrivebordet, som du forsøger at lave ?
Nu er der heller ikke noget som hedder '.../'. Det er enten '../' (parent directory) eller './' (same directory).
Java er ligeglad med hvilken separator man bruger.