Partially transparent images in javafx - java

I am making a project in eclipse. I have uploaded a .png file using 'image view' option.It has some transparent areas .I want to write code such that it only detects the click of the user's mouse when it is over opaque area and take that user to a new window defined by me.

This code should work in most circumstances.
imageView.addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
Color color = image.getPixelReader().getColor(e.getX(),e.getY()));
if(color.getAlpha() != 0)
{
//execute your code here
}
});

Related

Processing for Android | save() "File contains a path separator"

I'm using Processing 3.5.4.
I’m trying to save() an image of the screen to data/frames (relative to my sketch file). The code I’m using works in Java mode with no problems (I can see the image saving into the correct folder on my computer), but when running it on my Android device I get java.lang.IllegalArgumentException: File data/frames/frameasdf.tif contains a path separator. I’m guessing this is because of a difference in file storage systems.
Is there any way I can avoid path separators other than saving images directly into the sketch folder?
I’m new to Java (just moved over from Javascript for more professional development), so if possible, please link to any helpful documentation.
PImage drawing;
void setup() {
size(displayWidth, displayHeight);
}
boolean clicked = false;
String name = "asdf";
void mouseReleased() {
clicked = true;
}
void draw() {
background(255);
if(drawing != null) {
image(drawing, 0, 0);
}
fill(0);
noStroke();
ellipse(mouseX, mouseY, 50, 50);
if(clicked) {
save("data/frames/frame" + name + ".tif");
drawing = loadImage("frames/frame" + name + ".tif");
}
clicked = false;
}
This is a shortened version of my code. It's a simple program that should add a dot to the screen whenever you click.
I do plan on saving more than one frame in the frames folder.
I didn't understand your question very well. Are you trying to save the file inside the specific app files or in shared storage files? the root files of the different storage systems can be got with Context.getExternalFilesDirs() that returns a vector with the files.
you can start reading the documentation here: Android storage

Java (JFace Application Window) Setting external label text

I am looking to figure out how to set the text of a label on an external Application Window.
What I have:
I have two windows so far. The first one is the main application window that will appear when the user starts the program. The second window is another separate window that I have created specifically to display a custom error window.
The problem: I seem to be unable to call the label that I have created on the error window and set the text to something custom. Why? I want to be able to reuse this window many times! This window is aimed for things like error handling when there is invalid input or if the application cannot read/save to a file.
I was going to post screen shots but you need 10 rep for that. It would have explained everything better.
Here is the code for the label on the Error_dialog window:
Label Error_label = new Label(container, SWT.NONE);
Error_label.setBounds(10, 10, 348, 13);
Error_label.setText("Label I actively want to change!");
Here is the condition I would like to fire off when it is met:
if(AvailableSpaces == 10){
//Set the label text HERE and then open the window!
showError.open();
}
I have included this at the top of the class as well:
Error_dialog showError = new Error_dialog();
Just save the label as a field in your dialog class and add a 'setter' method. Something like:
public class ErrorDialog extends Dialog
{
private Label errorLabel;
... other code
public void setText(String text)
{
if (errorLabel != null && !errorLabel.isDisposed()) {
errorLabel.setText(text);
}
}
You will need to use your dialog like this:
ErrorDialog dialog = new ErrorDialog(shell);
dialog.create(); // Creates the controls
dialog.setText("Error message");
dialog.open();
Note: you should stick to the rules for Java variable names - they always start with lower case.
Further learn to use Layouts. Using setBounds will cause problems if the user is using different fonts.

How to name application and give it an icon?

I'm working in eclipse and I have made an application without name and icon. When i start the application it's a really creepy name displaying in the upper left corner (Mac). It's some thing like. I wonder how I can change this to my own name. Second question is how i can change the icon. Can I do that in eclipse?
If you're using JFrames, you can try setting the icon image as follows.
frame.setIconImage(img);
Also, by name it sounds a bit like you mean the frame's title. When you create the a frame, you can set the title as follows:
Frame frame = new JFrame("Title goes here");
To change name: How to change an Android app's name?
To change icon: How to change the icon of an Android app in Eclipse?
It's already here at stackoverflow, please check if your answers are here before posting.
The prefered method would be to create a Mac OS application bundle (and the bundler), but if this seems like to much work, you can supply custom properties to, for example, you could supply the Xdock:name property when running the application, for example...
-DXdock:name="Application Name"
If you can't do that, you can set it when your application runs, for example...
public static void main(String[] args) {
try {
// Sets the application name on the menu bar
System.setProperty("Xdock:name", "Application Name");
// Set the applications dock icon...
Application application = Application.getApplication();
application.setDockIconImage(ImageIO.read(TestDockIcon.class.getResource("/Icon.png")));
// Start the application...
new TestDockIcon();
} catch (IOException exp) {
exp.printStackTrace();
}
}

How can I detect that a menu is open in JavaFX?

I'm writing a JavaFX app and have a menubar with partial transparency. When the user mouses over the menubar, it becomes fully opaque. I'd also like it to be opaque when the user has opened one of the menus. Is this possible somehow? I'm using JavaFX 2 if it matters.
Thanks.
try this..!!
menu.setOnShowing(new EventHandler<Event>() {
#Override
public void handle(Event t)
menubar.setStyle("-fx-background-color:transparent"); //
// or you can use set opacity property
menubar.setOpacity(0.25);
}
});
this event occurs when you show you menu...there also menu hidden propety..you can also use it.

Encoding transparent animated gif in Java

I am using GifDecoder to read an animated .gif file and AnimGifEncoder to write it. (link)
If I display the original frames read by GifDecoder they display correctly and are transparent, but if I display the frames created by AnimatedGifEncoder the transparency is all wrong.
GifDecoder gif = new GifDecoder();
gif.read("image.gif");
AnimatedGifEncoder e = new AnimatedGifEncoder();
e.start("newimage.gif");
e.setTransparent(Color.BLACK);
for (int i=0;i<gif.getFrameCount();i++) {
anim.addFrame(gif.getFrame(i));
anim.setDelay(gif.getDelay(i));
}
anim.finish();
In this example I set the transparent color to black. But actually I want to get the transparent color information from the GifDecoder but I don't know how.
I'm using the following solution, although there are still some gif files that
are a mess after scaling... :
(At least it seems to get along with most non-transparent gifs)
Color transparentColor = null;
BufferedImage firstFrameImage = ImageIO.read([sourceFileHere]);
if (firstFrameImage.getColorModel() instanceof IndexColorModel) {
IndexColorModel cm = (IndexColorModel) firstFrameImage.getColorModel();
int transparentPixel = cm.getTransparentPixel();
transparentColor = new Color(cm.getRGB(transparentPixel), true);
}
e.setTransparent(transparentColor);
This was all a long time ago, but I did manage to get it working at the time. Without digging out the code again... I got it working by:
Scan the original image and set the transparent areas to a colour that does not exist inside the original image.
Set the transparent colour inside the AnimGifEncoder to the colour that was previously assigned to the transparent areas.

Categories