Back to project page java-androidframework.
The source code is released under:
This project is licensed under the [CC0 1.0 Agreement](http://creativecommons.org/publicdomain/zero/1.0/). To the extent possible under law, Pete Schmitz has waived all copyright and related or neigh...
If you think the Android project java-androidframework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.gamepatriot.framework2d.screens; //ww w.j av a 2s .co m import android.graphics.Rect; import com.gamepatriot.framework2d.implementation.Atlas.SpriteID; import com.gamepatriot.framework2d.implementation.Image; import com.gamepatriot.framework2d.implementation.Image.PivotPosition; import com.gamepatriot.framework2d.implementation.Main; import com.gamepatriot.framework2d.implementation.MusicHandler.Music; import com.gamepatriot.framework2d.implementation.Screen; import com.gamepatriot.framework2d.implementation.Shape; import com.gamepatriot.framework2d.implementation.SoundHandler.Sound; public class Example extends Screen { //Containers private Rect playSoundRect; private Rect playMusicRect; private final Shape buttonsShape; private final Image movingImage; public Example(Main $main){ super($main); /* Build and add the shape buttons will be stored on */ buttonsShape = new Shape(); addShape(buttonsShape); /* Build buttons */ buildPlaySoundButton(); buildPlayMusicButton(); /* Build an image to move around */ movingImage = SpriteID.EXAMPLE.getImage(true); movingImage.setPivot(PivotPosition.CENTER_MIDDLE); addImage(movingImage); } @Override public void update() { /* Move an image around */ //Change the scale if (movingImage.rotationDegrees < 180) movingImage.setScale(movingImage.scale.x + .01); else movingImage.setScale(movingImage.scale.x - .01); //Change the rotation movingImage.setRotationDegrees(movingImage.rotationDegrees + 1); //Move the image float $newX; float $newY; $newX = movingImage.position.x + 2 > Main.CANVAS_WIDTH + movingImage.frameWidth ? -movingImage.frameWidth : movingImage.position.x + 2; $newY = movingImage.position.y + 2 > Main.CANVAS_HEIGHT + movingImage.frameHeight ? -movingImage.frameHeight : movingImage.position.y + 2; movingImage.setPosition($newX, $newY); } @Override public void onBackPressed() { } @Override public void touchRelease(int $x, int $y) { } @Override public void touchPress(int $x, int $y) { /* Play a sound if the 'Play Sound' button was touched */ if (playSoundRect.contains($x, $y)){ sound.playSound(Sound.SFX_EXAMPLE); } /* Play music if 'Play Music' button was touched */ if (playMusicRect.contains($x, $y)){ music.playMusic(Music.HIP_HOP_PIANO, false); } } @Override public void touchMove(int $x, int $y) { } /** Add a 'Play Sound' button **/ private void buildPlaySoundButton(){ int $x = 70; int $y = 70; int $height = 60; int $width = 180; /* Set rectangle hit area for the button */ playSoundRect = new Rect($x, $y, $x + $width, $y + $height); /* Make a shape to display and label the button */ buttonsShape.addRect(playSoundRect); buttonsShape.addText("Play Sound", 22, $x + 35, $y + 38); } /** Add a 'Play Music' button **/ private void buildPlayMusicButton(){ int $x = 270; int $y = 70; int $height = 60; int $width = 180; /* Set rectangle hit area for the button */ playMusicRect = new Rect($x, $y, $x + $width, $y + $height); /* Make a shape to display and label the button */ buttonsShape.addRect(playMusicRect); buttonsShape.addText("Play Music", 22, $x + 35, $y + 38); } }