Image rounded corner

Image rounded corner

Postby kapw001 » Fri Oct 09, 2015 7:41 am

Hi
I want to add rounded corner for image.how to add please help me

Below code
package com.me.mygdxgame;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Net.HttpMethods;
import com.badlogic.gdx.Net.HttpRequest;
import com.badlogic.gdx.Net.HttpResponse;
import com.badlogic.gdx.Net.HttpResponseListener;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.StreamUtils;

public class backup implements ApplicationListener {
private Stage stage;
private static final String reallyLongString1 = "dfjghdjkfhgdhfvxckv ";

private static final String reallyLongString = "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
+ "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
+ "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n";

@Override public void create() {

HttpRequest request = new HttpRequest(HttpMethods.GET);
request.setUrl("");
Gdx.net.sendHttpRequest(request, new HttpResponseListener() {
@Override
public void handleHttpResponse (HttpResponse httpResponse) {
String a=httpResponse.getResultAsString();
JsonValue root = new JsonReader().parse(a);
for(int i=0;i<root.size;i++){
System.out.println("KKKk"+root.get(i).getString("subname"));
// downloadimage(root.get(i).getString("url"));
}
// System.out.println("KKKk"+root.size);
//
// System.out.println("KKKk"+root+" "+a);
// Gdx.app.log("HttpRequestExample", "response: " + httpResponse.getResultAsString());
// Json json = new Json();
// json.fromJson(p.class, httpResponse.getResultAsString());
// System.out.println();

}

@Override
public void failed (Throwable t) {
Gdx.app.error("HttpRequestExample", "something went wrong", t);
}

@Override
public void cancelled () {
Gdx.app.log("HttpRequestExample", "cancelled");
}
});








this.stage = new Stage();
Gdx.input.setInputProcessor(this.stage);
// final Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
BitmapFont font=new BitmapFont();
LabelStyle style=new LabelStyle(font, Color.WHITE);
final Label text = new Label(reallyLongString1, style);
text.setAlignment(Align.center);
text.setWrap(true);
final Label text2 = new Label("This is a short string!", style);
text2.setAlignment(Align.center);
// text2.setWrap(true);
final Label text3 = new Label(reallyLongString, style);
text3.setAlignment(Align.center);
// text3.setWrap(true);

Image imageLogo = new Image();
imageLogo.setDrawable(new TextureRegionDrawable(
new TextureRegion(new Texture(Gdx.files.internal("data/libgdx.png")))));
imageLogo.setScaling(Scaling.fillX);
final Table scrollTable = new Table();
// scrollTable.add(text);
// scrollTable.row();
// scrollTable.add(text2);
// scrollTable.row();
// scrollTable.add(text3);
// scrollTable.row();
// scrollTable.add(imageLogo).fill().expand();
float w=Gdx.graphics.getWidth();
float h=Gdx.graphics.getHeight();
Table container = new Table();
for(int i=1;i<31;i++){
scrollTable.row();

//scrollTable.setBackground(new NinePatchDrawable(getNinePatch(("data/test/ninepatches.png"))));
Pixmap pixels1 = new Pixmap(Gdx.files.internal("data/test/"+i+".jpg"));

Pixmap pixels=new Pixmap(pixels1.getWidth(), pixels1.getHeight(), Format.RGBA8888);
pixels.drawPixmap(getPixmapRoundedRectangle(pixels.getWidth(), pixels.getHeight(), 54,Gdx.files.internal("data/test/"+i+".jpg")),0, 0);

// pixels1.dispose();

Texture t=new Texture(pixels1);
// t.draw(pixels, 0, 0);
Image im=new Image(t);

float xx=(w/im.getWidth())*.96f;
System.out.println("hhh before"+im.getPrefHeight());

scrollTable.add(im).width(w).height(im.getHeight()*xx).pad(10f);


pixels.dispose();

}


final ScrollPane scroller = new ScrollPane(scrollTable);
scroller.setScrollingDisabled(true, false);
final Table table = new Table();
table.setFillParent(true);
// table.setWidth(200.0f);
table.add(scroller).fill().expand().pad(10);

this.stage.addActor(table);
}

private NinePatch getNinePatch(String string) {
final Texture t = new Texture(Gdx.files.internal(string));

// create a new texture region, otherwise black pixels will show up too, we are simply cropping the image
// last 4 numbers respresent the length of how much each corner can draw,
// for example if your image is 50px and you set the numbers 50, your whole image will be drawn in each corner
// so what number should be good?, well a little less than half would be nice
return new NinePatch( new TextureRegion(t, 1, 1 , t.getWidth() - 2, t.getHeight() - 2), 10, 10, 10, 10);
}

@Override public void render() {
this.stage.act();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.stage.draw();
}

@Override public void resize(final int width, final int height) {
stage.getViewport().update(width, height, false);

}
@Override public void pause() {}
@Override public void resume() {}
@Override public void dispose() {}

public static Pixmap getPixmapRoundedRectangle(int width, int height, int radius,FileHandle f) {

Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
pixmap.setColor(Color.WHITE);

// Pink rectangle
pixmap.fillRectangle(0, radius, pixmap.getWidth(), pixmap.getHeight()-2*radius);

// Green rectangle
pixmap.fillRectangle(radius, 0, pixmap.getWidth() - 2*radius, pixmap.getHeight());


// Bottom-left circle
pixmap.fillCircle(radius, radius, radius);

// Top-left circle
pixmap.fillCircle(radius, pixmap.getHeight()-radius, radius);

// Bottom-right circle
pixmap.fillCircle(pixmap.getWidth()-radius, radius, radius);

// Top-right circle
pixmap.fillCircle(pixmap.getWidth()-radius, pixmap.getHeight()-radius, radius);
//pixmap.drawPixmap(new Pixmap(f), 0,0);
return pixmap;
}







}
kapw001
 
Posts: 2
Joined: Mon Mar 24, 2014 3:38 pm

Return to Newton

Who is online

Users browsing this forum: No registered users and 1 guest