When I create a Canvas and a JFrame, then put the canvas onto the JFrame.
With this code
final Graphics g = Display.getBufferStrategy().getDrawGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 480, 360);
g.dispose();
Display.getBufferStrategy().show();
However when I put the code in
The drawing only occurs whenever its's in a loop.
This isn't a huge problem since it would be in a loop anyways, but it's annoying having to have it in a loop to have it draw/keep the image on the frame
Please someone help me.
Thanks.
Related
I was wondering how I would use something like a bufferstrategy in a Java Applet.
I want to draw something like an image
gfx.drawImage(frame, 0, 0, width, height, null);
to my applet but I am getting white flashes due to the graphics drawing to the screen not being buffered.
Any help would be greatly appreciated!
Edit: I have a thread that is repainting this image
Edit 2: The image is constantly changing so I need to repaint it each time it changes
Ah, so the public void update(Graphics g) method doesn't have white flashes due to the graphics drawing to the screen not being buffered. But the paint method does. Odd.
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);
}
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);
I've been trying to draw a string in a JFrame, but for some reason it wont show , here is the code with the drawings command:
g.setColor(Color.WHITE);
g.fillRect(0, 0, 900, 500);
g.drawImage(backToMain,0,0,this);
g.setColor(Color.BLACK);
g.drawString("name", 300, 0);
but for some reason the screen I get is white with the Image in the left corner but missing the String.
I thought I might have a problem with the paint() command but I have tried several other things but it seams that the string just refuses to appear.
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();
}