Archive for Mart 2010

Eclipse Ogre4j 3d Motoru Kurulumu

1. Eclipsi başlatıp yeni bir java projesi oluşturun. Boş HelloWorld class ı oluşturun.

2. Ogre4j yi aşağıdaki adresten indirin.

http://ogre4j.sourceforge.net/

ogre4j-1.6.2-beta6-bin-win32.tar.bz2 dosyasının inmiş olması gerekmektedir.

3. OGRE yi www.ogre3d.org adresinden indirin. İndirmek için aşağıdaki dosyayı seçin.

downloads -> SDK -> OGRE 1.6.2 SDK for Visual C++ .Net 2005 (8.0) SP1

4. OGRE SDK yi kurun.

5. SWT nin son versiyonunu http://www.eclipse.org/swt/ adresinden indirin. Dosya adı swt-x.x-win32-win32-x86.zip olmalıdı r.

6. http://ogre4j.sourceforge.net/webstart/lib/media.jar dosyasını indirin.

7. dll ve jar dosyalarını ogre4j-1.6.2-beta6-bin-win32.tar.bz2 klasöründe çıkarıp projeniniz root klasörüne ekleyin. (c:\users\ugur\workspace\OgreHelloWorld)

8. OGRE SDK daki dll dosyalarını projenizin root klasörüne ekleyin. (C:\OgreSDK_1.6.2\bin\release\*.dll)

9. Projenizin root klasöründe ‘media’ adlo bir klasör oluşturun. media.jar dosyasının ismini media.zip yapıp içindeki zuh.mesh ve zuh.material dosyalarını oluşturduğunuz media klasörüne kopyalayın.

10. swt.jar dosyasını swt-x.x-win32-win32-x86.zip dosyasından çıkararak projenizin root klasörüne kopyalayın.

11. Eclipse projenizi seçip f5 e basarak klasörlerin yenilenmesini sa?lay?n.

12. Projenize sağ tıklayıp "Build path"->"Configure build path->libraries->Add jars” e gelerek bütün jar dosyalarını ekleyin(swt.jar ve ogre4j.jar).

13. http://ogre4j.svn.sourceforge.net/viewvc/ogre4j/trunk/org.ogre4j.examples.swt/src/org/ogre4j/examples/swt/HelloWorld.java?view=markup linkindeki içeriği HelloWorld classı na ekleyerek çalıştırın.

Java: Scanner nextInt() hasNextInt() Example

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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;

public class ScannerExample {

    public static void main(String[] args) {
        Vector vector = new Vector();
        Scanner scanner;
        try {
            scanner = new Scanner(new File("map.txt"));
            Integer i;
            while (scanner.hasNextInt()) {
                i = scanner.nextInt();
                vector.addElement(i);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("the elements of vector: " + vector);
        System.out.println(vector.size());
    }
}




Java Vector Example

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
import java.util.Random;
import java.util.Vector;

public class VectorDeneme {

    public static void main(String[] args) {
        Vector vector = new Vector();
        Random generator = new Random();

        int randomInteger;
        for (int i = 0; i < 10; i++) {
            randomInteger = generator.nextInt(100);
            vector.add(randomInteger);
        }

        System.out.println("the elements of vector: " + vector);
        System.out.println("The size of vector are: " + vector.size());
        System.out.println("The elements at position 2 is: " + vector.elementAt(2));
        System.out.println("The first element of vector is: " + vector.firstElement());
        System.out.println("The last element of vector is: " + vector.lastElement());

        vector.removeElementAt(2);
        vector.removeElementAt(8);

        System.out.println("the elements of vector: " + vector);
    }
}

Possible Output:
the elements of vector: [51, 72, 88, 70, 17, 73, 28, 61, 70, 62]
The size of vector are: 10
The elements at position 2 is: 88
The first element of vector is: 51
The last element of vector is: 62
the elements of vector: [51, 72, 70, 17, 73, 28, 61, 70]

Java Deserialization:Reading an Object Stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.io.*;
public class UnserializeBoolean {

    public UnserializeBoolean() {
        Boolean booleanData = null;
        try {
            FileInputStream fis = new FileInputStream("boolean.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            booleanData = (Boolean) ois.readObject();
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("Unserialized boolen from " + "boolean.ser");
        System.out.println("Boolean data: " + booleanData);
        System.out.println("Compare data with true: " + booleanData.equals(new Boolean("true")));
    }
    public static void main(String [] args) {
        UnserializeBoolean uab = new UnserializeBoolean();
    }
}

Java Serialization: Writing an Object Stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.io.*;
public class SerializeBoolean {
    public SerializeBoolean() {
        Boolean booleanDAta = new Boolean("true");
        try {
            FileOutputStream fos = new FileOutputStream("boolean.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(booleanDAta);
            oos.close();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
    public static void main(String [] args) {
        SerializeBoolean ab = new SerializeBoolean();
    }
}

Java ReentrantReadWriteLock Example

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
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.xml.crypto.Data;

public class ReadWriteMap {
    final Map m = new TreeMap();
    final ReentrantReadWriteLock rw1 = new ReentrantReadWriteLock();
    final Lock r = rw1.readLock();
    final Lock w = rw1.writeLock();

    public Data get(String key) {
        r.lock();
        try {
            return m.get(key);
        } finally  {
            r.unlock();
        }
    }

    public Data put(String key,Data value) {
        w.lock();
        try {
            return m.put(key, value);
        }
        finally {
            w.unlock();
        }
    }

    public void clear() {
        w.lock();
        try {
            m.clear();
        }
        finally {
            w.unlock();
        }
    }
}

Java BlockingQueue Example

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
class Producer implements Runnable {
   private final BlockingQueue queue;
   Producer(BlockingQueue q) { queue = q; }
   public void run() {
     try {
       while(true) { queue.put(produce()); }
     } catch (InterruptedException ex) { ... handle ...}
   }
   Object produce() { ... }
 }

 class Consumer implements Runnable {
   private final BlockingQueue queue;
   Consumer(BlockingQueue q) { queue = q; }
   public void run() {
     try {
       while(true) { consume(queue.take()); }
     } catch (InterruptedException ex) { ... handle ...}
   }
   void consume(Object x) { ... }
 }

 class Setup {
   void main() {
     BlockingQueue q = new SomeQueueImplementation();
     Producer p = new Producer(q);
     Consumer c1 = new Consumer(q);
     Consumer c2 = new Consumer(q);
     new Thread(p).start();
     new Thread(c1).start();
     new Thread(c2).start();
   }
 }

Java TimerClass and TimerTask Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

public class TimerReminder {
    Timer timer;

    public TimerReminder(int seconds) {
        timer = new Timer();
        timer.schedule(new RemindTask(), seconds*1000);
    }

    class RemindTask extends TimerTask {
        public void run() {
            System.out.format("Time's up!%n");
            timer.cancel(); //Terminate the timer thread
        }
    }

    public static void main(String [] args) {
        System.out.format("About to schedule task.%n");
        new TimerReminder(5);
        System.out.format("Task scheduled.%n");
    }
}

OpenGl glPointSize() Example

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
#include
#include

using namespace std;

void glut_display_cb(void);
void my_init(void);

int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(250, 250);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow("glPointSize example");
    glutDisplayFunc(glut_display_cb);

    my_init();
    glutMainLoop();

    return 0;
}

void glut_display_cb(void){
    glClearColor(0.9f, 0.9f, 0.9f, 0.9f);
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINT);
         glVertex2f(points[i].get_X(),points[i].get_Y());
    glEnd();
}

void my_init(void)
{
    // any initialization before the main loop of GLUT goes here
    glViewport(0,0,600,600);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-300,300,-300,300,-10,10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glPointSize(10);
}

OpenGl glLineWidth() Example

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
#include
#include

using namespace std;

void glut_display_cb(void);
void my_init(void);

int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(250, 250);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow("glPointSize example");
    glutDisplayFunc(glut_display_cb);

    my_init();
    glutMainLoop();

    return 0;
}

void glut_display_cb(void){
    glClearColor(0.9f, 0.9f, 0.9f, 0.9f);
    glClear(GL_COLOR_BUFFER_BIT);
  glLineWidth (3);
    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(1.0f, 0.0f);
    glVertex2f(90.0f, 10.0f);
    glVertex2f(90.0f, 90.0f);
    glVertex2f(10.0f, 90.0f);
        glVertex2f(100.0f, 45.0f);
  glEnd();
}

void my_init(void)
{
    // any initialization before the main loop of GLUT goes here
    glViewport(0,0,600,600);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-300,300,-300,300,-10,10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}