Display image in GUI using JPanel and button - java

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");
}

Related

JLabel not Moving or Appearing

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) {}
}
}

How do you display an image on a panel in Java? [Beginner]

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();
}

Java swing: scale image read from url

I have a JFrame that shoud show an image (retrieved from web) and two lateral buttons for show next and previous image. Original image size is too big, and i would scale it before show. this is my code:
public class ShowPicture extends JFrame implements ActionListener {
private String link;
private JButton next;
private JButton prev;
private Image image;
public ShowPicture(String link) {
this.setSize(300, 200);
setLocationRelativeTo(null); //center
setVisible(true);
this.link = link;
this.setLayout(new FlowLayout());
prev = new JButton("prev");
next = new JButton("next");
URL url;
try {
url = new URL(link + "/front.jpg");
image = ImageIO.read(url);
} catch (MalformedURLException ex) {
Logger.getLogger(ShowPicture.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ShowPicture.class.getName()).log(Level.SEVERE, null, ex);
}
JLabel image_label = new JLabel(new ImageIcon(image));
image_label.setSize(100, 100);
this.add(prev);
this.add(image_label);
this.add(next);
pack();
}
but image is not scaled
how can i do?
I would scaled the image itself not the JLabel. Use :
image = ImageIO.read(url);
image = image.getScaledInstance(100,100,Image.SCALE_DEFAULT);
JLabel image_label = new JLabel(new ImageIcon(image));

Why don't I see the image or the button?

I am trying to add a button over the image. But neither see the button nor the image. Why is that ? I am calling this method from the constructor just after the initComponents method is called by the IDE.
public void initD() {
try {
BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\user\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\meaning.JPG"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
JButton b = new JButton("Press me");
jPanel1.add(picLabel);
jPanel1.add(b);
System.out.println("last statement");
}catch(Exception exc) {
exc.printStackTrace();
}
}
I only see the frame as an output.
I dont know which layout you are using, however you should implement button.setIcon(); like this;
public void initD() {
JButton button = new JButton("Press me");
try {
BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\user\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\meaning.JPG"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
button.setIcon(new ImageIcon(myPicture));
System.out.println("last statement");
}catch(Exception exc) {
exc.printStackTrace();
}
}
In addition you may need to consider resource of your image maybe this implemantation can be helpfull ;
ImageIO.read(getClass().getResource("resources/meaning.JPG")));

JLabel refresh icon with updated image

I'm trying to make an experiment in image manipulation.
Basically I have an image that is continously updated by a timer and i display that image in a JLabel.
My problem is that JLabel does'nt refresh the image.
Here is my timer code:
Timer timer = new Timer(200, new ActionListener() {
public void actionPerformed(ActionEvent e) {
count++;
System.out.println("timer");
System.out.println(filename);
ImageIcon icon = new ImageIcon(filename);
label = new JLabel();
label.setIcon(icon);
label.setText(""+count);
panel = new JPanel();
panel.add(label);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.repaint();
frame.validate();
try{
FileWriter fstream;
fstream = new FileWriter(filename,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("text to append");
out.close();
}catch (Exception ex){
System.err.println("Error: " + ex.getMessage());
}
}
});
Where filename is path to my image.
Image is displayed but JLabel never refresh my image.
I tested my code and is working if I swich between two different images...
EDIT:
I solved by duplicate every time last image created and renaming with a timestamp.
label = new JLabel();
label.setIcon(icon);
label.setText(""+count);
panel = new JPanel();
panel.add(label);
frame.getContentPane().removeAll();
frame.getContentPane().add(panel);
frame.repaint();
frame.validate();
Replace all that with something like:
label.setIcon(icon);
If the label is not visible at that point, declare it as a class attribute of the outer class or at the same level as the frame (which is obviously accessible in that snippet).

Categories

Resources