Mr

 Mr. Rogers' IB Computer Science -- Android SmartPhone Programming Project
Help boost K-12 science, math, and computer education. Donate your obsolete Android phone to us (see below)! We promise to put it to good use in a classroom.

Mr Rogers Android Project Menu

Home

Projects

2010-2011


   3D Compass

   3D Accelerometer

   Friction Tester

   Drum

   Spherical Game

   Level

 


Greenville, SC
Bubble Level
Description
This Android application is used for testing how level an object is, mimicking the appearance of an actual bubble level. This application is useful for avoiding carrying an actual bubble level around, and having a multi-purpose device for testing how level an object is and having other tools all-in-one.
This is a screen capture of the Android application in action. The green oval represents what would be the bubble in an actual bubble level. When in use, the green bubble will appear to "float" away from the center, and as the object the device is laying on is rotated, the green bubble moves closer or further from the center.
This is another screen capture of the program. This time, the device has been rotated the opposite direction of the first screen capture.




Features
  • Visually aids in making a surface level.
  • Mimics the appearance of an actual bubble level.
Version
1.0 - 25 October 2010

Future Plans
The level needs to be more sensitive, adjust accordingly.
Make the level more aesthetically pleasing.

Instructions
Place the phone on a flat object and adjust the object to center the bubble drawn on the device.


CODE

Source Code
Bubble Class
package tessier.jean.com;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Bubble {
        private int x, y, radius;

        public Bubble(int xP, int yP, int r)
        {
                x = xP;
                y = yP;
                radius = r;
        }

        public void draw(Canvas c, Paint g)
        {
                g.setColor(Color.GREEN);
                c.drawCircle(x, y, radius, g);
        }

        public void setY(int newY)
        {
                y = newY;
        }

        public void setX(int newX)
        {
                x = newX;
        }

        public void setR(int newR)
        {
                radius = newR;
        }
}
Board Class
package tessier.jean.com;

import android.R.color;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Board extends View
{
        int _height, _width;
        int cX, cY1, cY2, r;

        Bubble bubble;

        Bitmap _bitmap;
        Canvas _canvas;
        Paint g;

        public Board(Context context)
        {
                super(context);
                g = new Paint();
                g.setColor(Color.WHITE);
                g.setStyle(Paint.Style.FILL);

                bubble = new Bubble(10,50000,0);
        }
        /**
	 * Draws the level to the screen. Then draws the bubble on top.
	 */
        private void drawBoard()
        {
                g.setColor(Color.BLACK);
                _canvas.drawRect(0,0,500,500,g);
                g.setColor(Color.WHITE);
                _canvas.drawRect(cX-r, cY1, cX+r, cY2, g);//left, top, right, bottom, paint
                _canvas.drawCircle(cX, cY1, r, g); //centerX, centerY, radius, paint
                _canvas.drawCircle(cX, cY2, r, g);
                bubble.draw(_canvas, g);
        }

        public Bubble getBubble()
        {
                return bubble;
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
                _height = View.MeasureSpec.getSize(heightMeasureSpec);
                _width = View.MeasureSpec.getSize(widthMeasureSpec);

                setMeasuredDimension(_width, _height);

                _bitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);
                _canvas = new Canvas(_bitmap);

                cX = _width/2;
                cY1 = _height/4;
                cY2 = (_height*3)/4;
                r = _width/10;
                bubble.setR(r);
                bubble.setX(cX);
                drawBoard();
        }
        @Override
        protected void onDraw(Canvas canvas) {
                drawBoard();
                canvas.drawBitmap(_bitmap, 0, 0, g);
            invalidate();
        }
}
SensorProgram Class
package tessier.jean.com;


import org.openintents.sensorsimulator.hardware.SensorManagerSimulator;

import android.app.Activity;
import android.content.Context;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class SensorProgram extends Activity implements SensorListener
{
        private double highAccel, totalAccel;
        private SensorManagerSimulator mSensorManager;
        private static Context CONTEXT;
        private Board b;

        /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        b=new Board(this);
        setContentView(b);
        CONTEXT = this;
        highAccel = 0;
        totalAccel = 0;
        mSensorManager = SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE);

        mSensorManager.connectSimulator();
        mSensorManager.registerListener(this, SensorManager.SENSOR_ACCELEROMETER
                                | SensorManager.SENSOR_MAGNETIC_FIELD
                                | SensorManager.SENSOR_ORIENTATION,
                                SensorManager.SENSOR_DELAY_FASTEST);
    }

    public static Context getContext()
    {
        return CONTEXT;
    }

        public void onAccelerationChanged(float x, float y, float z) {
                ((TextView)findViewById(R.id.x)).setText("X: "+String.valueOf(x));
                ((TextView)findViewById(R.id.y)).setText("Y: "+String.valueOf(y));
                ((TextView)findViewById(R.id.z)).setText("Z: "+String.valueOf(z));

        }

        @Override
        public void onSensorChanged(int sensor, float[] values) {
                if(sensor == SensorManager.SENSOR_ORIENTATION)
                {
                        b.getBubble().setY(215+(int)(values[1]*1.194));
                }
        }
}

Biography

Jean Tessier is a senior at Southside High School. He enjoys reading
about psychology and learning new programming languages.


   
Mr

SAM Team--Southside High School's STEM and Computer Science extra-curricular club (Mr. Rogers Sponsor)

Android Developer Site

Mr. Rogers' Twitter Site

Mr. Rogers Teacher's Blog

Mr. Rogers T-shirts

Check out other web sites created by Mr. R:

 
Want to learn more about movie physics in Star Trek and find out :
  • what makes Star Trek unique
  • how Star Trek compares to Star Wars
  • why the star ship Enterprise needs to remain in space
  • what should and shouldn't be done in space battles
  • what it takes to blast off and travel the galaxy
  • the basics of orbiting
Insultingly Stupid Movie Physics is one of the most humorous, entertaining, and readable physics books available, yet is filled with all kinds of useful content and clear explanations for high school, 1st semester college physics students, and film buffs.

It explains all 3 of Newton's laws, the 1st and 2nd laws of thermodynamics, momentum, energy, gravity, circular motion and a host of other topics all through the lens of Hollywood movies using Star Trek and numerous other films.

If you want to learn how to think physics and have a lot of fun in the process, this is the book for you!

 

First the web site,

now the book!


Mr. Rogers Home | Common Sylabus | AP Comp Sci I | AP Comp Sci II | AP Physics Mech | AP Physics E&M | AP Statistics | IB Design Tech | Southside

[ Intuitor Home | Physics | Movie Physics | Chess | Forchess | Hex | Intuitor Store |

Copyright 1996-2010 T. K. Rogers, all rights reserved. Forchess is a registered trademark of T. K. Rogers.
No part of this website may be reproduced in any form, electronic or otherwise, without express written approval.