Archive for the ‘Java’ Category

Java Geek Humor

Monday, March 10th, 2008

I’ve come across a really funny dialog here:

- I am looking for a downloadable java written practical test. We are in the process of employing some new staff and we need a test of some sorts to gauge the skills.
- Hey pls let me know ur company name…. ;-)
So that I can also apply…
- Upon reviewing your posting record we recommend you apply to
www.mcdonalds.com

RIA’s war

Thursday, August 2nd, 2007

RIA warIsn’t this me-too RIA buzz a little bit naive? A long time ago Java “invented” applets - a great idea for RIA that has gracefully failed. Flash has taken the seat. MS’s silverlight is not getting developers anywhere for too many reasons. JavaFX requires the latest Java JVM installed on the user’s computer. I believe in the power of the Sun, but the UI is not their strongest side. Flex is the only way to go if you want to have a good-looking RIA application. Almost everybody has it installed and it is a really quick download if you don’t have it. Great look, nice cooperation with J2EE technology.

Fake it!

Friday, July 13th, 2007

Sometimes you need test data to... test some class or jsp page. Here is the java class that can be of help to someone.



JAVA:

  1. import org.apache.commons.lang.RandomStringUtils;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.commons.lang.math.RandomUtils;
  4.  
  5. public class Fake {
  6.  
  7.     public static String getAddress() {
  8.         String str = RandomStringUtils.randomNumeric(2) + "/"
  9.                 + RandomStringUtils.randomNumeric(3);
  10.         String streetName = get(7) + 8;
  11.         streetName = StringUtils.capitalise(streetName);
  12.         str += " ";
  13.         str += streetName;
  14.         return str;
  15.     }
  16.  
  17.     public static String getWord() {
  18.         String streetName = RandomStringUtils.randomAlphabetic(7).toLowerCase();
  19.         streetName = StringUtils.capitalise(streetName);
  20.         return streetName;
  21.     }
  22.  
  23.     public static String get(int maxLength) {
  24.         int l = RandomUtils.nextInt(maxLength);
  25.         return RandomStringUtils.randomAlphabetic(maxLength + 3).toLowerCase();
  26.     }
  27.    
  28.     public static String getSentence(int numberOfWords) {
  29.         StringBuffer sb = new StringBuffer();
  30.         for (int i = 0; i <numberOfWords; i++) {
  31.             sb.append(get(16));
  32.             sb.append(" ");
  33.         }
  34.         return StringUtils.capitalise(sb.toString());
  35.     }
  36.  
  37.     public static String getEmail() {
  38.         StringBuilder sb = new StringBuilder(get(4));
  39.         sb.append("@");
  40.         sb.append(get(4));
  41.         sb.append(".");
  42.         sb.append(RandomStringUtils.randomAlphabetic(3).toLowerCase());
  43.         return sb.toString();
  44.  
  45.     }
  46.  
  47.     public static int getId() {
  48.         return RandomUtils.nextInt(99999);
  49.     }
  50.  
  51.     public static String getPhoneNumber() {
  52.         return RandomStringUtils.randomNumeric(12);
  53.     }
  54.  
  55.     public static String getPostalCode() {
  56.         return RandomStringUtils.randomNumeric(7);
  57.     }
  58.  
  59.     public static String getState() {
  60.         return RandomStringUtils.randomAlphabetic(3).toUpperCase();
  61.     }
  62. }

Java 3D Application in JavaScript (Irrlicht & Jirr)

Tuesday, April 24th, 2007

With the new features of Java 1.6 it is incredibly easy to add scripting capabilities to your programs. By using JavaScript you do not have to recompile your application every time you change something, also JavaScript is much easier especially for those new to Java.

Check out the way we implement an Irrlicht application in JavaScript. Of course, you have to download Jirr & have JDK 1.6 installed.

This is the Java program to load and execute the JavaScript code:

JAVA:

  1. package com.javazing.gameengine;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.Reader;
  9.  
  10. import javax.script.ScriptEngine;
  11. import javax.script.ScriptEngineManager;
  12.  
  13. /**
  14. *
  15. * This program loads and executes the JavaScript program
  16. *
  17. * @author Sergey Nechaev (snechaev@javazing.com)
  18. *
  19. */
  20. public class GameEngine {
  21.  
  22.     private static final String SCRIPT_ENGINE_NAME = "js";
  23.  
  24.     private static final String SCRIPT_FILE_NAME = "script/js.js";
  25.  
  26.     private ScriptEngine jsEngine;
  27.  
  28.     private ScriptEngineManager mgr;
  29.  
  30.     static {
  31.         System.loadLibrary("irrlicht_wrap");
  32.     }
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.  
  36.         new GameEngine();
  37.  
  38.     }
  39.  
  40.     public GameEngine() throws Exception {
  41.  
  42.         mgr = new ScriptEngineManager();
  43.         jsEngine = mgr.getEngineByExtension(SCRIPT_ENGINE_NAME);
  44.  
  45.         // InputStream is = this.getClass().getResourceAsStream("js.js");
  46.         File file = new File(SCRIPT_FILE_NAME);
  47.         InputStream is = new BufferedInputStream(new FileInputStream(file));
  48.  
  49.         Reader reader = new InputStreamReader(is);
  50.         jsEngine.eval(reader);
  51.  
  52.     }
  53.  
  54. }

And the JavaScript with the 'HelloWorld' example from the Irrlicht distribution:

JavaScript:

  1. importPackage(net.sf.jirr);
  2. importPackage(java.lang);
  3.  
  4.  
  5. var device = Jirr.createDevice(E_DRIVER_TYPE.EDT_OPENGL,
  6. new dimension2di(640, 480), 16, false, false, false, null);
  7.  
  8. print("idevice = " + device);
  9.  
  10. var windowCaption = "Java@Hello World! - Irrlicht Engine Demo";
  11. device.setWindowCaption(windowCaption);
  12.  
  13. var driver = device.getVideoDriver();
  14. var smgr = device.getSceneManager();
  15. var guienv = device.getGUIEnvironment();
  16.  
  17. var text = "Hello World! This is the Irrlicht Software engine for Java!";
  18. var rect = new recti(10, 10, 280, 30);
  19. var staticText = guienv.addStaticText(text, rect, true, true, null, -1);
  20.  
  21. smgr.addCameraSceneNode(null, new vector3df(0,10,-60),
  22. new vector3df(0,0,0), -1);
  23.  
  24. var step = 100;
  25. var counter = 0;
  26. var diff /*= 0*/;
  27. var timer1 = System.currentTimeMillis();
  28. var timer2 /*= 0*/;
  29. while(device.run())
  30. {
  31.  
  32.  
  33.     var a = 0;
  34.     var r = 100;
  35.     var g = 100;
  36.     var b = 100;
  37.  
  38.     driver.beginScene(true, true, new SColor(a, r, g, b));
  39.  
  40.     smgr.drawAll();
  41.     guienv.drawAll();
  42.  
  43.     driver.endScene();
  44.  
  45.     counter++;
  46.     if (counter>= step)
  47.     {
  48.         timer2 = System.currentTimeMillis();
  49.         diff = 1000.0 / ((timer2 - timer1) / step);
  50.         counter = 0;
  51.         timer1 = System.currentTimeMillis();
  52.  
  53.         //text = Jirr.createWchar("This is the Irrlicht Software
  54.         //engine for Java! FPS: " + diff);
  55.         text = "This is the Irrlicht Software engine for Java! FPS: " + diff;
  56.         staticText.setText(text);
  57.     }
  58. }
  59.  
  60. device.closeDevice();