draw rectangle on canvas


Avsana Khan

I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle is drawn on the far left. Can someone help me?

class gameui(context: Context, attrs: AttributeSet): View(context,attrs){
private val bluePaint = Paint().apply {
    color = Color.BLUE
    strokeWidth = 8f
}
var paddle1 = Rect()
var paddle2 = Rect()
var paint =  Paint()

init{

    val x = 50
    val y = 200
    val sideLength = 20


    paddle1 = Rect(x, y, sideLength, sideLength)
    paddle2 = Rect(x, y, sideLength, sideLength)



    paint = Paint()
    paint.color = Color.RED
}



override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    canvas!!.drawRect(paddle1, paint);

}

my output on the emulator

Patrick Elmquist

That's because you're positioning it at the same X,Y position as the first paddle, you want to position it correctly at

val x = canvas.width - 20 - sideLength

However, init {}since the view hasn't been measured or laid out, you don't have access to the canvas (or view width) when calling . So you should probably move the xcoordinate settings to the inside onDraw(..)or onSizeChanged(...)where you know the actual width of the view.

Related


draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle on canvas

Avsana Khan I'm trying to develop a ping pong game in Android where I will have two bats to hit the ball. I drew a bat on the canvas and it was placed at the far left of the screen. I need another bat on the far right of the screen, but every time the paddle i

Draw resizable rectangle on canvas

Inde · 穆里罗 (Masinde Muliro) I am trying to draw a resizable rectangle on a canvas element. I check the position of the mouse relative to the rectangle on the screen and then update the width/height of the triangle the mouse falls into. So far I've managed to s

draw rectangle in canvas

125fur I can draw rectangle on video on canvas. But it's hidden behind the video but can't suggest how I should draw the rectangle on the video through the side.Pls like I can draw the code on the image. : <p>Video to use:</p> <video id="video1" controls wi

Javascript Canvas draw rectangle or circle

nameless I'm looking for a way to draw rectangles or circles "on the fly" on a canvas. I found various ways to fillRect()draw rectangles, but not in real time. I mean, being able mouseDown()to move it from one point to another point in the canvas, it defines t

Canvas issue, unable to draw rectangle

Jose Jimenez I have the following code to draw a blue rectangle and a red line on a black background. function love.load() love.window.setMode(300,200,{fullscreen=false}) end function love.draw() love.graphics.setColor(0, 0, 255, 255) love.graphic

How to draw a rectangle with border on canvas?

je3rdty var can = document.getElementById('canvas1'); var ctx = can.getContext('2d'); //ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise); ctx.beginPath() ctx.arc(100,100,100,0,Math.PI*2, false); // outer (filled) ctx.arc(100,100,55,0,Math.PI*2, true)

Draw rectangle with fixed width canvas

Derek Palmer I'm trying to let the user draw a rectangle on an image inside a canvas using the mousemove event. It works fine with this code: HTML: <canvas id="canvas"></canvas> JavaScript: window.onload = drawCanvas('http://www.therebeldandy.com/wp-conte

Canvas issue, unable to draw rectangle

Jose Jimenez I have the following code to draw a blue rectangle and a red line on a black background. function love.load() love.window.setMode(300,200,{fullscreen=false}) end function love.draw() love.graphics.setColor(0, 0, 255, 255) love.graphic

How to draw a rectangle with border on canvas?

je3rdty var can = document.getElementById('canvas1'); var ctx = can.getContext('2d'); //ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise); ctx.beginPath() ctx.arc(100,100,100,0,Math.PI*2, false); // outer (filled) ctx.arc(100,100,55,0,Math.PI*2, true)

How to draw a rectangle with border on canvas?

je3rdty var can = document.getElementById('canvas1'); var ctx = can.getContext('2d'); //ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise); ctx.beginPath() ctx.arc(100,100,100,0,Math.PI*2, false); // outer (filled) ctx.arc(100,100,55,0,Math.PI*2, true)

How to draw a rectangle with border on canvas?

je3rdty var can = document.getElementById('canvas1'); var ctx = can.getContext('2d'); //ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise); ctx.beginPath() ctx.arc(100,100,100,0,Math.PI*2, false); // outer (filled) ctx.arc(100,100,55,0,Math.PI*2, true)

How to draw a rectangle with border on canvas?

je3rdty var can = document.getElementById('canvas1'); var ctx = can.getContext('2d'); //ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise); ctx.beginPath() ctx.arc(100,100,100,0,Math.PI*2, false); // outer (filled) ctx.arc(100,100,55,0,Math.PI*2, true)

How to draw a rectangle with border on canvas?

je3rdty var can = document.getElementById('canvas1'); var ctx = can.getContext('2d'); //ctx.arc(x,y,radius,startAngle,endAngle, anticlockwise); ctx.beginPath() ctx.arc(100,100,100,0,Math.PI*2, false); // outer (filled) ctx.arc(100,100,55,0,Math.PI*2, true)

How to set stroke color to draw rectangle on canvas?

sunjinbo I want to draw a rounded rectangle with blue stroke and red fill, but I can't find a way to set the stroke color in the Paint class. How can I do this? mCanvas.drawColor(mBackgroundColor, PorterDuff.Mode.CLEAR); mCanvas.setDrawFilter(mPaintFla

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

How to draw rectangle around text in html canvas?

tom I know I read something about rise and font height on canvas, but I don't get it at all. First, why draw text from right to top instead of right to bottom like a rectangle. I can't find it anywhere in the documentation. Then what if I want to draw a rectan

How to set stroke color to draw rectangle on canvas?

sunjinbo I want to draw a rounded rectangle with blue stroke and red fill, but I can't find a way to set the stroke color in the Paint class. How can I do this? mCanvas.drawColor(mBackgroundColor, PorterDuff.Mode.CLEAR); mCanvas.setDrawFilter(mPaintFla

How to set stroke color to draw rectangle on canvas?

sunjinbo I want to draw a rounded rectangle with blue stroke and red fill, but I can't find a way to set the stroke color in the Paint class. How can I do this? mCanvas.drawColor(mBackgroundColor, PorterDuff.Mode.CLEAR); mCanvas.setDrawFilter(mPaintFla

How to draw partial rounded rectangle on android canvas?

Rocky I want to draw a rounded rectangle on canvas that is filled with color (or bitmap) and has a stroke , but without some selected corners and without borders on some selected sides . Any ideas how I can do this? 7 heaven You can divide the drawing process

Draw canvas text outside rectangle along edge

coders I'm trying to write descriptions for each edge along a rectangle. The reason is to describe the length of each edge inside and outside (along the sides) of the rectangle. Is there a way to achieve this? var c = document.getElementById("myCanvas"); var c

draw canvas 3d rectangle

zero I have extended the View class and overridden the draw method. In the draw method, I use the canvas to draw the rectangle. But this rectangle is simple and I need to behave as a blow. How can I draw the rectangle shown in the image below? Joa Create a pat

How to draw rectangle manually in html without canvas?

Utku Demir In this photo what I mean is better understood In the given photo, I am using the canvas to draw; however, I want to do it the other way, because I want to create a draggable rectangle and give an ID to the created rectangle. For example, if I use a

Tkinter won't draw rectangle on Canvas

Cadel Watson I'm trying to initialize a grid of blue rectangles with a user-specified size. However, the rectangle is not drawn on the initialized canvas. I am trying to store them in a matrix for later use. My code is as follows: import Tkinter import sys fro

Draw rounded rectangle using RectF and canvas?

TokTok123 I am trying to draw a rounded rectangle using RectF and canvas.drawRoundRect(). Please see my code below: package com.example.test; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas

HTML canvas - draw rectangle at selected location

Marcos I'm making a simple game with AngularJS and I want to add a canvas to the game. I've never used it before, but am still learning. I do not know what to do: I made a grid for easy viewing. So what I want is when the user clicks on a rectangle, that recta

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co