• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to draw image in canvas

 
Yugo Amaryl
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Google found many similar topics, but either they were unhelpful, or nobody answered the questions.
So, here is my problem. I'm following this tutorial: Android Game Development – Displaying Images with Android

To summarize, the MainActivity has this code:


So, the code above creates a MainGamePanel with a reference to itself as a parameter.
MainGamePanel's constructor launches MainThread with a reference to itself too:



MainGamePanel contains this code:



And MainThread contains this code:



I think the only thing I changed as compared to the tutorial, is that I call "draw(canvas)" instead of "onDraw(canvas)".
As expected, the onDraw method is called automatically (I checked it is actually called, and it is), but nothing is ever drew. The screen remains completely black.

I unsuccessfully tried many tweaks. Among other things I figured it could be useful, in MainGamePanel.onDraw to call:



It didn't change anything though.

Any idea?
Thanks.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yugo Amaryl wrote:I think the only thing I changed as compared to the tutorial, is that I call "draw(canvas)" instead of "onDraw(canvas)".
...
I unsuccessfully tried many tweaks....

The 'I think...' and 'tried many tweaks' part make me wary. First thing I would try would be revert back (or make a new test application) to the exact code as listed in the tutorial and see if it works. Just because it is on the internet doesn't mean it actually works. Then you need to find out exactly what is happening. Look at LogCat to see if there are any error messages. Run the application through the debugger and step through the game loop to see what is happening. Look at thread dumps to see if there are hung threads/deadlock. Don't forget to check the simple things - like the code which is supposed to paint onto the canvas is called, the sprites/images exist and are loaded, etc...

Unfortunately, the code you posted isn't complete, and going through the tutorial to build the code from scratch will be tedious work and time best spent elsewhere (and likely to come up with different code than you because you tweaked things). If you still have problems after going through the above debugging steps, then you need to post a full working code example - create an SSCCE (<- link) which we can have a look at that shows the problem (please don't post your entire current program, too much code is as bad as too little. Just post an example that has minimal code and behaves the same way).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic