Android Open Source - Hungry-Mouse Android Graphics






From Project

Back to project page Hungry-Mouse.

License

The source code is released under:

MIT License

If you think the Android project Hungry-Mouse listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

//Name:   AndroidGraphics.java
//Purpose:  draw pixels, lines, rectangles and pixmaps to the framebuffer
//      also create pixmap instances from asset files
//  w w w  . j ava 2 s .c o  m
package com.hungry.mouse.framework.implementation;
import com.hungry.mouse.framework.Graphics;
import com.hungry.mouse.framework.Image;

//java libraries
import java.io.IOException;//signal that i/o exception has occurred
import java.io.InputStream;//represent input stream of bytes

//android libraries stored in SDK platform
import android.content.res.AssetManager;//provide access to application`s asset files
import android.graphics.Bitmap;//make use of bitmap operations
import android.graphics.Bitmap.Config;//describes how pixels are stored
import android.graphics.BitmapFactory;//create bitmap objects from various sources
import android.graphics.BitmapFactory.Options;//extended capabilities of BitmapFactory
import android.graphics.Canvas;//hold the draw calls
import android.graphics.Paint;//hold style and color information to draw
import android.graphics.Paint.Style;//specifies if the primitive being drawn, stroked or both
import android.graphics.Rect;//hold 4 integer coordinates for rectangle



public class AndroidGraphics implements Graphics {
  
  //use to load bitmap instances
    AssetManager assets;
    
    //represents artificial framebuffer
    Bitmap frameBuffer;
    
    //use to draw to artificial framebuffer
    Canvas canvas;
    
    //use to draw
    Paint paint;
    
    //need to implement drawpixmap() methods
    Rect srcRect = new Rect();
    Rect dstRect = new Rect();

    //constructor
    public AndroidGraphics(AssetManager assets, Bitmap frameBuffer) {
        this.assets = assets;
        this.frameBuffer = frameBuffer;
        this.canvas = new Canvas(frameBuffer);
        this.paint = new Paint();
    }

    //try to load a bitmap from an asset file using the imageformat
    @Override
    public Image newImage(String fileName, ImageFormat format) {
        Config config = null;
        if (format == ImageFormat.RGB565)
            config = Config.RGB_565;
        else if (format == ImageFormat.ARGB4444)
            config = Config.ARGB_4444;
        else
            config = Config.ARGB_8888;

        Options options = new Options();
        options.inPreferredConfig = config;
        
        
        //try to load bitmap from asset via bitmap factory
        InputStream in = null;
        Bitmap bitmap = null;
        try {
            in = assets.open(fileName);
            bitmap = BitmapFactory.decodeStream(in, null, options);
            if (bitmap == null)
                throw new RuntimeException("Couldn't load bitmap from asset '"
                        + fileName + "'");
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load bitmap from asset '"
                    + fileName + "'");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        
        //construct a new bitmap instance based on the loaded bitmap and its imageformat 
        if (bitmap.getConfig() == Config.RGB_565)
            format = ImageFormat.RGB565;
        else if (bitmap.getConfig() == Config.ARGB_4444)
            format = ImageFormat.ARGB4444;
        else
            format = ImageFormat.ARGB8888;

        //return it to the caller
        return new AndroidImage(bitmap, format);
    }

    //extract the red, green and blue components
    //alpha value is ignore, because is transaparent
    @Override
    public void clearScreen(int color) {
        canvas.drawRGB((color & 0xff0000) >> 16, (color & 0xff00) >> 8,
                (color & 0xff));
    }

    //draw a line to the artificial framebuffer
    @Override
    public void drawLine(int x, int y, int x2, int y2, int color) {
        paint.setColor(color);
        canvas.drawLine(x, y, x2, y2, paint);
    }

    //draw a rectangle to the artificial framebuffer
    @Override
    public void drawRect(int x, int y, int width, int height, int color) {
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        canvas.drawRect(x, y, x + width - 1, y + height - 1, paint);
    }
    
    //draw based on ARGB values
    @Override
    public void drawARGB(int a, int r, int g, int b) {
        paint.setStyle(Style.FILL);
       canvas.drawARGB(a, r, g, b);
    }
    
    //draw a string
    @Override
    public void drawString(String text, int x, int y, Paint paint){
      canvas.drawText(text, x, y, paint);
  
    }
    
    //show a part of an image/or the whole image
    @Override
    public void drawImage(Image Image, int x, int y, int srcX, int srcY,
            int srcWidth, int srcHeight) {
        srcRect.left = srcX;
        srcRect.top = srcY;
        srcRect.right = srcX + srcWidth;
        srcRect.bottom = srcY + srcHeight;
        
        
        dstRect.left = x;
        dstRect.top = y;
        dstRect.right = x + srcWidth;
        dstRect.bottom = y + srcHeight;

        canvas.drawBitmap(((AndroidImage) Image).bitmap, srcRect, dstRect,
                null);
    }
    
    //show the whole image
    @Override
    public void drawImage(Image Image, int x, int y) {
        canvas.drawBitmap(((AndroidImage)Image).bitmap, x, y, null);
    }
    
    //show a part of an image/or the whole image at the specified scale
    public void drawScaledImage(Image Image, int x, int y, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight){
      
      
      srcRect.left = srcX;
        srcRect.top = srcY;
        srcRect.right = srcX + srcWidth;
        srcRect.bottom = srcY + srcHeight;
        
        
        dstRect.left = x;
        dstRect.top = y;
        dstRect.right = x + width;
        dstRect.bottom = y + height;
        
   
        
        canvas.drawBitmap(((AndroidImage) Image).bitmap, srcRect, dstRect, null);
        
    }
    
    //getters//
    @Override
    public int getWidth() {
        return frameBuffer.getWidth();
    }

    @Override
    public int getHeight() {
        return frameBuffer.getHeight();
    }
}




Java Source Code List

com.hungry.mouse.framework.Audio.java
com.hungry.mouse.framework.FileIO.java
com.hungry.mouse.framework.Game.java
com.hungry.mouse.framework.Graphics.java
com.hungry.mouse.framework.Image.java
com.hungry.mouse.framework.Input.java
com.hungry.mouse.framework.Music.java
com.hungry.mouse.framework.Pool.java
com.hungry.mouse.framework.Screen.java
com.hungry.mouse.framework.Sound.java
com.hungry.mouse.framework.implementation.AccelerometerHandler.java
com.hungry.mouse.framework.implementation.AndroidAudio.java
com.hungry.mouse.framework.implementation.AndroidFastRenderView.java
com.hungry.mouse.framework.implementation.AndroidFileIO.java
com.hungry.mouse.framework.implementation.AndroidGame.java
com.hungry.mouse.framework.implementation.AndroidGraphics.java
com.hungry.mouse.framework.implementation.AndroidImage.java
com.hungry.mouse.framework.implementation.AndroidInput.java
com.hungry.mouse.framework.implementation.AndroidMusic.java
com.hungry.mouse.framework.implementation.AndroidSound.java
com.hungry.mouse.framework.implementation.MultiTouchHandler.java
com.hungry.mouse.framework.implementation.SingleTouchHandler.java
com.hungry.mouse.framework.implementation.TouchHandler.java
com.hungry.mouse.main.AboutScreen.java
com.hungry.mouse.main.Animation.java
com.hungry.mouse.main.Assets.java
com.hungry.mouse.main.Background.java
com.hungry.mouse.main.Bomb.java
com.hungry.mouse.main.Cheese.java
com.hungry.mouse.main.Enemy.java
com.hungry.mouse.main.GameScreen.java
com.hungry.mouse.main.HelpScreen1.java
com.hungry.mouse.main.HelpScreen2.java
com.hungry.mouse.main.HelpScreen3.java
com.hungry.mouse.main.HelpScreen4.java
com.hungry.mouse.main.Kamikazi.java
com.hungry.mouse.main.LevelSelectorScreen.java
com.hungry.mouse.main.LoadingScreen.java
com.hungry.mouse.main.MainMenuScreen.java
com.hungry.mouse.main.Mouse.java
com.hungry.mouse.main.Projectile.java
com.hungry.mouse.main.Rewards.java
com.hungry.mouse.main.SampleGame.java
com.hungry.mouse.main.Settings.java
com.hungry.mouse.main.Sign.java
com.hungry.mouse.main.SplashLoadingScreen.java
com.hungry.mouse.main.Tile.java