Canvas.DrawBitmap() doesn´t draw anything on android!! :( - java

i´m trying to make a game, but im stuck on the drawing.....
i make a custom surfaceView (with a draw() and update() methods) and my game loop(updating and drawing the view).
Everythong goes perfectly until i use the method Canvas.drawBitmap(Bitmap, Rect src, Rect dst, Paint p), becuase when i try to draw, for example :
Canvas.DrawBitmap(bitmap, null, new Rect(100, 100, 200, 150), null);
it doenst draw anything!! but here is the wearest part, if i put sumething like this:
Canvas.DrawBitmap(bitmap, null, new Rect(0,0,100,150), null);
it draw it perfectly as it has to be!!!! and if i put something like this:
Canvas.DrawBitmap(bitmap, null, new Rect(50,0,100,150), null);
it forms like limits, i mean, thas everything over the(for example, becuase im not sure how many pixels) 100px height doesn´t apear!!! i have tried many ways, PLEASE!!! can you help me!!!
(PD: My native language is spanish, i sorry if my text have some issue.)

The last parameter to any Canvas#draw methods as an object of Paint class. Without it, Canvas cannot do any drawing.
Before you do any drawing, initialize a Paint object with Paint paint = new Paint() and pass it to the Canvas#draw method. Remember to null the Paint object when you're done drawing, as it takes up a lot of memory.

Related

Android Canvas Graphics

I'm extremely new to Android development, but I have lots of experience with Java. What I'd like to know is how to independently draw on a View using a Canvas object.
What I want is an Android equivalent of this Java code:
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.drawImage(whateverImage, 0, 0, null);
g = frame.getGraphics(); /*just pretend 'frame' is a JFrame that we're using*/
g.drawImage(img, 0, 0, 100, 100, null);
Basically all that code does is make a blank bitmap image, draw on that image, then draw the bitmap onto a JFrame to display it.
I'm aware that Android doesn't use BufferedImage or Graphics, but I believe that the Android pseudo-equivalents are Bitmap and Canvas, respectively.
The problem is that when I have code that like this:..
Bitmap guy = BitmapFactory.decodeResource(getResources(), R.drawable.guy);
Canvas c = new Canvas(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888));
c.drawBitmap(guy, 0, 0, null);
...I take it that the same concept of a blank bitmap being created and then drawn on occurs. However, I don't know how to then draw the Canvas onto a View, SurfaceView, or some other method that will display it on my device's screen.
It's important to note that I do not want to #Override onDraw and use it to draw my graphics, as I would like to control the drawing independently using some sort of thread loop.
If I did not provide enough information or my question is nonsensical, feel free to either ask me about it or ignore it. Any help is greatly appreciated.
Also, if anybody has any tips, tutorials, references, or anything else to better my understanding of layouts, views, etc. that would be greatly appreciated as well. Like I said, I am very inexperienced and trying to learn.
EDIT: Now working. Thanks :) https://i.stack.imgur.com/ugWlu.png
The only way to draw onto a View is to override onDraw. What you can do though is draw to an in memory bitmap (like you do here) in any other function, then draw the bitmap to the screen in onDraw. Just do
public void onDraw(Canvas canvas) {
canvas.drawBitmap(inMemoryBitmap, 0, 0, paint);
}

Apply AffineTransform to TexturePaint

I would like to know how can I rotate a texture paint with image in java.
Normally it is easy to rotate the Graphics2D g2 object, but I don't want to do that. I also don't want to rotate my shape and do some crazy rotation scales, etc. It will be easy to normally do the drawing and fills with paint while paint itself is scaled, rotated, etc.
I also don't want to create a rotated image and then texturepaint it.
There is a createContext inside the texturepaint class but I don't know how can I use it to rotate the original image.
I hope you understand what I mean by rotating the texture itself nwo :) ?
Thank you.
Edit:
I was playing with Java custom Paint implementation performance issue but there is an issue I cannot handle. When there is an offset to where tiling will be started, it does not start correctly.
Sorry for mixing up the problem.
I decided to create a second shape before transform the canvas, and then rotate it back to match the inverse of the trnsform.
This solved my problem.
try {
this.shape = new Path2D.Double(bx.createInverse().createTransformedShape(shape));
} catch (NoninvertibleTransformException ex) {
}

Canvas drawText, how to rotate and how to change "anchor-point"?

Good evening,
It is about an android app.
I would like to use the method Canvas.drawText(...). But I don't know how I can rotate the text. I need the text at a certain position at a certain angle. Do you know how I can achieve this?
Next question, usually the point to which the position coordinates are refering is the lower left corner. I want to change this "anchor-point" to the lower center. Is that possible? The pivot-point of the rotation should be the same.
I guess simple questions, but I don't know how to do this. Thanks for help.
here is a basic example of drawing text on a path, to get it how you want you should mess with the path and the paint Path, Paint
Paint paint = new Paint();
Path path = new Path();
paint.setColor(Color.BLACK);
path.moveTo(canvas.getWidth()/2, 0);
path.lineTo(canvas.getWidth()/4, 400);
canvas.drawTextOnPath("text manipulated", path, 0, 0, paint);

J2ME Game Canvas

In J2ME, only a screen or a canvas can be displayed at a time. The screen can have multiple objects inside it (textfield, form, etc.) while a canvas can only hold a gamecanvas.
The question is: Is it possible to have multiple game canvases in one canvas?
I'm trying to display two at the same time, one at the top and one at the bottom.
I'd like to repaint the bottom canvas without repainting the top.
Thank you in advance! Any form of help will be appreciated! :)
No, you can only display one Canvas or GameCanvas object at a time.
But: If you're asking about having 2 Canvas objects, because you need to update 2 parts of the screen independent of each other, you can do that by using 2 Image objects.
Simply get the Graphics object of each Image with Image.getGraphics();
Then you can draw onto each image.
And finally draw both images on the canvas (or just one of them, if you only want to update one part of the screen).
Example using GameCanvas:
Image topImage = Image.createImage(width, height);
Image bottomImage = Image.createImage(width, height);
Graphics topG = topImage.getGraphics();
Graphics bottomG = bottomImage.getGraphics();
Graphics g = getGraphics(); // Get graphics of the GameCanvas
drawStuffOn-topG();
drawStuffOn-bottomG();
g.drawImage(topImage, 0, 0, g.TOP|g.LEFT);
g.drawImage(bottomImage, 0, halfScreenHeight, g.TOP|g.LEFT);
flushGraphics();
No, you use just one canvas, but repaint only the bits that have changed using
Canvas.repaint(int x, int y, int w, int h);

Drawing images onto AWT and initialising Graphics

I'll start off by telling you what Im trying to do if thats OK, as Im not certain the route Im struggling with is even the best way of achieving my ends.
I have a JFrame containing two JPanels. One contains a number of buttons (buttonPanel), the other is, initially, blank (displayPane). When buttons are pressed the stuff shown in displayPanel changes. The way this is working is each press of a button creates a new object that extends JPanel and then adds that to displayPane
So all the above is working fine and dandy (although I freely admit it may not be the best way of doing it) except for one particular case.
In this particular case I need to create a JLayeredPanel and then draw a clipped image on it. JLayeredPanel because I want to draw some stuff on top of it, clipped because I only want to show part of the area (which exact part is passed to the constructor).
Now, the problem Im having is this. The only way I know to draw a clipped image is through g=thingie.getGraphics(), g.setClip(Shape shape), g.drawImage(various). However, all of that relies on being able to get graphics. But because I am assembling the object first there is no graphics object associated with the JLayeredPane (because its not displayed) so getGraphics is returning null and g.setClip() is throwing a Null Pointer Exception.
Obviously I am doing this wrong somehow and somewhere. Any help would be appreciated, sorry if the question is confusing. I tried to include as much detail as possible and now I am a little concerned I've muddied the issue. I'll keep an eye on this and clarify if required.
Warning!: Wrong answer, see below the line
Why don't you just create a new Graphics object, paint on it and then use it with the update() method?
Graphics g = new Graphics();
g.drawStuff();
thingie.update(g);
This showd be correct
As stated on the comments the previous solution was wrong but it can be done with an Double buffer, create a buffered image and draw on it, then override the paint method of the jLayeredPane pane to draw the image.
private void addStuff() {
BufferedImage bi =
new BufferedImage(100, 100, BufferedImage.TYPE_4BYTE_ABGR);
Graphics bufferedGraphics = bi.getGraphics();
//Paint stuff
bufferedGraphics.drawLine(0, 0, 50, 50);
javax.swing.JLayeredPane layered;
layered = new JLayeredPane() {
#Override
public void paint(Graphics g) {
g.drawImage(bi, 0, 0, null);
}
};
this.add(layered);
this.validate();
this.repaint();
}

Categories