I trying to make mini game and this game have option to choose map. Diffrent chooice, choose diffrent map. Every choice have own map ( background image ). For background image I useing JLabel and method: setIcon().
My problem is that when I set image, all my components get hide. This is picuture: http://prntscr.com/qi5m8a ( You can see that only image can be seen ).
For map choose I use this structure, here is picture: http://prntscr.com/qi5n4c There is Play button with event like this:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(mapOneRadioButton.isSelected())
{
this.dispose();
GameWindow game = new GameWindow();
game.setVisible(true);
game.setLocationRelativeTo(null);
backgroundLabelGameWin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/main/earthProject.gif")));
}else if(mapTwoRadioButton.isSelected())
{
}else if(mapTreeRadioButton.isSelected())
{
}else if(mapFourRadioButton.isSelected())
{
}else if(mapFiveRadioButton.isSelected())
{
}
}
I useing JPanel where I add JLabel for background image and I useing JLayeredPanel for rest of components.
My problem/question is, How can I change/set image without breaking order in my frame ?? I mean, I want to change/set image in background so all my components can be seen.
Please help me, I have no idea how to fix this.
For background image I useing JLabel and method: setIcon(). My problem is that when I set image, all my components get hide.
Two options:
add the components to the JLabel. Then the components will paint on top of the image. The restriction of this approach is that the components must fit inside the image, since the image is always painted at its actual size.
do custom painting of the image on a JPanel by overring the paintComponent(...) method of the panel and then invoke the Grapics.drawImage(...) method. Then add your components to the panel. See Background Panel for a class that provides this support. This provides more flexibility as you can scale the image to fill the panel.
It will be better if you change the background of the JPanel instead of using a JLabel for image. You can't directly change the background of a JPanel. But you can refer following link to do it
http://www.java2s.com/Code/Java/Swing-JFC/Panelwithbackgroundimage.htm
Related
I'm working on a 2D game and am now stumped on a drawing issue. I'm repainting the JPanel with the background image and characters every frame, but the GUI is created in a setup function. I think that causes the JButton to appear behind the background image, and its worth mentioning this class extends JPanel. I'm using setBounds in order to arrange GUI elements a special way. What is a possible solution to this issue?
/** Draw game to the screen **/
private void gameDraw() {
Graphics g2 = this.getGraphics();
g2.drawImage(image, 0, 0 ,null);
g2.dispose();
}
public void SetupUI() {
uploadButton = new JButton("UPLOAD");
uploadButton.setBounds(WIDTH / 2, HEIGHT - 40, 50, 30);
uploadButton.setToolTipText("Press this button to upload your AI to the ship");
uploadButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//TO DO: Make upload button compile and run the users code
}
});
this.add(uploadButton);
this.setVisible(true);
}
I decided to redo the code for rendering the game component. The issue seems to have been the GUI button and the image sharing the same screen space, then the image redrawing over the button each frame. What I was trying to go for was the gui overlaid over the image, but I still do not know how to achieve that.
Instead I created two separate JPanels, one to hold the GUI elements in a 'ribbon' above the image, and one to hold the image where neither panels are overlapping then packed both of those into another panel which was then assigned to the JFrame (feel like this is a scene from the Emperors New Groove lol) . Each panel has a layout assigned. The image in its own panel can now be rendered as frequently as needed without interfering with the gui. Just remember to draw whatever images you want in order e.g. draw the background, then the player.
I couldn't solve the original problem, but this is a decent workaround.
I want to add a background image to a JFrame which doesn't have any panels. It is a project I'm working on and I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code to do that and also I'm using netbeans. Is there any solution for this?
I want to add a background image to a JFrame which doesn't have any panels.
The content pane of the frame is a JPanel, so yes it does have panels.
I have almost completed it. So, I can't add a background using a JLabel because I will have to change a lot of code
If you want a background image then you will need to change your code to make sure the content pane can display the image. So yes you will need to change your code whether you use a JLabel of a JPanel that paints an image.
Check out Background Panel for code that will allow you to use either approach.
The key is that you need to set the content pane of your frame BEFORE you start adding components to the frame. So the code might look something like:
BackgroundPanel panel = new BackgroundPanel( yourImage );
frame.setContentPane( panel );
frame.add(northPanel, BorderLayout.PAGE_START);
frame.add(centerPanel, BorderLayout.CENTER);
I don't know what the Netbeans generated code looks like so I'll leave it up to you to figure out where to put the code.
I am trying to display an image in a JFrame GUI in Java. I have successfully loaded the image from a resource file and been able to display the image in a JOptionPane. This was achieved using a JLabel containing a Image Icon in the constructor. When trying to add this image to a JPanel nothing is displayed.
JLabel imgLabel1 = new JLabel(new ImageIcon(tsr.getTileImage(1,1)),JLabel.CENTER);
jpnDisplay.add(imgLabel1);
tsr is my custom code for getting a subimage from a tileset. The image returned is of type BufferedImage.
One thing I did notice is if I display the image in a JOptionPane then add it to the JPanel the image is displayed. I am unsure why this is.
JLabel imgLabel1 = new JLabel(new ImageIcon(tsr.getTileImage(1,1)),JLabel.CENTER);
JOptionPane.showMessageDialog(null, imgLabel1,"Label",-1);
jpnDisplay.add(imgLabel1);`enter code here
--EDIT--
After playing around with my code, I have discovered my issue was not with the way I was trying to display the images, but that for some reason my JFrame was not repainting unless a JOptionPane was shown before the JFrame was shown. It also only paints the same instance that was shown in the JOptionPane. Any other images to be painted get ignored. The reason is unclear.
You must subclass your JPanel and override the redraw method to draw you image.
I need some help with Java Swing components and its capabilities. I need to add a JPanel to a JFrame and paint an Ellipse2D on it. Onto the Ellipse2D I want to add another element, in my case it is a picture (right now I use an ImageIcon, maybe wrong). How can I achieve adding the Ellipse2D and the picture on the panel as shown in the image I attached?
The reason why I need the images separated is, because I need to change the filling color of the ellipse sometimes.
Thanks for any help.
What you need is to create a custom JPanel implementation and override paintComponent method.
Inside it, you just do:
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw ellipse here
// Draw your image here. It will be drawn on top of the ellipse.
}
This way, you can hold the ellipse fill color in the CustomPanel class, and just call repaint() method after you change the color.
your idea could be very good described (including code example) in the Oracles tutorial How to Decorate Components with the JLayer Class
notice JLayer is available only for Java7, but its based on (for Java6) JXLayer
you can use (I'm using) GlassPane too, with the same / similair output to the Swing GUI
EDIT
quite easy and nice output is by using OverlayLayout, there is possible to overlay J/Component(s) with Graphics e.g., a few examples
take the two images as image icons like
ImageIcon car=new ImageIcon("image path");
ImageIcon elipse=new ImageIcon("image path");
add those two image icons two label
JLabel carLabel=new JLabel(car);
JLabel ellipseLabel=new JLabel(ellipse);
and set the position of ellipse and car
carLabel.setBounds(0,0,50,50);
ellipseLabel.setBounds(10,10,50,50);
I'd like to implement a DragAndDrop for an Image but can't seem to get the Swing repaint function to work on the specific Image.
Code:
public class playerFrame extends JFrame{
...
private void destroyerImageMouseDragged(java.awt.event.MouseEvent evt)
}
repaintCurrentPosition(evt);
}
public void repaintCurrentPosition(MouseEvent e){
this.setLocation(e.getX(), e.getY());
this.repaint();
}
this.repaint <- this function repaints the whole frame and not just the Image I'd like it to repaint, which is about 50x50 size.
How do you repaint a specific JPEG image without creating a new class?
thanks.
this.repaint will force the parent frame to be repainted. Call repaint only on the control holding your image.
Example: to refresh this image loaded onto the JLabel:
ImageIcon icon = createImageIcon("images/middle.gif");
label = new JLabel("Image and Text", icon, JLabel.CENTER);
You do:
label.repaint();
not just the Image I'd like it to repaint, which is about 50x50 size
JComponent#paintImmediately carefully with EDT
How are you doing the drag and drop?
The easiest way is to just add an Icon to a JLabel and then drag the label around. Everytime you invoke setLocation(...) on the label it will repaint() itself.
The Component Mover class does all the hard work for you.
Call repaint only on the panel where your image is drawn.