Java, C++: ClassLoader and Qt Resource System

December 8, 2009 CallToPower Leave a comment

Here a little comparison between the Java ClassLoader and the Qt Resource System:
If you want to load an Icon and/or an Image in a Java Class (e.g. in a .jar-File) you have to do the following:

Icon icon;
Image image;
ClassLoader CLDR = this.getClass().getClassLoader();
String path = "src/to/images/name.png";
URL url = CLDR.getResource(path);
// An Icon
if (!(url == null)) {
    icon = new ImageIcon(url);
}
// An Image
try {
    iconDock = ImageIO.read(CLDR.getResource(path));
} catch (IOException e) {
    // ExceptionHandling here
}

In Qt they have another approach:
The Resources associated with an Application are specified in a XML-based .qrc File.
The specified Paths are relative to the Directory containing the .qrc File (listed Files must be located in the same Directory or any Sub-Directory).
In the Source-Code the Resources are accessible under the same Name they have in the Source Tree, with a “:/” Prefix.
Here an Example:

<RCC>
    <qresource prefix="/dir/subDir" lang="en">
        <file alias="License">subDir/FileName<file>
    <qresource>
</RCC>

In your Source Code you can access FileName (if FileName e.g. is a QFile) via

QFile file(":/dir/subdir/AliasName");

after adding the following Line to your .pro-File:

RESOURCES = yourRsrcFileName.qrc

The Option lang=”langCode” (here: “en”) is for Localization:
If you want to add another Language to your Application you have to have the File “AliasName_langCode” in your Source Tree.


Here an Example for the Java ClassLoader and here an Example for the Qt Resource System:

C++: How to Include the Boost Library in XCode Projects

December 3, 2009 CallToPower 1 comment

0. Download the boost-Library and unzip it anywhere (here: /Developer/Tools/boost”)

1. Create a Project.

2. Right-click at the Project-Name and select “Get Info”

3. Set the “Header Search Paths” to the Folder you unzipped the Library (here: /Developer/Tools/boost”)

4. Ready to boost.

Categories: Programming Tags: , , , ,

About Videos, Music and more…

November 25, 2009 CallToPower 1 comment
This posting is no longer available due to a copyright claim by a very big company.
This posting is no longer available in your country.

Thanks to Superlevel for the Idea!

Java: jCTPbundle – Useful Java Functions and Classes bundled

November 10, 2009 CallToPower 2 comments

I decided to bundle some of my (hopefully useful) Java Functions and Classes.
Available in the current Version of the so-called jCTPbundle are the following Packages:

  • file – File and Settings-File Functions
  • image – Image and Icon Functions
  • text – Text(stream) Functions
  • gui – GUI Functions, Dialogs, Message Boxes, Text Windows, About Windows etc.
  • screen – Screen Functions
  • audio – Audio Functions, e.g. Sound-Output and Text to Speech
  • math – E.g. Functions to generate random Numbers (int/double) in between Intervalls
  • time – Functions about Time and Space
  • mac – Mac OS X specific Options
  • helper – Some different Functions

I’ve also created an extra Package for Examples:

  • examples – Some Examples how to use the Bundle
  • resources – Resources for the Examples

Here a small Example: “Display a Filechoser to select a File to remove and remove the Selection subsequently. Display an Information Dialog if the Delection succeeded or not.”:

// import file.FileFunctions;
// import gui.Dialog;

// A Dialog to display if Deletion succeeded or not
Dialog dial = new Dialog();

// Use File Functions to select a File
FileFunctions fileFunc = new FileFunctions();
String file = fileFunc.getFileDialog("");
if((file != null) && fileFunc.removeFile(file)) {
	dial.displayInformationMessage("File " +
			fileFunc.getFileName(file) +
			" successfully removed.", "Success");
} else {
	dial.displayErrorMessage("Something went wrong...", "Error");
}

In the Future I will extend and update it.
You can download the jCTPbundle or have a look at the github-Repository.

Applescript: Fast Messenger Status-Message-Change

October 15, 2009 CallToPower 2 comments

Today kernelpanic twittered a cool Applescript for fast-changing your iChat-Status-Message I don’t want to withhold from you. Here’s the Script with a small Addition (I added the delay(seconds)):
[Applescript-Editor-formatted Image]
ichat_status_message_change
[Text]

repeat
	tell application "iChat"
		set the status message to "Status 1"
		delay (1)
		set the status message to "Status 2"
		delay (1)
	end tell
end repeat

And a similar Script for Adium:

[Applescript-Editor-formatted Image]
adium_change_status_message
[Text]

repeat
	tell application "Adium"
		repeat with theAccount in accounts
			go away theAccount with message "Currently away..."
		end repeat
		delay (2.0)
		repeat with theAccount in accounts
			go away theAccount with message "...back in 10 minutes."
		end repeat
		delay (2.0)
	end tell
end repeat

Mac OS X – “Automator”: Combine PDF Files

October 10, 2009 CallToPower 4 comments

Today I had to find a Program that combines/merges 2 or more PDF-Files.
Because I didn’t want to use Adobe Acrobat (Pro?) I turned my Internet-Search on and found an interesting Program: The Mac OS X Automator.
Without reading any Tutorial or something different (Automator is really self-explanatory!) I created a good-working “Automation”/Program that performs that Task.
Here’s the Workflow:

  1. Ask for Finder-Items
  2. Combine PDF Pages
  3. Move Finder Items

Combine_PDF_files

- Update 11.10.2009 -
Created a Sub-Site named Automator-Workflows at www.calltopower.de/automatorworkflows where you can see Screenshots of the Workflows and/or download the Workflows and/or Programs. I created among other things “Save Pictures from the current Website”, “Take Screenshot”, “Take Image from Webcam (iSight or other)”, “Pictures to PDF”.

C: KaR – Exercise 1-14 – Horizontal Histogram of Char-Frequencies

October 4, 2009 CallToPower 2 comments

It’s Time for another Exercise from “Kernighan and Ritchie – The C Programming Language” – Exercise 1-14:

Write a program to print a histogram of the
frequencies of different characters in its input

The 2 main Routines are:
1. Read in the Characters and count up the Position in an Array

    /* Get a Character */
    while((c = fgetc(text_file)) != EOF) {
        /* If c is a Character */
        if(isalpha(c)) {
            /* Count up the Array at Chars Position */
            char_counter[(int)(toascii(tolower(c))-toascii('a'))]++;
            /* Count up Counter for total Characters */
            chars_total++;
        }
    }

2. Print the horizontal Histogram

    /* Print the horizontal Histogram */
    printf("\n\tChar\tTimes\n");
    for(i = 0; i < ALPHABET_LENGTH; i++) {
        /* Print Header */
        printf("\t%c\t%d\t", (i+'a'), char_counter[i]);
        for(j = 0; j < char_counter[i]; j++) {
            /* If Diagram Print Limit not exceeded */
            if(j < diagramm_print_length) {
                printf("-");
            } else {
                /* Print a '+' and the Number of Chars >
                  Diagram Print Limit and break the Loop */
                printf(" + %d", (char_counter[i] - j));
                break;
            }
        }
        printf("\n");
    }
    printf("\t------------------------");
    printf("\n\tTotal Chars: %d\n", chars_total);

Here the full Program to download.

And if the Program analyses itself you will get the following Output:

SICK Robot Day 2009

October 3, 2009 CallToPower 3 comments

Today’s the SICK Robot Day 2009 (Link1 | Link2).
I have worked on it for the University of Osnabrueck.
We’ve had tough luck because we had to completely “re-write” our Behaviours a Week ago but theoretically the Robot is ready now :) .
He avoids Collisions, respects the Rule “left yields right”, is able to detect Numbers as well as Markers and drives to the right Numbers from 0-9 and/or vice versa.
So…good Luck to everyone in Waldkirch.
Here some Photos of Kurt2 at our self-made Parcours:
kurt2_2 kurt2_1

And some Videos at our self-made Parcours:

Java: Implementation of the Unix ls-Command (“List”)

September 28, 2009 CallToPower 1 comment

Here you can find my Interpretation of the List-Command ls (at Windows: dir) with the Options

  • -r (Recursion)
  • -s (Print Size)
  • -h (Print hidden Files)

or combined implemented in Java.
The main Routine who does the Listing-Work is

/**
 * Prints the Content of the Directory
 *
 * @param dir
 *            Directory
 * @param recur
 *            Recursion
 * @param size
 *            SizeRecursion
 * @param hidden
 *            Show hidden Files
 */
private static void list(File dir, boolean recur, boolean size,
        boolean hidden) {
    if ((dir != null) && dir.exists() && dir.isDirectory()) {
        File[] filelist = dir.listFiles();

        printSpaces(lvl - lvlStep);
        System.out.println(dir.getName() + ":");
        for (int index = 0; index < filelist.length; index++) {
            File file = filelist[index];
            // Print hidden Files
            if (hidden || !file.isHidden()) {
                // If Directory Recursion
                if (file.isDirectory() && recur) {
                    lvl += lvlStep;
                    list(file, recur, size, hidden);
                    lvl -= lvlStep;
                    // File Output
                } else {
                    String name = file.getName();
                    printSpaces(lvl);
                    // Print Size
                    if (size && !file.isDirectory()) {
                        long length = file.length();
                        System.out.printf("%-" + sizeLength + "dKB %-"
                                + nameLength + "s", length, name);
                    } else {
                        System.out.printf("%-" + nameLength + "s ", name);
                    }
                    System.out.println();
                }
            }
        }
    } else {
        System.err.println("Error: " + dir + " is not a Directory!");
    }
}

And some Screenshots of Directory-Listings:

Java: Text to Speech on Mac OS X

September 23, 2009 CallToPower 2 comments

Tonight I wanted to do something different: Text to Speech on my Mac – via Java.
So here the Main Routine to do that Task:

Runtime.getRuntime().exec(new String[] { "say", "-v", "YourVoice", "Text" });

(YourVoice is a Mac OS X-supported Voice, e.g. “Zarvox”)

You can see the full Class here.
For testing it without embedding the Class compile it

javac TextToSpeech.java

and run it with one of the Voices

java TextToSpeech Hi dude, whats up? Zarvox

or without (=random Voice)

java TextToSpeech Hi dude, whats up?

To see it live in Action have a Look at my jPortscanner.

Categories: Programming Tags: , , , , , , ,