Java: How to do GUI animations? - java

In all my time so far working with Java and its Swing GUI framework, I've never quite figured out (or even attempted to try) how to make the interface animate components.
Say I wanted the screen to slide left into the next screen or have a JLabel "fly" to a new location. Perhaps you want a menu to smoothly open in an animated fashion. How does this work?
Do you have to use SwingWorker? Even if that's the case... how can you control the painting of components if the layout manager is already doing that?

Have a look at the book Filthy Rich Clients, you will find some really good answers there.

I think that there no reason for use SwingWorker, SwingWorker is designated for running long Backgroung Task(s) on output would be on Event dispatch Thread,
For animations in Swing is there javax.swing.Timer, examples here

Take a look at Trident library. You can use it to interpolate various properties in your class.

Related

Futuristic GUI in Java?

First of all sorry for my english, it's not my mother tongue.
I want to made a program in java, i was thinking that it will look great with a futuristic graphical user interface. When i say "futuristic", i mean something like this:
I know it's the image of an Android application but i'd like to do something like this in a java program.
Usually when i have to do GUIs in java i use Swing components, but now i need to personalize everything, and i'd also like that when i click "Send" for example the circle becomes bigger and other things appears inside it.
So i need to have a complitely personalized and animated GUI. Which is the best way to do it? Swing, Awt, JavaFx or what else?
Thank you in advance.
Consider to use JavaFX. It supports styling via CSS, has APIs for effects, animations etc. and can work with the graphic card.
Swing is currently in maintenance mode and JavaFX is the new proposed (by Oracle) standard Java GUI toolkit for Java.
You could extend JComponent for every different component you want and then override the paint(Graphics g) method. Then you just draw on g object like on any other graphics object.

Writing an AWT widget

I fear I'm just making a ten-times duplicate question, but I've been trying to Google this for quite some time without finding anything, neither in general nor even here on SO, so I'm starting to think it's worth a shot.
I'm wondering, is there a good guide on what is necessary to implement an AWT widget in Java? What needs to be implemented in order to respond to such things as redrawing, resizing, placement, focus behavior and all other such things as widgets may or should do, in a proper manner? All the articles I manage to find describe the process from the perspective of a user of widgets, never from the perspective of the one implementing them.
Also, what would be the primary differences between implementing an AWT widget and implementing a Swing widget? Is there even a difference from the implementer's point of view?
Probably the best way to learn how to write an AWT widget would be to look at the source code for an AWT widget. Here's the source code for the java.awt.Frame class.
I haven't worked with AWT much, but Swing is much better from the GUI developer's point of view. The only difference that I know about from the implementer's point of view is Swing's usage of Listeners.
Again, you can look at the source code to see what's different. Here's the source code for the javax.swing.JFrame class
Not too much difference aside from the fact that AWT widgets are simpler.
You extend one of the 4 classes: Component, Container, Canvas, Panel. The first two are heavyweight, the latter two are lightweight (don't have native window system peers). Override paint(Graphics) (or update()) and getPreferredSize() to make it visible, then look into javadocs for overriding event handling methods.
Sure you know this link, http://docs.oracle.com/javase/6/docs/api/java/awt/Component.html

Java, Swing, Netbeans: how to create an interface similar to the iPhone's SMS screen?

I'm looking to create an interface similar to that of the iPhone's SMS screen. More specifically I'm looking to replicate the "bubbles" coming from each side of the page which contain the messages, as shown here http://www.bidslammer.com/images/iphone_shot1.png .
I do also want to recreate the date and time above the bubbles like you can see in that image. I need to be able to do this by code because its use will be to display the messages that it receives over my socket connection, and show the messages I send over the socket.
I'm really new to Java, and even newer to Swing, so I'm looking for some pointers on how I should go about this.
Can anyone offer my any suggestions about how I would go about doing this? I'm not looking for someone to do the work for me, just a few pointers, perhaps some things I should learn how to use/do and perhaps a helpful tutorial or two.
Google for "swing tutorial" gives a lot of tutorial links, so just pick one. When I was learning Swing I used original Java Swing tutorial.
As for some pointers, I think good idea is to use images to represent the bubbles - that will be the easiest. Inherit some basic component like JLabel or JPanel and override a drawing method - do a custom drawing. First draw the bubble image and then the text over it. This may help with image drawing.
Generally with custom component drawing you use Graphics class, which provides a lot of useful drawing methods.
The sticky notes demo might get you started in the right direction.
Even though that demo is a NetBeans platform module, the sticky note itself is a pure Swing component and should be usable without the platform.

Custom Swing component: questions on approach

I'm trying to build a new java swing component, I realise that I might be able to find one that does what I need on the web, but this is partly an exercise for me to learn ow to do this.
I want to build a swing component that represents a Gantt chart. it would be good (though not essential for people to be able to interact with it (e.g slide the the tasks around to adjust timings)
it feels like the best approach for this is to subclass JComponent, and override PaintComponent() to 'draw a picture' of what the chart should look like, as opposed to doing something like trying to jam everything into a custom JTable.
I've read a couple of books on the subject, and also looked at a few examples (most notably things like JXGraph) - but I'm curious about a few things
When do I have to switch to using UI delegates, and when can I stick to just fiddling around in paintcomponent() to render what I want?
if I want other swing components as sub-elements of my component (e.g I wanted a text box on my gantt chart)
can I no longer use paintComponent()?
can I arbitrarily position them within my Gantt chart, or do I have to use a normal swing layout manager
many thanks in advance.
-Ace
I think that the article i wrote a few years ago for java.net is still correct today. Doing everything in one monolithic class gets you going faster in the beginning, but becomes a mess quite fast. I highly recommend doing the separation between the model (in your main class) and the view (UI delegate). The view is responsible for:
interaction with the user - mouse, keyboard etc.
painting
creating "worker" subcomponents as necessary
In the medium and long run this is the approach that has been validated over and over again in the Flamingo component suite, which you can use as an extra reference point (in addition to how core Swing components are implemented).
Using UI delegates is a good idea if you think that your component should look different for different Look And Feels. Also it is generally a good idea from design point of view to separate you presentation from your component
Even when overrding paintComponent you can still put any sub components on it.
Using null layout you arbitrarey position your components. Alternatively you can use layouts too.
Here is a very good starting point for you.

Listening to all JInternalFrame events - Java

I'm trying to internationalise a Java applet and with that, support scripts which are written from right to left. I want to set up component orientations for all java components added to the view automatically.
My solution so far has to listen to all AWTEvent's using the windows mask:
c.getToolkit().addAWTEventListener(listener, AWTEvent.WINDOW_EVENT_MASK);
...and then setting the c/o on each window added, as well as adding component listeners to set c/o on any components added to the window at a later point.
My issue is that JInternalFrames are not handled by this solution, I want to be able to add another listener for these events, much like I have done for windows. Any ideas?
Or alternatively, are there any better approaches to handling script direction for all components in an applet?
Add a ContainerListener to the JDesktopPane. As a component is added to the desktop you can change its orientation.
Do you have a handle on all those JInternalFrames? If so, try the internal frame listener.
http://java.sun.com/javase/6/docs/api/javax/swing/event/InternalFrameListener.html
It notes that it's the analogue to the AWT WindowListener.
AWTEventListener on the current Toolkit will only give you events coming from the toolkit. Generally events generated by lightweight components will have been caused by mouse or key events.
Asking for all of something in a process is usually a very bad sign. A low-level piece of code is making policy for the whole program. A much better approach is to add listeners near to where you create the component, before it is "realised". This is repeated code, but then you probably already have repeated code. So factor out into a method. Then you have only one place to update, unless you have any cases where it doesn't apply which would have broken the global approach.

Categories