Smooth animation (Java SWT) - java

i've got a question about drawing animations in Java (SWT).
I try to draw an animation of some process.
When i use canvas.redraw() program firstly erases everything that has been drawn and then draws again.
My program draws about 1000 of rectangles per time step (this big quantity is necessary) so animation doesn't looks smooth - it blinks all the time.
Is there a way to make it look smoother, for example to paint new objects over old ones, without erasing them (that would look better anyway)?

The solution for flickering when doing custom painting is to use double buffering. The SWT Canvas object has built-in double-buffering, use it by adding the flag to the styles in the constructor:
Canvas myCanvas = new Canvas (parentComposite, SWT.DOUBLE_BUFFERED);

Related

JavaFX: how to clear a drawing without affecting background

Very simple question, but I could not find the answer in JavaFX docs or StackOverflow:
I have a JavaFX Canvas filled with a graph (various calls to strokeLine(), not the issue here). I need to be able to draw a rectangle over this graph, then simply clear the rectangle, without affecting the graph in the background. (Like an undo operation).
Code to draw the rectangle ('p' and 'e' are points):
gc.rect(p.getX(), p.getY(), e.getX()-p.getX(), e.getY()-p.getY());
gc.stroke();
The most obvious answer would be to use the clearRect() method, but the problem is that it clears also the portion of the graph in the background...
So the question is: how do I clear a drawing that was made with stroke(), without affecting the other drawings in the background?
You can't do this with one canvas.
Canvas only store the result of your painting operation.
This is the interest of the canvas you can stroke million times the same line and it will only store and represent the result and doesn't consume more memory.
So you if you need to draw Something over your chart you should put an other canvas over the chart and draw on the second canvas.
It might be more straight forward and much more the JavaFX-way of doing things if you just put your canvas into a Group and then just add a Rectangle node to the Group which you can remove at any time if you want.
This can be acheaved by taking snapshot(s) of your Canvas, using the .snapshot(SnapshotParameters params, WritableImage image) method. Basicly, every time you draw something on your Canvas, you take a snapshot of it and store it somewhere (for example in a ArrayList). Then you can use those snapshots to create a 'undo' operation, by using the . drawImage(Image img, double x, double y) method of Canvas's GraphicsContext, in which you would pass the snapshot you want to go back to as the Image parameter.

Rotate 2d graphics images on fixed background

I am writing Java code to rotate images on a fixed background image, like rotating a needle image on clock background.
There are multiple needle in the watch, one needle for hours and another one for minutes.
How to rotate 2d graphics images on fixed background?
Start with a base, static image (ie a clock face), where the parts you need to animate have being cut out.
On top of this you can render each element you need at the required locations.
Start by having a look at...
Performing Custom Painting
2D Graphics
You'll need some way to update the movement. This can be achieved through the use of a single or multiple javax.swing.Timer timers, see Concurrency in Swing for details.
The rotation is a little more fun, but can be achieved through the use of an AffineTransform
Take a look at...
Rotate BufferedImage Inside JPanel
Rotate an image in java by the specified angle
Image not at proper place after rotating (graphics) (which is close to what you asked)

how to stop buffering(blinking ) in my applet

I am making a card game with a java applet. I have a class that extends JApplet. Now I am trying to draw 104 images on applet, but when I drag one image and move, my entire applet is blinking. Throughout the execution of my program, I am calling the repaint method in mousedragged and mousepressed, because it is required to repaint the image on the applet after dragging it.
I know my paint method is running every time I drag a card and move the mouse across the screen, and that's why my 104 cards redraw on applet every time. I think this is the reason for blinking in my applet.
I have tried many times but not solving. How can I prevent this blinking?
Look into double buffering and BufferStrategy.
Once upon a time you'd do it yourself by painting to an offscreen image then drawing the offscreen image to your applet in a single go - but these days you can use BufferStrategy. Here's a tutorial on how to do it the old fashioned way. BufferStrategy javadoc shows you how to use the new way.

Java backbuffer and animations

I'm creating a simple 2D game in java. I've only done this in C++ with the Windows API so far. In java, I'm getting a Graphics object by extending JFrame and calling this.getContentPane().getGraphics(). With this object, I'm drawing everything to the screen, every frame. This raises a few questions:
Backbuffer: I'm not getting any flickering effects, while I'm not using a backbuffer (I'm drawing directly on the Graphics object). Why is this? Does java has a built-in backbuffer or something?
Animations: I'm used to put all animation parts in a single sprite sheet, like in this example:
http://www.envygames.com/share/sample_animation.jpg
Now, someone has told me that you can just draw animated .gif's and java will draw these independent of the game loop. I've tried this out and it doesn't seem to work. Is this true or am I also supposed to use these sprite sheets in java?
Thanks
Yes, Java has a double buffer rendering strategy that can be switched on and off...
Java: how to do double-buffering in Swing?
About the animated gifs, I think it is right, but you may have to put them in the appropriate container (maybe as the icon of a JLabel?).
getting a Graphics object by extending JFrame and
calling this.getContentPane().getGraphics().
don't painting directly to the JFrame for Custom Painting you have to look for JLabel that allows painting everything, another choise will be extending JCompoments, or JPanel for that
for painting in the Swing you have to look for paintComponent(Graphics g), not paint(Graphics g), because this method are lots of time used in examples and ditributed on some of Java ExamplesDepots, that's wrong method with possible lacks

java changing attributes of shapes programmatically

I want to create a number of straight lines connecting small circle shapes. then I want to change the color and width of the lines from within my program. I was thinking of using Canvas to do this but there does not seem to be a way to access individual shapes drawn on canvas to change their attributes after they are drawn. What would be an easy way to implement this?
First of all, what version of Java and which UI toolkit? AWT's Canvas is very dumb, it will not even "remember" what you have painted; when you minimize and restore the window, it will send a paint() event because it wants to be repainted.
The easiest way (if you are using AWT and stuck to Canvas) is to have a List of your shapes (either one list for all or one for circles and one for lines, or whatever you like) and make your paint method draw all of them. Then update the objects in your list. When you are done updating, call repaint() on your canvas and it will call paint() for you again.
You don't paint shapes onto a Canvas if you're using Graphics and Graphics2D functions like drawRect, drawPolygon, drawOval, etc. Once they're drawn, they don't exist as shapes anymore. You just have an image with filled-in pixels.
As mihi said, you may have to keep track of the shapes you're trying to draw, then regenerate your image if it changes. Perhaps you could also "unpaint" a shape you're trying to change by painting over it in the background color and repainting the changed shape.

Categories