Jump to content
  • Advertisement
Sign in to follow this  

Unity [java] Hello! First post, a little introduction and few questions! :)

This topic is 1677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys!
Before I start with anything, I'd like to express my hapiness for having a chance to be a member of such a functional and big community of game developers and programmers.
It would be rude to start without introducing, so here it is:
My name is Marko, I am from Rijeka, Croatia. I am a student at Engineering Faculty of Rijeka, my field is electrical engineering.
Even though I am (future) electrical engineer, it always was a pleasure to make programs.
Recently I decided to earn some extra money by making some Android games.

I always was a passionate gamer, I mostly understand what happens everywhere, I always dreamt about making few of my own games, so I decided to try learning it a bit, I've had some previous experience in Python, c++, basic, VB, perl etc., but I mostly learned everything about syntax, not much about each and every one of them individually.
I learned java was used for android phones so I found a good tutorial at kilobolt.com and started learning, after few weeks I stopped learning and repeating everything they did and started implementing my own code.
Now, I have a few questions (I'll try to be very precise, my code is huge since I am a bit inexperinced so it usually takes few more lines to do something than usually, so I'd rather keep it at words here. If anyone wants me to send them my code, I'll gladly do it, but I doubt you'll find any information in that pile haha! :))

Ok, my first question would be:

I used JFrame superclass to make a GUI.
At paint method, when I decide to assign coordinates of an object to a picture everything works well, but the problem is, I don't know how to enter whih coordinates I want. To be precise, of course, I know how to add them, but for example if I want to add a circular object (or some more complex) and add a .png file, it is being recognizes as a cube. So when I try to implement any simple collision recognition system, other objects interact with it as if it was a cube.
That means I can't implent some laws of physics or geometry. For example if ball collided with a wall at some certain point it start heading in different direction, depending on angle between the balls moving line and wall's horizontal line. So if the wall wasn't flat, if it had some spikes, it would be still recognized as a cube with dimensions equal to maximum distance between both coordinates.
I am using following command:
g.drawImage(picture object, object.getX(),object.getY(),this);
And the second question would be:
I am sometimes getting NullPointerException when I run my code, or series of errors in Graphics superclass.
I rechecked the code hundreds of times.
I scanned my computer for viruses and found few trojans. It turns out my girlfriend downloaded some game which installed few adware programs, and I noticed it when files started missing from the folder I placed my image files for the game.

So, is it possible, Eclipse got damaged during virus disinfection or by the adware trojans themselves?

So sorry for writing such a long text, English is not my mother language so I can't explain everything in short.
I heard you guys are very friendly so wanted to get some help here, I also want to stay here and be a prt of this community.
Well, good night folks, it's pretty late here.
First thing in the morning, I'll check for the answers, and once again, sorry for the length of the text and if you need my code, let me know!


Share this post

Link to post
Share on other sites
Sounds like you are having trouble handling collision with objects of different shape.

The reason being even if you have an image of a cube. The image itself is a rectangular shape. So bounding box collision will not work in this case.

There is an written article on this forum that talks sbout pixel perfect collision. You can try to see if you can use a library from Standard Java that lets you look at the rgb values of the image and think of an algorithm from that.

If the above strategy sounds like overkill, my other strategy is to create a polygon object that can wrapped around your cube object given x,y coordinate of each vertex of the cube. Then connect the edges from initial vertex to final vertex cube to produced a "bounding cube" collision.

Personally, I never done the above. I only worked with rectangular shapes like you have. But my gut says the second approach might produced better accurate collision given the fixed position coordinate mapping of this approach.

Null pointer exception typically occurs when no object exists in other words you have not written the object. You need to create a object reference to the object. Just declaring an object but has a null value will produce a null pointer.

When drawing an image to the canvas, setting the imageobserver argument to null will still allow image to be drawn.

Share this post

Link to post
Share on other sites

Well, I had an idea of trying to make my own equation for the shape of an object, I was a bit confused for object more complex than a circle or triangle, I mean I know I could have done something with sinuses and cosinuses, but I guess the rgb value thing could work :)
I'll try to learn something about it these days!

About the NPE, the weird thing is that it appears once in 20 runs, the Graphics error appears wheter I used coordinates of an object or set some none changing coordinates, it just seems like it has a problem fetching the picture from the folder since the pictures were dissappearing, but that can't be the only problem because it throws me the error even if pictures are there sometimes.

Thanks a lot!

Share this post

Link to post
Share on other sites

Hello! :-)


So you figured out the NPE is always related to missing images? Did you add debugging (System.out.println() for example) to figure out where in the code the problem occurs?

Do you know how to print and analyze StackTraces?


Missing images ... I would not suspect higher forces like viruses. Most of the time when we (companies I worked at) thought some other program or the system were at fault we had to learn that we were doing something wrong.


What does your build setup look like? The build process might delete and recreate the output / target folder (where the .class files are compiled to).

Putting the resources there does not work.

If that is how your project currently is set up you can try to create another source folder for the resources (... or put them in the source folder with the .java files which is not a nice solution). Then the build process should move those resources to the target folder after it has been cleared.


You could also use Ant or something similar to write logic for the build process but that might be too much of a distraction right now ... it does take a while to learn how to use it unless you know where to "steal" efficiently.



Btw. you might get away with using other languages, if you are not a fan of Java.

There are solutions like Apache Cordova that turn programs into iPhone or Android apps ... even if they were written in other languages (JavaScript in this case).

Probably something similar exists for other languages as well.

Engines like Unity can turn relatively neutral code into Apps for different platforms as well (I think you can do "scripting" in C# or something like that).

Edited by DareDeveloper

Share this post

Link to post
Share on other sites

Well, to be honest, I really like Java, it's not that complicated, and I learnt the basics very well, now I am able to make classes with constructors and make objects in other classes with those constructors.
I've also heard about engines like Unity, but I like building everything from scratch.

Regarding the NPE, well, if you think you could analyze the code a little bit, I would zip the whole program and send it to you for analyzing, but I'm not sure you'll like it, it's a bit messy, I got flamed hard and banned from StackOverflow because they didn't like my code. Thanks btw :D

Edited by xxSavagexx

Share this post

Link to post
Share on other sites

I do not think they ban anybody because of bad code. It is hard to ask questions that get approval, though. rolleyes.gif


Guess the NPE is taken care of, now. Maybe somebody here knows something about the remaining problem.

  • We initialize objects now before the thread is started.
  • We use a MediaTracker to wait until the images are loaded before we set the Applet to visible and start the main thread.

What does not work is that the logic has already progressed quite a bit when the graphics are rendered for the first time.


Is there a mechanism that would allow us to wait for whatever setVisible does asynchronously to complete?

Waiting (Thread.sleep(...)) after calling setVisible seems to help, so that is why I think that must be the remaining problem.

I tried to react to a COMPONENT_SHOWN ContainerEvent, but I think they are thrown right away when setVisible is called.

Is there any Event that tells us that everything is set up has been rendered successfully once?


Another question: The game is currently an Applet. How different or not is programming graphical apps with the Android SDK?

Is it important to make the transition early or is it pretty similar?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!