Scalable Vector Graphics in J2ME


SVG merupakan salah satu implementasi Low Level dalam membuat tampilan di jendela mobile phone. Berikut adalah contoh animasi bola memantul dengan menggunakan SVG J2ME

 

Untuk source code-nya adalah sebagai berikut:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.Enumeration;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m2g.SVGImage;
import javax.microedition.m2g.ScalableGraphics;
import javax.microedition.midlet.*;
import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGRGBColor;
import org.w3c.dom.svg.SVGSVGElement;

/**
 * @author LADUK
 */
public class contoh extends MIDlet implements CommandListener{

    kanfas kanfas;
    Form form;
    Command tambah, exit, tambah2;
    Vector v;
    Enumeration enums;

    public contoh()
    {
        this.form = new Form("form");
        this.v = new Vector();
        this.kanfas = new kanfas();
        this.tambah = new Command("tambah", Command.OK, 0);
        this.tambah2 = new Command("tambah", Command.OK, 0);
        this.exit = new Command("exit", Command.EXIT, 0);
        this.kanfas.addCommand(exit);
        this.kanfas.addCommand(tambah);
        this.kanfas.setCommandListener(this);
        this.form.addCommand(tambah2);
        this.form.setCommandListener(this);
    }

    public void startApp() {
        Display.getDisplay(this).setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if(c==exit)
        {
            notifyDestroyed();
            destroyApp(true);
        }
        if(c==tambah)
        {
            new tret().start();
        }
        if(c==tambah2)
        {
            Display.getDisplay(this).setCurrent(kanfas);
            new tret().start();
        }
    }

    public class kanfas extends Canvas
    {

        SVGImage svgImage = SVGImage.createEmptyImage(null);
        Document doc = svgImage.getDocument();
        SVGSVGElement svg = (SVGSVGElement) doc.getDocumentElement();
        ScalableGraphics sg = ScalableGraphics.createInstance();

        public kanfas(){}

        protected void paint(Graphics g) {
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, getWidth(), getHeight());
            sg.bindTarget(g);
            svgImage.setViewportWidth(getWidth());
            svgImage.setViewportHeight(getHeight());
            sg.render(0, 0, svgImage);
            sg.releaseTarget();
        }
    }

    public class tret extends Thread
    {

        SVGElement elm;
        SVGRGBColor warna;
        Timer t;
        TimerTask ts;
        Random r = new Random();
        int i = (int) (r.nextFloat() * 12);

        public tret()
        {
            this.elm = (SVGElement)kanfas.doc.createElementNS("", "rect");
            switch(i)
            {
                case 0:
                case 6:
                    this.warna = kanfas.svg.createSVGRGBColor(255,0,0);
                    break;
                case 1:
                case 7:
                    this.warna = kanfas.svg.createSVGRGBColor(0,255,0);
                    break;
                case 2:
                case 8:
                    this.warna = kanfas.svg.createSVGRGBColor(0,0,255);
                    break;
                case 3:
                case 9:
                    this.warna = kanfas.svg.createSVGRGBColor(255,255,0);
                    break;
                case 4:
                case 10:
                    this.warna = kanfas.svg.createSVGRGBColor(255,0,255);
                    break;
                case 5:
                case 11:
                    this.warna = kanfas.svg.createSVGRGBColor(0,255,255);
                    break;
            }

            this.elm.setRGBColorTrait("fill", warna);
            this.elm.setFloatTrait("width", 30);
            this.elm.setFloatTrait("height", 30);
            int rt = (int) this.elm.getFloatTrait("height");
            System.out.println("height : "+String.valueOf(rt));
            kanfas.svg.appendChild(this.elm);
            v.addElement(this.elm);
            t = new Timer();
            ts = new task(this.elm);

        }

        public void run()
        {
            this.t.schedule(ts, 0, 30);
        }

    }

    public class task extends TimerTask
    {
        SVGElement e, elm;
        int x, y, rx = 0, ry = 0, xx, yy, counter;
        boolean ab = true;
        Random r = new Random();

        public task(SVGElement e)
        {
            this.e = e;
            counter = 0;

            this.x = kanfas.getWidth()/2;
            this.y = kanfas.getHeight()/2;
            this.xx = (this.r.nextInt() % 3);
            if (this.xx == 0)  this.xx = 1;
            this.yy = (this.r.nextInt() % 3);
            if (this.yy == 0)  this.yy = 1;
        }

        public void run()
        {

                this.e.setFloatTrait("x", x);
                this.e.setFloatTrait("y", y);
                this.e.setFloatTrait("rx", rx);
                this.e.setFloatTrait("ry", ry);

                if(x + 30 + xx > kanfas.getWidth()||x - 30 + xx < -25)
                {
                    xx = -xx;
                }
                if(y + 30  + yy > kanfas.getWidth() + 45||y - 30 + yy < -25)
                {
                    yy = -yy;
                }

                x+=xx;
                y+=yy;
                if(ab)
                {
                    if(rx<15&&ry<15)
                    {
                        rx++;
                        ry++;
                    }
                    else
                    {
                        ab = false;
                    }

                }
                else
                {
                    if(rx>0&&ry>0)
                    {
                        rx--;
                        ry--;
                    }
                    else
                    {
                        ab = true;
                    }
                }

                kanfas.repaint();
        }
    }

}

Lha Tugasnya temen2 adalah:

  • Modifikasi source code di atas sedemikan rupa sehingga
  • Terapkan hukum pemantulan pada Fisika, untuk bola yang saling bersentuhan/bersenggolan/bertabrakan/berpelukan/bersalam-salaman/berciuman/ber…gitu’an… dan ber-ber yang lain
  • Sekali klik Tambah akan menghasilkan 2 anak (2 bola maksud beta)
  • Kumpulkan saat kuliah
  • Tetaplah Semangat Kawan!

Salam Sayang,

Aku dan Kamu Bisa!!! (bisa gila wkwkwk)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: