Positioning a label using swing in java - java

I have been using jFrame to build a GUI. I had to insert images in the GUI, for which i inserted a label and put the image as an icon for the label. Now, i have to find out the position of the image in terms of the x and y co-ordinates and i am unable to do that. I have used
setLocaction(x,y);
but it still doesn't seem to work. I even disabled the layout manager by using
setLayout(null);
What is the possible solution for this problem?
Edit
Basically i am creating a Solar system GUI using Swing, so the positions of the planets are to be set by me. I being new to java, there is being some difficulty in implementing the layouts.

This isn't a layout issue at all, but a drawing and possibly an animation issue. If this were my project, I'd
First and foremost, separate out the program logic from its display, a la the MVC or Model-View-Control pattern, or one of its many variants.
Create one JPanel for the GUI's graphics and do all my drawing in this
I would not display my planet images using ImageIcons inside of JLabels.
Rather, I'd create a background image and draw my planet sprites inside of the drawing JPanel's paintComponent method.
I'd create non-GUI Planet classes that a non-GUI model would hold.
In the GUI portion of my program, I would associate a BufferedImage with each Planet, probably using a HashMap<Plant, Image>.
I'd then draw each Planet's associated image in the drawing JPanel's paintComponent(...) method, and place it depending on the Planet's position field values.
If I wanted to animate this, I'd use a Swing Timer to drive my simple animation.

With null layout you should use setSize and setLocation methods on you label to get your image visible correctly inside your frame.

Related

Prevent JPanel from redrawing background on repaint() calls

Some animations (such as some gif files) only store in each frame the pixels that change from the previous frame. To draw these animations correctly, you have to paint the first frame, then paint the succeeding frames on top of them without erasing the previous frames. JPanels, however, always redraw the background when you call repaint(), erasing the previous frames. Is there a way to prevent a JPanel from redrawing the background? I've been told that calling super.paintComponent(g) in the JPanels paintComponent(Graphics g) method is what redraws the background, but I tried commenting that out and it lead to some strange behaviour, so I'm assuming it does more than just repaint the background.
I'd recommend to build your code upon the already existent code provided by the API instead of messing with it. Just store the image as BufferedImage. This allows you to display it using an ImageIcon, so it's an additional simplification. This allows you to update single pixels without any hassles with the API. If you absolutely insist on excluding the JPanel from the repaint-routine, this question might help.
In general:
Follow the conventional use of the API. If you want to permanently store data of an image, us a BufferedImage. JComponents are supposed to be entirely overridden every time the frame is updated.
Depending on your exact requirement there are two common approaches:
Draw on a BufferedImage and then display the BufferedImage
Keep an ArrayList of Objects you want to paint and paint all the Objects in the list every time paintComponent() is invoked.
Check out Custom Painting Approaches for more information and working examples of each approach.

Java - Swing - displaying multiple JComponents in one JFrame

in a game, I would like to display simultaneously two JComponent instances in a JFrame. One would be a background board, the another - a player character.
So, one component (a background) would be behind another (a character). The character would be drawn of several rectangles and thus it will most commonly have some wholly transparent area.
How to do that? I know that normally, when I add two components to a frame (method add(Component)), only the last-added component is visible. This is done by following code:
frame.add(backg); // backg is an instance of a certain class that derives from JComponent
// (...)
frame.add(psc); // psc is an instance of an another class that derives from JComponent
frame.pack();
frame.setVisible(true);
How should I change the code above?
First of all, if you are looking to write a game in Java, try out Slick2D provides numerous tools and far better graphical capabilties (being wrapped around LWJGL which wraps around OpenGL). Secondly, if you do decide to go the Swing route, here's a simple solution:
//Player + background components defined here
playerComp.setOpaque(false);
JLayeredPane layer = new JLayeredPane();
layer.add(backgroundComp,1,0);
layer.add(playerComp,2,0);
I believe that both these solutions were mentioned above in the comments. Setting the player component opaquity to false allows those transparent areas to show components behind the player, or the background image. If you're familiar with a z-index in CSS/HTML, a JLayeredPane basically adds a z-index to Swing by allowing you to set the order in which components are rendered. So, set the player to opaque, and then render it in front of the background component.

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 Swing : best way to set background image clickable

I try to find the best way to make an image clickable with different action depending on the area you click. For now, my window display an EFTPOS (image found on google image) using :
getContentPane().add(new JPanelBackground("./img/background.png", "./img/eftposAboveBackground.png", this.getWidth(), this.getHeight()));
Now, I would like to click on every EFTPOS's buttons. I was thinking of create some transparent buttons at the buttons's positions but :
First, I don't want to use an absolute layer
Second, the Swing GUI design editor (Netbeans) doesn't display my background image since I used the code above.
I'm pretty sure there is another way than setting my eftpos on the background, maybe using the ImageIcon class..
Any help would be greatly appreciated!
You can to create an ArrayList of Rectangles (or Shapes) to represent the area of each button on the image.
You need to add a MouseListener to the image.
In the mousePressed() event you need to iterate through all the Rectangle in the ArrayList using the Rectangle.contains(mouse point) method to see if the user clicked in a Rectangle.
When you find the Rectangle that was clicked you execute your custom code.

how add different shapes partially on JPanel+image which is on JPanel

My Project is in Java Swing.
I have a JPanel on which I am adding some images with .png extension (which are on JLabels) at center.
Now I want to add a line which will be partially on the JPanel & partially on that image.
Currently when I am adding a line, JPanel shows the line but when I resize the image & drag it to the image, the image hides the line.
What can be done so that the image doesn't hide my line & shows it on image?
You're probably better off drawing the image yourself and drawing the line over the top in the same control. Create a class that extends Canvas and in the paint method write your own code to paint the image and then draw the line.
Another option is to use JLayeredPane instead of JPanel as your main container and place a non-opaque (setOpaque(false)) JPanel on a higher layer
Use JLayeredPane.setLayer(yourPanel, highNumber) and fill your JLayeredPane using something like GridBagLayout or a simple custom LayoutManager.
You can then implement the custom painting on that panel.
You could try using JXLayer and defining a custom LayerUI for it that would draw the lines. These would then appear above the components you need to draw over.
This is a little more advanced and involves using a 3rd party (open source) custom component but will allow you to change you mind about what Swing component you use to render your images later.
I think this article best describes how to achieve what you want.
I've solved many similar issues to this in the past in a variety of ways and none have had the flexibility and maintainability of JXLayer.

Categories

Resources