Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default Why is my array of images not showing up?

    My program is supposed to come up and show 4 images, and then the user inputs which image they think doesn't belong. Depending on what they answer it simply replies "Right" or "Wrong"
    The only problem that i am having is that the pictures are not coming up. Here is my code:


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class yea extends JFrame implements ActionListener{
      ImageIcon[] ball = new ImageIcon[5];
      Timer clock;
      int frame;
      Container p = this.getContentPane();
          
      
        JLabel lblOutput = new JLabel("");
    	JTextField txtInput = new JTextField("Answer here: first, second, third, or fourth");
    	
     
    	public static void main(String args[]){
    	  new yea();
      } // end main
      
      public yea(){
    	  super("Animate Images");
    	  //load up the images
    	  ball[1] = new ImageIcon("1.gif");
    	  ball[2] = new ImageIcon("2.gif");
    	  ball[3] = new ImageIcon("3.gif");
    	  ball[4] = new ImageIcon("4.gif");
    	  
    	  clock = new Timer(1000, this);
    	  
    	  
    	  this.setVisible(true);
    	  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    	  p.setLayout(new BorderLayout());
    		
    	  this.init();
    	  this.setVisible(true);
          this.pack();
          this.setSize(1270,1000);
    	  clock.start();
      } // end constructor
      
      public void paint(Graphics g) {
    	  super.paint(g);
    	  g.drawImage(ball[frame].getImage(),300, 300, null);
    	 
      } // end paint
      public void init(){
    	  Panel pNorth = new Panel();
    		pNorth.setLayout(new FlowLayout());
    		
    		pNorth.add(new JLabel("Which Picture doesn't belong? first,second,third, or fourth?"));
    
    		Panel pSouth = new Panel();
    		pSouth.setLayout(new FlowLayout());
    		pSouth.add(lblOutput);
    		pSouth.add(txtInput);
    		
    		
    		p.add(pNorth, BorderLayout.NORTH);
    		p.add(pSouth, BorderLayout.SOUTH);
    	  
      }
     
      public void actionPerformed(ActionEvent e){
    	  
    	  frame++;
    	  if (frame > 4){
    	    frame += 1;
    	    
      String input = txtInput.getText();
    		if (input.equals("first")){
    			lblOutput.setText("yes!");
    		}
    			if(input.equals("second")) {
    				lblOutput.setText("No!");
    		}
    	       if(input.equals("third")) {
    				lblOutput.setText("No!");
    		}
    		   if(input.equals("fourth")){ 
    				lblOutput.setText("No!");
    			
    		}
    			
    	    
    	  } 
      } 
    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Maybe it is the hour but I cannot see what you have implementing ActionListener

  3. #3
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    is this what you mean?

    Java Code:
    [COLOR="Red"]public class yea extends JFrame implements ActionListener{[/COLOR]
      ImageIcon[] ball = new ImageIcon[5];
      Timer clock;
      int frame;
      Container p = this.getContentPane();
          
      
        JLabel lblOutput = new JLabel("");
    	JTextField txtInput = new JTextField("Answer here: first, second, third, or fourth");
    	
     
    	public static void main(String args[]){
    	  new yea();
      } // end main
      
      public yea(){
    	  super("Animate Images");
    	  //load up the images
    	  ball[1] = new ImageIcon("1.gif");
    	  ball[2] = new ImageIcon("2.gif");
    	  ball[3] = new ImageIcon("3.gif");
    	  ball[4] = new ImageIcon("4.gif");
    	  
    	  clock = new Timer(1000, this);
    	  
    	  
    	  this.setVisible(true);
    	  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    	  p.setLayout(new BorderLayout());
    		
    	  this.init();
    	  this.setVisible(true);
          this.pack();
          this.setSize(1270,1000);
    	  clock.start();
      } // end constructor
      
      public void paint(Graphics g) {
    	  super.paint(g);
    	  g.drawImage(ball[frame].getImage(),300, 300, null);
    	 
      } // end paint
      public void init(){
    	  Panel pNorth = new Panel();
    		pNorth.setLayout(new FlowLayout());
    		
    		pNorth.add(new JLabel("Which Picture doesn't belong? first,second,third, or fourth?"));
    
    		Panel pSouth = new Panel();
    		pSouth.setLayout(new FlowLayout());
    		pSouth.add(lblOutput);
    		pSouth.add(txtInput);
    		
    		
    		p.add(pNorth, BorderLayout.NORTH);
    		p.add(pSouth, BorderLayout.SOUTH);
    	  
      }
     
      public void actionPerformed(ActionEvent e){
    	  
    	  frame++;
    	  if (frame > 4){
    	    frame += 1;
    	    
      String input = txtInput.getText();
    		if (input.equals("first")){
    			lblOutput.setText("yes!");
    		}
    			if(input.equals("second")) {
    				lblOutput.setText("No!");
    		}
    	       if(input.equals("third")) {
    				lblOutput.setText("No!");
    		}
    		   if(input.equals("fourth")){ 
    				lblOutput.setText("No!");
    			
    		}
    			
    	    
    	  } 
      } 
    }

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Yes but what in your program is going to make that actionPerformed method execute?

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    also it does look like the integer frame is not initialised to anything but then you are using it to access an array element which *might* most likely will cause a null pointer exception
    Last edited by al_Marshy_1981; 12-10-2010 at 04:47 AM. Reason: ....

  6. #6
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    I am not sure! :confused:

    i would assume it goes in here somwhere

    Java Code:
    public void actionPerformed(ActionEvent e){
    	  
    	  frame++;
    	  if (frame > 4){
    	    frame += 1;
    	    
      String input = txtInput.getText();
    		if (input.equals("first")){
    			lblOutput.setText("yes!");
    		}
    			if(input.equals("second")) {
    				lblOutput.setText("No!");
    		}
    	       if(input.equals("third")) {
    				lblOutput.setText("No!");
    		}
    		   if(input.equals("fourth")){ 
    				lblOutput.setText("No!");
    			
    		}

  7. #7
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    could i put a new panel in the CENTER, and have the array of images show up on there, or is that even necessary?

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    You could add an actionlistener to JTextField but you actionPerformed method would need more logic, here is an article, still not sure about that frame integer not being initialised to a value where it is declared

    How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    To get an image up originally I would try setting your integer frame equal to 1 where it is declared

  10. #10
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    i changed it to
    int frame = 1;
    and yes, now my 1st image shows up! but its isnt going through them. I suppose that link is has a solution?

  11. #11
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    *I suppose that link has the solution?

  12. #12
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Yup you are implementing ActionListener but no components e.g. JButton, JTextField are using it.... look at the link...I would use a JButton for simplicity

  13. #13
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    here is the reason you were gettin no image originally

    Java Code:
    int frame; // not initialised
    an array of imageicons, array access begins at 0 not 1 (for future reference)
    Java Code:
     ball[1] = new ImageIcon("1.gif");
     ball[2] = new ImageIcon("2.gif");
     ball[3] = new ImageIcon("3.gif");
     ball[4] = new ImageIcon("4.gif");
    accessing the element of ball at index frame, but frame is not initialised (e.g. is not 0,1,2,3 or any number etc )
    Java Code:
     g.drawImage(ball[frame].getImage(),300, 300, null);

  14. #14
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    im not putting the link and JButton together. Do i need to add a JButton so that when i click it, it causes the images to cycle?

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Yeah I think that would be the simplest way, say two JButtons one for next one for previous image

    Java Code:
    JButton next=new JButton("next");
    next.addActionListener(this);
    JButton prev=new JButton("Previous");
    prev.addActionListener(this);
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource().equals(next)){
            frame++;
            repaint();
         }
         else if(e.getSource().equals(prev)){
            frame--;
            repaint();
         }
    }
    I think that should be pretty close to what you want...do look up the code though, dont follow blind.

  16. #16
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    oh okay, i got it. Thank you, I appreciate the help!

    :)

  17. #17
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    No problem, come back if you have any more problems

  18. #18
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    im gonna be working on it tomorrow and adding some more stuff, so i def. will if i do! thanks!!!

  19. #19
    CNew is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    I have highlighted my new code in red. I basically just added what was needed for a new array. What i want do do, is if the person guesses the right image, then the next array of images show up. Can i do this with this current setup?




    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class yea extends JFrame implements ActionListener{
      ImageIcon[] ball = new ImageIcon[5];
      [COLOR="Red"]ImageIcon[] color = new ImageIcon[5];[/COLOR]
     
      int frame = 0;
      [COLOR="red"]int frame2 = 0;[/COLOR]
      Container p = this.getContentPane();
      JButton next=new JButton("next");
      
      JButton prev=new JButton("Previous");
      
      
        JLabel lblOutput = new JLabel("");
    	JTextField txtInput = new JTextField("Answer here: first, second, third, or fourth");
    	
     
    	public static void main(String args[]){
    	  new yea();
      } // end main
      
      public yea(){
    	  super("Animate Images");
    	  //load up the images
    	  ball[0] = new ImageIcon("1.gif");
    	  ball[1] = new ImageIcon("2.gif");
    	  ball[2] = new ImageIcon("3.gif");
    	  ball[3] = new ImageIcon("4.gif");
    	  
    	  [COLOR="red"]color[0] = new ImageIcon("11.gif");
    	  color[1] = new ImageIcon("12.gif");
    	  color[2] = new ImageIcon("13.gif");
    	  color[3] = new ImageIcon("14.gif");[/COLOR]
    	  
    	  
    	  
    	  next.addActionListener(this);
    	  prev.addActionListener(this);
    	  this.setVisible(true);
    	  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    	  p.setLayout(new BorderLayout());
    		
    	  this.init();
    	  this.setVisible(true);
          this.pack();
          this.setSize(1270,1000);
    	  
      } // end constructor
      
      public void paint(Graphics g) {
    	  super.paint(g);
    	  g.drawImage(ball[frame].getImage(),300, 300, null);
    	  [COLOR="red"]g.drawImage(color[frame2].getImage(),300, 300, null);[/COLOR]
    	 
      } // end paint
      public void init(){
    	  Panel pNorth = new Panel();
    		pNorth.setLayout(new FlowLayout());
    		
    		pNorth.add(new JLabel("Which Picture doesn't belong? first,second,third, or fourth?"));
    
    		Panel pSouth = new Panel();
    		pSouth.setLayout(new FlowLayout());
    		pSouth.add(lblOutput);
    		pSouth.add(txtInput);
    		pSouth.add(next);
    		pSouth.add(prev);
    		p.add(pNorth, BorderLayout.NORTH);
    		p.add(pSouth, BorderLayout.SOUTH);
    	  
      }
     
      public void actionPerformed(ActionEvent e){
    	  
    	  if(e.getSource().equals(prev)){
    	        frame--;
    	        repaint();
    	     }
    	     else if(e.getSource().equals(next)){
    	        frame++;
    	        repaint();
    	    
      String input = txtInput.getText();
    		if (input.equals("first")){
    			lblOutput.setText("yes!");
    		}
    			if(input.equals("second")) {
    				lblOutput.setText("No!");
    		}
    	       if(input.equals("third")) {
    				lblOutput.setText("No!");
    		}
    		   if(input.equals("fourth")){ 
    				lblOutput.setText("No!");
    			
    		}
    			
    	    
    	  } 
      } 
    }
    Last edited by Fubarable; 12-11-2010 at 05:08 AM. Reason: Moderator Edit: Code Tags Fixed

  20. #20
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    have you run this code? does it do what you want?
    Last edited by al_Marshy_1981; 12-11-2010 at 04:13 AM. Reason: past tense marshy

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 04-30-2010, 04:06 AM
  2. Difficulties randomizing images in an array
    By phb5004 in forum New To Java
    Replies: 2
    Last Post: 12-11-2009, 05:45 AM
  3. Showing
    By bostonstate in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 08:49 PM
  4. How do I create an Array of Images
    By wco5002 in forum New To Java
    Replies: 3
    Last Post: 03-21-2008, 04:45 PM
  5. Showing Images with J2ME
    By luisarca in forum CLDC and MIDP
    Replies: 0
    Last Post: 06-18-2007, 11:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •