Powered By Blogger

Senin, 06 Juli 2015

GPS dan Accelerometer Android

File Main Activity.java

package com.example.coba;


import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity implements SensorEventListener
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        magnetX = (TextView) findViewById(R.id.textView5);
        magnetY = (TextView) findViewById(R.id.textView1);
        magnetZ = (TextView) findViewById(R.id.textView3);
       
       
       
        mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;
    }
   
   
    SensorManager mSensorManager;
    private float mAccel; // acceleration apart from gravity
    private float mAccelCurrent; // current acceleration including gravity
    private float mAccelLast; // last acceleration including gravity

    TextView magnetX;
    TextView magnetY;
    TextView magnetZ;
   
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
        // Ignoring this for now
    }

    public void onSensorChanged(SensorEvent se)
    {
        float x = se.values[0];
        float y = se.values[1];
        float z = se.values[2];
        mAccelLast = mAccelCurrent;
        mAccelCurrent = (float) Math.sqrt((double) (x * x + y * y + z * z));
        float delta = mAccelCurrent - mAccelLast;
        mAccel = mAccel * 0.9f + delta; // perform low-cut filter
        magnetX.setText(Float.toString(x));
        magnetY.setText(Float.toString(y));
        magnetZ.setText(Float.toString(z));
        if (mAccel > 12)
        {
            Toast toast = Toast.makeText(getApplicationContext(), "Device has shaken.", Toast.LENGTH_SHORT);
            toast.show();
        }

    }

    protected void onPause()
    {
        // Unregister the listener
        mSensorManager.unregisterListener(this);
        super.onPause();
    }

    protected void onStop()
    {
        // Unregister the listener
        mSensorManager.unregisterListener(this);
        super.onStop();
    }

    protected void onResume()
    {
        super.onResume();
        // Register magnetic sensor
        mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    }

}





File activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mag_x"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="188dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textZ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textY"
        android:layout_marginLeft="49dp"
        android:layout_marginTop="29dp"
        android:text="Z"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textZ"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="28dp"
        android:text="Y"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textY"
        android:layout_alignRight="@+id/textView2"
        android:layout_marginRight="52dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textZ"
        android:layout_alignBottom="@+id/textZ"
        android:layout_alignLeft="@+id/textView1"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textY"
        android:layout_alignLeft="@+id/textZ"
        android:text="X"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/textView1"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView5"
        android:layout_marginBottom="18dp"
        android:layout_toLeftOf="@+id/textView5"
        android:text="Acceleration"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="Longitude :"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView7"
        android:layout_below="@+id/textView7"
        android:layout_marginTop="28dp"
        android:text="Latitude :"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView8"
        android:layout_marginLeft="23dp"
        android:layout_toRightOf="@+id/textView7"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView8"
        android:layout_alignBottom="@+id/textView8"
        android:layout_alignLeft="@+id/textView9"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Senin, 29 Juni 2015

Tugas

File activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editpas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editanda"
        android:layout_marginTop="19dp"
        android:layout_toRightOf="@+id/spas"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editanda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editpas"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp"
        android:ems="10" />

    <EditText
        android:id="@+id/editresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnhitung"
        android:layout_alignRight="@+id/btnhitung"
        android:layout_below="@+id/btnhitung"
        android:layout_marginTop="37dp"
        android:ems="10" />

    <TextView
        android:id="@+id/spas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editpas"
        android:layout_alignBottom="@+id/editpas"
        android:layout_alignRight="@+id/sanda"
        android:text="Nama Pasangan" />

    <TextView
        android:id="@+id/sanda"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editanda"
        android:layout_alignBottom="@+id/editanda"
        android:layout_alignParentLeft="true"
        android:text="Nama Anda" />

    <TextView
        android:id="@+id/sresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editresult"
        android:layout_alignBottom="@+id/editresult"
        android:layout_alignParentLeft="true"
        android:text="Result" />

    <Button
        android:id="@+id/btnhitung"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="hitung"
        android:text="Proses" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editresult"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/editresult"
        android:text="%" />

</RelativeLayout>



File MainActivity.java

package com.example.android_tugas;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;

public class MainActivity extends Activity{

    private EditText editanda,editpas,editresult;
    private String sanda,spas,sresult;
    private Button btnhitung;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editanda=(EditText) findViewById(R.id.editanda);
        editpas = (EditText) findViewById(R.id.editpas);
        btnhitung = (Button) findViewById(R.id.btnhitung);
        editresult = (EditText) findViewById(R.id.editresult);
    }
 
 
    public void hitung(View view) {
        String stranda = "";
        String strpas = "";
        try{
        stranda = editanda.getText().toString();
        strpas = editpas.getText().toString();
        char[] arrCharStr = stranda.toCharArray();
        char[] arrCharStr2 = strpas.toCharArray();
        }catch(Exception e){
         
        }
   
        char[] arrCharStr = stranda.toCharArray();
        char[] arrCharStr2 = strpas.toCharArray();
     
        int sum_a=0;
        int sum_pas=0;
        for (char c : arrCharStr) {
            String biner = "0" + Integer.toBinaryString(c);
            int ASCII = Integer.parseInt(biner, 2);
            char karakter = (char) ASCII;
            System.out.println(biner + " :: " + ASCII + " :: " + karakter);
            sum_a = sum_a + ASCII;
        }
        System.out.println(sum_a + "");
        for (char c : arrCharStr2) {
            String biner = "0" + Integer.toBinaryString(c);
            int ASCII = Integer.parseInt(biner, 2);
            char karakter = (char) ASCII;
            System.out.println(biner + " :: " + ASCII + " :: " + karakter);
            sum_pas= sum_pas + ASCII;
        }
     
        System.out.println(sum_pas + "");
        int nilai_anda=sum_a%100;
        int nilai_pas=sum_pas%100;
        System.out.println(nilai_anda + "");
        System.out.println(nilai_pas + "");
        int selisih;
        if (nilai_anda > nilai_pas){
            selisih=nilai_anda - nilai_pas;
            System.out.println(selisih + "");
        }
        else{
            selisih=nilai_pas-nilai_anda;
            System.out.println(selisih + "");
        }
        int hasil = 100 - selisih;
        System.out.println(hasil + "% cocok");
        String x= String.valueOf(hasil);
        editresult.setText(x);
    }
}

Senin, 04 Mei 2015

PAPAN CATUR

package yosafat;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;


public class PapanMIDlet extends MIDlet {
    private Display display;
    papancatur pantur;
 
    public void startApp() {
        if(display==null){
            pantur = new papancatur(this);
            display = Display.getDisplay(this);
        }
        display.setCurrent(pantur);
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
 
    protected void Quit(){
        destroyApp(true);
        notifyDestroyed();
    }
    class papancatur extends Canvas implements CommandListener{
        private Command exitCommand = new Command("Exit", Command.EXIT, 7);
        private PapanMIDlet segiempat;
 
        public papancatur(PapanMIDlet segiempat){
            this.segiempat = segiempat;
            addCommand(exitCommand);
            setCommandListener(this);
        }
 
        protected void paint(Graphics g) {
            int width = getWidth();
            int height = getHeight()-61;
            g.setGrayScale(255);
            g.fillRect(0, 0, width-1, height-1);
           
            g.setColor(0, 0, 0); //pengaturan canvas warna hitam
           
            //untuk potongan diagonal kanan
            for (int baris = 0; baris < width; baris+=30) {
                for (int kolom = 330; kolom>= 30; kolom-=60) {
                    g.fillRect(baris+kolom, baris, 30, 30);
                }
            }
           
            //untuk potongan diagonal kiri
            for (int baris = 0; baris < width; baris+=30) {
                for (int kolom = 30; kolom <= 330; kolom+=60) {
                    g.fillRect(baris, baris+kolom, 30, 30);
                }
               
            }
           
 
        }
 
        public void commandAction(Command c, Displayable d) {
            if(c==exitCommand){
                segiempat.Quit();
            }
        }
    }
}

Dan hasilnya


Sabtu, 11 April 2015

Tugas Latihan 1 Java Mobile , 11 April 2015

import.javax.microedition.midlet.*;
import.javax.microedition.Lcdui;
public class Latihan1 extends MIDlet implements CommandListener{
Display dis;
Command exitCommand = new Command("Exit", Command.EXIT,1);
Command hal1Command = new Command("Page 1", Command.OK,1);
Command hal2Command = new Command("Page 2", Command.OK,1);
Command hal3Command = new Command("Page 3", Command.OK,1);
Command homeCommand = new Command("Home", Command.OK,1);
Form homeForm  = null;
Form hal1Form = null;
Form hal2Form = null;
Form hal3Form = null;
public Tugas1(){
super();
homeForm=new Form("Selamat Datang Di Politeknik Negeri Medan");
homeForm.addCommand(exitCommand);
homeForm.addCommand(hal1Command); 
homeForm.addCommand(hal2Command); 
homeForm.addCommand(hal3Command);
homeForm.setCommandListener(this);
hal1Form=new Form("Page 1");
hal1Form.addCommand(homeCommand);
hal1Form.addCommand(hal2Command);
hal1Form.addCommand(hal3Command);
hal1Form.addCommand(exitCommand);
hal1Form.setCommandListener(this);
hal2Form=new Form("Page 2");
hal2Form.addCommand(homeCommand);
hal2Form.addCommand(hal1Command);
hal2Form.addCommand(hal3Command);
hal2Form.addCommand(exitCommand);
hal2Form.setCommandListener(this);
hal3Form=new Form("Page 3"); 
hal3Form.addCommand(homeCommand);
hal3Form.addCommand(hal1Command);
hal3Form.addCommand(hal2Command);
hal3Form.addCommand(exitCommand);
hal3Form.setCommandListener(this);
}
public void startApp() {
if (dis==null){
dis=Display.getDisplay(this);
}
dis.setCurrent(homeForm);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable d){
if(c==exitCommand){
destroyApp(true);
notifyDestroyed(); //Exit
}
else if(c==page1Command){
dis.setCurrent(page1Form);
}
else if(c==page2Command){
dis.setCurrent(page2Form);
}
else if(c==page3Command){
dis.setCurrent(page3Form);
}
else if(c==homeCommand){
dis.setCurrent(homeForm);
}
}
}