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)
Related
I'm working on a 2D game in Java using swing JFrame.
At the moment I can get >60fps repainting a grid of 64x64 image tiles, but when I want to add my tree images which are 80x170 each and have transparent edges around the tree, my fps grinds to about 8.
I've considered drawing the trees to their own BufferedImage grid, but it needs to be updated frequently, whenever a player moves or AI moves, etc.
I've noticed using smaller images and drawing them at a larger scale does not help, and am really struggling to see how I can get around this.
I have some very small images (20 by 20 pixels) which I am drawing using matrices onto a canvas using Canvas.drawBitmap(Bitmap, Matrix, Paint). The problem is that I am scaling these up about 5-10 times larger when I am drawing them and it is automatically re-sampling these images with smoothness. What I want is nearest-neighbour style re-sampling (so it will look pixelated) not the smoothness. I cannot find a way to change this. Also creating another whole image that is larger to store a properly re-sampled picture is not an option since I am under memory constraints. Thanks for any help!
You need to set up the paint you pass to drawBitmap, like so:
paint.setFilterBitmap(false);
I have problems that are not as selecting a figure and that figure selected apply a rotation as many figures as I do not do the following as the first two points and I have but not the third:
The program should allow multiple draw figures in the plane.
Each time you insert a figure in the plane should ask the dimensions of the figure and the coordinates in the insertion.
Once inserted the figure the program should allow the selected shape transformations, rotations, translations and scaling.
You've not provided enough details about your problem, but, you can take a look at the following that deal with rotation
AffineTransform.rotate() - how do I xlate, rotate, and scale at the same time?
How to mirror an image with Java.awt
Change the angle/position of a drawing with a algorithm in Java
The following deal with resizing
How do I resize images inside an application when the application window is resized?
Java: maintaining aspect ratio of JPanel background image
I also suggest you take a look through
Graphics2D
Performing Custom Painting
Using Mouse Listeners
I'm trying to draw a 2D contour plot of some data on Android and I'm wondering what the best approach would be to draw those. The whole datasets can be relatively large (2k * 2k points) and zooming and moving inside the plot should be very fast. Most of the time only a small part of the data will be drawn as the user has zoomed in on the data.
My idea now would be to draw the whole plot onto a large canvas, but clip it to the portion visible on the screen, so that only that part would be really drawn in the end. I find the 2D drawing API of Android somewhat confusing and I'm not sure if this is really a feasible approach and how I would then go about executing it.
So my questions are:
Is it a good idea to draw onto a canvas much larger than the screen and use clipping to display only the relevant part?
How would I create a larger canvas and how would I select which parts should be drawn?
You should start the other way around. Instead of creating a huge canvas you should detect what part of your plot you need to draw and draw only that.
So basically you need some navigation/scrolling and you need to keep the offset from the starting point in memory to calculate where you are. Using the offset you can easily zoom in and out because you just need to scale the plot to the screen.
Is it a good idea to draw onto a
canvas much larger than the screen and
use clipping to display only the
relevant part?
A better question might be, do you have any other options. Some might argue that this is a bad idea since your going to keep memory in use when it isn't relevant to whats happening on the UI. However, from my experiences with the Canvas, I think you'll find this should work out just fine. Now, if you are trying to keep "5 square miles" of canvas in memory your definitely going to have to find a better way to manage it.
How would I create a larger canvas and
how would I select which parts should
be drawn?
I would expect that you will be creating your own "scrolling" method when the user touches the screen via overriding the onTouchEvent method. Basically your going to need to keep track of a starting point X and Y and just track that value as you move the Canvas on screen. In order to move the Canvas there are a number of built in's like translate and scale that you can use to both move the Canvas in X and Y as well as scale it when the user zooms in or out.
I don't think that is a good idea to draw your 2D contour plot on a big bitmap because you need a vector type graphics to zoom in and out in order to keep it sharp. Only pictures are good to scale down but graphs will lose thin lines or come out deformed when scaled down in bitmaps.
The proper way is to do it all mathematically and to calculate which part of the graph should be drawn for required position and zoom. Using anti_alias paint for lines and text, the graph would always come out sharp and good...
When the user zooms out, some items should not be drawn as they could not fit into the screen or would clutter it. So the graph would be always optimised for the zoom level...
We have an old (more than 10yrs old) Java Swing applicatin which draws lots of circles and connections (lines) between those circles on a JCanvas (a subclass of JComponent) based on lab data.
Because the data becames bigger and bigger, we cannot display the entire drawing now. We have put the JCavans into a JScrollPane but it is not convenience to scroll the drawing.
Can we add zoom in zoom out for it? if yes, how? I know we can zoom image but the drawing on Canvas is an image?
thanks,
EDIT:
we draw those circles and line with Graphics within paintComponent(Graphics g) method.
You could apply a scaling Transform to the Graphics2D object passed to the paintComponent method. You can learn how to use it in the Java 2D programming trail.
Without knowing anything about your application it's hard to provide useful advice (adding a code snippet or better yet a cutdown example app would be helpful to show how things are being drawn), but I'll give it a shot:
Why don't you multiply the x,y and width,height values by a scaling factor before you draw each circle/line? I assume that somewhere your canvas is using a Graphics object to draw each shape?