Hey guys, how can I make a JAR program read a file inside itself?
Like if I open the JAR, the file is at /DeXML/Database.xml
When it's needed, I use
[code]File outFile = new File("DeXMLPack/Database.xml");[/code]
This works fine in Netbeans, but once I pack it into a JAR, it wants Database.xml to be in the directory with the JAR. Then I have to change it to
[code]File outFile = new File("Database.xml");[/code]
[QUOTE=Chris122990;21834412]Hey guys, how can I make a JAR program read a file inside itself?[/QUOTE]
ClassLoader objects have a getResource() method that can be used to find arbitrary files using the same mechanisms used for finding .class files, which means it can read things from a jar if that's where the classloader is getting your classes from. Class objects also have a getResource() method, which is a wrapper around the ClassLoader one that lets you use relative paths for convenience.
The getResource() methods return a URI object; for reading the file you'll probably want to use the related getResourceAsStream() methods, which return an InputStream, instead.
Assuming your class is called "Database", you can arrange for the Database.xml file to be put in the same directory as the compiled Database.class file within the JAR. Then, in your code, you can do this:
[cpp]
final InputStream databaseXmlStream = Database.class.getResourceAsStream("Database.xml");
[/cpp]
and then read from the stream.
Note: In your question you used the variable name "outFile", which implies that you want to write to the file. You can't modify resources in a JAR, so if your program needs to modify the database, it'll need to be handled as an external data file, not something built into the program. You can use something like a FileDialog to let the user select it, and/or take the path to the file as a command-line parameter.
Has anyone bought this book?
[URL="http://oreilly.com/catalog/9780596800963"]http://oreilly.com/catalog/9780596800963[/URL]
Is it any good? How advanced is it?
I've had a good experience with O'Reilly but I wouldn't want to waste $50 for something that isn't worth it.
[QUOTE=Darwin226;21839799]Has anyone bought this book?
[URL="http://oreilly.com/catalog/9780596800963"]http://oreilly.com/catalog/9780596800963[/URL]
Is it any good? How advanced is it?
I've had a good experience with O'Reilly but I wouldn't want to waste $50 for something that isn't worth it.[/QUOTE]
O'Reilly books are generally pretty good.
[QUOTE=Wyzard;21836151]ClassLoader objects have a getResource() method that can be used to find arbitrary files using the same mechanisms used for finding .class files, which means it can read things from a jar if that's where the classloader is getting your classes from. Class objects also have a getResource() method, which is a wrapper around the ClassLoader one that lets you use relative paths for convenience.
The getResource() methods return a URI object; for reading the file you'll probably want to use the related getResourceAsStream() methods, which return an InputStream, instead.
Assuming your class is called "Database", you can arrange for the Database.xml file to be put in the same directory as the compiled Database.class file within the JAR. Then, in your code, you can do this:
[cpp]
final InputStream databaseXmlStream = Database.class.getResourceAsStream("Database.xml");
[/cpp]
and then read from the stream.
Note: In your question you used the variable name "outFile", which implies that you want to write to the file. You can't modify resources in a JAR, so if your program needs to modify the database, it'll need to be handled as an external data file, not something built into the program. You can use something like a FileDialog to let the user select it, and/or take the path to the file as a command-line parameter.[/QUOTE]
THANK YOU SIR!!! You are a Godsend.
And about the outFile part, I pasted the wrong part into my original post. That segment is from a database write method so I can open Database.xml and ensure the formatting is showing up correctly. The actual JAR program doesn't use it.
In XNA, I have a texture being drawn and I would like to know how to delete a certain bit of it when called for. Like how to do I edit the sprites in real-time I guess you could say.
What would be the best method/algorithm for quantizing a 24-bit image into a 4-bit pallete (16 colours) with regards to speed and memory consumption?
I've been working on getting an entire XML tree into a hash map in Java to where I could just call it like this:
xml.get("profile").get("steamID");
I used DOM and it worked alright, but I think that using SAX in Java would be easier. I just can't find a *simple* way to parse the entire thing into nested hashtables, etc.
This is what I have so far that I can get all of the information from the XML file, just not 'nested' or even put into a hash table.
[code]
package srcsrv.community;
import java.util.HashMap;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
public class CommunityXMLParser extends DefaultHandler {
HashMap<String, Object> xmlTree = new HashMap<String, Object>();
String currentElement = "";
boolean hasWrittenData = false;
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) {
System.out.println("[Start] " + qName);
}
public void characters(char ch[], int start, int length){
String outputText = new String(ch, start, length);
if(outputText.trim().length() > 0){
System.out.println(outputText);
}
}
public void endElement(String namespaceURI, String localName, String qName) {
System.out.println("[End] " + qName);
}
}
[/code]
Does anyone have any suggestions for parsing the entire document into hashmaps?
Tile based platformer game. The level is 4096x4096 tiles, and 100% randomly generated (top level, caves ETC).
I'm having problems with the platforming part of it, I just don't know where to start. I've created a Player object, with some member variables for position and velocity.
I need help with the collision detection between floor/walls/ceiling and player (remember it's tile based). Any ideas?
Use AABB's :[url]http://www.gamedev.net/reference/articles/article735.asp[/url]
I would recommend giving every object a Rectangle class that defines their bounding box. Then use that for the AABB detection. If it gets slow with every tile having it's own Rect then try making larger rects for large lumps of land., if possible (this could get complicated with destructible blocks) or just give blocks that are by the edge collision detection. It's really up to you.
This is the strangest bug I have ever seen and I have no idea how to fix it.
I have this function :
[cpp]
public static void CenterOn(Point point) {
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glTranslatef(-(point.X - CenterPosition.X + 400), -(point.Y - CenterPosition.Y + 300), 0);
CenterPosition = point;
CenterPosition += new Point(400, 300);
}[/cpp]
When I call it with
CenterOn( -new Point( 400, 300 ) );
it works fine but if I add
point -= new Point( 400, 300 );
to the function and call it with
CenterOn( new Point( 0, 0 ) );
it doesn't work correctly.
How can that be possible? It's exactly the same thing.
[QUOTE=iPope;21857330]Use AABB's :[url]http://www.gamedev.net/reference/articles/article735.asp[/url]
I would recommend giving every object a Rectangle class that defines their bounding box. Then use that for the AABB detection. If it gets slow with every tile having it's own Rect then try making larger rects for large lumps of land., if possible (this could get complicated with destructible blocks) or just give blocks that are by the edge collision detection. It's really up to you.[/QUOTE]
Thanks, that seems reasonable. I will post back with results :D
What would be the best approach to doing collision detection between potentially infinite objects? This is my idea:
Every object with collision registers at one static class that all of them can access which places a reference of that object in List<List<object>> at [(int)object.X][(int)object.Y] since 1 pixel is precise enough.
(Let's say that there can't be 2 objects at the same pixel)
Then, when checking for collision, only get the objects that are inside the bounding box of the object and then do more precise collision detection.
The problem is that some objects can have negative coordinates and I have no idea how I would be able to store them in an array.
Why are X and Y not already ints?
My objects have their coordinates in a Point struct which has X and Y as floats since I use Point for other things too.
object.X was just to show what I mean and make it less confusing than object.Position.X
The simple way (although not necessarily the most performant) would be to just have a list of objects and use LINQ to find nearby objects:
[code]
int distance = 1;
var touching = myObjects
.Where(x => Math.Abs(x.X - theObject.X) <= distance
&& Math.Abs(x.Y - theObject.Y) <= distance);
[/code]
You should also setup a get accessor for X and Y in case you decide to not use Point in future:
[code]
public int X { get { return (int)(Position.X + 0.5); } }
[/code]
To optimise collisions, it is useful to divide your world up into chunks: depending on how 3D it is, you might like to do cubes or top down squares. Every object is a member of one or more (you would normally pick a size of square so it can only be up to four), and then only check collisions with objects in those chunks.
You need to have a sort of reciprocative list for this arrangement: the list both knows what's in it, and the objects know what lists there in. Hope that's helpful.
Problem is if you go so fast in a frame you could pass right through the chunk
Well that's not really a problem in my case. I just needed a way to screen out objects that don't have a chance to collide.
Reading up on LINQ
[QUOTE=layla;21876937]Problem is if you go so fast in a frame you could pass right through the chunk[/QUOTE]
Update chunk membership after movement?
[QUOTE=Darwin226;21877025]Reading up on LINQ[/QUOTE]
Don't do that. You'll get confused with stuff like LINQ to Objects/SQL/whatever.
Instead, read up on lambda expressions and extension methods, and let Intellisense help you use the LINQ extension methods.
Is there some way to do this:
[cpp]
if (baseClass is DerivedClass){
DerivedClass temp = (DerivedClass)baseClass;
temp.SomeDeriverClassFunction();
}[/cpp]
but without the temporary variable? Like (DerivedClass)baseClass.SomeDerivedClassFunction();
(baseClass as DerivedClass).SomeDerivedClassFunction()
Nice. I tried using as before but probably forgot parentheses.
[cpp]class Entity
{
int X, Y, W, H;
int OldX, OldY;
float XVel, YVel;
float MoveSpeed = 1;
float Gravity = 0.3;
boolean OnGround = false;
Entity(int x, int y, float xvel, float yvel, int w, int h)
{
X = x;
Y = y;
XVel = xvel;
YVel = yvel;
W = w;
H = h;
OldX = X;
OldY = Y;
}
void Update()
{
OldX = X;
OldY = Y;
if (checkKey("w") && OnGround) YVel = -5; Y -= 1;
if (checkKey("s")) YVel += MoveSpeed;
if (checkKey("a")) XVel -= MoveSpeed;
if (checkKey("d")) XVel += MoveSpeed;
OnGround = false;
int Collision = CheckCollision();
if (Collision == COLLISION_TOP)
YVel = 0;
else if (Collision == COLLISION_BOTTOM)
{
YVel = 0;
OnGround = true;
}
else if (Collision == COLLISION_SIDE)
XVel = 0;
if (!OnGround)
YVel += Gravity;
else
XVel *= 0.9;
X += XVel;
Y += YVel;
}
int CheckCollision()
{
for (int i = 0; i < BlockList.length; i++)
{
if (BlockList[i].Type != BLOCK_AIR)
{
int BlockMinX = BlockList[i].X * BlockSize;
int BlockMinY = BlockList[i].Y * BlockSize;
int BlockMaxX = BlockMinX + BlockSize;
int BlockMaxY = BlockMinY + BlockSize;
if (CheckOverlap(X, Y, X + W, Y + H, BlockMinX, BlockMinY, BlockMaxX, BlockMaxY))
{
X = OldX;
Y = OldY;
if (BlockMaxY > Y + H)
return COLLISION_BOTTOM;
if (BlockMinX >= X + W - XVel || BlockMaxX <= X + XVel)
return COLLISION_SIDE;
}
}
}
return -1;
}
void Draw()
{
fill(255, 0, 0);
stroke(0);
pushMatrix();
translate(X + Cam.X, Y + Cam.Y);
rect(0, 0, W, H);
popMatrix();
noStroke();
}
}[/cpp]
This is my Entity class, used for the player in my MC ripoff (made in Processing). The collision is partly working, but still very glitchy. Would any physics-guru help me out here?
I am using XNA, I am making an asteroids clone and I started implementing the shooting, but it does't work as expected, even after hours of trying too fix the problem. Maybe you guys can help.
[cpp]
protected override void Update(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Up))
{
Player.velocity += new Vector2((float)Math.Cos(Player.rotation),
(float)Math.Sin(Player.rotation)) * 0.1f;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
Player.velocity -= new Vector2((float)Math.Cos(Player.rotation),
(float)Math.Sin(Player.rotation)) * 0.1f;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
Player.rotation -= 0.08f;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
Player.rotation += 0.08f;
}
if (keyboardState.IsKeyDown(Keys.Space) && previouskeyboardstate.IsKeyUp(Keys.Space))
{
foreach (GameObject Bullet in Bullets)
{
if (!Bullet.alive)
{
Bullet.alive = true;
Bullet.position = Player.position - Bullet.center;
Bullet.velocity = new Vector2((float)Math.Cos(Player.rotation),
(float)Math.Sin(Player.rotation)) * 10.0f;
return;
}
}
}
updatepositions();
previouskeyboardstate = keyboardState;
base.Update(gameTime);
}
public void updatepositions()
{
//Player///////////////////////////////////////////////
Player.position += Player.velocity;
if (Player.position.Y <= 0)
{
Player.position.Y = graphics.GraphicsDevice.Viewport.Height - 1;
}
if (Player.position.Y >= graphics.GraphicsDevice.Viewport.Height)
{
Player.position.Y = 1;
}
if (Player.position.X >= graphics.GraphicsDevice.Viewport.Width)
{
Player.position.X = 1;
}
if (Player.position.X <= 0)
{
Player.position.X = graphics.GraphicsDevice.Viewport.Width - 1;
}
foreach (GameObject Bullet in Bullets)
{
if (Bullet.alive)
{
Bullet.position += Bullet.velocity;
if (!viewportrect.Contains(new Point((int)Bullet.position.X, (int)Bullet.position.Y)))
{
Bullet.alive = false;
continue;
}
}
}
[/cpp]
Hey guys, I'm working on a C# program that basically takes all the steps [url=http://www.idroidproject.org/wiki/Installing_iDroid_%28Linux%29]here[/url] and doing them automatically. I've pretty much got everything done, I just need to mount android.img and toss the firmware here. Android.img is an ext2 filesystem. Anyone got a clue how to mount/extract an ext2 filesystem from file? (Ext2FSD only works with HDD partitions and Ext2IFS doesn't want to run on 7)
[QUOTE=turb_;21894550]Don't do that. You'll get confused with stuff like LINQ to Objects/SQL/whatever.
Instead, read up on lambda expressions and extension methods, and let Intellisense help you use the LINQ extension methods.[/QUOTE]
There's no difference between LINQ to Objects/SQL/whatever.
The only difference with LINQ to SQL is that the queries are translated to SQL statements at compile-time.
I'm attempting to write a simple file transfer (over TCP sockets) application. But the file being written isn't the same. The written file is the same length, but is different.
[url=http://pastebin.com/axVqVfpJ]pastebin[/url]
Any help on why what I send is not what I receive and write?
[QUOTE=xxxkiller;21900561]I am using XNA, I am making an asteroids clone and I started implementing the shooting, but it does't work as expected, even after hours of trying too fix the problem. Maybe you guys can help.
[cpp]
protected override void Update(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Up))
{
Player.velocity += new Vector2((float)Math.Cos(Player.rotation),
(float)Math.Sin(Player.rotation)) * 0.1f;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
Player.velocity -= new Vector2((float)Math.Cos(Player.rotation),
(float)Math.Sin(Player.rotation)) * 0.1f;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
Player.rotation -= 0.08f;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
Player.rotation += 0.08f;
}
if (keyboardState.IsKeyDown(Keys.Space) && previouskeyboardstate.IsKeyUp(Keys.Space))
{
foreach (GameObject Bullet in Bullets)
{
if (!Bullet.alive)
{
Bullet.alive = true;
Bullet.position = Player.position - Bullet.center;
Bullet.velocity = new Vector2((float)Math.Cos(Player.rotation),
(float)Math.Sin(Player.rotation)) * 10.0f;
return;
}
}
}
updatepositions();
previouskeyboardstate = keyboardState;
base.Update(gameTime);
}
public void updatepositions()
{
//Player///////////////////////////////////////////////
Player.position += Player.velocity;
if (Player.position.Y <= 0)
{
Player.position.Y = graphics.GraphicsDevice.Viewport.Height - 1;
}
if (Player.position.Y >= graphics.GraphicsDevice.Viewport.Height)
{
Player.position.Y = 1;
}
if (Player.position.X >= graphics.GraphicsDevice.Viewport.Width)
{
Player.position.X = 1;
}
if (Player.position.X <= 0)
{
Player.position.X = graphics.GraphicsDevice.Viewport.Width - 1;
}
foreach (GameObject Bullet in Bullets)
{
if (Bullet.alive)
{
Bullet.position += Bullet.velocity;
if (!viewportrect.Contains(new Point((int)Bullet.position.X, (int)Bullet.position.Y)))
{
Bullet.alive = false;
continue;
}
}
}
[/cpp][/QUOTE]
It would help if you actually told us the problem.
Sorry, you need to Log In to post a reply to this thread.