Show Posts
|
|
Pages: [1] 2 3
|
|
1
|
Java Game APIs & Engines / J2ME / loading resources from another module on a Blackberry
|
on: 2005-09-07 18:52:26
|
Hello Let's say I have two code modules installed on my Blackberry: module A and module B. Module B is full of images and config files, while module A is full of code. It has been posted on the Blackberry Dev forum that code in module A can access resources in module B in the following way: 1
| Class.forName("com.class.in.module.B").getResourceAsStream("/imageB.png"); |
I have had varied success with this method, and somehow deleted the only version of my code and project that worked. Does anyone have experience loading resources from other code modules on the Blackberry? If so I'd be all ears. Ideally module B will be created sometime after the application is deployed and module A would have no declared dependency on module B. For reference here is the original thread from the Blackberry forum. (I hope it's cool to post it here) http://www.blackberry.com/developers/forum/thread.jsp?forum=1&thread=4697 Cheers!
|
|
|
|
|
3
|
Java Game APIs & Engines / Java 2D / finding the points along a QuadCurve2D?
|
on: 2005-04-08 19:39:58
|
|
Hello
I need to draw a special arrow for our system today and it kinda looks like this: )--->
That ascii arrow is where I am at now. Given two points and a value for width I draw an arrow from tip to tail with a curve who's end points are width pixels apart. My curve is a QuadCurve2D.Float and by carefully choosing my control point the curve intersects the end of the arrow.
The tricky part comes next: drawing line segments off the curve which are perpendicular to the arrow. How can I find the points on the curve? Should I even bother?
My only idea thus far is to define a clip that is my existing curve, an identical curve transformed behind my arrow and two lines connecting their end points. That clip will allow me to draw line segments of equal length off the back of my visible curve, but I'd still like to know how to find those points.
Any thoughts appreciated. thanks!
|
|
|
|
|
4
|
Discussions / General Discussions / Re: Where Are You?
|
on: 2005-03-17 17:43:47
|
Washington D.C. a half a mile from the White House. Bethesda Software ( http://www.bethsoft.com/) is in Rockville Maryland 30 minutes from downtown DC, but that's the only game company I know of around here. If you want to do graphics in Java you'll probaby be working on a military simulation - which is what I'm doing now.
|
|
|
|
|
6
|
Game Development / Newbie & Debugging Questions / Re: Reading BMPs in ImageIO
|
on: 2004-06-29 00:15:19
|
NeHe ( http://nehe.gamedev.net/) tutorial 8, as ported to Java by Abdul and Pepijn ( http://pepijn.fab4.be/nehe/), contains a rather handy class. 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
| package demos.common;
import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.DataBufferInt; import java.io.IOException; import java.io.InputStream;
public class BitmapLoader { public static BufferedImage loadBitmap(String file) throws IOException { BufferedImage image; InputStream input = null; try { input = ResourceRetriever.getResourceAsStream(file);
int bitmapFileHeaderLength = 14; int bitmapInfoHeaderLength = 40;
byte bitmapFileHeader[] = new byte[bitmapFileHeaderLength]; byte bitmapInfoHeader[] = new byte[bitmapInfoHeaderLength];
input.read(bitmapFileHeader, 0, bitmapFileHeaderLength); input.read(bitmapInfoHeader, 0, bitmapInfoHeaderLength);
int nSize = bytesToInt(bitmapFileHeader, 2); int nWidth = bytesToInt(bitmapInfoHeader, 4); int nHeight = bytesToInt(bitmapInfoHeader, 8); int nBiSize = bytesToInt(bitmapInfoHeader, 0); int nPlanes = bytesToShort(bitmapInfoHeader, 12); int nBitCount = bytesToShort(bitmapInfoHeader, 14); int nSizeImage = bytesToInt(bitmapInfoHeader, 20); int nCompression = bytesToInt(bitmapInfoHeader, 16); int nColoursUsed = bytesToInt(bitmapInfoHeader, 32); int nXPixelsMeter = bytesToInt(bitmapInfoHeader, 24); int nYPixelsMeter = bytesToInt(bitmapInfoHeader, 28); int nImportantColours = bytesToInt(bitmapInfoHeader, 36);
if (nBitCount == 24) { image = read24BitBitmap(nSizeImage, nHeight, nWidth, input); } else if (nBitCount == 8) { image = read8BitBitmap(nColoursUsed, nBitCount, nSizeImage, nWidth, nHeight, input); } else { System.out.println("Not a 24-bit or 8-bit Windows Bitmap, aborting..."); image = null; } } finally { try { if (input != null) input.close(); } catch (IOException e) { } } return image; }
private static BufferedImage read8BitBitmap(int nColoursUsed, int nBitCount, int nSizeImage, int nWidth, int nHeight, InputStream input) throws IOException { int nNumColors = (nColoursUsed > 0) ? nColoursUsed : (1 & 0xff) << nBitCount;
if (nSizeImage == 0) { nSizeImage = ((((nWidth * nBitCount) + 31) & ~31) >> 3); nSizeImage *= nHeight; }
int npalette[] = new int[nNumColors]; byte bpalette[] = new byte[nNumColors * 4]; readBuffer(input, bpalette); int nindex8 = 0;
for (int n = 0; n < nNumColors; n++) { npalette[n] = (255 & 0xff) << 24 | (bpalette[nindex8 + 2] & 0xff) << 16 | (bpalette[nindex8 + 1] & 0xff) << 8 | (bpalette[nindex8 + 0] & 0xff);
nindex8 += 4; }
int npad8 = (nSizeImage / nHeight) - nWidth; BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_ARGB); DataBufferInt dataBufferByte = ((DataBufferInt) bufferedImage.getRaster().getDataBuffer()); int[][] bankData = dataBufferByte.getBankData(); byte bdata[] = new byte[(nWidth + npad8) * nHeight];
readBuffer(input, bdata); nindex8 = 0;
for (int j8 = nHeight - 1; j8 >= 0; j8--) { for (int i8 = 0; i8 < nWidth; i8++) { bankData[0][j8 * nWidth + i8] = npalette[((int) bdata[nindex8] & 0xff)]; nindex8++; } nindex8 += npad8; }
return bufferedImage; }
private static BufferedImage read24BitBitmap(int nSizeImage, int nHeight, int nWidth, InputStream input) throws IOException { int npad = (nSizeImage / nHeight) - nWidth * 3; if (npad == 4 || npad < 0) npad = 0; int nindex = 0; BufferedImage bufferedImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_4BYTE_ABGR); DataBufferByte dataBufferByte = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()); byte[][] bankData = dataBufferByte.getBankData(); byte brgb[] = new byte[(nWidth + npad) * 3 * nHeight];
readBuffer(input, brgb);
for (int j = nHeight - 1; j >= 0; j--) { for (int i = 0; i < nWidth; i++) { int base = (j * nWidth + i) * 4; bankData[0][base] = (byte) 255; bankData[0][base + 1] = brgb[nindex]; bankData[0][base + 2] = brgb[nindex + 1]; bankData[0][base + 3] = brgb[nindex + 2]; nindex += 3; } nindex += npad; }
return bufferedImage; }
private static int bytesToInt(byte[] bytes, int index) { return (bytes[index + 3] & 0xff) << 24 | (bytes[index + 2] & 0xff) << 16 | (bytes[index + 1] & 0xff) << 8 | bytes[index + 0] & 0xff; }
private static short bytesToShort(byte[] bytes, int index) { return (short) (((bytes[index + 1] & 0xff) << 8) | (bytes[index + 0] & 0xff)); }
private static void readBuffer(InputStream in, byte[] buffer) throws IOException { int bytesRead = 0; int bytesToRead = buffer.length; while (bytesToRead > 0) { int read = in.read(buffer, bytesRead, bytesToRead); bytesRead += read; bytesToRead -= read; } } } |
|
|
|
|
|
9
|
Java Game APIs & Engines / Java 2D / Re: custom Scrollable Component?
|
on: 2004-06-23 15:56:26
|
|
The image is a representation of float data already in memory. In this example, the 22meg image would have been generated via a simple algorithm from 2.4meg (33*20,000*4) of floats.
> 60000*100*4 is only 22meg or so - whats the problem with loading the entire image in (if its from a file)?
Perhaps I'm being overly pessimistic. I'll give this a try with the complete image and standard Swing, and post my results.
|
|
|
|
|
10
|
Java Game APIs & Engines / Java 2D / custom Scrollable Component?
|
on: 2004-06-23 00:23:10
|
|
Hello
The task before me is to scroll a HUGE image, preferably using Swing. The image is going to be on the order of 100 pixels by 60,000 pixels. I don't want to create an image that big or create a bunch of tiles that add up to it, but rather generate the visible portion of the image on the fly. Basically I want a JTable-like widget that only draws what is visible and scrolls quickly. In fact JTable would be useful if I could actually get cells that were 3 pixels by 3 pixels with absolutely no space between them. [ JTable.setIntercellSpacing( new Dimension( 0, 0 ) ) leads to intercell spacing of 2 pixels. perhaps I'm using that wrong. ]
I'm fairly confident I can do this with a custom Component wired to a JScrollbar through an AdjustmentListener. I had hoped to find examples somewhere that used JScrollPane and a custom widget, but nothing is coming up.
Any thoughts or pointers to examples appreciated.
- duncan
|
|
|
|
|
12
|
Game Development / Newbie & Debugging Questions / Re: trouble loading images in Applet
|
on: 2004-05-27 23:59:14
|
Regarding forward & backward slashes... Try something like java.io.File.separator (a String) or java.io.File.separatorChar (a primitive char).
In case you still care :-/ The thing is, the slashes I'm talking about are in the HTML of the page that launches my applet  But I suppose if I was writing a JSP and used File.separatorChar I'd get the correct slash and things would be cool.
|
|
|
|
|
13
|
Game Development / Newbie & Debugging Questions / Re: trouble loading images in Applet
|
on: 2004-05-27 23:56:44
|
Might be the ImageIO bug. You could try the following workaround in the second block: 1
| background = ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("logo2.jpg") )); |
I tried this (with the right filename ), but background remains null after the try and then the code blows up. Wild. Well, I'm off 'til Tuesday celebrating my new Junior Member status all weekend! woohoo! /duncan
|
|
|
|
|
14
|
Game Development / Newbie & Debugging Questions / Re: trouble loading images in Applet
|
on: 2004-05-27 19:42:31
|
You aren't using ImageIO so it could be something completely different. Actually I am, in the second block of code. That same code always works from a doubleclick jar which contains the jpg, but it seems that something is different when it's run in the browser. Again, it works in the appletviewer, so I suppose it's the plugin JVM or IE generally mucking with me.
|
|
|
|
|
16
|
Game Development / Newbie & Debugging Questions / Re: trouble loading images in Applet
|
on: 2004-05-26 01:13:21
|
|
Gross! The problem I was having with Applet.getImage(...) was that I wasn't using forward slashes!
Wrong: <PARAM name="splash_image" value="splash/images/tgen_logo2.jpg"> Correct: <PARAM name="splash_image" value="splash\\images\\tgen_logo2.jpg">
I still have no idea why I couldn't load from the jar, but now it doesn't really matter.
Cheers!
|
|
|
|
|
17
|
Game Development / Newbie & Debugging Questions / trouble loading images in Applet
|
on: 2004-05-26 00:06:33
|
UPDATE: both methods currently work in the AppletViewer but not Internet ExploderI am having a heck of a time loading images in my applet. Since the project has just begun, I'm launching from a directory on my computer. Using JRE version 1.4.2 Java HotSpot(TM) Client VM, No proxy. The first problem I noticed is that Applet.getDocumentBase() is returning the wrong thing. It's supposed to return a URL to the directory that the HTML page lives in, but no, it returns a URL to the HTML page itself. Fine, I'll cull the real base out of the URL. I put together a real document base URL and try Applet.getImage(). It never worked. I draw the image and there's just nothing there. I switched my code around to use a MediaTracker ala nonnus29's game tutorial ( thanks! ) but now I just know that I have errors. I'm really stuck on this one. How do I find out what the error was? Why is my documentBase screwed up? Perhaps even more interesting is what happens when I load the image from the jar. Using my old fade-in Splash Screen code I got some weird results. It opens the image and "reads" it without error, but the image loaded is mostly screwed up. You can tell it came from my image file, but more than half of it looks like stretched out pixels. But it fades in nicely! Sure glad to see my rendering loop and bufferstrategy work cause not much else is. this always prints "Error loading Image" 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
| URL docBase = getDocumentBase(); String docPath = docBase.toString(); if ( !docPath.endsWith("/") ) { int pos = docPath.lastIndexOf('/'); docPath = docPath.substring(0, pos ); } URL realDocBase = null; try { realDocBase = new URL( docPath ); } catch (MalformedURLException mfux ) { mfux.printStackTrace(); throw new RuntimeException( "cannot open images" ); } System.out.println( realDocBase ); MediaTracker t = new MediaTracker (this); image = getImage(realDocBase,getParameter("splash_image" )); t.addImage(image,0); try { t.waitForAll(); } catch (InterruptedException e) {} if (t.isErrorAny()) { System.out.println("Error loading Image"); } |
And this loads a broken image from the jar 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
| private void loadImage() { BufferedImage background = null; URL imageURL = getClass().getClassLoader().getResource( "logo2.jpg"); try { background = ImageIO.read( imageURL );
} catch ( IOException e ) { System.err.println( "couldn't load image" ); e.printStackTrace(); } compatible = graphicsConfig .createCompatibleImage(background.getWidth(), background.getHeight(), Transparency.BITMASK);
backBuffer = graphicsConfig .createCompatibleImage(background.getWidth(), background.getHeight(), Transparency.BITMASK);
Graphics g = null; try { g = compatible.getGraphics(); g.drawImage(background, 0, 0, null); } finally { if (g != null) g.dispose(); } background = null;
} |
Thanks!
|
|
|
|
|
20
|
Game Development / Artificial Intelligence / Re: [AI] Java Rules engines - especially Rete-base
|
on: 2004-04-19 18:26:33
|
For what it's worth I used JESS on a project about 3 years ago. We optimized our code to death and it was still too slow for needs. We were running an extraction engine on a news feed and the JESS component was at the end of the pipeline. Ultimately we decided on a homegrown XML rule language which was translated into compilable code giving us the flexibility of a rule language and the speed of a hardcoded solution. The inventor of RETE has made a RETE2 algorithm which he claims is faster for large amounts of data. Rete II: http://www.pst.com/rete2.htm OPSJ: http://www.pst.com/opsjbro.htm ( the java based Rete II engine ) There's a development license but no runtime license. Might be worth looking into.
|
|
|
|
|
21
|
Discussions / Miscellaneous Topics / Re: building my own computer
|
on: 2004-04-19 18:05:51
|
Also, don't worry about FSB above 400MHz I'm curious to hear your reasoning on the FSB, as it sets the speed limit for memory and all future upgrades. Which leads to another of the Athlon64's advantages: since it has a memory controller on dye, it can run the board at the frequency of the chip ( or at least the memory. ) I don't have my special issue of MaximumPC in front of me, but I was reading about it last month in their "355 PC questions answered" issue.
|
|
|
|
|
22
|
Discussions / Miscellaneous Topics / Re: building my own computer
|
on: 2004-04-19 15:59:04
|
|
Raghar
> I thought about 2.4 GHz Celleron, or a low end prescott.
The Prescotts have not been getting the kind of press I'd like to see. Basically they aren't any faster than the cheaper Northwoods.
> 256 MB RAM
You'll want at least 512 if you are running XP and want performance. I want a gig so I can edit video or run doom3 in memory.
> How long is waranty for that memory?
That's a good question, and I don't know, but Cosair seems to be the quality reccomendation across the board.
> Actually I don't understand why do you want both DVD burner and CD-RW?
There's no good reason, just the convience of two trays for copying.
|
|
|
|
|
23
|
Discussions / Miscellaneous Topics / Re: building my own computer
|
on: 2004-04-15 19:00:36
|
I *almost* built a new computer recently, but I choose to wait. Here are the specs and then my reasons for waiting. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Case: Lian Li PC-65 PC Case w/ 480W or higher name brand PSU - $191 Processor: Athlon 64 3400+ Retail - $423 Cooling: Retail Heatsink/Fan - $0 Motherboard: ASUS K8V Deluxe - $139 Memory: 2 X 512-MB Corsair TwinX XMS DDR400/PC3200LL - $249 Hard Drive: Western Digital 250 GB 7200RPM SATA - $212 Video Card: ATI Radeon 9800 Pro- $421 Sound Card: SoundBlaster Audigy Platinum 2 ZS Retail - $166 Speakers: Logitech Z-680 5.1 - $284 CD-ROM/DVD-RW: NEC 8X DVD+RW/-RW ND-2500A OEM - $121 CD-RW: Lite-On 52x32x52 CD-RW LTR-52327S - $33 Mouse: MSFT Explorer - $30 Keyboard: Microsoft Natural Multimedia Keyboard - $20 Operating System: Windows XP Pro - $134 Total: USD 2423 |
It's been years, so I was going all out. I've decided to wait a bit for three reasons: - DDR2 compatible mobos
- PCI Express compatible mobos
- BTX form factor
That's three big changes set to hit the market any days now. Will I buy DDR2 memory? No. Will I get a PCI Express Graphics card? Maybe! Do I want a motherboard that is ready to upgrade to that stuff? Definitely. I'd love to hear opinions on this. I really need a new machine at home and I'm trying to draw the line between upgradable and not-worth-waiting for.
|
|
|
|
|
26
|
Game Development / Newbie & Debugging Questions / Re: find all implementors of an interface?
|
on: 2004-04-07 21:00:59
|
Good point. This version of ClassPath fixes that issue ( use with SubClasses above ) by scanning the manifest and digging through any Class-Path entries. note: javac assumes Class-Path entries are relative to the path of the jar they are in, so I do as well!! I have tested this with a main.jar, a second.jar referenced in main's manifest, and a third.jar referenced in second.jar's manifest. 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
| import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; import java.util.zip.ZipEntry; import java.util.zip.ZipFile;
public class ClassPath { public static final int SILENT = 0; public static final int QUIET = 1; public static final int VERBOSE = 2; public static final String JAR_EXT = ".jar"; public static final String ZIP_EXT = ".zip"; public static final String CLASS_EXT = ".class";
private ArrayList classNames; private int outputLevel; public ClassPath() { this( SILENT ); } public ClassPath(int type) { super(); outputLevel = type; findAllClassNames(); } public Iterator classNameIterator() { return classNames.iterator(); } public ArrayList getClassNames() { return classNames; }
private void findAllClassNames() { String path = null; classNames = new ArrayList(); try { path = System.getProperty( "java.class.path" ); } catch ( Exception x ) { x.printStackTrace(); } if ( outputLevel != SILENT ) System.out.println( "scanning classpath: " + path ); StringTokenizer toke = new StringTokenizer( path, File.pathSeparator ); analyzeClasspathTokens( toke );
if ( outputLevel != SILENT ) System.out.println( "found " + classNames.size() + " classes." ); if ( outputLevel == VERBOSE ) { Iterator i = classNames.iterator(); while ( i.hasNext() ) { String name = (String)i.next(); System.out.println( name ); } } } private void addClass( File classFile ) { classNames.add( getClassNameFrom( classFile.getName() ) ); } private void addJarContents( File jarFile ) { JarFile jar = null; try { jar = new JarFile( jarFile );
} catch ( IOException iox ) { } if ( jar != null ) { Manifest man = null; try { man = jar.getManifest(); } catch( IOException iox ) { System.err.println("error obtaining manifest from: " + jar.getName()); } finally { if ( man != null ) { this.scanClasspath( man, jar, jarFile ); } } Enumeration e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = (JarEntry)e.nextElement(); if ( !entry.isDirectory() && entry.getName().endsWith( CLASS_EXT ) ) { String className = getClassNameFrom( entry.getName() ); classNames.add( className );
} }
} } private void addZipContents( File zipFile ) { ZipFile zip = null; try { zip = new JarFile( zipFile ); } catch ( IOException iox ) { } if ( zip != null ) { Enumeration e = zip.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry)e.nextElement(); if ( !entry.isDirectory() && entry.getName().endsWith( CLASS_EXT ) ) { String className = getClassNameFrom( entry.getName() ); classNames.add( className ); } } } }
private void addDirectoryContents( File dir ) { File files[] = dir.listFiles(); for( int i = 0; i < files.length ; ++i ) { File f = files[i]; if ( f.isDirectory() ) { addDirectoryContents( "", f ); } else { if ( f.getName().endsWith( CLASS_EXT ) ) addClass( f ); } } } private void addDirectoryContents( String pathTo, File dir ) { String pathToHere = pathTo + dir.getName() + File.separator; File files[] = dir.listFiles(); for( int i = 0; i < files.length ; ++i ) { File f = files[i]; if ( f.isDirectory() ) { addDirectoryContents( pathToHere, f ); } else { if ( f.getName().endsWith( CLASS_EXT ) ) { String absFilePath = pathToHere + f.getName(); classNames.add( getClassNameFrom( absFilePath ) ); } } } } private void analyzeClasspathTokens( StringTokenizer toke ) { while ( toke.hasMoreTokens()) { String pathElement = toke.nextToken(); analyzeClasspathElement( pathElement );
} } private void analyzeClasspathElement( String pathElement ) { File elementFile = new File( pathElement ); String elementName = elementFile.getAbsolutePath(); if ( elementName.endsWith( JAR_EXT ) ) { addJarContents( elementFile ); } else if ( elementName.endsWith( ZIP_EXT ) ) { addZipContents( elementFile ); } else if ( elementName.endsWith( CLASS_EXT ) ) { addClass( elementFile ); } else { addDirectoryContents( elementFile ); } } private String getClassNameFrom( String entryName ) { String foo = new String(entryName).replace( '/', '.' ); foo = foo.replace( '\\', '.' ); return foo.substring( 0, foo.lastIndexOf( '.' ) ); } private void scanClasspath( Manifest man, JarFile jar, File jarFile ) { Map map = man.getEntries(); if ( map != null ) { Attributes atts = man.getMainAttributes(); if ( atts != null ) { Set keys = atts.keySet(); Iterator i = keys.iterator(); while ( i.hasNext() ) { Object key = (Object)i.next(); String value = (String)atts.get( key ); if ( outputLevel == VERBOSE ) System.out.println( jar.getName() + " " + key + ": " + value ); if ( key.toString().equals( "Class-Path" )) { if ( outputLevel != SILENT ) System.out.println( "scanning " + jar.getName() +"'s manifest classpath: " + value); StringTokenizer toke = new StringTokenizer( value ); while (toke.hasMoreTokens() ) { String element = toke.nextToken(); if ( jarFile.getParent() == null ) analyzeClasspathElement( element ); else { analyzeClasspathElement( jarFile.getParent() + File.separator + element ); } } } } } } }
} |
|
|
|
|
|
27
|
Game Development / Newbie & Debugging Questions / Re: find all implementors of an interface?
|
on: 2004-04-06 23:07:53
|
Not exactly a timely post, but here is code to find all subclasses of a class (or all implementors of an interface) on your classpath. To use it, add these classes to your classpath and from somewhere in your program call: Class[] interestingClasses = Subclasses.of( MyInterface.class ); // or add a regex Class[] interestingClasses = Subclasses.of( MyClass.class, ".*Suffix$ ); Once you have the array of classes you can instantiate them with relfection, but I beleive that only works if your class has an empty constructor. 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
| import java.util.ArrayList; import java.util.Iterator;
public class Subclasses { public static Class[] of( Class targetType ) { return of( targetType, ".*" ); } public static Class[] of( Class targetType, String regex ) { ArrayList matches = new ArrayList(); ClassPath cp = new ClassPath(); Iterator i = cp.classNameIterator(); while ( i.hasNext() ) { String className = (String)i.next(); if ( className.matches( regex ) && !className.equals( targetType.getName() ) ) { Class clazz = null; try { clazz = Class.forName( className ); } catch (ClassNotFoundException cnfx ) { continue; } finally { if ( clazz != null && targetType.isAssignableFrom( clazz ) ) { matches.add( clazz ); } } } } return (Class[])matches.toArray( new Class[0] ); } } |
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
| import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.StringTokenizer; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.zip.ZipEntry; import java.util.zip.ZipFile;
public class ClassPath { public static final int SILENT = 0; public static final int QUIET = 1; public static final int VERBOSE = 2; public static final String JAR_EXT = ".jar"; public static final String ZIP_EXT = ".zip"; public static final String CLASS_EXT = ".class";
private ArrayList classNames; private int outputLevel; public ClassPath() { this( SILENT ); } public ClassPath(int type) { super(); outputLevel = type; findAllClassNames(); } public Iterator classNameIterator() { return classNames.iterator(); } public ArrayList getClassNames() { return classNames; }
private void findAllClassNames() { String path = null; classNames = new ArrayList(); try { path = System.getProperty( "java.class.path" ); } catch ( Exception x ) { x.printStackTrace(); } if ( outputLevel != SILENT ) System.out.println( "scanning classpath: " + path ); StringTokenizer toke = new StringTokenizer( path, File.pathSeparator ); while ( toke.hasMoreTokens()) { String pathElement = toke.nextToken(); File elementFile = new File( pathElement ); String elementName = elementFile.getAbsolutePath(); if ( elementName.endsWith( JAR_EXT ) ) { addJarContents( elementFile ); } else if ( elementName.endsWith( ZIP_EXT ) ) { addZipContents( elementFile ); } else if ( elementName.endsWith( CLASS_EXT ) ) { addClass( elementFile ); } else { addDirectoryContents( elementFile ); }
} if ( outputLevel != SILENT ) System.out.println( "found " + classNames.size() + " classes." ); if ( outputLevel == VERBOSE ) { Iterator i = classNames.iterator(); while ( i.hasNext() ) { String name = (String)i.next(); System.out.println( name ); } } } private void addClass( File classFile ) { classNames.add( getClassNameFrom( classFile.getName() ) ); } private void addJarContents( File jarFile ) { JarFile jar = null; try { jar = new JarFile( jarFile );
} catch ( IOException iox ) { } if ( jar != null ) { Enumeration e = jar.entries(); while (e.hasMoreElements()) { JarEntry entry = (JarEntry)e.nextElement(); if ( !entry.isDirectory() && entry.getName().endsWith( CLASS_EXT ) ) { String className = getClassNameFrom( entry.getName() ); classNames.add( className );
} } } } private void addZipContents( File zipFile ) { ZipFile zip = null; try { zip = new JarFile( zipFile ); } catch ( IOException iox ) { } if ( zip != null ) { Enumeration e = zip.entries(); while (e.hasMoreElements()) { ZipEntry entry = (ZipEntry)e.nextElement(); if ( !entry.isDirectory() && entry.getName().endsWith( CLASS_EXT ) ) { String className = getClassNameFrom( entry.getName() ); classNames.add( className ); } } } }
private void addDirectoryContents( File dir ) { File files[] = dir.listFiles(); for( int i = 0; i < files.length ; ++i ) { File f = files[i]; if ( f.isDirectory() ) { addDirectoryContents( "", f ); } else { if ( f.getName().endsWith( CLASS_EXT ) ) addClass( f ); } } } private void addDirectoryContents( String pathTo, File dir ) { String pathToHere = pathTo + dir.getName() + File.separator; File files[] = dir.listFiles(); for( int i = 0; i < files.length ; ++i ) { File f = files[i]; if ( f.isDirectory() ) { addDirectoryContents( pathToHere, f ); } else { if ( f.getName().endsWith( CLASS_EXT ) ) { String absFilePath = pathToHere + f.getName(); classNames.add( getClassNameFrom( absFilePath ) ); } } } } private String getClassNameFrom( String entryName ) { String foo = new String(entryName).replace( '/', '.' ); foo = foo.replace( '\\', '.' ); return foo.substring( 0, foo.lastIndexOf( '.' ) ); }
} |
|
|
|
|
|
29
|
Game Development / Newbie & Debugging Questions / Re: How to get in to Java games programming?
|
on: 2004-04-02 00:03:09
|
|
To add to the good advice above I'll say that I learned a LOT by reading these forums, searching the archives, and picking apart sample code found on the site.
There are many technologies out there and choosing one can be really tough if you want your game to run on many platforms. I agree with a previous poster that you should start 2D, and that J2SE is a nice place to start using the Java2D APIs.
Finally I'll plug a great book in line with my suggestion: Vincent J. Hardy's "Java 2D API Graphics" ISBN 0-13-014266-2. Easy to read with great pictures although it never mentions game programming.
|
|
|
|
|
30
|
Discussions / Miscellaneous Topics / Re: Sun Makes Linux 3D
|
on: 2004-04-01 23:39:44
|
I am actually referring in a very straightforward, straightfaced and honest way to women, who are genuinely wired differently. Present a 3D interface to Jane Doe and she'll just be totally baffled. My girlfriend gets sick to her stomach just watching me play Quake3. She would never in a million years want a 3D desktop. I do though! putting windows far into the screen seems kinda cool. No more guessing what percent-size to view your document at, just slide your window in or out. I can see Photoshop and the like being more fun to use with the ability to see things overlaid just by moving windows around. Rendering your background onto a surface might be cool too. All eye candy though.
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|