JButton hover effect delay in Java - java

I want to know about these two questions.
1. I am using NetBeans and I have created a project, I already have saved all the images in "src" folder, but when I put just image name like (image.png) then no image will displayed on button. I have attached working code as well as no working code below -
Not Working Code:
private void loginMouseEntered(java.awt.event.MouseEvent evt) {
login.setIcon(new ImageIcon("login_button_hover.png"));
}
Working Code
private void loginMouseEntered(java.awt.event.MouseEvent evt) {
login.setIcon(new ImageIcon("C:\\Users\\Zeeshan\\Documents\\NetBeansProjects\\Attandence Software\\src\\images\\login_button_hover.png"));
}
2. I want to give delay to the "Hover" image.
For example: If I take my mouse pointer to the Button/Image, the Hover image will display slowly and take 1 sec to display completely.

Read the javadoc of the constructor:
public ImageIcon(String filename)
Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path.
(emphasis mine)
It doesn't expect a classpath resource path as argument, but a file path (i.e. the path of a file on the file system, not the path of a resource of your app).
If you want to load a resource using the ClassLoader, then use
new ImageIcon(MyClass.class.getResource("/login_button_hover.png"));
Also, you'd better load this ImageIcon once and reuse it, instead of reconstructing it every time the mouse enters the button.
Regarding your second point, you could start a swing Timer every time the mouse enters the button, and have the timer change the icon once the second is elapsed. But you'll have to deal with multiple enters per second, etc. What's wrong with setRolloverIcon()?

Related

App loses Images when built

For a project I am working on, I have made a Bingo Card. The basic functionality is that a Card is randomly generated and displayed in STD out as well as in a Swing GUI I made by hand. This application has multiple java files within.
BingoFinal.java - The main file. This is what's run when the program is run.
Bingo_Card.java - creates the Bingo card, prints it to STD out, and checks for bingo
BingoBG.java - Draws the background of the GUI with 2D graphics
DrawBingoCard.java - Calls BingoBG and also creates 25 labels with the board values. When Bingo_Card finds a matching number(inputted by STD IN), it no longer prints the number, and prints Chip.png(which is in the same package folder as the java files), a bingo chip image, making it look covered.
This works flawlessly when I run it through NetBeans, but when I clean and build it, then run the jar in terminal, everything works except for displaying the bingo chip image. Does anybody know why that would happen?
EDIT: Here is how I load the Image
ImageIcon chip; //declare ImageIcon
chip = new ImageIcon("chip75.png"); //define it as chip75.png. It is stored in package folder
JLabel B1Chip; //declare empty Label
B1Chip = new JLabel(chip); //define the Label with just the ImageIcon
B1Chip.setBounds(22, 112, chip.getIconWidth(), chip.getIconHeight()); //place at (22,112)
frame.add(B1Chip, null); //Add to frame
You should be accessing the images, by using :
ImageIcon chip = new ImageIcon(ClassName.class.getResource("/chip75.png"))
More information can be found on the info page of tag: embedded-resource A detailed answer regarding adding images to the NetBeans Project is mentioned on the info-page link.
More importantly, it would be wise, that you use ImageIO.read, since atleast it will let you know, if something goes wrong, by throwing IOException
Throws:
IllegalArgumentException - if input is null.
IOException - if an error occurs during reading.
ImageIcon on the other hand will hide the exception if any :-)
copy the image files into jar or give absolute path of the image files

How do I solve problems in my screenshot-taking application?

I wrote a GUI-based Java program that takes 25 screenshots per second and saves them at a user-defined location.
It works pretty well except that it has two problems:
The mouse cursor is missing from the images and I know it will be because BufferedImages do not contain cursor information in them. They have to be added programatically.
The thread that takes screenshots is a daemon thread. So if I close the application, the thread is killed and the PNG image that was being written gets corrupted. I want to avoid that.
Here are the images of my application:
The award-winning, intuitive GUI:
The high-definition images captured look like this:
As you can see from the image the cursor information is being displayed in the console using MouseInfo's static methods.
Please let me know how to solve the above mentioned two problems.
After solving the problem, the images now look like this:
The mouse cursor is missing from the images and I know it will be
because BufferedImages do not contain cursor information in them. They
have to be added programatically.
That is correct, you have to add the cursor afterwards. The reason for this is that a screenshot taken with the Robot class never contains the cursor. Not really because "the BufferedImage doesn't contain mouse info". A BufferedImage is a class that contains a raster of pixels.
The thread that takes screenshots is a daemon thread. So if I close
the application, the thread is killed and the PNG image that was being
written gets corrupted. I want to avoid that.
Simply, in the screenshot thread, use a flag that indicates wether it should continue or not. Keep taking screenshots as long as that boolean is set to true. Make sure to make it non-deamon. So, when you close the application, set the flag to false.
Probably the most easy way to do this is to add a WindowListener:
yourFrame.addWindowListener(new WindowAdapter()
{
public void windowClosed(WindowEvent e)
{
screenshotThread.stopTakingScreenshots(); // This methods set the flag
}
}
Also notice that you are not taking the time it takes to make and save a screenshot in count. You use a fixed sleep of 40 milliseconds, but let's say that it takes 4 milliseconds to take and save the screenshot, then you should sleep for only 36 milliseconds. To time how long it takes to make the screenshot, use System.currentTimeMillis(); before and after your takeShot() method and make the difference.

combine multiple windows to a single window

I am developing an application which have the following windows
If the information entered in the windows are correct than only the user will be prompted with the windows in the above sequence. Now the customer has demanded me with this user interface.
Now I have to add all these windows in the last window format, with the specification's as the user will be allowed in the 2nd portion of the last image if the first information entered is correct.And the user when launches this app see the last image and can change the values as any time in the respective portions of the last window.
I have coded it in Swing Java.I am new to Java. I am working in Netbeans 7.1.2 I have three files as
1)Login.java
-containing my LoginDemo class which have main and form object of extended Jframe class
-Login class extending J frame and implementing action listener(this class creates an J frame of next file Enter the information.
2)Algorithm.java
creates new J frame object of next file if information is correct.
3)TravellingSalesmanProblem.java
gives the output as shown in Optimal Travel Route window.
I am accessing the information using REST call to a website.
So can anyone help me in this?
This will depend on how you structured you code. If you simply placed components directly onto the the windows/frames themselves, then you might be in for some work.
Alternatively, if you used panels, which you then placed onto windows, this might save you some time.
Anyway. Assuming you only have windows.
for each window do
myLastWindow.add(window.getContentPane());
This is pretty simple, but you'll also need to know the layout you want. I'd suggest something like GridLayout or VerticalLayout from the SwingX project.

java game picture not display

I make 2D game in AWT and I had all files in one package. Now I divided files into some packages. Images, which I called with:
ImageIcon ii=new ImageIcon(this.getClass().getResource(image));
img=ii.getImage();
now I call with:
ImageIcon ii=new ImageIcon("cz/ryska/awtgame/images/"+image);
img=ii.getImage();
This code is in class in package cz.ryska.awtgame.basic
But when I start game, displays game scene but not display images. Scene is empty. But any Java error is not induced. Images are probably found but not display. What is problem?
..Painting was functional before I change package structure.
You did more than change the package structure. The change is that the first is loading an image by URL, while the 2nd is loading an image from a File (where the String represents the path). An URL can be used with an embedded resource, while a File cannot.
See the info. page on embedded-resource for further info.

Adding icon to my application issues

I want to change the icon of my application. system.gif is in current directory. getDefaultToolkit() is not working. getToolkit() is working but another minimized window is also open. Other components are Swing components.
Toolkit theKit = jtfMainFrame.getDefaultToolkit();
Image icon = theKit.getImage("system.gif");
To get an icon for an app.
Add it to the Jar and use Class.getResource("/system.gif") to obtain an URL to it. File objects will generally not work for such an 'application resource'.
To load the image, use ImageIO.read(URL). This is a blocking method that will ensure the image is entirely loaded before proceeding. It will also throw informative exceptions if anything goes wrong.

Categories