Java - Getting screen position of buffered image - java

I'm working with some code where I have a few buffered images in a scrollbar. When I click one of these images, I want to draw a rectangle around the image to indicate that the image is selected.
My issue is that because my panel is scrollable, the image position set when drawing is not necessarily the actual screen position. So when the mouse is clicked, the point of the cursor's position doesn't intersect any of the position attributes (x, y, height, & width) I have for the object holding the actual image.
Does anybody know how to get the actual position of an image relative to the screen instead of the panel?
So far I haven't found any solutions to this online.
Thanks.

You can use the SwingUtilities.convertPoint(...) method.
I would guess you need to convert the mouse point the coordinates of the viewport of the scroll pane.
Maybe an easier approach is to use a JList with a custom renderer. You can set the JList to wrap components horizontally. Then in you custom renderer you just add a Border to the selected item. Read the section from the Swing tutorial on How to Use Lists for more information and examples.

Related

How do I refer to the distance I’ve scrolled in a JScrollPane?

I’m writing code for drawing in a JPanel as if it’s a canvas, where I can create shapes by clicking inside the panel. When I click, it places the dots where I want them to go, however if I scroll and then click, it places the dots where the mouse would be if I hadn’t scrolled. I assume this is because the mouse coordinates don’t change, so I’m guessing I need to add the number of pixels I’ve scrolled (horizontally and vertically). Is there a command I can use to refer to these values?
It's been a while since I've worked with Swing, but I believe you want to call:
JScrollPane sp = ...
Point viewPosition = sp.getViewport().getViewPosition();
The viewPosition.x and .y will return the offset in pixels from the top left corner.

Automatically crop image boundaries

I'm looking for an automatic way to grab a piece of a bitmap and extract a certain part of it.
Let me explain:
If I have this image:
http://imgur.com/B9U9E
It has a big white border. (see link for better white result) I'm looking for a way to only grab the dialog above. So no white borders around the dialog. Is there a way to do this by code or with a library?
You should know that the image could have any form and positioned anywhere on the white dialog.
So a user draws something on the white panel and i need the program to automatically make a rectangle about where the users drew on the canvas and save that bitmap where the user drew on the canvas (Everything in between that rectangle).
Pseudocode
Define the background color.
Scan from the left, right, bottom, top and store the locations of the transitions from background to drawing.
The rectangle defined by (left, bottom) and (right, top) defines the cropping area
For a Java code example, please see: How to auto crop an image white border in Java?
Look into Bitmap.createBitmap.

Java. Swing. JComponent's clickable area

I have a custom component displaying *.png image. The image has transparent and non-transparent area. If I add ActionListener(or MouseClickListener) to component, it will raise events even if I click on transparent area of component. Visually it looks like clicking outside the component.
How can I declare which area should react on clicks and which should not?
I've read about getting pixel from image your coordinates from event object and check its transparency. It seems difficult and ineffective.
Maybe define custom-border of this component or something else?
You answered your own question.
Within the mousePressed() event handler, you're going to have to check if you're within the JComponent, and then check the pixel at the x and y coordinate of the mouse click for transparency.
How can I declare which area should react on clicks and which should not?
This is done at the JComponent level by overriding the contains(...) method. So for example you extend JLabel to create a TransparentLabel which contain your image in the form of an icon.
Then whenever this method is invoked you only need to check this one location to determine if the pixel is transparent or not.
If your main issue about "overhead" is that you only want to make it opaque when the mouse enters a non-transparent part of the image, I'd consider pre-computing an image "mask".
On image load, make another image (or a 2d array, or something similar) that will be binary (i.e. black-and-white only, or 1 and 0 values only in an array). 0/white = transparent, 1/black = non-transparent.
Then, on mouse events, you can just check the exact pixel in the mask if it is set (value = black or 1), and trigger if it is.
Did you try to bunk two same pictures file and just for the second picture give it a short width ? Like that I think you will can to add differents listerners for the both pictures.

Making screenshot of drawing application + drawing outlines of 2D shapes

I am making a drawing program, using the Graphics 2D objects (lines, rectangles and ovals, namely) by placing them on a panel. With that in mind, I have 2 questions:
1) How can I store the images currently portrayed on the panel as PNG, JPG or similar file onto disk?
2) I have added a drag function. How can I implement a function so that one can see the "outline" of the rectangle, line or oval, before it is actually put onto the canvas (but not placing the outline on the canvas after the mouse button has been released)? I can't see that any of the MouseListener methods can do such a thing.
1) How can I store the images
currently portrayed on the panel as
PNG, JPG or similar file onto disk?
You can create a BufferedImage and paint any component onto it. The Screen Image class does this for you.
2) How can I implement a function so that one can see the "outline" of the rectangle, line or oval?
In this example, the shape itself may be dragged, rather than its outline, but the draw() method of class Node may be modified as desired. A rectangular outline is used for selection, as on a desktop.
1) ImageIO
http://www.java-tips.org/java-se-tips/javax.imageio/how-to-save-a-bufferedimage-to-a-png-file.html
2) Can't think of an answer for 2.

Java sampling pixel color in swing

I have to create a special TextFieldUI that draws an image as the background. That image contains some alpha components. However, whenever a character is written in that text field, first it redraws the background and then draws the character. This is fine when the background contains no alpha components, but after a few characters have been typed, the alpha areas sum up to become black.
The only way I can see around this is in the paintBackground method of TextfieldUI (which I'm overriding), I have to first sample the color of the background at that location, paint the entire graphics component that color, and then paint my background.
Does anyone know how to sample the color of a pixel when all I have access to is the Graphics object?
Is there a better way to draw a custom image as the textfield background other than overriding paintBackground in TextfieldUI?
Thanks
I haven't tried it before, but Swing is built on top of AWT, and the Robot class had a way of sampling specific pixels in the AWT
Well, I don't know what your custom code looks like in the paintBackground method, but I would make sure you fill in the text field background before you draw the image.
I'll let you decide if its "better" or not, but you can use the Background Panel which allows you to add an image to a panel. Then you add the text field to the panel (the text field is automatically made non-opaque so the image shows through). Then you add the panel to the GUI.
If that doesn't work then it would be nice to have a demo of your code so we can see whats actually happening.
When you override paintBackground, you're calling the superclass version first, right? It already lays down a background-color rectangle that would give your image a fresh-start.
Rather than 'sampling' the background color, it's probably already correct (the superclass paintBackground code gets it from the parent component if not locally set). If that default is not correct, set it in initial interface construction. (Your field isn't being overlaid on other complicated arbitrary interface of unknown solid colors, is it?)

Categories