I created 5 sprites using libgdx library, then want to assign the different size to all of them between 0 to 10 and then move from left to right. 5 sprite is succussfully created but the problem is in the size. i tried the S.nextInt(10). but its not working.
Also i want to create all the sprites between 100-200 height. how to do it?
int number = 5;
sprite = new Sprite[number];
for (int i = 0; i < number; i++) {
sprite[i] = new Sprite(texture);
int size = S.nextInt(10);
sprite[i].setSize(size, size);
speed = S.nextInt(2);
sprite[i].setPosition(speed, 100);
}
batch.begin();
for (int i = 0; i < number; i++) {
sprite[i].draw(batch);
}
batch.end();
Related
game.batch.begin();
for (Array obstacle_array123: obstacle_array) {
body = obstacle_array123;
for (Body bodies: body) {
if (bodies.getUserData() instanceof Array && bodies.isActive()) {
sprites_array = (Array)bodies.getUserData();
for (int fix_pos = 0; fix_pos < sprites_array.size; fix_pos++) {
sprite = sprites_array.get(fix_pos);
if (verts.size != 0) verts.removeRange(0, verts.size - 1);
f = bodies.getFixtureList().get(fix_pos);
s = (PolygonShape)f.getShape();
transform = bodies.getTransform();
for (int i = 0; i < s.getVertexCount(); i++)
{
s.getVertex(i, tmp);
transform.mul(tmp);
verts.add(new Vector2(tmp));
}
rotation_point.set((verts.get(0).x + verts.get(1).x + verts.get(2).x + verts.get(3).x) / 4, (verts.get(0).y + verts.get(1).y + verts.get(2).y + verts.get(3).y) / 4);
sprite.setPosition(rotation_point.x - sprite.getWidth() / 2, rotation_point.y - sprite.getHeight() / 2);
sprite.setRotation(bodies.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(game.batch);
}
}
}
}
game.batch.end();
I have a game where my bodies are made from multiple square fixtures, so this is the code to render each square sprite on each square fixture.
2 problems - 1.st --> it only renders the first sprite in the array
2.nd --> if you look at the following loop (SOLVED)
for (int i = 0; i < s.getVertexCount(); i++)
{
s.getVertex(i, tmp);
transform.mul(tmp);
verts.add(new Vector2(tmp));
}
well it is apperantly different compared to
for (int i = 0; i < s.getVertexCount(); i++)
{
s.getVertex(i, tmp);
transform.mul(tmp);
verts.add(tmp);
}
The spawned coordinates in 2nd example are wrong for half width and half height of the square.
When I try to get the coordinated from both examples the numbers are the same, but when setting the sprite position, 2nd example goes off.
You should probably ask both questions separately, but to answer your second question, then they ARE different.
In the first one you add a new Vector2 to verts each time through the loop. So verts will end up holding a load of different Vector2's.
In the second one you add the same Vector2 to verts over and over again, so it will just have one Vector2 with the same value over and over again (remember Java is pass by reference).
Caveat - My answer assumes that verts is some sort of standard collection or a libgdx Array.
In my Android project,
Here is my code.
for (int x = 0; x < targetBitArray.length; x += weight) {
for (int y = 0; y < targetBitArray[x].length; y += weight) {
targetBitArray[x][y] = bmp.getPixel(x, y) == mSearchColor;
}
}
but this code wastes a lot of time.
So I need to find way faster than bitmap.getPixel().
I'm trying to get pixel color using byte array converted from bitmap, but I can't.
How to replace Bitmap.getPixel()?
Each Bitmap.getPixel method invocation requires a lot of resources, so you need to avoid the amount of requests in order to improve the performace of your code.
My suggestion is:
Read the image data row-by-row with Bitmap.getPixels method into a local array
Iterate along your local array
e.g.
int [] rowData= new int [bitmapWidth];
for (int row = 0; row < bitmapHeight; row ++) {
// Load row of pixels
bitmap.getPixels(rowData, 0, bitmapWidth, 0, row, bitmapWidth, 1);
for (int column = 0; column < bitmapWidth; column ++) {
targetBitArray[column][row] = rowData(column) == mSearchColor;
}
}
This will be a great improvement for the performace of your code
i need to combine multiple images to a single bitmap. i need to combine the images in the range from 2-6. The path of the image is taken from my app's database and image is stored in my external sd card.
i have combined 2 images . But i need to combine 3, 4, 5 and 6 images in to single canvas.
My code for combining 2 images is provided below:-
private Bitmap combineImageIntoOne(ArrayList<Bitmap> a) {
int top = 0;
int width = 0, height = 0;
for (int j = 0; j < a.size(); j++) {
width += a.get(j).getWidth();
}
height = a.get(0).getHeight();
Bitmap combineBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(combineBitmap);
for (int i = 0; i < a.size(); i++) {
System.out.println("image width = " + top);
comboImage.drawBitmap(a.get(i), top, 0f, null);
top = top + a.get(i).getWidth();
}
System.out.println("combineBitmap combineBitmap..in.."+combineBitmap);
return combineBitmap;
}
I think you can combine 3, 4, 5 and 6 images in to single canvas by the same code.
If you want to use the tallest bitmap's height,you can change the code like this.
//get the max height
height = a.get(0).getHeight();
for (int j = 0; j < a.size(); j++) {
width += a.get(j).getWidth();
if(height<a.get(j).getHeight()){
height=a.get(j).getHeight();
}
}
i have a large image ,then i cut it into 9 equal small images. that is my code
Image pricer[][] = new Image[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
pricer[i][j] = new Image(TextureRegion.split(bg, tile, tile)[i][j]);
pricer[i][j].setSize(200, 200);
pricer[i][j].setPosition((j*200),600-(i*200));
stage.addActor(pricer[i][j]);
}
}
now, i want to create border for those images that i devide into small peace.many thanks for your help
You can use a ShapeRenderer:
ShapeRenderer renderer = new ShapeRenderer();
renderer.begin(ShapeType.Line)
renderer.setColor(color);
renderer.rect(image.getImageX(), image.getImageY(), image.getImageWidth(), image.getImageHeight);
renderer.end();
If I have an image containing black lines, like the image below. How can I detect the black lines.( to know the x,y of dots on this lines) ?
I want to know how to get started with this topic. What library should I learn to go through
You can use Catalano Framework.
http://code.google.com/p/catalano-framework/
FastBitmap image = new FastBitmap(bitmap);
image.toGrayscale();
int width = image.getWidth();
int height = image.getHeight();
ArrayList<IntPoint> lst = new ArrayList<>();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (image.getGray(i, j) == 0)
lst.add(new IntPoint(i,j));
}
}