Can we draw things on any Swing GUI component? - java

Can we draw things on any Swing GUI component? If yes then Why should we use a panel as
a canvas for drawings rather than a label or a button?

Start by taking a look at Perfoming Custom Painting for details about how painting is done in Swing
The second part of your question depends on what it is you want to achieve.
Generally speaking, you shouldn't paint directly onto existing components because painting is actually done by the UI delegate. If you want to change the look and feel of these components, you should consider creating a custom UI delegate to perform the painting as you need. This ensures that the painting is done correctly.
This doesn't stop you from overriding its paintComponent method, but with complex controls like buttons, lists, tables and trees, you may find it difficult to achieve the results you want.
Equally, tables, trees and lists use renderers to provide customised output

Lets assume a label is a price tag and a panel is a piece of paper. Do you usually paint() on buttons and price tags in real life? :)

Labels and Buttons have a lot of extra "features" and capabilities that will probably get in your way or bite you with bugs. JPanel is, almost literally, a clean slate on which to start.

Related

Java Swing: how to shade a game gui after the match has finished

In a board game we are developping in Java we would like the gui to be overshadowed when the game is finished. We have a Jframe in which there is JPanel with the board on which there are some colored pawns and boxes (JButtons) and we would like that everything becomes a sort of black and white and grey. Is there an authomatic method in Java to do this in Java Components?
There are several different kinds of panes to look at that could achieve this, or something similar, if you are using Swing (which I assume from the tag, that you are).
You could use a Glass Pane. http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html#glasspane
glass panes could be thought of as like a CSS overlay. They dis-allow interaction with components behind them.
Also, take a look at JXLayers and JLayers, they allow you to modify the way components are actually painted.
Good Luck!
Override paintComponent() (Or if you have some components added to the container it's better to override paintComponents() method).
Call
super.paintComponent(g);
Color semiColor=new Color(0,0,0,128);//the last param represents alpha
g.fillRect(semiColor);

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.

Does using setOpaque(false) impact painting performance on Swing JComponents?

My question is whether the use of setOpaque(false), which I've come to use with JPanel to layout UI in Swing, impacts painting performance more than keeping everything opaque (where isOpaque() returns true).
I'm not very familiar with how Swing renders UI but I would guess that if a JComponent is not opaque it would be harder to render what's behind the component then to simply paint an opaque background on the component.
Yes it does add extra work when painting but I doubt you would have to worry about it.
Basically, whenever you repaint a component that is transparent you need to go up the chain to find a parent that is not opaque and then paint that component first, before painting the child component.
So the bottom line is don't worry about it. If you have a reason to use transparency then use it. If you don't have a reason then you shouldn't be using it.
You may want to check out Background With Transparency. It goes into a little more detail on what the opaque property means and how it affects painting and the problem you will have if you do use a transparent background.

How to draw a JPanel on a custom graphic context and handle clicks (or alternative solution)

For my current project I have the situation that I need an interactive element that will be drawn on a custom graphics context and allows user interaction (e.g. mouse clicks).
Since this elements will be quite complex I decided the best solution would be to use a JPanel for layouting and manually paint it and dispatch mouse and keyboard events.
Currently my plan is to:
call setSize and setLocation on the Jpanel
call paint with the graphic context
Catch desired events (in this example clicks) create a new MouseEvent and pass it on with JPanel.dispatchEvent().
I suspect there will be many traps and edge cases along the way so
a.) Is this the correct approach or is it missing something fundamental?
b.) Is there any existing library which could be uses?
This is a bad idea, you are throwing away half of what Swing does for you and then trying to re-implement it yourself.
If you really must do this then something like this: How to make canvas with Swing? is probably your best way forwards. Really I would look to see if you can instead do this just by building the screen from custom controls though.

Animation with JComponents on Top

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)

Categories

Resources