Resize BufferedImage without Scaling image - java

I am trying to upgrade a Java Swing Application, and I have a part where I manipulate some small icons I am drawing in my application as BufferedImages. I want a few specific icons at certain circumstances to display an additional triangle symbol on top. The symbol+triangle is considered after the addition as a simple symbol and I thought it would be easier to do if I could resize the BufferedImage I have already and draw on top. But I have not been able to do so. Is there any efficient way to do this?

Related

Is there a function to find color of point in AWT/SWING

So, I've created this little maze building algorithm (trying to understand how Java AWT & Swing works).
Now, I've used the Graphics (paint method) to create the maze itself, meaning I didn't store it anywhere.
Now I wanted to know if there is a way to know if at given a specific point on the JFrame, is there a way to tell which color it is?
If so, how can I do it?
Let's say for example in my maze I want to see what color is on the PURPLE dot (seeing if it's a wall there basically).
Is there a way to do it, or do I have to do a work-around that?
Thanks.
There is no direct way to a Component's image buffer. But there are two indirect ways.
(Better) Create an Image (Either BufferedImage or Component.createImage) with the same height and width as your Component. You can then run myComponent.paint(myBuffer.getGraphics());. This will draw the component on the image, and from there you can get the pixel Color you are interested in
(Alternative) You can use java.awt.Robot to capture the screen in an Image. Use this image similar as described above

Image on Canvas vs Label

What are the differences between using a Canvas control or a Label control for drawing an image?
Both controls seem to have similar capabilities in terms of drawing images onto their displayable area, and can both use PaintListeners to further customize the act. I have always assumed that Canvases are more suited for the purpose than labels simply due to their name, but I cannot determine the actual practical differences. In what scenarios would you use one over the other for loading an image file and painting the image onto the control?
The Label inherits its drawing capabilities from Control, as well as many of the features in Canvas. But Canvas was built with extra controls for images, like dynamic drawing, buffering and partial update.
Summing up, it is ok to use labels for static small images, but when it comes to heavy-duty use, a canvas is more appropriate.

How to select a figure to move, rotate and scale with Java2D

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

Android UI Design Suggestion

This is the scenario:
I have one image background set on an activity. On this background, a rectangle has been drawn (in other words, one image depicting a rectangle). I need to display text WITHIN this rectangle.
Right now, I have one solution in mind: since I'm going to optimize the UI for most screens (incl. tablets), I'm going to customize the main .xml layout for each screen size (multiple .xml layouts). Thus, I can manually set the place where the text area goes within the rectangle (+ its size).
I am most certain that this solution is NOT good. I'd like to hear some suggestions from more advanced developers. What would an elegant way of placing text over a background image showing a rectangle, so that the text stays within the rectangle's borders be?
Because I need to set particular positions for other UI elements (centered buttons (vertically/horizontally), I am currently using a Relative Layout.
By default if you have used dp as dimensional measure, your app should in theory work fine for all resoultions. Android by default scales the screen to fit different screens. AndroidDeveloper You just have to make sure that you have different images for resources (Rectangle image). There is another post in SO which you might be intrested to look into link
Cheers
RIchie

Using a canvas much larger than the screen

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...

Categories