I am currently making an android app which plays the puzzle game hashi. I am currently struggling to output the game grid. i want to output a 2d array like bellow-
1 0 0 0 1
0 2 0 0 2
2 0 3 0 1
0 0 0 0 0
0 0 2 0 2
however when i run the application in the emulator it outputs just a blank white screen.
main activity-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new boardView(this));
//sets the view to the board view to show the puzzle on open.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
game board class-
public class boardView extends View {
public float IslandX;
public float IslandY;
public int islandDiameter;
private Canvas canvas;
public boardView(Context context) {
super(context);
}
int gameBoard[][] = {{0, 1, 0, 0, 1}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void DrawBoard(Canvas canvas){
Paint Island = new Paint();
Island.setStyle(Paint.Style.FILL);
Island.setStyle(Paint.Style.FILL);
float stepX = canvas.getWidth() / 5.f;
float stepY = canvas.getHeight() / 5.f;
for (int i = 0; i < 5; i++) {
for (int R = 0; R < 5; R++) {
IslandX = i * stepX;
IslandY = R * stepY;
if (gameBoard[i][R] == 0) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 1) {
Island.setColor(Color.BLACK);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 2) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
}
}
}
}
}
To make your board visible, you have to move your code from Drawboard to the overwritten method onDraw and in the onCreate method, you have to call invalidate() on an instance of the class boardView, that calls the onDraw method to update the screen, if visible.
Related
I am currently making an android app which plays the puzzle game hashi. I am currently struggling to output the game grid. i want to output a 2d array like bellow-
1 0 0 0 1
0 2 0 0 2
2 0 3 0 1
0 0 0 0 0
0 0 2 0 2
however when i run the application in the emulator it outputs just a blank white screen.
main activity-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new boardView(this));
//sets the view to the board view to show the puzzle on open.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
game board class-
public class boardView extends View {
public float IslandX;
public float IslandY;
public int islandDiameter;
private Canvas canvas;
public boardView(Context context) {
super(context);
}
int gameBoard[][] = {{0, 1, 0, 0, 1}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void DrawBoard(Canvas canvas){
Paint Island = new Paint();
Island.setStyle(Paint.Style.FILL);
Island.setStyle(Paint.Style.FILL);
float stepX = canvas.getWidth() / 5.f;
float stepY = canvas.getHeight() / 5.f;
for (int i = 0; i < 5; i++) {
for (int R = 0; R < 5; R++) {
IslandX = i * stepX;
IslandY = R * stepY;
if (gameBoard[i][R] == 0) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 1) {
Island.setColor(Color.BLACK);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 2) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
}
}
}
}
}
To make your board visible, you have to move your code from Drawboard to the overwritten method onDraw and in the onCreate method, you have to call invalidate() on an instance of the class boardView, that calls the onDraw method to update the screen, if visible.
I am currently making an android app which plays the puzzle game hashi. I am currently struggling to output the game grid. i want to output a 2d array like bellow-
1 0 0 0 1
0 2 0 0 2
2 0 3 0 1
0 0 0 0 0
0 0 2 0 2
however when i run the application in the emulator it outputs just a blank white screen.
main activity-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new boardView(this));
//sets the view to the board view to show the puzzle on open.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
game board class-
public class boardView extends View {
public float IslandX;
public float IslandY;
public int islandDiameter;
private Canvas canvas;
public boardView(Context context) {
super(context);
}
int gameBoard[][] = {{0, 1, 0, 0, 1}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void DrawBoard(Canvas canvas){
Paint Island = new Paint();
Island.setStyle(Paint.Style.FILL);
Island.setStyle(Paint.Style.FILL);
float stepX = canvas.getWidth() / 5.f;
float stepY = canvas.getHeight() / 5.f;
for (int i = 0; i < 5; i++) {
for (int R = 0; R < 5; R++) {
IslandX = i * stepX;
IslandY = R * stepY;
if (gameBoard[i][R] == 0) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 1) {
Island.setColor(Color.BLACK);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 2) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
}
}
}
}
}
To make your board visible, you have to move your code from Drawboard to the overwritten method onDraw and in the onCreate method, you have to call invalidate() on an instance of the class boardView, that calls the onDraw method to update the screen, if visible.
I'm looking to create randomly moving clickable buttons in Android (for a childrens game).
I followed the code on this website to create some randomly moving circles around the screen
http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/
with the Animated words view changed to this:
public class AnimatedWordsView extends ImageView {
private Context myContext;
int [] xCoOrd = {-1, -2, -3, -4, -5, -6, -7, -8};
int [] yCoOrd = {-1, -2, -3, -4, -5, -6, -7, -8};
int [] xVeloc = {4, 8, 12, 16, 20, 20, 20, 20};
int [] yVeloc = {2, 4, 6, 8, 10, 12, 14, 16};
private Handler handler;
private final int FRAME_RATE = 30;
public AnimatedWordsView(Context context, AttributeSet attributes){
super(context, attributes);
myContext = context;
handler = new Handler();
}
private Runnable run = new Runnable() {
#Override
public void run() {
invalidate();
}
};
protected void onDraw(Canvas canvas){
BitmapDrawable [] word = {(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green),
(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_red), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_yellow),
(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_green), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_blue),
(BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_red), (BitmapDrawable) myContext.getResources().getDrawable(R.drawable.word_bubble_yellow)};
for(int count = 0; count <=7; count++ ) {
if (xCoOrd[count] < 0 && yCoOrd[count] < 0) {
xCoOrd[count] = this.getWidth() / 2;
yCoOrd[count] = this.getHeight() / 2;
} else {
xCoOrd[count] += xVeloc[count];
yCoOrd[count] += yVeloc[count];
}
if ((xCoOrd[count] > this.getWidth() - word[count].getBitmap().getWidth()) || (xCoOrd[count] < 0)) {
xVeloc[count] = xVeloc[count] * -1;
}
if ((yCoOrd[count] > this.getHeight() - word[count].getBitmap().getHeight()) || (yCoOrd[count] < 0)) {
yVeloc[count] = yVeloc[count] * -1;
}
canvas.drawBitmap(word[count].getBitmap(),xCoOrd[count],yCoOrd[count],null);
}
handler.postDelayed(run, FRAME_RATE);
}
What i'm looking for is something that does something similar to what this does, but allows me to add text to the circles and make them clickable
Is there any way to do this?
Thanks
You can use drawText method for Canvas to draw text:
public void drawText (CharSequence text, int start, int end, float x, float y, Paint paint)
Something like this:
Paint paint = new Paint();
canvas.drawPaint(paint);
paint.setColor(Color.MY_COLOR);
paint.setTextSize(24);
canvas.drawText("My Text", x, y, paint);
To click the view you just need to add a onClickListener to it:
myAnimatedWordsView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
I have three classes and am trying to make a game whereby users move along a grid depending on what is rolled by a die.
I have my main BoardGame class, containing the GUI and the counters which currently are Jlabel images (i'm open to suggestions as to what I could use instead of a JLabel - i wasnt so sure myself). I have a Grid class which I have arranged into a 2D array and called an instance of in the BoardGame class, and I have a die class which rolls a random number from 1-6.
I am trying to get me counters to start at the first square on the grid, and then advance in a left-to-right-right-to-left fashion. I am unsure however of how to make the counters move through the grid in the first place. Hopefully, if I can figure this out, I believe I can then implement them moving a specific amount via the die.
Thanks for the help in advance
GameBoard class:
public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel GreenDot;
private JLabel redDot;
private JButton startButton;
private Grid grid;
private Die die;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Grid grid = new Grid();
GameBoard inst = new GameBoard(grid);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public GameBoard(Grid grid) {
super();
this.grid = grid;
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
redDot = new JLabel();
getContentPane().add(redDot);
redDot.setText("jLabel1");
redDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/download.png")));
redDot.setBounds(220, 434, 20, 12);
redDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
GreenDot = new JLabel();
getContentPane().add(GreenDot);
GreenDot.setText("jLabel1");
GreenDot.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/3d-green-ball-th.png")));
GreenDot.setBounds(222, 453, 21, 13);
GreenDot.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Game");
startButton.setBounds(64, 443, 83, 23);
}
{
Board = new JLabel();
getContentPane().add(Board);
Board.setLayout(null);
Board.setText("jLabel1");
Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
Board.setBounds(204, -1, 742, 484);
}
pack();
this.setSize(963, 523);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
}
Grid class:
public class Grid {
int[][] multi = {
{ 0, 0,-1, 0, 0,-1, 0,-1, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 0,-1, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0,-1, 0, 0, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 1, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{ 0, 0, 0,-1, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0}
};
}
Die class:
public class Die {
public Die() {
}
public void dieRoll() {
int SIDES = 6;
int roll = (int) (Math.random() * SIDES) + 1;
System.out.println(roll);
}
}
A simple way of doing this would be to change the die class so it has
{
private int sides;
public Die(int numSides){
sides = numSides;
}
public int roll(){
return (int) (Math.random() * SIDES) + 1
}
}
Then you can roll a six sided die like so
//this creates the die
Die sixSides = new Die(6);
//this rolls the die and prints the roll
System.out.print("That roll got you a " + sixSides.roll());
to move along a 2D array, all you need to do is use the modulus and you need to create a point object class
public position move(int num, int x, int y){//input is dice roll int then current position
for(int i = 0; i < num;i++){
if(i%10==0){
y++;
x = 0;
}else{
x++;
}
}
return new point(x,y);
}
To access the x and y positions of the point object you would need to write get and set methods for the object.
something like this
point player1 = new point(0,0);
int xposition = player1.getX
I tried to implement the following processing code in a JFrame in netbeans.
code was taken from http://www.geekmomprojects.com/mpu-6050-dmp-data-from-i2cdevlib/
However after running MyFrame.java the it only shows a still image in the frame.
It is not taking the serial readings.
MyProcessingSketch.java
`package testprocessing;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Arrays;
import processing.core.*;
import processing.serial.*;
import processing.opengl.*;
import toxi.geom.*;
import toxi.processing.*;
public class MyProcessingSketch extends PApplet {
ToxiclibsSupport gfx;
Serial port; // The serial port
char[] teapotPacket = new char[14]; // InvenSense Teapot packet
int serialCount = 0; // current packet byte position
int synced = 0;
int interval = 0;
float[] q = new float[4];
Quaternion quat = new Quaternion(1, 0, 0, 0);
float[] gravity = new float[3];
float[] euler = new float[3];
float[] ypr = new float[3];
#Override
public void setup() {
// 300px square viewport using OpenGL rendering
size(300, 300, OPENGL);
gfx = new ToxiclibsSupport(this);
// setup lights and antialiasing
lights();
smooth();
// display serial port list for debugging/clarity
System.out.println(Arrays.toString(Serial.list()));
// get the first available port (use EITHER this OR the specific port code below)
//String portName = Serial.list()[0];
// get a specific serial port (use EITHER this OR the first-available code above)
String portName = "COM10";
// open the serial port
port = new Serial(this, portName, 9600);
// send single character to trigger DMP init/start
// (expected by MPU6050_DMP6 example Arduino sketch)
port.write('r');
}
#Override
public void draw() {
if (millis() - interval > 1000) {
// resend single character to trigger DMP init/start
// in case the MPU is halted/reset while applet is running
port.write('r');
interval = millis();
}
// black background
background(0);
// translate everything to the middle of the viewport
pushMatrix();
translate(width / 2, height / 2);
// 3-step rotation from yaw/pitch/roll angles (gimbal lock!)
// ...and other weirdness I haven't figured out yet
//rotateY(-ypr[0]);
//rotateZ(-ypr[1]);
//rotateX(-ypr[2]);
// toxiclibs direct angle/axis rotation from quaternion (NO gimbal lock!)
// (axis order [1, 3, 2] and inversion [-1, +1, +1] is a consequence of
// different coordinate system orientation assumptions between Processing
// and InvenSense DMP)
float[] axis = quat.toAxisAngle();
rotate(axis[0], -axis[1], axis[3], axis[2]);
// draw main body in red
fill(255, 0, 0, 200);
box(10, 10, 200);
// draw front-facing tip in blue
fill(0, 0, 255, 200);
pushMatrix();
translate(0, 0, -120);
rotateX(PI/2);
drawCylinder(0, 20, 20, 8);
popMatrix();
// draw wings and tail fin in green
fill(0, 255, 0, 200);
beginShape(TRIANGLES);
vertex(-100, 2, 30); vertex(0, 2, -80); vertex(100, 2, 30); // wing top layer
vertex(-100, -2, 30); vertex(0, -2, -80); vertex(100, -2, 30); // wing bottom layer
vertex(-2, 0, 98); vertex(-2, -30, 98); vertex(-2, 0, 70); // tail left layer
vertex( 2, 0, 98); vertex( 2, -30, 98); vertex( 2, 0, 70); // tail right layer
endShape();
beginShape(QUADS);
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80);
vertex( 100, 2, 30); vertex( 100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80);
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(100, -2, 30); vertex(100, 2, 30);
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, -30, 98); vertex(-2, -30, 98);
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, 0, 70); vertex(-2, 0, 70);
vertex(-2, -30, 98); vertex(2, -30, 98); vertex(2, 0, 70); vertex(-2, 0, 70);
endShape();
popMatrix();
}
void serialEvent(Serial port) {
interval = millis();
while (port.available() > 0) {
int ch = port.read();
if (synced == 0 && ch != '$') return; // initial synchronization - also used to resync/realign if needed
synced = 1;
print ((char)ch);
if ((serialCount == 1 && ch != 2)
|| (serialCount == 12 && ch != '\r')
|| (serialCount == 13 && ch != '\n')) {
serialCount = 0;
synced = 0;
return;
}
if (serialCount > 0 || ch == '$') {
teapotPacket[serialCount++] = (char)ch;
if (serialCount == 14) {
serialCount = 0; // restart packet byte position
// get quaternion from data packet
q[0] = ((teapotPacket[2] << 8) | teapotPacket[3]) / 16384.0f;
q[1] = ((teapotPacket[4] << 8) | teapotPacket[5]) / 16384.0f;
q[2] = ((teapotPacket[6] << 8) | teapotPacket[7]) / 16384.0f;
q[3] = ((teapotPacket[8] << 8) | teapotPacket[9]) / 16384.0f;
for (int i = 0; i < 4; i++) if (q[i] >= 2) q[i] = -4 + q[i];
// set our toxilibs quaternion to new data
quat.set(q[0], q[1], q[2], q[3]);
/*
// below calculations unnecessary for orientation only using toxilibs
// calculate gravity vector
gravity[0] = 2 * (q[1]*q[3] - q[0]*q[2]);
gravity[1] = 2 * (q[0]*q[1] + q[2]*q[3]);
gravity[2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
// calculate Euler angles
euler[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
euler[1] = -asin(2*q[1]*q[3] + 2*q[0]*q[2]);
euler[2] = atan2(2*q[2]*q[3] - 2*q[0]*q[1], 2*q[0]*q[0] + 2*q[3]*q[3] - 1);
// calculate yaw/pitch/roll angles
ypr[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
ypr[1] = atan(gravity[0] / sqrt(gravity[1]*gravity[1] + gravity[2]*gravity[2]));
ypr[2] = atan(gravity[1] / sqrt(gravity[0]*gravity[0] + gravity[2]*gravity[2]));
// output various components for debugging
//println("q:\t" + round(q[0]*100.0f)/100.0f + "\t" + round(q[1]*100.0f)/100.0f + "\t" + round(q[2]*100.0f)/100.0f + "\t" + round(q[3]*100.0f)/100.0f);
//println("euler:\t" + euler[0]*180.0f/PI + "\t" + euler[1]*180.0f/PI + "\t" + euler[2]*180.0f/PI);
//println("ypr:\t" + ypr[0]*180.0f/PI + "\t" + ypr[1]*180.0f/PI + "\t" + ypr[2]*180.0f/PI);
*/
}
}
}
}
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
// If it is not a cone, draw the circular bottom cap
if (bottomRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, tall, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}
public static void main(String[] args) {
PApplet.main(new String[] { "--present", "MyProcessingSketch" });
}
}
`
MyFrame.java
package testprocessing;
import javax.swing.JFrame;
public class MyFrame extends JFrame{
private MyProcessingSketch mysketch;
public MyFrame() {
setTitle("IMU");
setDefaultCloseOperation(EXIT_ON_CLOSE);
mysketch = new MyProcessingSketch();
mysketch.init();
add(mysketch);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.pack();
frame.setVisible(true);
}
}