0

Hello again ladies and gentlemen, I have once again not been able to figure out something that to you will probably be simple. I am trying to load 3 images space.jpeg, treefrog.jpeg and yosemite.jpeg (As you click a radio button that the corresponding image is displayed) and have tried adapting every example I can find. If any one wants to point me in the right direction I would greatly appreciate it.

package guiprogramming;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;

public class ChooseImage extends JPanel {
   private JPanel panel1;
   private SimplePanel drawingPanel;
   JRadioButton button1, button2, button3;

public ChooseImage() {
   setLayout(new BorderLayout());
   panel1 = new JPanel();
   drawingPanel = new SimplePanel();
   button1 = new JRadioButton("Scenery");
   panel1.add(button1);
   button2 = new JRadioButton("Space");
   panel1.add(button2);
   button3 = new JRadioButton("Tree Frog");
   panel1.add(button3);

   ButtonGroup group = new ButtonGroup();
    group.add(button1);
    group.add(button2);
    group.add(button2);

  this.add(panel1, BorderLayout.NORTH);
  this.add(drawingPanel, BorderLayout.CENTER);


  button1.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent e) {
     //This is where I am trying to get the images to load
     }});

  button2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
     //This is where I am trying to get the images to load
     }});

  button3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
    //This is where I am trying to get the images to load
    }});








  }

  public static void main(String[] args) {
  JFrame window = new JFrame("Choose Image");
  ChooseImage panel = new ChooseImage();
  window.setContentPane(panel);
  window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  window.setSize(500, 500);
  window.setVisible(true);
  }
  }
  • What happens instead when your code runs? Are any images displayed? Also, can you please improve your code formatting so that we can more easily read and understand it? Your not showing your attempts to load an image -- why? – Hovercraft Full Of Eels Apr 29 '13 at 23:44
  • 1
    `//This is where I am trying to get the images to load` Fine. What have you tried (I mean besides making claims of research then asking us)? – Andrew Thompson Apr 29 '13 at 23:45
  • How would I improve my formatting I am currently a student there have been no such lessons yet, so advice appreciated. – Ozzyfan2323 Apr 29 '13 at 23:48
  • Please read up on proper Java code formatting. Links can be found on Google. A key is to use consistent and regular indenting so that all code on a single block is indented equally, usually 3 spaces. You've got some code without any indentations whatsoever which is very hard to read. – Hovercraft Full Of Eels Apr 29 '13 at 23:50
  • Image image=new ImageIcon("space.jpeg").getImage(); g.drawImage(image) this was my first attempt from information I have found on here (StackOverflow) – Ozzyfan2323 Apr 29 '13 at 23:50
  • As per my answer, put this program on hold and work on a much simpler program first, one that displays a single image as an ImageIcon inside of a JLabel. Also look into using the `ImageIO.read(...)` method to get your image. Please look at the API for the ImageIO class for more on this. – Hovercraft Full Of Eels Apr 29 '13 at 23:55

1 Answers1

2
  1. Don't load your images each time an ActionListener is called.
  2. Load your images once on program start up.
  3. Use ImageIO.read(...) to read in the image. You can find the ImageIO API here.
  4. Put the images into ImageIcons.
  5. In the ActionListeners, swap a JLabel's Icon via its setIcon(...) method.
  6. When implementing complex functionality, do so step-wise and in isolation. In other words, first create a small program that displays an image, and only when that's successful work on creating one that swaps on button press.
  7. In the future, please show us your attempts to implement functionality. Doing this gives us a much better understanding of what your misconceptions or problems are.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373