How to make images appear on your screen without an associated window - java

MAJOR EDIT
I have restructured my question to be more appropriate and less arbitrary, but the basic message still holds.
What I want to do is make some text appear without an associated window in any of the following languages: Java, C or python. It would just be the text that I want to appear completely overlaid on whatever you happen to currently have on your desktop. It would be overlaid on all programs, etc.
Any recommendations/starting points on how to do this?
OLD POST
I've heard rumors that it's possible to use assembly to make "images" appear (I don't know what they'd actually be considered, what I heard was that my friend professor made a semi-transparent, green dollar bill sign appear and bob up and down on the lower right side of the screen)
So my question is:
Are there any assembly commands/series of commands to make things appear on your screen?
Can anyone point me in the direction to use assembly to interact with what appears on the screen?
Is it possible to have an independent symbol appear without a window associated with it? Or alternatively, is it possible to make the background window completely transparent while still keeping the image intact?
How difficult would it be to create an assembly program which makes text appear in this manner? (i.e. overlaid whatever you currently have on your screen without opening it's own window)
NOTE if you would like to provide any examples, which would be greatly appreciated, I am currently on a mac so my computer only interprets AT&T syntax
OLD POST

Ultimately you DO create a window, you just set its background to transparent. Drop this code into a file called test.java and give it a shot - just tried it out on Mac OS 10.7 and it works. (The text shows up black near the top left corner of the monitor - you have to look carefully to see it.) Note that according to this post you do need Java > 1.6.
import javax.swing.*;
import java.awt.*;
public class test extends JWindow{
public test(){
super();
this.setSize(500, 400);
this.setLocation(50, 50);
setBackground(new Color(0,0,0,0)); // this is the critical line - that fourth 0 represents alpha (or opacity)
setAlwaysOnTop( true ); // keeps it in the foreground so you don't click away from it - note that clicks on the transparent part DO pass through to the desktop, at least on Lion
JLabel testLabel = new JLabel("Floating text hah");
this.add(testLabel);
}
public static void main(String[] args){
System.out.println("Sup");
test t = new test();
t.setVisible(true);
}
}
Hopefully this puts you on the right path.

This is certainly possible, but will depend on your operating system and windowing system as most operating system prevent a program from directly accessing the video memory (to allow program to share it, they have to impose some restriction). The basic idea is to create a transparent borderless window and to draw your content in it. If you ask the windowing system to display your window before all the other window (Z order), then, it will appear as if your program is directly drawing to the screen.
There are some library that does exactly that. For X11 you can use libXosd. For Windows, the OSD executable can display a single string (the source don't appear to be available).

Related

What is the functional difference between creating a Java frame inside the main class and creating a Java Frame inside a constructor? [duplicate]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm developing an application which displays images, and plays sounds from a database. I'm trying to decide whether or not to use a separate JFrame to add images to the database from the GUI.
I'm just wondering whether it is good practice to use multiple JFrame windows?
I'm just wondering whether it is good practice to use multiple JFrames?
Bad (bad, bad) practice.
User unfriendly: The user sees multiple icons in their task bar when expecting to see only one. Plus the side effects of the coding problems..
A nightmare to code and maintain:
A modal dialog offers the easy opportunity to focus attention on the content of that dialog - choose/fix/cancel this, then proceed. Multiple frames do not.
A dialog (or floating tool-bar) with a parent will come to front when the parent is clicked on - you'd have to implement that in frames if that was the desired behavior.
There are any number of ways of displaying many elements in one GUI, e.g.:
CardLayout (short demo.). Good for:
Showing wizard like dialogs.
Displaying list, tree etc. selections for items that have an associated component.
Flipping between no component and visible component.
JInternalFrame/JDesktopPane typically used for an MDI.
JTabbedPane for groups of components.
JSplitPane A way to display two components of which the importance between one or the other (the size) varies according to what the user is doing.
JLayeredPane far many well ..layered components.
JToolBar typically contains groups of actions or controls. Can be dragged around the GUI, or off it entirely according to user need. As mentioned above, will minimize/restore according to the parent doing so.
As items in a JList (simple example below).
As nodes in a JTree.
Nested layouts.
But if those strategies do not work for a particular use-case, try the following. Establish a single main JFrame, then have JDialog or JOptionPane instances appear for the rest of the free-floating elements, using the frame as the parent for the dialogs.
Many images
In this case where the multiple elements are images, it would be better to use either of the following instead:
A single JLabel (centered in a scroll pane) to display whichever image the user is interested in at that moment. As seen in ImageViewer.
A single row JList. As seen in this answer. The 'single row' part of that only works if they are all the same dimensions. Alternately, if you are prepared to scale the images on the fly, and they are all the same aspect ratio (e.g. 4:3 or 16:9).
The multiple JFrame approach has been something I've implemented since I began programming Swing apps. For the most part, I did it in the beginning because I didn't know any better. However, as I matured in my experience and knowledge as a developer and as began to read and absorb the opinions of so many more experienced Java devs online, I made an attempt to shift away from the multiple JFrame approach (both in current projects and future projects) only to be met with... get this... resistance from my clients! As I began implementing modal dialogs to control "child" windows and JInternalFrames for separate components, my clients began to complain! I was quite surprised, as I was doing what I thought was best-practice! But, as they say, "A happy wife is a happy life." Same goes for your clients. Of course, I am a contractor so my end-users have direct access to me, the developer, which is obviously not a common scenario.
So, I'm going to explain the benefits of the multiple JFrame approach, as well as myth-bust some of the cons that others have presented.
Ultimate flexibility in layout - By allowing separate JFrames, you give your end-user the ability to spread out and control what's on his/her screen. The concept feels "open" and non-constricting. You lose this when you go towards one big JFrame and a bunch of JInternalFrames.
Works well for very modularized applications - In my case, most of my applications have 3 - 5 big "modules" that really have nothing to do with each other whatsoever. For instance, one module might be a sales dashboard and one might be an accounting dashboard. They don't talk to each other or anything. However, the executive might want to open both and them being separate frames on the taskbar makes his life easier.
Makes it easy for end-users to reference outside material - Once, I had this situation: My app had a "data viewer," from which you could click "Add New" and it would open a data entry screen. Initially, both were JFrames. However, I wanted the data entry screen to be a JDialog whose parent was the data viewer. I made the change, and immediately I received a call from an end-user who relied heavily on the fact that he could minimize or close the viewer and keep the editor open while he referenced another part of the program (or a website, I don't remember). He's not on a multi-monitor, so he needed the entry dialog to be first and something else to be second, with the data viewer completely hidden. This was impossible with a JDialog and certainly would've been impossible with a JInternalFrame as well. I begrudgingly changed it back to being separate JFrames for his sanity, but it taught me an important lesson.
Myth: Hard to code - This is not true in my experience. I don't see why it would be any easier to create a JInternalFrame than a JFrame. In fact, in my experience, JInternalFrames offer much less flexibility. I have developed a systematic way of handling the opening & closing of JFrames in my apps that really works well. I control the frame almost completely from within the frame's code itself; the creation of the new frame, SwingWorkers that control the retrieval of data on background threads and the GUI code on EDT, restoring/bringing to front the frame if the user tries to open it twice, etc. All you need to open my JFrames is call a public static method open() and the open method, combined with a windowClosing() event handles the rest (is the frame already open? is it not open, but loading? etc.) I made this approach a template so it's not difficult to implement for each frame.
Myth/Unproven: Resource Heavy - I'd like to see some facts behind this speculative statement. Although, perhaps, you could say a JFrame needs more space than a JInternalFrame, even if you open up 100 JFrames, how many more resources would you really be consuming? If your concern is memory leaks because of resources: calling dispose() frees all resources used by the frame for garbage collection (and, again I say, a JInternalFrame should invoke exactly the same concern).
I've written a lot and I feel like I could write more. Anyways, I hope I don't get down-voted simply because it's an unpopular opinion. The question is clearly a valuable one and I hope I've provided a valuable answer, even if it isn't the common opinion.
A great example of multiple frames/single document per frame (SDI) vs single frame/multiple documents per frame (MDI) is Microsoft Excel. Some of MDI benefits:
it is possible to have a few windows in non rectangular shape - so they don't hide desktop or other window from another process (e.g. web browser)
it is possible to open a window from another process over one Excel window while writing in second Excel window - with MDI, trying to write in one of internal windows will give focus to the entire Excel window, hence hiding window from another process
it is possible to have different documents on different screens, which is especially useful when screens do not have the same resolution
SDI (Single-Document Interface, i.e., every window can only have a single document):
MDI (Multiple-Document Interface, i.e., every window can have multiple documents):
I'd like to counter the "not user friendly" argument with an example that I have just been involved with.
In our application we have a main window where the users run various 'programs' as separate tabs. As much as possible we have tried to keep our application to this single window.
One of the 'programs' they run presents a list of reports that have been generated by the system, and the user can click on an icon on each line to pop open a report viewer dialog. This viewer is showing the equivalent of the portrait/landscape A4 page(s) of the report, so the users like this window to be quite big, almost filling their screens.
A few months ago we started getting requests from our customers to make these report viewer windows modeless, so that they could have multiple reports open at the same time.
For some time I resisted this request as I did not think this was a good solution. However, my mind was changed when I found out how the users were getting around this 'deficiency' of our system.
They were opening a viewer, using the 'Save As' facility to save the report as a PDF to a specific directory, using Acrobat Reader to open the PDF file, and then they would do the same with the next report. They would have multiple Acrobat Readers running with the various report outputs that they wanted to look at.
So I relented and made the viewer modeless. This means that each viewer has a task-bar icon.
When the latest version was released to them last week, the overwhelming response from them is that they LOVE it. It's been one of our most popular recent enhancements to the system.
So you go ahead and tell your users that what they want is bad, but ultimately it won't do you any favours.
SOME NOTES:
It seems to be best practice to use JDialog's for these modeless windows
Use the constructors that use the new ModalityType rather than the boolean modal argument. This is what gives these dialogs the task-bar icon.
For modeless dialogs, pass a null parent to the constructor, but locate them relative to their 'parent' window.
Version 6 of Java on Windows has a bug which means that your main window can become 'always on top' without you telling it. Upgrade to version 7 to fix this
Make an jInternalFrame into main frame and make it invisible. Then you can use it for further events.
jInternalFrame.setSize(300,150);
jInternalFrame.setVisible(true);
It's been a while since the last time i touch swing but in general is a bad practice to do this. Some of the main disadvantages that comes to mind:
It's more expensive: you will have to allocate way more resources to draw a JFrame that other kind of window container, such as Dialog or JInternalFrame.
Not user friendly: It is not easy to navigate into a bunch of JFrame stuck together, it will look like your application is a set of applications inconsistent and poorly design.
It's easy to use JInternalFrame This is kind of retorical, now it's way easier and other people smarter ( or with more spare time) than us have already think through the Desktop and JInternalFrame pattern, so I would recommend to use it.
Bad practice definitely. One reason is that it is not very 'user-friendly' for the fact that every JFrame shows a new taskbar icon. Controlling multiple JFrames will have you ripping your hair out.
Personally, I would use ONE JFrame for your kind of application. Methods of displaying multiple things is up to you, there are many. Canvases, JInternalFrame, CardLayout, even JPanels possibly.
Multiple JFrame objects = Pain, trouble, and problems.
I think using multiple Jframes is not a good idea.
Instead we can use JPanels more than one or more JPanel in the same JFrame.
Also we can switch between this JPanels. So it gives us freedom to display more than on thing in the JFrame.
For each JPanel we can design different things and all this JPanel can be displayed on the single JFrameone at a time.
To switch between this JPanels use JMenuBar with JMenuItems for each JPanelor 'JButtonfor eachJPanel`.
More than one JFrame is not a good practice, but there is nothing wrong if we want more than one JFrame.
But its better to change one JFrame for our different needs rather than having multiple JFrames.
If the frames are going to be the same size, why not create the frame and pass the frame then as a reference to it instead.
When you have passed the frame you can then decide how to populate it. It would be like having a method for calculating the average of a set of figures. Would you create the method over and over again?
It is not a good practice but even though you wish to use it you can use the singleton pattern as its good. I have used the singleton patterns in most of my project its good.

How to change the color of the Title bar when running a GUI program?

I want to change the color of the title bar and also the Java icon on the upper left of the frame, so I can make my program (GUI) look better. Is there a simple way to change it?
I'm not sure how useful this observation is, but under the X Window System (e.g. on Linux), the titlebar and the icons in it aren't usually under the control of the application, they're “decorations” under the control of the window manager (WM). I'm not sure exactly how this might impact a Java program — they might be able to use undecorated windows and add their own equivalent — but it's equally possible that what you want to do simply isn't possible (without an inordinate amount of effort) on any platform.

Capturing events in X11 even after the loss of focus

I am trying to develop an application which responds to multiple digital pens (IRIS Pens) so that if any of the pen writes on paper; I relay the output to a single screen. Thus making a multi-input whiteboard for myself.
In Ubuntu these pens are recognized as mouse and thus can be handled in a similar manner as mouse events are handled.
So now what I plan to do is to handle these events in C/C++ using XLib and pass these events to a Java Swing application using JNI callback. I am able to do this but when the X11 window looses focus no events are transferred to the Swing frame. I also tried to use the root window in X11 but it does not seem to work.
Any help would be really appreciated. Thanking you in advance.
How about maximizing the C/X11 window in front of the Java one, and making it transparent? You should be able to see the Java window while still focusing on the C/X11 one.
Since you are using Ubuntu, you can achieve this using the "Opacity, brightness and saturation" plugin for Compiz. It is in the compiz-plugins-main package, and you can activate it with Compiz Settings Manager (from the compizconfig-settings-manager package). When you activate the plugin, alt+wheel is bound by default to change the transparency of the focused window.
Try reading the mouse directly. I don't remember the exact location, but you should find it in something like '/dev/input/mouseX', where X is the number of your device, ranging from 0 to n-1 devices..
When you read the packet, your application should block until the mouse moves and then your read function will return a raw mouse packet which describes the delta (which is probably more useful then the screen coordinates, in your case) and the mouse button statuses.
The raw packet can be decoded as described here: http://www.computer-engineering.org/ps2mouse/
Create a modal dialog and set it to XmDIALOG_SYSTEM_MODAL (the actual name of the property depends on your toolkit: Motif, Gtk, Qt, ...). Dialogs like this block the whole display and can never loose focus.
The drawback is of course that you can't do anything else while this dialog is on the screen.
This entry in the X11 FAQ might help.

Is there anyway to display a custom form in Java that acts similar to TrayIcon.displayMessage()?

Using Java is there anyway to display a custom form/image that behaves similar to TrayIcon.displayMessage() function in that it displays just above the system tray for a while then disappears?
I am also looking for a way to display multiple notifications at the same time by having them display above each other.
If not, how do I find the pixel location for the lower left corner just above the system tray?
I don't think there's a shortcut for drawing frames that act just like the standard TrayIcons (with the x button in the corner) that support stacking akin to the Mac Growl notifications.
you will probably have to implement it yourself.
I've found that stacking messages like that is a complete waste of time as the user will not pay attention to them - the better location for these is in the status area of the application in a simple popup menu.
You can use java.awt.GraphicsEnvironment. getLocalGraphicsEnvironment() to get information about the desktop, which contains 'getMaximumWindowBounds()' which takes care of things like the taskbar position.
You can use a subclass of a javax.swing.JWindow to create a window without a border which can be positioned on the desktop relative to the bottom right corner. This will not always work as the default tray icon, as the location of the icon generator can be somewhere else other than that.
You can add a button that acts like the 'x' button of a standard desktop window - but it's going to be platform dependent.
I use square windows that stack up from the top right corner if I'm using LTR, and it seems to work well.
Go check out Java GNOME. It has Java bindings for GTK, including a status icon for the tray, and notification events.

Gui - Best way to navigate between windows?

I try to build a gui (Swing) for a simple java application. The application should have a start window like a menu. From there I would like to navigate to several other windows.
My question is what is the best-practice to achieve such a navigation? Should I build several JFrames and switch the visibility of them on/off when navigating OR should I better have one JFrame and add/remove JPanels in this single frame to navigate between the windows?
Thanks.
I recommend
Do not do a MDI application with sub-frames like those found in the old Windows days. They suck as they make the matter confusing.
Do design a tabbed interface. The welcome page/menu will be displayed on a first tab that is always created on the start.
All cool kids do that nowadays:
Visual Studio
Eclipse
Firefox
If each of your windows correspond to different task (possibly nested), you could present your application as a SDI, with on the left a task panel like:
Each of the task would display one JFrame with the associated window.
Other solution: a table of content like this picture on the left side
(note: it actually also displays a task panel in this example on the bottom right)
Multiple JFrames sounds like a better idea to me. Much more OO.
You must find a balance between these goals:
Not too many things in one "window"
The user must quickly be able to find the correct window to do the next step of work
All relevant information must be visible at any time
Eclipse solves this by creating many small editors where each editor shows some specific information and allows to modify it. Editors are then arranged within one OS window in tabs and "views". A view is always completely visible and they can be arranged. Think of a view as a way to cut an existing editor in half (horizontal or vertical) and then being able to replace one of the halves with another editor. Between each half, you have a splitter so you can adjust the sizes.
Arrangements of views are then saved in "perspectives".
This allows every user to create a perspective which contains all the necessary editors at the same time, arrange them as they need it and work effectively.

Categories