Animation with JComponents on Top - java

i wanted to ask, if somebody might have a solution about a problem i face. I am working at an application, which draws an animation - for instance a map with objects moving onto. My problem is, that on top of the drawing, a Jtable, Jlist as well as other Components are also placed.
In my particular example all of those components have been added to the Panel, which holds the map. In result each component gets redrawn as often as good my fps is. Therefore making one of the tables invisible reduces the already high cpu usage of sometimes around 50% to less than 30%.
My question is, how can i avoid calling somewhat static visual contents paintComponent() method, without having the "background" - the map - whited out the menu.
Since the animation redraws permanently the menu is not shown at all, if its separated from the corresponding JPanel.
First thoughts move into following directions:
Clipping - actually not as good as i would like to, since id like to enable moving around the menus.
JLayeredPane - already tried but seemed to turn out, that the paintComponent method of menus still gets called frequently.
JWindow/Internal Frame - had that thought a couple of minutes ago. Having a complete independent container shall be able to handle my regard, or?
I am looking forward, if somebody has an elegant idea, how to fix that and reduce the cpu usage significantly.
Thanks!!
Best regards.

I would create a custom Shape for clip. Use Area class and subtract from the Area all the children components' bounds.

For painting over JComponent(s) placed into JPanel you have look at
JLayer (Java7) based on JXLayer(Java6)
GlassPane, notice all JComponents must be lightweight, otherwise is GlassPane behind heavyweight (J)Components
is possible painting to the JViewport,
EDIT
you have to use Swing Timer for moving with Icon (in the JLabel) placed into JXLayer, GlassPane or JViewport, don't use Runnable#Thread and never, not by using plain Thread.sleep(int)

Related

Java button within graphics panel

I'm relatively new to developing GUI's within java so this may well be a stupid question or quite simply not possible to achieve but here we go.
I've created 1 single JPanel with no border layout set up or anything like that and I intended to paint a GUI on top of it using the graphics class. The JPanel is just plain black and then I've drawn a huge box over it leaving the black just as a border, and painted the whole GUI within this white box.
I want to add buttons within that white box GUI as well but I've no idea how. In fact they don't even have to be traditional buttons JButtons, if I could just draw a shape and have that act as a button then add an event handler to just that shape that would work also but I don't know how I'd do that either.
I have so much code for my whole program (it's a school coursework project) that I'm not sure which parts would even be worth sharing to assist with this question since there's so many GUI aspects I've already drawn so I've tried to just explain my issue in words.
Honestly I have no clue what I'm doing so any help would be appreciated.
EDIT: Here's a screenshot of my current GUI with a 'sketch' of how and where I'd like to be able to add buttons.
GUI Image
As with any suitably complex UI, you need to start by breaking it down into manageable chunks, focusing on areas of mutual interaction and functionality.
For example...
Says to me that you have two primary UI elements, the left and the right.
This could easily be established with a GridLayout, but, if the two sides are not equal in width, a GridBagLayout might be more appropriate
The right side to me says simply, JTable. You could place this within a container using a BorderLayout, allowing the table to occupy the CENTER position.
The key information would then a component laid out with either a GridLayout (top and bottom) or a GridBagLayout if the requirements are more complex. This component would then be added to the SOUTH position of the BorderLayout.
Again, this is pretty simple. The primary layout would probably be a BoderLayout, with the title in the NORTH position, the graph in the CENTER and the buttons wrapped in a component in the SOUTH.
You could use either a FlowLayout or GridBagLayout to layout the buttons depending on your how you want them to appear
Recommendations
Have a look at:
Laying Out Components Within a Container
How to Use Tables
And for the "border", I'd recommend you have a look at LineBorder. Take a look at How to use Borders more details

JPanel vs JFrame when animating

I recently did a program which moves an image along the window and forces it to change its direction when it hits the borders. The animation happened within a JPanel class which was then of course added to a JFrame class which contains the main() function. Now my question is why must it be done in this manner. Can't I simply have just the JFrame and use that both as my window and my animation class which moves the image? Is the JFrame incapable of representing animation on it's own.
Regards.
Omar's answer is correct but I thought I might elaborate just a bit:
Though JFrame's are capable of hosting your animation, it is not a typical use of the JFrame. JFrame's are typically the outermost visual container and host one or more other visual containers (such as JPanel). Though there are exceptions, I would consider the JPanel the most flexible and common way to contain your animation (or other GUI 'controls') to leave you flexibility for adding visual features later.
But as Omar points out, either JFrame or JPanel can be used as they are both considered "containers" in Swing. Tying back to my last comment, using a JPanel is a bit more flexible. For instance, you might later want to incorporate your animation into an Applet/JApplet which you might find confusing and/or difficult if you used a JFrame.
I tried to just comment on Omar's answer but I am a new user and it won't let me add comments yet. Therefore, upvotes are appreciated :)
It's just better to use JPanel as you may want multiple Panels in one frame each with different animations.
Yes, you could do it with JFrame as well.

JAVA: How can I add SWING components to my game HUD with custom textures

-- Little note, I attempted to upload an image of my game to illustrate my question, however I do not yet have the required reputation to do so. I apollogise for this.
I would like to create a drop down screen from the top HUD element on my game which the player can type into, effectively becoming a chat window, the actually window is not an issue and I understand that you can disable background and boarder rendering of Java's Swing components so that isn't an issue.
My question is simple, can I take advantage of java's Swing components like JTextField and position them exactly within the bounds of this area, without having to deal with java's layout classes. So this is a summary:
How do I set the final size of the swing components MANUALLY and
How do I set coordinates of the components MANUALLY With out using a layout manager
Yes you can use a null layout on the container and call setBounds(...) on the component to MANUALLY place them. And this is usually a VERY BAD THING to do as it forces you to paint yourself into a layout corner making it very hard to upgrade or enhance your GUI later. It also guarantees that your GUI will look terrible on all platforms and screen resolutions other than one. Many newbies usually go this route initially, and then most leave it eventually after gaining more experience with Swing as they run into its failings, weaknesses and limitations.
For a more complete answer, consider giving more specifics and in image (we can help with this) of your GUI layout requirements.

How to resize components when resizing a JFrame? Using Null Layout

I am using the NetBeans Designer to create a JFrame. Also it is worth mentioning I am fairly new to Java so I might not understand some things / do things correctly. The frame has about 100 panels, more buttons than I would even think about counting, about ~40 tables, basically most swing components the NetBeans designer provides are being used within the frame. Also for the main frame I am using Null Layout (in order to have a background image inside a JLabel). I know it is not recommended but it doesn't affect the general layout of things as I'm using panels/LayeredPane/TabbedPane for everything, each with it's own design (most of them on Free Design with no Layout specified - that's how I started, didn't know about Layouts and it would take ages now to rearange everything after using Grid Bag Layout for example).
Now getting to my problem, I need to be able to resize the frame and make it resize all components contained. I have to carry a presentation tomorrow of it and I just noticed it doesn't fit on smaller displays (and resizing it doesn't do it properly, it just hides components). I do not care much if it's just an improvisation / not the best approach to the problem as after the 15 min presentation I will probably never open it again.
Thanks.
I would try to go through all the components tree and try to set them smaller font and reduce all their bounds to some static %.
In other words for each component multiply x,y,widht,height to e.g. 0.75 and call setFont() passing derived font of 25% smaller.

My JApplet disappears when moved to secondary monitor

So I wrote a small program for a class. I designed it as a JApplet inside an undecorated JFrame, not in a browser. Other than that, it's a simple drawing program of sorts. You click two points to draw the selected shapes, then it calls a repaint. The problem I'm having is that when you draw while the program has been moved to my secondary monitor, the entire JApplet seems to disappear, only displaying the drawn shape. It only disappears after the 2nd point is selected, so I presume it does this on repaint().
My secondary monitor is using the exact same brand and resolution, even color profile.
Any other technical details, I'm using Java 1.7 (Can't recall which update off the top of my head), Windows 8 Enterprise 64x, using Eclipse's Run button to test.
Thanks in advance for any help!
I am indeed calling getGraphics(); in the init() method of the JApplet..
That is the problem. The Graphics object is a transient thing that will be repainted the very next time the JVM thinks there is any need to do so. That might be triggered by:
Changing the size or location of the window.
Covering it with another program and then removing the covering app.
Adding new components or changing values that are displayed.
See Performing Custom Painting for more details on how to do what you are attempting to achieve. OTOH Swing has a JLabel that can show a BufferedImage. You can use the BufferedImage in the way you want. When it is updated, call repaint() on the label to see the effect.

Categories

Resources