Show Posts
|
|
Pages: [1] 2 3
|
|
1
|
Game Development / Newbie & Debugging Questions / Re: Searching for image files inside a jar
|
on: 2012-02-22 20:38:46
|
Im not sure if I have expressed what I want to do very good. I want to pack everything in one jar file, why do this? Because this program is meant for people having trouble with math and computers. It's educational software and I want it to be portable and easy to use. What I want to do is count how many images are stored inside the image folders inside the jar file. Like this: 1 2 3 4
| 1. Iterate the image folders inside the jar file this code executes from 2. count how many images are in thisjarfile/images/geometry/ and make an image array with that many elements 3. make the same thing with algebra, calculus, arithmetic etc 4. load the images and put them into the arrays created in step 2 |
The code I submitted can do that if you compile it and pack it into a jar then executes the jar like a regular java application, but when you try to run it inside eclipse to debug for example it doesn't find any images at all which makes my program crash. Why can't it find the images? @Ra4king: I did that to express my hate for nullpointers, it's a little joke actually  I will fix it when I make the code ready for release. @Sproingie: How do you put a zip in the classpath? O_o And how exactly do I make square wheels? I only asked a question xD
|
|
|
|
|
2
|
Game Development / Newbie & Debugging Questions / Searching for image files inside a jar
|
on: 2012-02-22 15:28:37
|
Hello folks! I have a problem in my code. I want to read image files from within a Jar, or to be more precise, I want to count them. I can load images from the loadImage() method I have created, looks like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Override public Image loadImage(String path) throws IOException{ if(Mothership.infoActive()){System.out.println("Loading image: " + path);} Image tempImg; try{ tempImg = new ImageIcon(getClass().getResource(path)).getImage(); if(tempImg != null){ if(Mothership.infoActive()){System.out.println("Image " + path + " loaded without errors");} return tempImg; }else{ return null; } }catch(NullPointerException assf**k){ throw new IOException(); } } |
Now, I want to scan the folders inside the jar file to see how many images are in there so that I can build my program to adapt itself to how many images there are in each folder. I found this piece of code on stack overflow which does the trick, but with one small problem. It won't let me run the program inside eclipse, it doesn't find the images if they are not packed in a Jar. I am not very good at file IO so help and advice would be greatly appreciated  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class Main { String[] lol; CodeSource src = Main.class.getProtectionDomain().getCodeSource(); List<String> list = new ArrayList<String>(); public static void main(String[] args){ Main m = new Main(); m.init(); } public void init(){ try { read(); } catch (IOException e) { e.printStackTrace(); } } public void read() throws IOException{ if(src != null){ System.out.println("Check"); URL jar = src.getLocation(); ZipInputStream zip = new ZipInputStream( jar.openStream()); ZipEntry ze = null; while((ze = zip.getNextEntry()) != null){ String entryName = ze.getName(); if(entryName.endsWith(".png") || entryName.endsWith(".PNG") ) { list.add(entryName ); System.out.println(entryName); } }
} lol = list.toArray( new String[ list.size() ] ); System.out.println(lol.length); } } |
|
|
|
|
|
3
|
Discussions / General Discussions / Re: Thank you guys, it's been great
|
on: 2011-12-06 13:12:07
|
@Hazzy: Glad to be able to spread some joy in the world  Nice, a little motivation is pretty much all that is needed to get a lot of coding done! I found it really fun to make a visual novel, so yeah, it's better than pong  It teached me a lot about graphics programming, swing, awt, threading and a lot of core principles. What it didn't teached me was how to program object oriented (My old visual novel is about 4 gigantic classes xD). But I'd say it's a great way to learn so go for it mate!  Good luck and happy coding mate! //Kurten
|
|
|
|
|
4
|
Game Development / Newbie & Debugging Questions / Re: New to Java
|
on: 2011-12-04 21:55:55
|
|
The best one can do when itching to write a game is read!
Good literature will give you and understanding of java that is very useful when writing games, I would recommend Head First: Java, that's for the fundamental stuff within the language, like tr try catch statement and what it does.
When wanting to read more about game programming, developing games in Java by David Brackeen is good, it teaches you to write small things like threadpools and screen managers that can later be used to make a game.
//Kurten
|
|
|
|
|
5
|
Discussions / General Discussions / Re: Thank you guys, it's been great
|
on: 2011-12-01 13:57:03
|
Yes, I understood ^_^ Was just explaining why im set on Skövde even if there are lots of other educations out there ^_^ Appreciated that you brought up the social environments though since that is a very important factor for me  Good advice as on grades as well, I appreciate it ^_^
|
|
|
|
|
6
|
Discussions / General Discussions / Re: Thank you guys, it's been great
|
on: 2011-12-01 13:49:08
|
|
Lund is very far though D: (Im in Västerås, about 10 km east of Stockholm) I have been playing with the idea of studying computer science and then get a master level instead of game programming in Skövde, but Skövde seems like so much fun! XD And i heard about 80% of the guys in skövde gets employed to game development companies like codemasters less than a year after their graduation. I heard this back in 2009 though so it could have been changed.
|
|
|
|
|
8
|
Discussions / General Discussions / Re: Thank you guys, it's been great
|
on: 2011-12-01 13:29:52
|
Me and my friend went to tokyo on a dragon  (It was a little joke me and one of my friends had with our former japanese teacher)  Well, in that case KTH might be a better choice for you, but I'm all set for HIS >=D (We can compare em after our graduation, though i expect a lot of double coupliation will be done in both places 8D)
|
|
|
|
|
9
|
Discussions / General Discussions / Re: Thank you guys, it's been great
|
on: 2011-12-01 12:53:31
|
theagentd, you should check Skövde out, allow me to switch to swedish, it will be more easy to explain, sorry english folks but this requires some words i can't really use on english D: I Skövde har dom ett renodlat spelprogrammeringsprogram där man arbetar i C++, man läser först programmering och matematik, sedan andra året läser man operativsystem och deploymentmiljöer, tredje året specialiserar man sig med valbara kurser, dock läser man även artificiell intelligens. Jag är inte säker, men jag tror att dom pretty much dissar verktyg som "förenklar" spelutveckling genom att ta bort själva kodandet. Vad jag vet så har spelutvecklarna på HIS (Högskolan i Skövde) utvecklat stridssimulatorer för militären också, så det verkar ganska seriöst. En polare tipsade mig om playground i Falun också, kolla upp det med. Vi kanske bör fortsätta den här konversationen på msn eller liknande då det kanske blir lite drygt för de övriga medlemmarna med en hel tråd där jag diskuterar på svenska xD *Goes back to english* So, you are in Japan at the moment?  I've only read basic japanese so I'm not very fluent in it, ぼくはぼくのともだちとりゅでときょへいきます。 is basically one of the only things I can say xD
|
|
|
|
|
10
|
Discussions / General Discussions / Re: Thank you guys, it's been great
|
on: 2011-12-01 08:35:09
|
I'm in the US and there are no game development high school nearby. There are game development colleges of course but who has all that $$$$$$$$$  And I believe Rebirth was suggesting that hanging with a girl != becoming a geek  Ah, that sucks man D: On, there are girls and geek girls, and the girls i pick up are of course geek girls  My mistake  @theagentd: Awesome, I didn't know there was one in Stockholm, the thing abouit "Love games hate math and programming" worries me a bit too though. However, have you looked up the one in Skövde? It seems to be fairly balanced between math, programming, and games  I'll begin working on a granade class to use for countering the double compile! 
|
|
|
|
|
11
|
Discussions / General Discussions / Re: Thank you guys, it's been great
|
on: 2011-11-30 13:30:23
|
@ra4king: I see you have cookies here, I have instructed my web browser to save the ones from JGO :3 Omnomnom geek side JGO cookies :3 Oh and where are you at if there's no game development schools in your contry? O: @theagentd: Oh, so you are in swe as well? Btw, after our little joke a few months back I always do this: 1 2 3 4 5 6 7 8 9 10
| try{ tempEll2[j] = new Element(imgpath[i][j], idvalues[i][j]); if(Mothership.infoActive()){ System.out.println("New element in " + i + " " + j + " with ID " + idvalues[i][j]); } }catch(IOException e){ e.getMessage(); e.printStackTrace(); System.exit(1); } |
 @Rebirth: I never touched C++ after that, is it still really cryptic? couse I'm planning on learning it soon. What paradox did you discover btw?
|
|
|
|
|
13
|
Discussions / General Discussions / Thank you guys, it's been great
|
on: 2011-11-30 02:25:38
|
|
Hello folks
Im not very known here, but I'd like to post something to thank you guys for what you have done for me. I wanted to give something back to this community for being such a good place, but I couldn't come up with anything of value to you, so I decided to write a text instead. Thanks for taking the time to read.
My story
It all began around July 2009 when I was 16. It was a heatwave here in Sweden and I was spending my days in front of my computer playing games. It was about a year after my parents' divorce so I had been away from school for about 9 months. During those 9 months I had spent most of my time in front of my computer, playing games, with no idea what to do with my life. While being away from school I grew bored and discovered that one could build small batch file ”viruses” using notepad so I was playing around with that, it's pretty silly now when I think about it. However, after getting hooked up on writing codey stuff I picked up FreeBasic, played around with it for a month or too, then went on to HTML which bored me to death, tried to learn C++ which was so cryptic I couldn't understand any of it. After a while I gave up on computer programming and decided to just play games and try to go outside the house a bit more.
Around July I began talking more and more to a girl called Klara. I had bought a game called The Sims 3 which she wanted to play but her computer couldn't handle it. So I invited her over to play the game at me instead. She was going away to Turkey on vacation for a week though so she told me we could schedule a day after she had gotten home. 3 days after she had left my computer died. As I said earlier, it was a heatwave and the PSU overheated. I was thinking about the possibility to have some computer store repair the computer but it would take a couple of weeks and I could not really afford it. While pondering whether to have a professional repair the computer or try to repair it myself I remembered that I had promised Klara to play The Sims 3 with her after she had returned. The responsible adult in me took over and I decided to open the computer up and manually repair it, even though I had never repaired a computer before in my entire life. I was very careful and managed to fix the computer by replacing the damaged PSU and when Klara got home we enjoyed some gaming together. That was the first step of my path towards becoming a geek.
After my first computer repair I found out I was enjoying playing around with computer tech. I decided to build my own computer, and half a year later, in March 2010, my current computer was finished. By that time I was still unsure what to do with my life though, I wanted to work with something game related, and I was somewhat hooked up on being a game designer, even though I knew nothing about game design.
It was also during early 2010 I began failing in school, my motivation was pretty much non-existent and I spent 3 weeks of only sleeping and eating, never leaving the house. My dad decided to take me to a high school here in Sweden which specializes in game development education to try to boost my motivation a bit. The high school is called Högskolan I Skövde (HIS for short). On HIS they have a bunch of game development related education programs, one for game programming, one for game design and one for game graphics. When driving to HIS I believed that I was going to walk around looking at what the game design guys were doing, but due to some mixup I got to walk around with the game programmers instead. Suddenly I began thinking about building programs again, I was unsure how to do it though. They introduced me to a simple tool, Game Maker. I spent maybe half a year playing around with Game Maker, but for me which knew nothing about real programming, it was mostly confusing since I couldn't find any good tutorials.
In September 2010 some guys in school contacted me because they wanted me to build a cellphone application for them. They had overestimated my technical knowledge but I saw it as a challenge and accepted. I began reading about the different cellphone operating systems and what languages was used to build programs on them. I told the guys that I wanted to build an application for android since everything I had read suggested it to be the 'hottest' operating system out there. They basically said: “Fine just give us the app later”. I began learning Java from the new boston's youtube channel and later from various books, mostly Head first – Java.
About a month later the guys that contacted me told me they were going to do something else instead, so they canceled the android app order. I however had discovered how wonderful it is to program, so I decided to stay with Java and learn more. Now about one year later, I'm still learning, and still experimenting, and still spending entire nights investigating some annoying bug, and still drinking tons of coffee, and most important of all, I'm still having lots of fun!
Because that's what programming is for me, a whole lot of fun!
I have done a lot of things during my first year learning Java.
In December, 2010 I built a horrible, unstable visual novel game. It didn't work though so I scrapped it and began building a new one from scratch in late January, 2011. That one worked pretty well, even though it was an applet and the code looked like shit. You guys helped me out with the code though and taught me some good OOP principles.
In May, 2011 I began playing around with probability and I learned that there is something called 'pseudo-random' (It was you guys here at JGO that taught me that). I built a pseudo-random number simulator that ran 4 different pseudo-random number generators in parallel and logged patterns. I was pretty basic stuff though so no real science here, but you guys helped me out here as well.
In June, 2011 I tried to build an android app using LibGdx, it worked but it wasn't really my thing so I pretty much left it at a android version of my first applet ever created which is called PilenBounce. Guess where I discovered LibGdx, that's right; JGO.
During the summer I was busy with other stuff, but in August, 2011 my current project was born, Project Mathematics, which has also received helpful code from the folks at JGO.
So, one year later, and I'm still learning, still experimenting, still debugging, still addicted to coffee, still having fun, still having lots of fun. Thank you all at JGO, for this would have been very different without you.
TLDR; Thanks to JGO and it's awesome members for being such a supportive community, There are many people here that I look up to and I want you guys to know that you have helped me a great deal on my road towards my dream, becoming a good game programmer.
|
|
|
|
|
15
|
Game Development / Game Mechanics / Re: A little Math
|
on: 2011-11-11 09:28:32
|
If you think this is bad you should see the code from when I started, for some odd reason when I am writing code variable names just don't come to me. but it is all in the works, getting better every day...
This is non-relevant to the original question, but one good way to write good variable names is to use mathematical names for them. pointX / posX //Point is a point in the coordinate grid, therefore a coordinate. pointY / posY velX //velocity is from what i have understood the same thing as speed velY accX //acceleration is change in velocity if i remember correctly accY That's my way of naming my variables 
|
|
|
|
|
16
|
Game Development / Newbie & Debugging Questions / Re: IllegalStateException, I can't get rid of it!
|
on: 2011-11-09 10:48:59
|
@ra4king: Ooooh, now i understood how the method works!  Or well, i think i have understood it, it tells the EDT to execute the run method after it is done with all it's current tasks, when it ahve told the EDT to executet it later, it moves on and what happened was that my other threads tried to use the bufferstrat before the EDT had created it, is that correct? Your solution worked btw, the program executes normally now  Once again, thanks for the help  @Cylab: thanks for the explanation, i'll play around a bit with the code you provided, it is a helper method for scheduling tasks for the EDT, or have i misunderstood it?
|
|
|
|
|
17
|
Game Development / Newbie & Debugging Questions / Re: IllegalStateException, I can't get rid of it!
|
on: 2011-11-09 10:21:36
|
still doesn't work, even though the code gets called =/ When exactly does it call this swingUtilities method? Because it doesn't seem to be called in chronical order 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| import java.awt.Color; import java.awt.Image; import com.yumeprojects.jimDB.JimDB; import java.awt.Graphics2D; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.awt.image.BufferStrategy; import java.io.IOException; import java.util.ArrayList;
import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.SwingUtilities;
public class GraphicsEngine implements Runnable{ protected boolean looping; private BufferStrategy strat; private Graphics2D g2d; private JFrame frame; private Mathematics math; private Image bgImage[]; private int bgIndex; protected Color bgColor = Color.BLACK; protected Color drawColor = Color.RED; protected boolean drawAlgebraChoices, drawGeometryChoices, drawArithmeticChoice, drawMainMenu; protected Button[] mainMenuButtons = { new Button("Algebra", "/images/Buttondark.png", "/images/theButton3.png", 100, 50), new Button("Geometry", "/images/Buttondark.png", "/images/theButton3.png", 205, 50), new Button("Arithmetic", "/images/Buttondark.png", "/images/theButton3.png", 310, 50), new Button("Statistics", "/images/Buttondark.png", "/images/theButton3.png", 415, 50), new Button("Probability", "/images/Buttondark.png", "/images/theButton3.png", 520, 50) }; protected Button[] algebraSectionButtons = { new Button("Linear Equations" , "/images/Buttondark.png", "/images/theButton3.png", 100,50), new Button("System of equations", "/images/Buttondark.png", "/images/theButton3.png", 205, 50), new Button("Variables", "/images/Buttondark.png", "/images/theButton3.png", 310, 50) }; protected Button[] geometrySectionButtons = { new Button("Square area and volume", "/images/kvadrat volym och area100xtrans.png", "/images/kvadrat volym och area100xtransglow.png", 100, 50), new Button("Circle area and volume", "/images/cirkel volym och area100pxtrans.png", "/images/cirkel volym och area100pxtransglow.png", 205, 50), new Button("Triangle area and volume", "/images/Triangelvoloarea.png", "/images/Triangelvoloareaglow.png", 310, 50), new Button("Angles", "/images/Vinklar.png", "/images/Vinklarglow.png", 415, 50), new Button("Patterns", "/images/Buttondark.png", "/images/theButton3.png", 520, 50), new Button("Square perimiter", "/images/Buttondark.png", "/images/theButton3.png", 100, 155), new Button("Circle perimiter", "/images/Buttondark.png", "/images/theButton3.png", 205, 155), new Button("Triangle perimiter", "/images/Buttondark.png", "/images/theButton3.png", 310, 155), new Button("Degrees and radians", "/images/Buttondark.png", "/images/theButton3.png", 415, 155), new Button("Symmetry", "/images/Buttondark.png", "/images/theButton3.png", 520, 155) }; protected Button switchBackgroundButton = new Button("Switch Background", "/images/Buttondark.png", "/images/theButton3.png", 695, 565); private String[][][] imageFilepath = { { {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/Buttondark.png", "/images/theButton3.png"} }, { {"/images/Buttondark.png", "/images/theButton3.png"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"}, {"/images/denshin enderman.PNG", "/images/enderman.PNG"} } }; private String[][][] imageIDKeys = { { {"Linear equations 1", "Linear equations 2"}, {"System of equations 1", "System of equation 2"} }, { {"Square area and volume 1", "Square area and volume 2"}, {"Circle area and volume 1", "Circle area and volume 2"}, {"Triangle area and volume 1", "Circle area and volume 2"}, {"Angles 1", "Angles 2"}, {"Patterns 1", "Patterns 2"}, {"Square perimiter 1", "Square perimiter 2"}, {"Circle perimiter 1", "Circle perimiter 2"}, {"Triangle perimiter 1", "Triangle perimiter 2"}, {"Degrees and radians 1", "Degrees and radians 2"}, {"Symmetry 1", "Symmetry 2"} } }; protected JimDB database = new JimDB(this, imageFilepath, imageIDKeys); private Image[] jimImage; private int jimImageIndex; WindowListener windowListener = new WindowAdapter(){ public void windowClosing (WindowEvent w){ exitProgram(); } }; public GraphicsEngine(JFrame f, BufferStrategy buff, Mathematics m) throws IOException{ frame = f; strat = buff; math = m; bgImage = new Image[5]; bgImage[0] = loadImage("/images/bakgrund01.png"); bgImage[1] = loadImage("/images/bakgrund02.png"); bgImage[2] = loadImage("/images/bakgrund03.png"); bgImage[3] = loadImage("/images/bakgrund04.png"); bgImage[4] = loadImage("/images/bakgrund05.png"); } public void initFrameAndStuff(){ frame = new JFrame("Project Mathematics"); frame.setSize(800, 600); frame.setResizable(true); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(windowListener); frame.setVisible(true); try{ SwingUtilities.invokeLater(new Runnable(){ public void run(){ System.out.println("Check"); frame.createBufferStrategy(2); strat = frame.getBufferStrategy(); } }); }catch(IllegalStateException e){ e.getMessage(); e.printStackTrace(); frame.setVisible(true); frame.createBufferStrategy(2); }finally{ math.startThreads(); } } public void exitProgram(){ math.stopAll(); frame.setVisible(false); frame.dispose(); } @Override public void run(){ looping = true; while(looping){ renderReInit(); render(); renderDispose(); renderUpdate(); try{ Thread.sleep(20); }catch(InterruptedException e){ System.out.println("Skiten kraschade"); e.printStackTrace(); } } } public void renderReInit(){ try{ g2d = (Graphics2D)strat.getDrawGraphics(); }catch(IllegalStateException e){ System.err.println("Buffer failed to recreate"); strat = frame.getBufferStrategy(); g2d = (Graphics2D)strat.getDrawGraphics(); System.err.println("Buffer recreated"); } } public void render(){ g2d.setColor(bgColor); g2d.fillRect(0, 0, 800, 600); g2d.drawImage(bgImage[bgIndex], 0, 0, null); g2d.setColor(drawColor); g2d.drawString("Choice: " + math.choice, 20, 50); g2d.drawString("Unit: " + math.unit, 20, 70); g2d.drawString("Dimensions on frame: 800x600 pix", 50, 300); g2d.drawString("Button dimensions: " + geometrySectionButtons[0].getWidth() + "x" + geometrySectionButtons[0].getHeight() + " pix", 50, 320); if(drawMainMenu){ for(int i = 0; i < mainMenuButtons.length; i++){ mainMenuButtons[i].draw(g2d); } switchBackgroundButton.draw(g2d); }else if(drawAlgebraChoices){ for(int i = 0; i < algebraSectionButtons.length; i++){ algebraSectionButtons[i].draw(g2d); } }else if(drawGeometryChoices){ for(int i = 0; i < geometrySectionButtons.length; i++){ geometrySectionButtons[i].draw(g2d); } } if(database.drawImage){ if(jimImageIndex < jimImage.length){ g2d.drawImage(jimImage[jimImageIndex], 200, 300, null); }else{ g2d.drawImage(jimImage[0], 20, 300, null); } } if(drawAlgebraChoices){ g2d.drawString("Algebra! :D", 100, 100); }else if(drawGeometryChoices){ g2d.drawString("Geometry! :D", 100, 100); }else if(drawArithmeticChoice){ g2d.drawString("Arithmetic! :D", 100, 100); } if(!strat.contentsLost()){ strat.show(); }else{ System.err.println("Buffer contents lost"); System.err.println("Calm down though, this shit ain't serious"); System.err.println("Just continue chillin', the program will handle it"); } } public void renderDispose(){ g2d.dispose(); } public void renderUpdate(){ database.update(); } public void setButtonHoverStatus(int index, boolean status){ if(!(mainMenuButtons[index].isHovered())){ mainMenuButtons[index].setHover(true); }else{ mainMenuButtons[index].setHover(false); } } public Image loadImage(String path) throws IOException{ System.out.println("Loading image: " + path); Image tempImg; try{ tempImg = new ImageIcon(getClass().getResource(path)).getImage(); if(tempImg != null){ System.out.println("Image " + path + " loaded without errors"); return tempImg; }else{ return null; } }catch(NullPointerException assf**k){ throw new IOException(); } } public void modifyBackgroundIndex(int delta){ if(bgIndex + delta < bgImage.length){ bgIndex += delta; }else{ bgIndex = 0; } } public JFrame getAppFrame(){return frame;} public void setJimImage(Image[] img){ jimImage = img; } public Image[] getJimImage(){return jimImage;} } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
| import java.awt.Graphics2D; import java.awt.image.BufferStrategy; import java.io.IOException; import java.util.Scanner; import javax.swing.JFrame;
public class Mathematics extends JFrame implements Runnable{
protected Choice c1Test = new Choice("lol"); protected boolean squareIncomplete; protected String choice = ""; private int amount; private float xValue, yValue, zValue; protected int unit; private int[] squareDim = new int[4]; private int squareIndex; private Scanner amed = new Scanner(System.in); private boolean running; protected boolean drawing; protected JFrame frame; protected BufferStrategy strat; protected Graphics2D g2d; protected Thread graphicsLoop; protected Thread listeningLoop; protected Thread mainLoop; protected GraphicsEngine ge; protected InputListener inLis; protected MathGeek charles = new MathGeek(); protected boolean drawShapes, drawSquare, drawTriangle, drawCircle; protected boolean performingEquation, performingQuadratic, performingNegativeQuadratic; protected double resultToGraphEng;
private char currentTypedChar; public static void main(String[] args){ Mathematics math = new Mathematics(); if(args.length < 1){ System.err.println("Please execute with the -Dsun.java2d.d3d=false command"); System.err.println("This can be done by typing java -Dsun.java2d.d3d=false -jar \"File path here\" in the start.bat script i have provided"); System.err.println("For devs: just add the line -Dsun.java2d.d3d=false to the program arguments section in run configuration"); try{ Thread.sleep(5000); }catch(InterruptedException e){ e.printStackTrace(); } }else{ System.out.println(args[0]); math.init(); } } public void init(){ try{ ge = new GraphicsEngine(frame, strat, this); }catch(IOException assf**kingIOException){ assf**kingIOException.printStackTrace(); try{ Thread.sleep(10000); }catch(InterruptedException e){ e.printStackTrace(); } System.exit(1); }
graphicsLoop = new Thread(ge); listeningLoop = new Thread(inLis); mainLoop = new Thread(this); ge.initFrameAndStuff(); inLis = new InputListener(this, ge); } public void startThreads(){ listeningLoop.start(); mainLoop.start(); graphicsLoop.start(); } public void stopAll(){ inLis.listening = false; ge.looping = false; running = false; } @Override public void run(){ running = true; while(running){ checkAction(); checkSubSection(); ge.database.update(); try{ Thread.sleep(20); }catch(InterruptedException e){ e.printStackTrace(); } } } public void checkAction(){ if(inLis != null && ge != null){ if(inLis.isInAlgebra){ ge.drawAlgebraChoices = true; ge.drawMainMenu = false; }else if(inLis.isInGeometry){ ge.drawGeometryChoices = true; ge.drawMainMenu = false; }else if(inLis.isInArithmetic){ ge.drawArithmeticChoice = true; ge.drawMainMenu = false; } else{ ge.drawAlgebraChoices = false; ge.drawGeometryChoices = false; ge.drawArithmeticChoice = false; ge.drawMainMenu = true; } } } public void checkSubSection(){ if(inLis != null && ge != null && ge.database != null){ if(inLis.isInAlgebra){ ge.database.inAlgebra = true; if(inLis.isInAlgLinearEquat){ ge.database.inLinearEquat = true; ge.drawAlgebraChoices = false; }else if(inLis.isInAlgSystemOfEquat){ ge.database.inSystemOfEquat = true; ge.drawAlgebraChoices = false; }else{ ge.database.inLinearEquat = false; ge.database.inSystemOfEquat = false; } }else if(inLis.isInGeometry){ ge.database.inGeometry = true; if(inLis.inGeoSquareAreaVolume){ ge.database.inSquareAreaVolume = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoCircleAreaVolume){ ge.database.inCircleAreaVolume = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoTriangleAreaVolume){ ge.database.inTriangleAreaVolume = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoAngles){ ge.database.inAngles = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoPatterns){ ge.database.inPatterns = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoSquarePerimiter){ ge.database.inSquarePerimiter = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoCirclePerimiter){ ge.database.inCirclePerimiter = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoTrianglePerimiter){ ge.database.inTrianglePerimiter = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoDegreesRadians){ ge.database.inDegreesRadians = true; ge.drawGeometryChoices = false; }else if(inLis.inGeoSymmetry){ ge.database.inSymmetry = true; ge.drawGeometryChoices = false; } else{ ge.database.inSquareAreaVolume = false; ge.database.inCircleAreaVolume = false; ge.database.inTriangleAreaVolume = false; ge.database.inAngles = false; ge.database.inPatterns = false; ge.database.inSquarePerimiter = false; ge.database.inCirclePerimiter = false; ge.database.inTrianglePerimiter = false; ge.database.inDegreesRadians = false; ge.database.inSymmetry = false; } }else if(inLis.isInArithmetic){ ge.drawArithmeticChoice = true; ge.drawMainMenu = false; } else{ if(!inLis.isInAlgebra || !inLis.isInGeometry){ ge.drawAlgebraChoices = false; ge.drawGeometryChoices = false; ge.drawArithmeticChoice = false; ge.drawMainMenu = true; ge.database.inAlgebra = false; ge.database.inGeometry = false; ge.database.inLinearEquat = false; ge.database.inSystemOfEquat = false; } } } } } |
|
|
|
|
|
18
|
Game Development / Newbie & Debugging Questions / Re: IllegalStateException, I can't get rid of it!
|
on: 2011-11-08 23:40:31
|
Sorry for double post, but i can't get this to work, The code now looks like this but i get a null pointer exception when i try to get a graphics object from the buffer, it's almost like the buffer never gets created, even though the code gets executed. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public void initFrameAndStuff(){ frame = new JFrame("Project Mathematics"); frame.setSize(800, 600); frame.setResizable(true); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(windowListener); frame.setVisible(true); try{ SwingUtilities.invokeLater(new Runnable(){ public void run(){ System.out.println("Check"); frame.createBufferStrategy(2); } }); }catch(IllegalStateException e){ e.getMessage(); e.printStackTrace(); frame.setVisible(true); frame.createBufferStrategy(2); }finally{ strat = frame.getBufferStrategy(); math.startThreads(); } } |
|
|
|
|
|
21
|
Game Development / Newbie & Debugging Questions / Re: IllegalStateException, I can't get rid of it!
|
on: 2011-11-06 12:48:26
|
I've looked around a bit and most sources suggest the command you suggested as well, i'll try it and see if the bug pops up again, if not then you have my thanks ^_^ You have my thanks anyway though since you took the time to help me with this ^_^ Also, I'm a bit ashamed I didn't google the exception first, I guess I learned something new xD I use a bufferstrategy since it is the technique I usually use, like this basically. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| @Override public void run(){ looping = true; while(looping){ renderReInit(); render(); renderDispose(); renderUpdate(); try{ Thread.sleep(20); }catch(InterruptedException e){ System.out.println("Skiten kraschade"); e.printStackTrace(); } } } public void renderReInit(){ try{ g2d = (Graphics2D)strat.getDrawGraphics(); }catch(IllegalStateException e){ System.err.println("Buffer failed to recreate"); strat = frame.getBufferStrategy(); g2d = (Graphics2D)strat.getDrawGraphics(); System.err.println("Buffer recreated"); } } public void render(){ g2d.setColor(bgColor); g2d.fillRect(0, 0, 800, 600); g2d.drawImage(bgImage[bgIndex], 0, 0, null); g2d.setColor(drawColor); g2d.drawString("Choice: " + math.choice, 20, 50); g2d.drawString("Unit: " + math.unit, 20, 70); g2d.drawString("Dimensions on frame: 800x600 pix", 50, 300); g2d.drawString("Button dimensions: " + geometrySectionButtons[0].getWidth() + "x" + geometrySectionButtons[0].getHeight() + " pix", 50, 320); if(drawMainMenu){ for(int i = 0; i < mainMenuButtons.length; i++){ mainMenuButtons[i].draw(g2d); } switchBackgroundButton.draw(g2d); }else if(drawAlgebraChoices){ for(int i = 0; i < algebraSectionButtons.length; i++){ algebraSectionButtons[i].draw(g2d); } }else if(drawGeometryChoices){ for(int i = 0; i < geometrySectionButtons.length; i++){ geometrySectionButtons[i].draw(g2d); } } if(database.drawImage){ if(jimImageIndex < jimImage.length){ g2d.drawImage(jimImage[jimImageIndex], 200, 300, null); }else{ g2d.drawImage(jimImage[0], 20, 300, null); } } if(drawAlgebraChoices){ g2d.drawString("Algebra! :D", 100, 100); }else if(drawGeometryChoices){ g2d.drawString("Geometry! :D", 100, 100); }else if(drawArithmeticChoice){ g2d.drawString("Arithmetic! :D", 100, 100); } if(!strat.contentsLost()){ strat.show(); }else{ System.err.println("Buffer contents lost"); System.err.println("Calm down though, this shit ain't serious"); System.err.println("Just continue chillin', the program will handle it"); } } public void renderDispose(){ g2d.dispose(); } public void renderUpdate(){ database.update(); } |
|
|
|
|
|
22
|
Game Development / Newbie & Debugging Questions / Re: IllegalStateException, I can't get rid of it!
|
on: 2011-11-06 12:35:09
|
|
If the first attempt fails i will get notified becuase i tell it to print a stack trace, then try to recreate the buffer. If i put the other buffer creation command in a try catch block i will have to create an infinite try catch block to solve all this once and for all. What happened in that stack trace was the first buffer creation failed due to an illegalStateException, the code went into the catch section and managed to recreate the buffer without it failing, but since this fail occurs random both of those could have failed and that would leave me with a crashed program that can't be shut down normally.
|
|
|
|
|
23
|
Game Development / Newbie & Debugging Questions / IllegalStateException, I can't get rid of it!
|
on: 2011-11-06 10:17:40
|
There has been a bug bothering me for some time now, I believed I managed to code around it, but in a very clumsy way. I have no idea why this bug appears, but it's there, and appear at what seems to be random. I need to get rid of it since it could clog up the entire program. This is what happens 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| java.lang.IllegalStateException: Buffers have not been created at sun.awt.windows.WComponentPeer.getBackBuffer(Unknown Source) at java.awt.Component$FlipBufferStrategy.getBackBuffer(Unknown Source) at java.awt.Component$FlipBufferStrategy.updateInternalBuffers(Unknown Source) at java.awt.Component$FlipBufferStrategy.createBuffers(Unknown Source) at java.awt.Component$FlipBufferStrategy.<init>(Unknown Source) at java.awt.Component$FlipSubRegionBufferStrategy.<init>(Unknown Source) at java.awt.Component.createBufferStrategy(Unknown Source) at java.awt.Window.createBufferStrategy(Unknown Source) at java.awt.Component.createBufferStrategy(Unknown Source) at java.awt.Window.createBufferStrategy(Unknown Source) at com.yumeprojects.mathematics.GraphicsEngine.initFrameAndStuff(GraphicsEngine.java:149) at com.yumeprojects.mathematics.Mathematics.init(Mathematics.java:76) at com.yumeprojects.mathematics.Mathematics.main(Mathematics.java:56) |
This is the method the exception occurs in and is catched in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public void initFrameAndStuff(){ frame = new JFrame("Project Mathematics"); frame.setSize(800, 600); frame.setResizable(true); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(windowListener); frame.setVisible(true); try{ frame.createBufferStrategy(2); }catch(IllegalStateException e){ e.getMessage(); e.printStackTrace(); frame.setVisible(true); frame.createBufferStrategy(2); }finally{ strat = frame.getBufferStrategy(); math.startThreads(); } } |
Why does this illegal state occur? I have tried to code around it by making the frame visible again before trying to recreate the buffers, yet it dosen't really work in this case. This bug is also quite hard to recreate, as I said, it occurs at what seems to be random (yet I know it hardly is random). Please help, I have been trying to get rid of this shit for about 2 months and when I finally thought I succeeded, this things pops up again. EDIT: The program didn't crash when i encountered this, but in the unlikely event of both of those frame.createBufferStrategy() giving an illegal state this program will crash and the user won't be able to shut it down normally. That's why I need this bug gone. //Kurten
|
|
|
|
|
24
|
Game Development / Newbie & Debugging Questions / Re: Java 6 or 7?
|
on: 2011-11-04 19:11:14
|
@Cero: I prefer using switch statements over if else because i find it easier to organize without all the curly braces, just my personal opinion and it would be great to organize the contents of my program with switch statements since it uses a lot of strings. I want it to be easy and stable for the user, but on the same time i want to have a somewhat pleasant time coding the application. @JL235: Thank you, i didn't know that about schools. It is settled then, i will continue using java 6 for this project, thanks 
|
|
|
|
|
26
|
Game Development / Newbie & Debugging Questions / Java 6 or 7? (Solved)
|
on: 2011-11-04 16:21:36
|
|
Hello JGO
I'm building an educational program in Java with secondary school students in focus. To be more precise, a program that explains math to em. When i started to work on this program Java 1.7 had just been released but no eclipse support for it and I don't like to work in netBeans. But now with the eclipse support I want to make use of java 1.7 stuff like switch statements for strings and the try with resource thingie, my problem is: Will a java 7 written program be of any use for students? Java 6 is installed on pretty much all computers i have seen so far. But i don't know how well java 1.7 has been welcomed by users, do you guys have any experience in this field? Do people use java 7 yet? if not, when will they start using it or will they skip it to go to java 8 that i have read somewhere will be released 2012?
//Kurten
|
|
|
|
|
28
|
Game Development / Newbie & Debugging Questions / Re: System.exit(), is there any alternative?
|
on: 2011-09-09 20:38:33
|
|
I do not expect this exception to be generated very often, there is only 2 possible errors that can trigger it, that I can think of anyway. One of those is sloppyness by me, in case I have misspelled something in the image path. In this case, the user can't do anything with the application and I will have to fix it, which requires me to be notified. The other one is if one of the user messes around inside the jar file for some reason and move or delete an image file. In this case the error can be fixed by the user, but I doubt that regular students will understand what went wrong, I also plan to supervise the early tests so in case of a crash, I want the user to grab hold of me instead of believing that everything is well.
But for later versions of the program Im building I will try to work around the problem of contacting me every time the program crashes, so I will probably try to build some kind of recovery code later on.
|
|
|
|
|
29
|
Game Development / Newbie & Debugging Questions / Re: System.exit(), is there any alternative?
|
on: 2011-09-09 15:26:06
|
Hmm, I thought the code work well, but I also understand there is a reason why certain conventions should be followed, which means I will rewrite the exception handling  I would consider loading of the images critical since I have methods within them that InputListener and GraphicsEngine is dependent on. So if a button image can't be loaded I want the program to close down and not try to recover. I will switch the Nullpointer to an IOException instead since that seems more appropriate, the reason I went with Nullpointer was that the image file that would be returned by the loadImage() would have been null. Thanks once again for the suggestions ^_^ I will try to implement them as good as I can ^_^
|
|
|
|
|
30
|
Game Development / Newbie & Debugging Questions / Re: Scrolling map
|
on: 2011-09-09 14:18:34
|
|
I worked out a very rough code block for this some time ago, i don't have the code but the general idea was like this:
Make the player coordinates final, put him in the middle of the screen, then load up a 2D array of tiles, give them relative coordinates, coordinates that is changeable that is. When the player moves, instead of adding velocity to the player, add it to the tiles so that they move. Each update you loop through the tiles within view of the player in a nested for loop to update their current position with the position X = velocity X * elapsed time Position Y = velocity Y * elapsed time
Rough sketch, but i hope this helps you a bit at least
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|