Java, how to use methods and constructor of the extended class

Let’s suppose you are working on class Extending which extends the class Base.
Now you want to overwrite method setName(name) but you also want to execute the code within the overwritten method plus some custom code, then just call within the Extending class

super.setName()
customcode();

If you need to do the same for the constructor just call

super()

if the extended constructor has no arguments, otherwise

super(name, surname, age, sex)
Annunci

JOGL, color picking

If you would like to be able to select objects in your scene, color picking is one possibility. Not that difficult and complex to realize.

OpenGL has two buffers, a back and a front one. This is useful to avoid that a scene not yet rendered completely could be delivered to the screen and shown to the user. So OpenGL renders in the back buffer and then “swap” all the content in the front one.

All what you have to do is simply disable the automatic buffer swapping when you are creating the canvas, by typing:

//  Disable auto swapping for buffers
gLCanvas.setAutoSwapBufferMode(autoSwapBuffers);

with autoSwapBuffers equals to false.

Then add at the end of the display() something like:


if (!autoSwapBuffers) {
    if (disableManualBufferSwapping) {
        disableManualBufferSwapping = false;
    } else {
        gLCanvas.swapBuffers();
    }
}

To manually swap buffer when the autoSwapBuffers is false. Moreover you can temporary disable it.

Supposing to have a unique pipeline containing all the elements you want to render, each of them may use its own index to generate a specific color.

Color index = new Color(-selection_index);
gl2.glColor3f(index.getRed() / 255.0f, index.getGreen() / 255.0f, 
index.getBlue() / 255.0f);

Each object will use this color to render itself only in the back buffer for picking purposes. User won’t see any strange color because we will disable temporary the buffer swapping by setting disableManualBufferSwapping to true.

Then in the mouse listener whenever the user click to select we need to:
– save the point
– set backBufferRenderingOnly to true
– refresh (I suppose you call the refresh every time you need, so no animator)

In the display() if backBufferRenderingOnly is true, we know we have to render only in the back buffer.


if (backBufferRenderingOnly) {
   //
   //  Rendering into the back buffer for color picking
   //
   Color index;
   int i;
   backBufferRenderingOnly = false;
   FloatBuffer pixels = FloatBuffer.allocate(4);
   RenderingObject object;

   //  Select the back buffer, once for all
   gl2.glDrawBuffer(GL2.GL_BACK);

   ArrayList objects = getRenderingObjects();

   if (!objects.isEmpty()) {
       /*
       * Rendering all objects.
       */
       for (int j = 0; j < objects.size(); j++) {
          ...
       }
                
       int[] viewport = new int[4];

       gl2.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);

       gl2.glReadBuffer(GL2.GL_BACK);
       gl2.glReadPixels(picking.x, viewport[3] - picking.y, 
                             1, 1, GL2.GL_RGBA, GL2.GL_FLOAT, pixels);

       index = new Color(pixels.get(0), pixels.get(1), pixels.get(2));

       i = index.getRGB();

       // -1 means click in the background
       if (i != -1) {

          // This will deselect all the previous selected items
          for (int j = 0; j < objects.size(); j++) {
              object = objects.get(j);
              object.setSelected(false);
          }

          object = getRenderingObjects().get(-i - 2);

          object.setSelected(true);
          this.selectedObject = object;
                    
       } 
       // Click user is on nothing
       else {
          this.selectedObject = null;
          for (int j = 0; j < objects.size(); j++) {
             object = objects.get(j);
             object.setSelected(false);
          }
       }
       //  Disable manual buffer swapping for one time
       disableManualBufferSwapping = true;
    }
}

And then you should be done ๐Ÿ™‚

Java, how to retrieve whatever number (int/float/etc) from a spinner

I don’t know why if I try to cast a spinner I get an error always and only at the second consecutive time:

setValue((int)spinner1.getValue(), ID1);
setValue((int)spinner1.getValue(), ID2);

First line no error, second line exception…

I solved by casting as following:

SpinnerNumberModel snm1 = (SpinnerNumberModel)spinner1.getModel();
SpinnerNumberModel snm2 =(SpinnerNumberModel)spinner2.getModel();
setValue(snm1.getNumber().floatValue());
setValue(snm2.getNumber().floatValue())

Substitute floatValue() with what you need ๐Ÿ™‚

how to look for packets to install or already installed

This is my first article since I am also using Debian (now just upgraded to Squeeze) ๐Ÿ˜€

However, since I am working only by ssh, I found out that the following two commands are very very useful to know…

Take in account that all the commands are run as root, so if you are not, you may need the ‘sudo’ in front..

If you want to search through the repository for tomcat, run an update:

apt-get update

Then you can be sure that the list you are going to look through is the most recent and therefore:

apt-cache search tomcat

This will print out all the result matching the string ‘tomcat’

Instead, if you wanna look through all the *installed* packet, then type:

dpkg --get-selections | grep tomcat

Among the result, packages with a corresponding ‘deinstall’ are those packages that have been removed without the ‘–purge’ option…

If you want to purge them all, just type:

aptitude purge $(dpkg --get-selections | grep deinstall | sed s/deinstall//)

Ps: a special thanks to Alessia for giving me a reason to continue toย publishย on this space ๐Ÿ˜€

Howto: unreal tournament 2004 on the new Ubuntu 11.04 Natty Narwhal

I assume you have already installed the game..

Since the libstdc++5 is no more in the repo, let’s download from here:

http://packages.ubuntu.com/maverick/libstdc++5

(choose your correct version, that is 32 or 64bit, and install it)

Then download the 3369-2 patch:

http://treefort.icculus.org/ut2004/ut2004-lnxpatch3369-2.tar.bz2

Copy and replace everything (default path /usr/local/games/ut2004/), if you prefer by graphical interface just run nautilus in root mode:

sudo nautilus

Now if you are on 64bit, just modify these file:

sudo gedit /usr/local/games/ut2004/ut2004

and substitute lines

if [ -x “${UT2004_DATA_PATH}/ut2004-bin” ]

exec “./ut2004-bin” $*

with

if [ -x “${UT2004_DATA_PATH}/ut2004-bin-linux-amd64” ]

exec “./ut2004-bin-linux-amd64” $*

And you are done

Ps: if you have problem with the sound, just run ut2k4 with aoss:

aoss ut2004

Hope its useful ๐Ÿ™‚

Ubuntu Maverick on Toshiba NB200

nb200

This netbook has a very particular problem, basically it stops if you don’t press any button..

I have never met such a problem

However, looking over internet, I found a easy and fast fix:

Enter in the bios, go to the processor power setting, and choose “always low” instead “dynamic”

Maybe this will not be the best for someone, but for my point of view, I have never experienced some slowness and saving battery is what I care most, so I didn’t have any problems ๐Ÿ™‚