Slick cannot load the image? - java

So I'm following thenewboston's tutorials on Slick 2D. I'm loading the Image exactly as he said:
private org.newdawn.slick.Image playNow;
...
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
...
playNow = new org.newdawn.slick.Image("res/playNow.png");
This is my Eclipse project tree:
This is the output it gives me:
Tue Jul 03 09:31:53 PDT 2012 INFO:Slick Build #274
Tue Jul 03 09:31:53 PDT 2012 INFO:LWJGL Version: 2.8.5
Tue Jul 03 09:31:53 PDT 2012 INFO:OriginalDisplayMode: 1366 x 768 x 32 #60Hz
Tue Jul 03 09:31:53 PDT 2012 INFO:TargetDisplayMode: 640 x 360 x 0 #0Hz
Tue Jul 03 09:31:53 PDT 2012 INFO:Starting display 640x360
Tue Jul 03 09:31:53 PDT 2012 INFO:Use Java PNG Loader = true
Tue Jul 03 09:31:53 PDT 2012 INFO:Controllers not available
Exception in thread "main" java.lang.RuntimeException: Resource not found: res/playNow.png
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
at org.newdawn.slick.Image.<init>(Image.java:196)
at org.newdawn.slick.Image.<init>(Image.java:170)
at org.newdawn.slick.Image.<init>(Image.java:158)
at org.newdawn.slick.Image.<init>(Image.java:136)
at net.sourceforge.whowantsakookie.hamblaster.Menu.init(Menu.java:21)
at net.sourceforge.whowantsakookie.hamblaster.Game.initStatesList(Game.java:20)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at net.sourceforge.whowantsakookie.hamblaster.Game.main(Game.java:31)
The tutorial is located here
I am doing this exactly as the tutorial said, and it works for him in the video. Thanks in advance :)

No your problem is that the "res" folder is in the same folder as the "src" file is in!
move your "res" folder containing all of your pictures INTO the "src/net.sourceforge.whowant/" folder/package in your program.
In your program you will load it by using
playNow = new org.newdawn.slick.Image("res/playNow.png");
Hope this helps! :)

Related

Spring Crontab pattern: every weekday at specific times

Here is the documentation for the Spring Crontab syntax.
I want to run a task every weekday at 14:40, 14:45, 14:50, 14:55, and 15:00, but I can't figure out how to express this with a Crontab pattern. The closest I've come up with so far is:
0 40/5 14 * * MON-FRI
But this doesn't run at 15:00.
Is this possible to express with a Crontab pattern at all?
You need to split it into two expressions :
0 40-55/5 14 * * MON-FRI
0 0 15 * * MON-FRI
If you only want to have one expression and you are okay with the last trigger time in a day run one minute before, you can use :
0 40-55/5,59 14 * * MON-FRI
Spring internally use CronSequenceGenerator to generate the next trigger time for a cron expression. You can use it to verify a cron expression.
For example , to verify the next several trigger times of 0 40-55/5,59 14 * * MON-FRI:
CronSequenceGenerator generator = new CronSequenceGenerator("0 40-55/5,59 14 * * MON-FRI");
Date d = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
for (int i = 0; i < 10; i++) {
d = generator.next(d);
System.out.println(d);
}
which will print out :
Tue Jun 02 14:40:00 HKT 2020
Tue Jun 02 14:45:00 HKT 2020
Tue Jun 02 14:50:00 HKT 2020
Tue Jun 02 14:55:00 HKT 2020
Tue Jun 02 14:59:00 HKT 2020
Wed Jun 03 14:40:00 HKT 2020
Wed Jun 03 14:45:00 HKT 2020
Wed Jun 03 14:50:00 HKT 2020
Wed Jun 03 14:55:00 HKT 2020
Wed Jun 03 14:59:00 HKT 2020

Lunar code magnet,Head first java, pg-314

I am new to java can anyone explain to me how this code works in this example
specially the LOOP portion
import java.util.*;
import static java.lang.System.out;
class FullMoons {
static int DAY_IM = 1000 * 60 * 60 * 24;
public static void main(String [] args) {
Calendar c = Calendar.getInstance();
c.set(2004,0,7,15,40);
long day1 = c.getTimeInMillis();
for (int x = 0; x < 60; x++) {
day1 += (DAY_IM * 29.52);
c.setTimeInMillis(day1);
out.println(String.format("full moon on %tc", c));
}
}
}
output is
full moon on Fri Feb 06 04:06:35 MST 2004
full moon on Sat Mar 06 16:38:23 MST 2004
full moon on Mon Apr 05 06:07:11 MDT 2004
an answer should be displayed in first three loops why it takes entire 60 runnings
What is the answer and why it should be displayed in first 3 loops?
This code creates Calendar and then added to the first full moon date the duration of lunar month (about 29.52 days).
The cycle body runs 60 times, because condition x < 60 and x++ increase x by 1.
//increase number of milliseconds
day1 += (DAY_IM * 29.52);
//set increased number to Calendar c
c.setTimeInMillis(day1);
//create formatted string
String s = String.format("full moon on %tc", c);
//output the string
out.println(s);
read more about how String.format() works in official documentation
This code will run 60 times throughout the Loop..
I realized my mistake which is iam only going by theory rather than doing practical...so guys this is lesson, and iam posting the result so that this question dosent create any unnecessary confusion further.
full moon on Fri Feb 06 04:08:48 IST 2004
full moon on Sat Mar 06 16:37:36 IST 2004
full moon on Mon Apr 05 05:06:24 IST 2004
full moon on Tue May 04 17:35:12 IST 2004
full moon on Thu Jun 03 06:04:00 IST 2004
full moon on Fri Jul 02 18:32:48 IST 2004
full moon on Sun Aug 01 07:01:36 IST 2004
full moon on Mon Aug 30 19:30:24 IST 2004
full moon on Wed Sep 29 07:59:12 IST 2004
full moon on Thu Oct 28 20:28:00 IST 2004
full moon on Sat Nov 27 08:56:48 IST 2004
full moon on Sun Dec 26 21:25:36 IST 2004
full moon on Tue Jan 25 09:54:24 IST 2005
full moon on Wed Feb 23 22:23:12 IST 2005
full moon on Fri Mar 25 10:52:00 IST 2005
full moon on Sat Apr 23 23:20:48 IST 2005
full moon on Mon May 23 11:49:36 IST 2005
full moon on Wed Jun 22 00:18:24 IST 2005
full moon on Thu Jul 21 12:47:12 IST 2005
full moon on Sat Aug 20 01:16:00 IST 2005
full moon on Sun Sep 18 13:44:48 IST 2005
full moon on Tue Oct 18 02:13:36 IST 2005
full moon on Wed Nov 16 14:42:24 IST 2005
full moon on Fri Dec 16 03:11:12 IST 2005
full moon on Sat Jan 14 15:40:00 IST 2006
full moon on Mon Feb 13 04:08:48 IST 2006
full moon on Tue Mar 14 16:37:36 IST 2006
full moon on Thu Apr 13 05:06:24 IST 2006
full moon on Fri May 12 17:35:12 IST 2006
full moon on Sun Jun 11 06:04:00 IST 2006
full moon on Mon Jul 10 18:32:48 IST 2006
full moon on Wed Aug 09 07:01:36 IST 2006
full moon on Thu Sep 07 19:30:24 IST 2006
full moon on Sat Oct 07 07:59:12 IST 2006
full moon on Sun Nov 05 20:28:00 IST 2006
full moon on Tue Dec 05 08:56:48 IST 2006
full moon on Wed Jan 03 21:25:36 IST 2007
full moon on Fri Feb 02 09:54:24 IST 2007
full moon on Sat Mar 03 22:23:12 IST 2007
full moon on Mon Apr 02 10:52:00 IST 2007
full moon on Tue May 01 23:20:48 IST 2007
full moon on Thu May 31 11:49:36 IST 2007
full moon on Sat Jun 30 00:18:24 IST 2007
full moon on Sun Jul 29 12:47:12 IST 2007
full moon on Tue Aug 28 01:16:00 IST 2007
full moon on Wed Sep 26 13:44:48 IST 2007
full moon on Fri Oct 26 02:13:36 IST 2007
full moon on Sat Nov 24 14:42:24 IST 2007
full moon on Mon Dec 24 03:11:12 IST 2007
full moon on Tue Jan 22 15:40:00 IST 2008
full moon on Thu Feb 21 04:08:48 IST 2008
full moon on Fri Mar 21 16:37:36 IST 2008
full moon on Sun Apr 20 05:06:24 IST 2008
full moon on Mon May 19 17:35:12 IST 2008
full moon on Wed Jun 18 06:04:00 IST 2008
full moon on Thu Jul 17 18:32:48 IST 2008
full moon on Sat Aug 16 07:01:36 IST 2008
full moon on Sun Sep 14 19:30:24 IST 2008
full moon on Tue Oct 14 07:59:12 IST 2008
full moon on Wed Nov 12 20:28:00 IST 2008

Subtracting dates in Java. Daylight Savings Time Changes

The problem is that the subtraction of the two dates leaves me double identical value.
From : Thu Nov 29 00:00:00 CET 2012
Value 121: To: Sat Mar 30 00:00:00 CET 2013
Value 122: To: Sun Mar 31 00:00:00 CET 2013
Value 122: To: Mon Apr 01 00:00:00 CEST 2013
Value 123: To: Tue Apr 02 00:00:00 CEST 2013
Long today = (To.getTime() - From.getTime()) / (1000 * 60 * 60 * 24);
Is it possible to somehow avoid that logically? (sorry for English)

Export .jar with .txt as resource won't work

I have a problem concerning the .jar Export. I have to include a .txt file into my jar from where I read words into a list. I used this to realize(in eclipse Run + Debug mode it works well):
File file = new File(Worte.class.getClassLoader().getResource("resource/wortliste.txt").getFile());
Now, if I export it into a JAR file, the JAR file is executable as it should, and it contains the list.txt and .java files in resource (as it should because it's homework ;-) ), but the program behaves like there was no file. I have the same problem when i use getResourceAsStream(). Does anybody know why this won't work? I don't understand that, because i did this 2 weeks ago with another code, and it worked o_O.
What i have tried:
deleting the Project and import into a new one
like 1, but into a new workspace
My System is a Windows 7 x64 PC, Eclipse Juno and JRE7.
Options I use for export:
[] Export generated class files and resources
[x] export all output folders...
[x] export java source files...
[] export refactorings
[x] compress the contents...
[x] add directory entries
[x] overwrite existing files without warning
jar tvf ...
39 Wed Jan 30 16:19:14 CET 2013 META-INF/MANIFEST.MF
0 Wed Jan 30 00:34:16 CET 2013 resource/
100250 Wed Jan 30 00:37:24 CET 2013 resource/wortliste.txt
0 Wed Jan 30 00:34:16 CET 2013 wortspiel/
1291 Wed Jan 30 01:19:14 CET 2013 wortspiel/BuchstabenKollektion.java
2251 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestBuchstabenKollektion.java
506 Sun Jan 27 17:38:48 CET 2013 wortspiel/UI.java
1187 Sun Jan 27 16:24:42 CET 2013 wortspiel/TestWorte.java
2932 Wed Jan 30 01:25:00 CET 2013 wortspiel/WortspielGUI.java
4384 Wed Jan 30 01:50:40 CET 2013 wortspiel/Worte.java
310 Sun Jan 27 16:25:08 CET 2013 .classpath
383 Sun Jan 27 16:22:32 CET 2013 .project
1992 Wed Jan 30 16:02:12 CET 2013 wortspiel/BuchstabenKollektion.class
2075 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestBuchstabenKollektion.class
1104 Wed Jan 30 16:02:12 CET 2013 wortspiel/TestWorte.class
942 Wed Jan 30 16:02:12 CET 2013 wortspiel/UI.class
4701 Wed Jan 30 16:02:12 CET 2013 wortspiel/Worte.class
688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$1.class
688 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI$2.class
3475 Wed Jan 30 16:02:12 CET 2013 wortspiel/WortspielGUI.class
You can't use java.io.File to get the contents of a file inside of a jar. Try running the following snippet,
public class TestGetResource
{
public static void main(String[] args) throws Exception
{
URL url = TestGetResource.class.getResource("/resource/wortliste.txt");
System.out.println("URL: " + url);
File f = new File(url.getFile());
System.out.println("File: " + f);
System.out.println("File exists: " + f.exists());
try {
FileReader reader = new FileReader(f);
reader.read();
} catch (FileNotFoundException notFoundEx) {
System.out.println("Caught FileNotFoundException: " + notFoundEx.getMessage());
}
}
}
You should see something similar to this,
URL: jar:file:/C:/some/path/my.jar!/resource/wortliste.txt
File: file:\C:\some\path\my.jar!\resource\wortliste.txt
File exists: false
Caught FileNotFoundException: file:\C:\some\path\my.jar!\resource\wortliste.txt (The filename, directory name, or volume label syntax is incorrect)
Instead, use Class.getClassLoader().getResourceAsStream(String) to get an InputStream for reading the file. If you still need to read the contents as a File, you could stream the contents out to a temp file.
Try this:
URL url = Thread.currentThread().getContextClassLoader().getResource("resource/list.txt");
File file = new File(url.getFile());

How to add src files to jar?

I have built a Java application that reads data from a txt file located in the src folder. The path I have specified in the program is /src/data.txt and it works when I run it from netbeans. However when I tried to open the jar file, nothing opens. So I tried using javac from command line and this gives me the error that data.txt can't be found.
How do I make sure that the data file is included in the jar so it will work as a standalone?
Thanks.
EDIT1 : Here is a snippet of the code I use to load the file. And the path used is the aforementioned /scr/data.txt
public String [] openFile() throws IOException {
FileReader fr = new FileReader(this.path);
BufferedReader br = new BufferedReader(fr);
String []text = new String[this.numberoflines];
for(int i=0;i<this.numberoflines;++i)
{
text[i]=br.readLine();
}
br.close();
return text;
}
EDIT2 : Well here is the tvf output:
Error: Could not find or load main class jar
C:\Users\Abhishek>jar -tvf Scrades.jar
0 Sun Jan 22 18:47:08 IST 2012 META-INF/
199 Sun Jan 22 18:47:06 IST 2012 META-INF/MANIFEST.MF
2562 Sun Jan 22 18:47:08 IST 2012 CombinationGenerator.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$1.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$2.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$3.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$4.class
684 Sun Jan 22 18:47:08 IST 2012 Gameplay$5.class
969 Sun Jan 22 18:47:08 IST 2012 Gameplay$6.class
18279 Sun Jan 22 18:47:08 IST 2012 Gameplay.class
2275 Sun Jan 22 18:47:08 IST 2012 PermutationGenerator.class
1252444 Sun Jan 22 18:47:08 IST 2012 eng_final1.txt
3771960 Sun Jan 22 18:47:08 IST 2012 english_huge.txt
815532 Sun Jan 22 18:47:08 IST 2012 english_long.txt
16104 Sun Jan 22 18:47:08 IST 2012 english_short.txt
1506 Sun Jan 22 18:47:08 IST 2012 readFile.class
You can manually inspect, place and remove the file in the jar archive using a rar extractor like winrar : Then access per using Class.getResourceAsStream(String);
InputStream is = getClass().getResourceAsStream("/src/data.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null){
//Perform operations
}
br.close();
isr.close();
is.close();

Categories

Resources