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);
Related
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);
}
I am trying to implement Connected Component Labeling algorithm using java swing library.
First, my code reads any B/W image from computer. Then, it iterates through image pixels (x,y) and returns specified pixel color.
The new image will be drawn on a JPanel. using Graphics library.
The code to draw a pixel is: g.fillRect(x, y, width, height);
The problem is that I am willing to get background color of the JPanel that I am drawing on it in order to fully implement this algorithm.
Is there any possible way to put all the drawn objects into an array? This is just for a drawing game so it is possible to save all objects and load them back from this way.
Thanks if this is possible!
Take a look at Picture class - http://developer.android.com/reference/android/graphics/Picture.html
After it's being attached to the canvas, it records all drawing operations and stores it.
After you can either draw all of them on another canvas, or store/transfer the Picture to make it on another device or later.
UPD: something like this
Picture picture = new Picture();
Canvas canvas = picture.beginRecording(300, 300);
canvas.drawRect(new Rect(5, 5, 10, 10), new Paint()); // configure paint here
picture.endRecording();
picture.writeToStream(new FileOutputStream("stored_drawing.pict"));
Now you'll be able to restore this rectangle from file and draw it on any another canvas.
So generally you can wrap you user's drawing through this class and it wil store all user drawing operations.
Good luck
I am trying to implement the fade in/fade out animation in swing.
I am using a JPanel which does not have any components in it. It is completely drawn by paintComponent() method.
Now in one of the portion of this JPanel, I want to implement the fade in/fade-out animation. When I tried using AlphaComposite, the animation is being triggered for whole JPanel.
Can I limit this animation in a small clipped region in that panel?
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.XOR, alpha));
Have you tried using an Graphics object (like rectangle, circle etc..) for your fade in/out? That way it won't be triggered for the complete panel.
Good luck!
Perhaps, but that may be more difficult to achieve than what it's worth. Create a JComponent of the size you want to animate (or fade), add it to your JPanel, and have repaint() called on your smaller component during animation instead of the larger JPanel.
You can use setClip() before painting to restrict the fading area.
Suppose you want a small fading rectangle. Using Area class create 2 Shapes. Intersection of original clip and fading rect and subtraction (subtract the fading rectangle from the original clip).
Call super.paintComponent() twice with 2 different clips. For the second paint you can set your alpha filter.
I have a JPanel which draws .png images. each image has 2 copies to signify whether or not it has been selected. One image is normal and one has a colored border to signify the selection has been made. (You can think of these images as simple shapes for the sake of argument)
Which version of the image is drawn is determined via MouseListeners. If the user selects image1, then image1 will display its highlighted version.
In general this is working, however upon repaint() the highlighted version of the image is left behind. Since the highlighted version is slightly larger than the normal version, you can see it sticking out behind the newly drawn normal image (overlapping).
I thought repaint() was supposed to completely dispose of the current drawings and start from scratch, this doesn't seem to be the case here. Below is the general idea, blackClicked is toggled inside a MouseListener.
So my question is how do I make repaint() get rid of its currently drawn images and start from scratch.
Also, if there is a simpler way to achieve all of this please let me know, making 2 copies of images is tedious work.
#Override
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if(blackClicked)
g2.drawImage(blackSelected,0, 0, null);
else
g2.drawImage(black,0, 0, null);
g2.drawImage(green,0, 0, null);
}
how do I make repaint() get rid of its currently drawn images and start from scratch.
You mean, how to make repaint fill the component with it's background color before painting? How about adding
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
in top of paintComponent()?