I am trying to make a fade out transition my moving a JLabel with a transparent gradient so it starts fading out from the bottom up. This will be activated by a button that when pressed, will begin the transition by creating a label with the transparent gradient image and changing its position using a for loop. However, whenever I click the button, it appears to do nothing and it does not load the image nor the animation.
This is the image I am trying to load https://imgur.com/a/WhSCVna
setSize(800, 600);
setLayout(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Start Menu Background
ImageIcon start_menu_img = new ImageIcon("src/img/menu_background.png");
JLabel start_menu = new JLabel("", start_menu_img, JLabel.CENTER);
start_menu.setBounds(0,0,800,600);
// Start Button
start = new JButton(new ImageIcon("src/img/start.png"));
start.setBounds(240,327,320,100);
start.setBorder(BorderFactory.createEmptyBorder());
// Sets main menu label and adds it to the frame
start.addActionListener(this);
JLabel main_menu = new JLabel();
main_menu.setBounds(getBounds());
main_menu.add(start);
main_menu.add(start_menu);
add(main_menu);
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == start) {
try {
AudioInputStream select = getAudioFile("select.wav");
Clip clip = AudioSystem.getClip();
clip.open(select);
clip.start();
Thread.sleep(500);
ImageIcon transition_ImageIcon = new ImageIcon("src/img/transition.png");
JLabel transition = new JLabel();
transition.setIcon(transition_ImageIcon);
transition.setBounds(0,600,800,1800);
add(transition);
for(int i = 600; i > -1200; i--) {
transition.setBounds(0, i, 800, 1800);
Thread.sleep(1);
}
} catch (Exception e1) {}
}
}
Related
I am trying to create a splash screen for a risk game that I am developing. For some reason the splash screen doesnt appear even though I am adding it to the Jwindow, I would appreciate an expert eye that will see what I can't. Thanks in advance
/new jWindow as it is better for splash screen as there is no borders
JWindow window = new JWindow();
ImageIcon imageIcon = new ImageIcon("Images/riskimage.jpg");
JLabel label = new JLabel(imageIcon);
window.getContentPane().add(label);
window.setBounds(200, 200, 200, 100);
window.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
window.setVisible(false);
// title of frame
JFrame frame = new JFrame("Risk");
//Creating a text field
JTextField textField = new JTextField(20);
frame.add(textField, BorderLayout.SOUTH);
JLabel welcome = new JLabel("");
welcome.setFont (welcome.getFont ().deriveFont (20.0f));
welcome.setText("Please Enter name for Player 1 in the text box at the bottom");
frame.add(welcome,BorderLayout.NORTH);
//action listener listens for enter key
textField.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
//checks if player Ones name is set and sets player Twos name aswell
if (!playerOneNameSet)
{
playerOneName = textField.getText();
textField.setText("");
welcome.setText("Please Enter name for Player 2 in the text box at the bottom");
playerOneNameSet = true;
}
else
{
playerTwoName = textField.getText();
textField.setText("");
//turning the nameSetUpDone to true as we are finished setting up the names
nameSetUpDone = true;
// repainting as we want to update the screen
frame.getContentPane().repaint();
welcome.setText("Player One:" + playerOneName +" Player Two:" + playerTwoName + " Awaiting player one move");
}
}
});
I'm making a project that when you hit the button, an image appears in a JPanel.
But when I hit that button, nothing happens.
How to fix this code?
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// button sluiten van de deur
try {
writer.println("execute(lock, \"aepu04:SI-Test\");");
writer.flush(); // flushes the buffer
String path = "http://chart.finance.yahoo.com/z?s=GOOG&t=6m&q=l";
System.out.println("Get Image from " + path);
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
System.out.println("Load image into frame...");
JLabel label = new JLabel(new ImageIcon(image));
JPanel panel = jPanel1;
panel.add(label);
panel.setVisible(true);
} catch (Exception ex) {
chatTextArea.append("Message was not sent. \n");
}
I have a GridBagConstraints gbcImage and a JLabel that is initialized like this:
gbcImage.gridx = 1; // column 0
gbcImage.gridy = 2; // row 2
gbcImage.ipady = 100;
gbcImage.ipadx = 100;
JLabel label = new JLabel("", null, JLabel.CENTER);
label.setOpaque(true);
label.setBackground(Color.WHITE);
panel.add(label, gbcImage);
Where panel is added to a JFrame.
So I implemented a MouseListener to the label:
public void mouseClicked(MouseEvent e) {
JFileChooser jfc = new JFileChooser();
int iRet = jfc.showOpenDialog(panel);
if (iRet == jfc.APPROVE_OPTION)
{
File file = jfc.getSelectedFile();
try
{
BufferedImage bi = ImageIO.read(file);
image = new ImageIcon(bi);
JLabel label = new JLabel("", image, JLabel.CENTER);
panel.add(label, gbcImage);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
But it didn't work. The image doesn't show in the panel at runtime.
What am I missing?
There is no need to create a new JLabel. The problem is you added a new label to the panel but its default size is (0, 0) because you didn't reavalidate() and repaint() the panel.
There is no need to create a new label.
Instead you keep a reference to the original label (like you do for the panel) and then you just replace the icon:
image = new ImageIcon(bi);
label.setIcon( image );
I need to put a menu in a game, so I've created a frame called menuFrame and a panel called menuPanel. I've been able to get a button and a label with text to appear on this panel, but I can't get an image to display.
Here is the bulk of my code:
try {
JPanel menuPanel = new JPanel();
BufferedImage img = ImageIO.read(this.getClass().getResource("background2.png"));
JLabel menuLabel = new JLabel(new ImageIcon(img));
menuLabel.setSize(800, 540);
menuLabel.setLocation(0, 0);
menuLabel.setVisible(true);
menuPanel.add(menuLabel);
this.add(menuPanel);
menuPanel.grabFocus();
menuPanel.requestFocusInWindow();
} catch (IOException e) {
e.printStackTrace();
}
I've been messing with it for a very long time, and the image simply wont appear. I've tried using just ImageIcon and no BufferedImage and that didn't work. I put the image in the same package as the class.
You could do something like this:
ImageIcon imgIcon = new ImageIcon("background2.png");
JLabel menuLabel = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(imgIcon);
try {
JPanel menuPanel = new JPanel();
BufferedImage img = ImageIO.read(this.getClass().getResource("background2.png"));
JLabel menuLabel = new JLabel(new ImageIcon(img));
menuLabel.setSize(800, 540);
menuLabel.setLocation(0, 0);
menuLabel.setVisible(true);
menuPanel.add(menuLabel);
this.add(menuPanel); //This might not work, try the name of the JFrame instance
menuPanel.grabFocus();
menuPanel.requestFocusInWindow();
this.revalidate() //Use the name of the JFrame if this is not a JFrame or if this.add(menuPanel); doesnt work
} catch (IOException e) {
e.printStackTrace();
}
I have a JTappedPane with a button on that I want to make close that tab.
I am doing it like so:
jTabbedPane1.addTab(title, null, panel, null);
JPanel pnl = new JPanel();
JButton close = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("x.png"));
close.setIcon(new ImageIcon(img));
} catch (IOException ex) {
ex.printStackTrace();
}
close.setPreferredSize(new Dimension(10, 10));
close.setBorderPainted(false);
close.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent evt) {
//TODO CLOSE THE TAP WHEN BUTTON IS PRESSED
}
}});
JLabel lab = new JLabel(s);
pnl.setOpaque(false);
pnl.add(lab);
pnl.add(close);
jTabbedPane1.setTabComponentAt(jTabbedPane1.getTabCount() - 1, pnl);
I am trying to get the title of the tab on the tab that the button has been pressed on.
I thought i could do something like
close.getContaining() to return the tab it is on but I was wrong.
Any Ideas?
If I understand you correctly, you want to find the index of the tab which has the parent of the button as tabComponent:
public void actionPerformed(ActionEvent evt) {
JComponent source = (JComponent) evt.getSource();
Container tabComponent = source.getParent();
int tabIndex = jTabbedPane1.indexOfTabComponent(tabComponent);
jTabbedPane1.removeTabAt(tabIndex);
}
You could simply write:
jTabbedPane1.removeTabAt(jTabbedPane1.getSelectedIndex());