I am trying to animate my object. For drawing an object I need to use a canvas. I can send canvas to the object constructor only from onDraw function. But doing that, a new object is creating on every step of animation.
public class DrawView extends View {
public static Paint paint;
public DrawView(Context context) {
super(context);
paint = new Paint();
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
Line s = new Line(10, 10, canvas);
super.onDraw(canvas);
s.move();
invalidate();
}
}
class Line{
private float x, y;
private Canvas canvas;
public Line(float x, float y, Canvas canvas) {
this.x = x;
this.y = y;
this.canvas = canvas;
}
public void move(){
draw();
x++;
y++;
}
public void draw(){
canvas.drawLine(x, y, x + 5, y + 5, DrawView.paint);
}
}
You mean this???
public void move(Canvas canvas){
draw(Canvas canvas);
x++;
y++;
}
public void draw(Canvas canvas){
canvas.drawLine(x, y, x + 5, y + 5, DrawView.paint);
}
Related
I am trying to move actor on stage from point to point by actions and it is not working, i have tried for hours and it just not working, i will be glad for help..
I have searched by the internet for codes and all i tried didn't worked, i really dont understand what is wrong with this code, it is basically the same as a working one on a tutorial in the internet
the actor class:
public class Player extends Actor {
float x,y;
float screenWidth,screenHeight;
private Sprite sprite;
Texture image;
float width,height;
public Player(Texture image,float x, float y,float width,float height, float screenWidth, float screenHeight)
{
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
this.image = image;
sprite = new Sprite(image, 0, 0, image.getWidth(), image.getHeight());
sprite.setPosition(x, y);
}
#Override
public float getX() {
return x;
}
#Override
public void setX(float x) {
this.x = x;
}
#Override
public float getY() {
return y;
}
#Override
public void setY(float y) {
this.y = y;
}
#Override
public float getWidth() {
return width;
}
#Override
public void setWidth(float width) {
this.width = width;
}
#Override
public float getHeight() {
return height;
}
#Override
public void setHeight(float height) {
this.height = height;
}
#Override
public void act(float delta) {
super.act(delta);
}
#Override
public void draw (Batch batch, float parentAlpha) {
batch.draw(sprite,x,y,width,height);
}
}
and the main class:
player = new Player(playerTexture,screenWidth/2,screenHeight-200,playerTexture.getWidth(),playerTexture.getHeight(),screenWidth,screenHeight);
MoveToAction action = new MoveToAction();
action.setPosition(300f,0f);
action.setDuration(10f);
player.addAction(action);
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(orthographicCamera.combined);
batch.begin();
// batch.draw(playerTexture,10,10,10,10);
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw(); //this will call the batch to draw the sprite
}
Actor already has x, y, width, and height fields. Since you created your own, you have hidden those parameters.
The source of your problem is that you failed to override setPosition() to use your fields. So the move action calls that and changes the fields that you have hidden and when you draw it, you are using your own fields that have not been changed.
Don't hide fields. It is very error-prone. You should delete the fields I mentioned above, along with all your overrides of getters and setters.
Here's a rule of thumb for OOP in general: if you are overriding any getter or setter without calling the super method, you are probably breaking something.
Unrelated, but you should be using TextureRegion instead of Sprite. A Sprite is a TextureRegion with additional parameters for size and position. Since those features are also in Actor, it is redundant and error-prone to use Sprite.
First, thank you for reading this post, any help is welcome.
My actor is correctly rendered on the stage, but with the Actions.moveTo, it leaves a trail ? I just don't get it. It's like the texture is rendered at a new position on every new frame.
Here is my code for my class :
public class SlidingCap extends Actor {
private Texture capOver;
private float xPosition;
private float yPosition;
public SlidingCap(float x, float y) {
this.xPosition = x;
this.yPosition = y;
this.capOver = new Texture(Gdx.files.internal("images/cappingPlate.png"));
setBounds(x, y, 288, 180);
}
#Override
public void act(float delta) {
super.act(delta);
}
#Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(capOver, getX(), getY(), 288, 180);
this.addAction(Actions.moveTo(xPosition+10, yPosition+10, 5f));
}
}
And the ScreenGame render method:
#Override
public void render(float delta) {
gameStage.act(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.capMiniGame = new SlidingCap(100, 100);
this.gameStage.addActor(capMiniGame);
gameStage.draw();
}
You're adding a new SlidingCap every frame with this code
this.capMiniGame = new SlidingCap(100, 100);
this.gameStage.addActor(capMiniGame);
Try adding the SlidingCap once in the Create or show method instead of every frame
Code:
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {
DrawView drawView;
private GestureDetectorCompat g1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.TRANSPARENT);
setContentView(drawView);
g1 = new GestureDetectorCompat(this,this);
g1.setOnDoubleTapListener(this);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
g1.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
DrawView.x_touch = e.getX();
DrawView.y_touch = e.getY();
Log.v("id2","message2");
return false;
}
Code for drawing:
public class DrawView extends View {
Paint paint = new Paint();
static float x_touch = -1;
static float y_touch = -1;
public DrawView(Context context) {
super(context);
super.setWillNotDraw(false);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x;
int maxY = mdispSize.y;
canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
canvas.drawLine(0, maxY/3, maxX, maxY/3, paint);
canvas.drawLine(0, maxY*2/3, maxX, maxY*2/3, paint);
Log.v("id1","message1");
if (x_touch>0 && y_touch > 0)
canvas.drawText("X",x_touch,y_touch,paint);
}
}
On doing some research I found that one need to call setWillNotDraw(false) while overriding onDraw() or use dispatchDraw().
I have tried both but Draw function is called only once.
I am new to android development.
Change your code as follow :
public class DrawView extends View {
Paint paint = new Paint();
float x_touch = -1;
float y_touch = -1;
public DrawView(Context context) {
super(context);
super.setWillNotDraw(false);
paint.setColor(Color.BLACK);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x;
int maxY = mdispSize.y;
canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
canvas.drawLine(0, maxY/3, maxX, maxY/3, paint);
canvas.drawLine(0, maxY*2/3, maxX, maxY*2/3, paint);
Log.v("id1","message1");
if (x_touch>0 && y_touch > 0)
canvas.drawText("X",x_touch,y_touch,paint);
}
public void setXandY(float x, float y) {
x_touch = x;
y_touch = y;
this.invalidate();
}
}
And :
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
drawView.setXandY(e.getX(), e.getY());
return (true);
}
So I made a painting class which has got an ontouchevent method where i set the xpos of my rectangle to the xpos of the touch event but the rectangle isnt moving! how can I correct that?
rectangle class:
public class myRectangle{
public int xpos;
public int ypos;
public int size;
private Paint paint;
public myRectangle(){
size = 40;
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.BLUE);
}
public void drawRectangle(Canvas c,int x, int y){
c.drawRect(x, y, size, size, paint);
}
}
and my paint view:
public class Painting extends View {
myRectangle player;
float x;
float y;
public Painting(Context context) {
super(context);
player = new myRectangle();
// TODO Auto-generated constructor stub
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
String s = Float.toString(event.getX());
String c = Float.toString(event.getX());
Log.d(c, s);
return super.onTouchEvent(event);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
player.drawRectangle(canvas, (int)x, (int)y);
}
}
I can also post the main activity if needed
the only thing missing is the invalidate() call in your onTouchEvent. invalidate() schedule a draw
I'm in the process of making a simple 2-D game, however I am having trouble drawing images. Below are a few classes that are relevant to the problem
private Vector<Bullet> ammo = new Vector<Bullet>(100);
public class Bullet{
Image img;
int x, y, speed;
boolean show;
Bullet(Image img,int x, int y, int speed, boolean show){
this.x = x;
this.y = y;
this.speed = speed;
this.img = img;
this.show = show;
}
public void draw(ImageObserver obs) {
if(show)
g2.drawImage(img, this.x, this.y, obs);
}
public void update(){
this.y -= 1;
}
}
public class Movement{
....
Movement(....){
.....
}
public void fly(){
......
ammo.add(new Bullet(bullet1, m.x, m.y, 7, true));
}
public class MyPlane {
KeyControl key;
Movement flight;
Image img;
int x, y, speed, move = 0;
int boom;
...
}
public void drawDemo() {
...
for(Bullet bullets: ammo)
bullets.update();
...
for(Bullet bullets: ammo)
bullets.draw(this);
}
}
When I call bullets.draw(this) nothing actually is drawn on the screen. I know however that the ammo vector does contain the correct information, such as the x coordinate, y coordinate... I'm using Graphics 2-D by the way. Any help and or suggestions would be greatly appreciated thanks.
public void paint(Graphics g) {
if(bimg == null) {
Dimension windowSize = getSize();
bimg = (BufferedImage) createImage(windowSize.width,
windowSize.height);
g2 = bimg.createGraphics();
}
drawDemo();
g.drawImage(bimg, 0, 0, this);
}
I think you should draw the image like this:
public void draw(Graphics g2) {
if(show)
g2.drawImage(img, this.x, this.y, null);
}
Then in your plane class you have to add Graphics as an argument of the drawDemo() method:
public void drawDemo(Graphics g2) {
...
for(Bullet bullets: ammo)
bullets.update();
...
for(Bullet bullets: ammo)
bullets.draw(g2);
}
}
and finally in you paint(Graphics g) method you call this:
public void paint(Graphics g) {
/*
...
*/
drawDemo(g2);
g.drawImage(bimg, 0, 0, this);
}