"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects." (Robert A. Heinlein)
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Monday, 17 October 2016

LibGDX: The “Swiss Army Knife” of Java Games Programming


My few readers might remember I sometimes explore, and play with, game engines and libraries. I suppose it's a sort of “compensation” for my everyday work with not-so-exciting middle-ware and web-services projects. I already posted about Slick2D libraries for Java games but I was looking for something more complete and, most of all, that would allow me to develop also Android applications.
LibGDX is a Java game-engine that provide a vast variety of features for developing games. It's mostly aimed to 2D games development but it even provides some 3D features. In addition LibGDX can deploy games on different platforms like Java desktop or Applet, Android, HTML5 and IOS (with some extra requirement due to peculiar Apple developing policies). Last but not least a LibGDX project can include additional extension libraries helping to develop different aspects of game programming like AI, physics or networking (and this is the why of this post title).

First project creation

You don't have to download the whole LibGDX project in order to start to develop. LibGDX people suggest using the handy utility they provide, LibGDX Project Setup, in order to generate a skeleton project. The generated project will be based on the Gradle building tool that will think about downloading from the Internet all needed libraries and their dependencies.

Saturday, 15 November 2014

The quest for new languages: Scala

As a programmer I try to keep track of the new tools are continuously made available to my work. It's a hard task, almost impossible, since every day new languages, framework or libraries are proposed on the Internet. I usually follow a conservative strategy by leaving novelties to “grown up” a little in order to see it they are more or less widely used.
I've been recently reading this article, among the many languages and technologies cited one particularly awakened my interest: Scala.

Why Scala?

Three things about Scala caught at my attention: first it runs on a standard Java virtual machine this means an easier integration with the programming ecosystem I'm mostly used to work with. Then I learned that Scala implements the functional programming paradigm. I really know little about functional programming but the idea of learning a new programming paradigm really enticed me. Last, but not least, Scala is nowadays widely used in big and complex software projects like Twitter or LinkedIn.

Scala on Eclipse (Luna)

A good Eclipse plug-in is available for Scala named Scala IDE for Eclipse. I fist tried downloading the one available at Eclipse Marketplace but I soon discovered it didn't work with Eclipse Luna. After some searching in the Internet I discovered the only working version for Eclipse Luna is version 4.0 release candidate 2. Installing the plug-in has been only matter of copying the update address into Eclipse “Install new software” window, agreeing to licenses and following instructions.

Tuesday, 5 August 2014

Neo4j and Java: demos with an embedded Ne04j graph


After my first experience in installing Neo4j graph database I decided to continue my experiments by writing a little Java demo program. The scope of my program just to learn how to connect to a Neo4j embedded graph, to generate ,connect and query some hundreds of nodes. Neo4j site and the downloaded manual provide plenty of documentation about interfacing with Java, and the other supported languages.

Project set-up

Setting up a Java project is quit simple: just matter of including all Neo4j libraries jars, available in the 'lib' folder, in the project class-path. To make easier future projects set-up I prepared, in Netbeans, a custom library configuration.


Saturday, 24 May 2014

Now something completely different: Neo4j


I've been interested in the variegated world of “NOSQL” databases since a while but I was mostly undecided which one start experimenting with. Most of NOSQL databases give their best in high data volumes, high availability, high scalability, “high everything” use cases. Going to test such databases in a realistic way is not an easy task. Testing them on a EEEPC while traveling by train is definitively impossible. I so concentrated my interest on “Graph Databases”: a kind of NOSQL databases designed to represent data tied by complex and deep relationships, hard to be described by the classic table form.

Neo4j

Among the many, Java based, graph databases the one that got my attention has been Neo4j. Two things made me decide for testing Neo4J: first Neo4J is embeddable in your Java project, the second is the huge amount of documentation and examples available at Neo4j site.

Installation

Installing Neo4j has been quite simple just matter of extracting the downloaded archive into a folder in my home directory (I have a “Projects” folder for this)
tar xv neo4j-community-2.0.3-unix.tar.gz
this is more than enough for a test installation or if you are going to use it only embedded in anoter Java project. Installing a Neo4j server would be a little more more tricky.
Neo4j service can be started by shell using the “neo4j” script provided in the bin directory:
cd Projects/neo4j-community-2.0.3/bin/
./neo4j start
at start it gives a warning about not running it in a Oracle Java JVM but during my tests it worked fine even with OpenJDK JVM. Of course fro a more intense use it'd better follow the suggestion and use the right JVM.
The same script can be used to stop Neo4j service:
./neo4j stop
User interface

Neo4j offers a web based user interface on the “http://localhost:7474” address. The web interface provide an easy way to send commands to Neo4j with the help of a good variety of saved scripts and examples available with one mouse click.

Wednesday, 9 April 2014

Java JDK 8 and Netbeans 8 on the EEEPC


With the recent release of the long awaited Java 8 Oracle also released the latest version of its development environment Netbeans. JDK8 and Netbeans have been made available as single updates or together in a bundled package. Since I wasn't fully satisfied from my recent upgrade to Netbeans 7.4 I was more than willing to upgrade.

Installation

The bundled package is in the form of the usual self-executable file Oracle provides to distribute Netbeans installations, let me say the installation process couldn't be easier.
I executed the installation file from shell …
chmod +x jdk-8-nb-8-linux-i586.sh
sudo ./jdk-8-nb-8-linux-i586.sh
the installation wizard started

Wednesday, 14 August 2013

Mercurial and Mercurial-server : playing with DVCS (part 2)

In my previous post I moved my first steps with Mercurial DVCS, now I'll install a Mercurial server implementation and configure both my computers to access it using SSH protocol.

Mercurial-server

With DVCS you don't have to use a central server, repositories could be shared over LAN using shared folders, but this doesn't mean you can't have one. Various mercurial server side implementations exists, using different protocols. May be I'm too server-client minded but I didn't feel satisfied by just sharing repositories over a shared folder so I decided to install Mercurial-server.
Installing Mercurial-server is an easy task the command
sudo apt-get install mercurial-server
complete the installation process and the creation of the application user (hg). A bit more complex is configuring SSH for accessing the server, I mainly followed instructions from here and from Mercurial-server documentation.
Mercurial-server uses public-key authentication and SSH-Agent in order to grant access to its clients, so the first step has been to generate a keys couple for SSH. The ssh-keygen command does this interactively.
maxx@VeritonS661:~$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/maxx/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/maxx/.ssh/id_dsa.
Your public key has been saved in /home/maxx/.ssh/id_dsa.pub.
I then copied the public key in mercurial-server keys configuration path and told mercurial-server to refresh its authentication files, using the following commands:
ssh-add -L > maxx.key
sudo mkdir /etc/mercurial-server/keys/root/maxx
sudo cp maxx.key /etc/mercurial-server/keys/root/maxx /veritons661
sudo -u hg /usr/share/mercurial-server/refresh-auth
the usual path for mercurial-server keys is (for root users)
/etc/mercurial-server/keys/root/<user-name>
but if the same user must be accessed from different machines a different path is used:
/etc/mercurial-server/keys/root/<user-name>/<machine-name>
since I was going to add the maxx user from the EEEPC too I had to use, of course, the second from. On the EEEPC side I generated SSH keys at the same manner then, after logging to the desktop computer (the server) with:
ssh -A veritons661
and I eventually registered EEEPC's maxx user like this
ssh-add -L > eeepc900.key
sudo cp eeepc900.key /etc/mercurial-server/keys/root/maxx/eeepc900
sudo -u hg /usr/share/mercurial-server/refresh-auth

Wednesday, 31 July 2013

Mercurial and Mercurial-server : playing with DVCS (part 1)

Version control systems (VCS) are an indispensable tool when programming and sharing code even for small groups. Even while programming alone, but on different computers, a version control system could easily prove useful for securely sharing code between desktop and laptop computer. I use daily SVN, as version control while at work. For my homely experiments, instead, I decided to install something different: Mercurial, a distributed version control system (DVCS). Distributed version control systems, most famous are Git and Mercurial, do not rely on a central server to keep the code repository, in DVCS every developing computer keeps its own copy of the repository. What interested me in DVCS was the capability to do version control also when off-line and also, of course, the chance to learn something new.

Installing Mercurial (command-line and plugged-in)

Installing Mercurial command-line version it's quite trivial:
sudo apt-get install mercurial
Both Eclipse and Netbeans offers their plug-ins to interface with Mercurial. Netbeans plug-in is already provided with version 7.3.1 I have installed on the EEEPC. On Eclipse, desktop-side, installing the MercurialEclipse plug-in has been as simple as selecting it from the Eclipse Marketplace and following installation wizard.

Thursday, 2 May 2013

Slick2D on Netbeans (and the EEEPC)


This post is sort of a corollary at my previous post: after trying Slick2D on my desktop computer, and Eclipse, I decided to port the demo projects to Netbeans, on the EEEPC, where I do many of my programming experiments.
The use Slick2D with Netbeans is covered, in the site wiki, as well as the Eclipse case. The first step is creating a Java application project
once the project has been created Slick2D and LWJGL Java libraries must be added to the project class-path. This can be done by selecting one-by-one the needed JAR files but, if you're going to make more than one project setting-up a custom library will be handier.
So I, from the project properties, I selected the libraries folder then the “Add Library ...” button.

Thursday, 25 April 2013

Fun programming: Java and Slick2D


Many may not believe me but programming can be fun. If you really love programming even watching a XML file going to, or coming, from a remote server can be fun but, of course, if your program produces something funny then programming is also more fun. So, while looking for something to relax after watching too much XML files, I decided to experiment with some 2D game engine. 2D gaming has been for many years confined to the Adobe Flash or J2MEmini game” context. More recently , with the ever wider smart-phones diffusion, 2D games have known a real revival.
Among the many 2D games engines available I first restricted my choice to the Java-based ones. After a short examining of available features and documentation I decided to try first the Slick2D engine.

Slick2D

Slick2D is a Java game engine mostly based on the LWJGL (Light-Weight Java Game Library) library. The aspect of Slick2D the more appealed to me, and triggered my interest, is the ability to begin coding a simple game by just extending a class and writing three methods.
Slick2D offers in fact an abstract class BasicGame, once this class is extended just three methods have to be implemented:
  • init() : is called once when the game is started
  • render() : is called every time the screen is refreshed
  • update() : is called when controls (keyboard, mouse, joystick, ...) are read
The programmer can mostly ignore the other aspects of the game and concentrate on these three events.
Of course writing a complete game will still be a complex task but, Slick2D makes very simple the initial approach to game programming easing a lot the learning curve at the beginning.

First project

To prepare my first project I first downloaded Slick2D jar file and LWJGL zip archive, I then extracted it in a convenient folder in my home directory. In Eclipse I first made a standard Java application project

Sunday, 5 September 2010

Upgraded to Netbeans 6.9.1 (on the EEEPC)

From time to time it happens I start dusting off the development environment on my EEEPC in order to start with some little new experiment. I so decided to upgrade Netbeans 6.8 there installed to the latest version 6.9.1.


Upgrading the JDK

As first step I decided, even if It's not strictly necessary, to upgrade to the latest JDK. I downloaded the self extracting archive from Oracle's download page and extracted it into the /opt directory:
chmod +x jdk-6u21-linux-i586.bin
mv jdk-6u21-linux-i586.bin /opt
sudo /opt/jdk-6u21-linux-i586.bin
As alternative JDK OpenJDK can be installed by a simple apt-get command:
sudo apt-get install openjdk-6-jdk

Installing Netbeans 6.9.1

I downloaded Netbeans installation file from Netbeans.org download page then I executed it:
chmod +x netbeans-6.9.1-ml-javase-linux.sh
sudo ./netbeans-6.9.1-ml-javase-linux.sh
the installation program starts and it's only matter of agreeing with licences and clicking “next”.

Monday, 23 February 2009

EEEBuntu: Restoring the Java development environment

After installing the operating system the second step in building up my EEEBuntu netbook is preparing a Java development environment by installing Java JDK, Netbeans and Apache Tomcat.
Java JDK installation:
EEEbuntu comes with  OpenJDK already installed, but I prefer having also Sun JDK installed. Java developer kit installation is done by terminal using apt-get command:
sudo apt-get install sun-java6-jdk
then Sun jdk is selected as default jdk by typing the command:
sudo update-java-alternatives --set java-6-sun

Thursday, 18 December 2008

Merry (fractal) Christmas

This is my first Christmas as a blogger and, an old coder like me couldn't help not to post some Christmas related piece of code. I spent some of my spare time, mainly while travelling by train, writing a small java program to draw a fractal tree and I finally tuned it to produce something resembling a Christmas tree.
The algorithm used is fairly simple:  just draw a branch (a segment) and then attach some others branches to it calling recursively the same branch method. Recursive calls are stopped when branch length falls below a giver value. to make the final result a little more realistic a little randomness is added and branch color is decided on the basis of branch length.
The result is far from being satisfying (and lightyears far from artistic results as in here) but I don't think I'll get something better before Christmas.
  
Here is the code of the tree drawing class while the user interface was generated by the Netbeans plugin with only minimal manual coding needed.
package ph.mm.demo.fractals;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;

/**
 *
 @author user
 */
public class TreePainter {

    Graphics2D graphics = null;
    private double scale = 0.42;
    private double scale2 = 0.49;
    private double angle =115;
    private double minLength = 3;
    private double rangle = 0;
    private double split = 0.6;
    private double noise = 0.05;
    private long seed = 3214;
    private Random rnd=null;
    private double x0=200;
    private double y0=350;
    private double x1=200;
    private double y1=200;
    float colScale = 0;
            
        
    public TreePainter(Graphics g) {
        graphics = (Graphics2D)g;
        graphics.setBackground(Color.BLACK);
        graphics.clearRect(00500500);
        rangle=Math.toRadians(angle);
        rnd = new Random(seed);
        colScale = (float)(1/(y0-y1));
    }
   
    public void paint(){
        Line2D l = new Line2D.Double(x0, y0, x1, y1);
        branch(l);
    }
    
    public void branch(Line2D l){
        Rectangle2D r = l.getBounds2D();
        double size = Math.sqrt(r.getWidth()*r.getWidth() + r.getHeight()*r.getHeight());
        graphics.setColor(getCol(size));
        graphics.draw(l);
        
        if(size>minLength){
            double cAngle=0;
            cAngle = Math.acos((l.getX2()-l.getX1())/size);
            System.out.println(size+ ", " + Math.toDegrees(cAngle));
            if(l.getY1()>l.getY2()) cAngle=-cAngle;
            
            Point2D pSplit = new Point2D.Double(l.getX1() (l.getX2()-l.getX1())*split,
                                                l.getY1() (l.getY2()-l.getY1())*split);
            Point2D pEnd1 =  new Point2D.Double(l.getX2() + size*scale*getNoise()*Math.cos(cAngle)
                                                l.getY2() + size*scale*getNoise()*Math.sin(cAngle));
            Point2D pEnd2 =  new Point2D.Double(l.getX2() + size*scale*getNoise()* Math.cos(cAngle*getNoise()+rangle)
                                                l.getY2() + size*scale*getNoise()*Math.sin(cAngle*getNoise()+rangle));
            Point2D pEnd3 =  new Point2D.Double(l.getX2() + size*scale*getNoise()* Math.cos(cAngle*getNoise()-rangle)
                                                l.getY2() + size*scale*getNoise()*Math.sin(cAngle*getNoise()-rangle));
            Point2D pEnd4 =  new Point2D.Double(pSplit.getX() + size*scale2*getNoise()* Math.cos(cAngle*getNoise()+rangle)
                                                pSplit.getY() + size*scale2*getNoise()*Math.sin(cAngle*getNoise()+rangle));
            Point2D pEnd5 =  new Point2D.Double(pSplit.getX() + size*scale2*getNoise()* Math.cos(cAngle*getNoise()-rangle)
                                                pSplit.getY() + size*scale2*getNoise()*Math.sin(cAngle*getNoise()-rangle));
            branch(new Line2D.Double(l.getP2(), pEnd1));
            branch(new Line2D.Double(l.getP2(), pEnd2));
            branch(new Line2D.Double(l.getP2(), pEnd3));
            branch(new Line2D.Double(pSplit, pEnd4));
            branch(new Line2D.Double(pSplit, pEnd5));
        }
        
    }
    
    private double getNoise(){
        return (rnd.nextDouble()-0.5)*2*noise+1;
    }
    
    private Color getCol(double size){
        if(size<minLength)
            return new Color(0.05F0.5F0.2F);
        else if(size<minLength*2)
            return new Color(0.05F,0.4F0.1F);
        else if(size<minLength*10)
            return new Color(0.05F,0.3F0.3F);
        else
            return new Color(0.3F,0.3F0.1F);
    }
            
}