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.
Related
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
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 image to my panel which is created by Netbeans GUI Designer.
here's my canvas class
private static class GraphicsClass extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Toolkit tkit;
tkit=getToolkit();
Image img=tkit.getImage("ampera.jpg");
g.drawImage(img,0,0,this);
}
}
and this is how i use it in JFrame class constructor
this.Jpanel1=new GraphicsClass();
but the image doesnt show. How to fix it?
this.Jpanel1=new GraphicsClass();
Won't work. Assuming you just drag and dropped a panel onto the frame and named it Jpanel1, then in the constructor you did the above code. When you first drag and drop the original panel, it gets initialized and laid out. When you do the above code, you are making a new component. The original panel is still the one added, and is not the same referenced panel and the new one you just created. So the new panel will never show, it is just the one (with no reference identifier). Like if you has something like
public class MyFrame extends JFrame {
public MyFrame() {
initComponents();
jPanel1 = new GraphicsClass();
}
private void initComponent() {
jPanel1 = new JPanel();
// add jPanel1 to frame
}
private class GraphicsClass extends JPanel {}
private JPanel jpanel1;
}
That's the basic netbeans setup. The code in the initComponents method is autogenerated and is not editable by default. You could edit it, but I would advise against it (unless you really know what you're doing). If you look at the code, you will see exactly what I wan talking about in the beginning.
Fix: Created a panel form from the designer (i.e. New->Swing Form->JPanel form). Then you can simply drag and drop the panel to the frame, as seen here
Other note:
Don't create your images in the paintComponent method. Make it a class member and create it in the constructor.
Instead of a panel, if you want to create an image (say for a background), you can use an ImageIcon and a JLabel and set the layout manager to the label, You can then start adding component onto the label.
Drag and drop an label
Go to the properties of the label and click the ... of the icon property. In the dialog, you can browse for an image.
Then right click the label from the design view, and set a layout manager for the label. You can then start adding components to the label.
You can fix this by using the BufferedImage class, or using the MediaTracker to track when your image has loaded
In my app, I show a popup dialog to show a large list of cards. I display them as images in many JLabel components in a JPanel subclass. I then put that object in a JScrollPane to allow for horizontal scrolling through the cards.
I want the unused space to be transparent with a dark background to show that what's behind it is disabled. I used setBackground(new Color(50, 50, 50, 200)) to achieve the look I want, but the content behind it does not redraw, so I get artifacting.
Here's what it looks like:
How would I go about fixing this? How do I get the content behind it to redraw when I scroll?
Thanks in advance.
Taking the window out of the equation for the momement.
The JScrollPane contains a JViewport which then contains you content. So you need to set your content pane to transparent, the viewport to transparent and then the scroll pane to transparent.
You can achieve this by using setOpaque(false) on each of these containers.
This will ensure that the repaint manager will now paint through the background.
The next problem is, Swing doesn't actually support "semi-transparent" components (that is, either it's opaque or transparent).
You can implement this by overriding the paintComponent method of the main component (the one on the viewport is probably sufficient)
Try the following...might give you some relief during scrolling.
You likely also have a problem when the main frame is maximized
or restored. You will need a listener for those events and a
similar fix.
jScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
#Override
public void adjustmentValueChanged(final AdjustmentEvent e) {
sevenWondersframe.repaint();
}
});
jScrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
#Override
public void adjustmentValueChanged(final AdjustmentEvent e) {
sevenWondersframe.repaint();
}
});
Easy Java question here. I have made a custom JSlider which paints squares at the last location the user has moved the slider to. We override paintcomponent and draw the squares relative to the size of the slider. Everything works good when you use the slider properly. Problems happen though when you resize the frame. The slider grows bigger, and the slider adjuster moves with it, but our squares that we placed don't move relative to the slider. Which function should we use from JSlider to repaint when we resize the frame?
I am trying to keep the JSlider code separate from the frame code, so we want the user to be able to use our custom JSlider and not be trying to handle this resizing feature themselves. Any help on which method we should use for this?
Thanks.
You can add a ComponentListener to your slider that repaints it on componentResized.
slider.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
e.getComponent().repaint();
}
}