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

    Default

    What I had done yesterday worked how i wanted it to. Now im trying to add another array and when the person guesses the right image, i want the other array of images to show up and have them be able to do the same thing with them. I am trying something but the code is going to be really sloppy and long if i do it this way once i have lets say 8 - 10 arrays :


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class yea extends JFrame implements ActionListener{
      ImageIcon[] ball = new ImageIcon[5];
      ImageIcon[] color = new ImageIcon[5];
     
      int frame = 0;
      int frame1 = 0;
      Container p = this.getContentPane();
      JButton next=new JButton("next");
      
      JButton prev=new JButton("Previous");
      
    JButton next1=new JButton("next");
      
      JButton prev1=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.jpeg");
    	  ball[1] = new ImageIcon("2.jpeg");
    	  ball[2] = new ImageIcon("3.jpeg");
    	  ball[3] = new ImageIcon("4.jpeg");
    	  
    	  color[0] = new ImageIcon("11.gif");
    	  color[0] = new ImageIcon("12.gif");
    	  color[0] = new ImageIcon("13.gif");
    	  color[0] = new ImageIcon("14.gif");
    	  
    	  
    	  
    	  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(),500, 300, null);
    	  g.drawImage(color[frame1].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);
    		pSouth.add(next);
    		pSouth.add(prev);
    		pSouth.add(next1);
    		pSouth.add(prev1);
    		p.add(pNorth, BorderLayout.NORTH);
    		p.add(pSouth, BorderLayout.SOUTH);
    		next1.setVisible(false);	
    		  prev1.setVisible(false);
      }
     
      [COLOR="Red"]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!");
    			next1.setVisible(true);	
    			  prev1.setVisible(true);
    			  next.setVisible(false);	
    			  prev.setVisible(false);
    			 
    			  if(e.getSource().equals(prev1)){
    			        frame1--;
    			        repaint();
    			     }
    			     else if(e.getSource().equals(next1)){
    			        frame1++;
    			        repaint();
      
      
      
      
     }
    			if(input.equals("second")) {
    				lblOutput.setText("No!");
    		}
    	       if(input.equals("third")) {
    				lblOutput.setText("No!");
    		}
    		   if(input.equals("fourth")){ 
    				lblOutput.setText("No!");
    			
    		}
    			
    	    
    		   
    		   
    	  } 
      } [/COLOR]
    } }



    i would have to keep doing that inside of another one everytime...Is there an easier way?

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

    Default

    Just so I know we are on the same page, if a person selects one of the ball array images then show the color array images and repeat the process?

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

    Default

    right now, it says "which image does not belong" at the top of the page. You use the next and previous buttons(at the bottom of the screen) to scroll through the pictures. Then at the bottom also is the input box. In there you answer which one you think. And depending on what they say an output box says Yes! or No! ( i guess the one thing i could change is that right now it detects there answer once they type it in and you push next or previous again..instead i could have a check answer button?)... but then when they say yes i want the new array to come up...the first array is ball(which is showing up) and then the 2nd array i want to come up is color

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

    Default

    but yes, what you said is the basic idea

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

    Default

    I would not recommend using much logic in a paint method usually but since you are learning I would recommend using an if statement in there, for e.g.

    Java Code:
    if(isGoodGuess){
      g.drawImage(ball[frame].getImage(),500, 300, null);
    }
    else{
       g.drawImage(color[frame1].getImage(),300, 300, null);
    }
    where isGoodGuess is a boolean variable that is set to true or false inside your actionPerformed method depending on a good guess or not

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

    Default

    ok, that makes sense. But do i have to use new buttons after i bring up a new array like i am doing now, or can i use the same original JButton next, JButton prev.....if you understand what i mean

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

    Default

    everything is working right except that when the user selects the right image, the new array does not show up.
    im highlighting the the new code in red, and what i think is not working in blue:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class yea extends JFrame implements ActionListener{
      ImageIcon[] ball = new ImageIcon[5];
      ImageIcon[] color = new ImageIcon[5];
     [COLOR="Red"]boolean GoodGuess;[/COLOR]
      int frame = 0;
      int frame1 = 0;
      Container p = this.getContentPane();
      JButton next=new JButton("Next");
      JButton prev=new JButton("Previous");
      JButton next1=new JButton("next");
      JButton prev1=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("Arrays");
    	  //load up the images
    	  ball[0] = new ImageIcon("1.jpeg");
    	  ball[1] = new ImageIcon("2.jpeg");
    	  ball[2] = new ImageIcon("3.jpeg");
    	  ball[3] = new ImageIcon("4.jpeg");
    	  
    	  color[0] = new ImageIcon("11.jpeg");
    	  color[1] = new ImageIcon("12.jpeg");
    	  color[2] = new ImageIcon("13.jpeg");
    	  color[3] = new ImageIcon("14.jpeg");
    	  
    	  
    	  
    	  next.addActionListener(this);
    	  prev.addActionListener(this);
    	  next1.addActionListener(this);
    	  prev1.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);
    	  
    	  [COLOR="red"]if(GoodGuess = false){
    		  
    		  g.drawImage(color[frame1].getImage(),500, 300, null);
    	  }
    	  if(GoodGuess = true){
    			g.drawImage(ball[frame].getImage(),500, 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);
    		pSouth.add(next1);
    		pSouth.add(prev1);
    		p.add(pNorth, BorderLayout.NORTH);
    		p.add(pSouth, BorderLayout.SOUTH);
    		next1.setVisible(false);	
    		 prev1.setVisible(false);
      }
     
      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!");
    			  [COLOR="red"]GoodGuess = false;[/COLOR]
    			  next1.setVisible(true);	
    			  prev1.setVisible(true);
    			  next.setVisible(false);	
    			  prev.setVisible(false);
    				
    			    [COLOR="Blue"]if (GoodGuess = false){
    			    	if(e.getSource().equals(prev1)){
    				        frame1--;
    				        repaint();
    				     }
    				     else if(e.getSource().equals(next1)){
    				        frame1++;
    				        repaint();
    			    }[/COLOR]
    				   
    				   
    			  } 
    			  
      
            }
    			[COLOR="Red"]if(input.equals("second")) {
    				lblOutput.setText("No!");
    				GoodGuess = true;
    		}
    	       if(input.equals("third")) {
    				lblOutput.setText("No!");
    				GoodGuess = true;
    		}
    		   if(input.equals("fourth")){ 
    				lblOutput.setText("No!");
    				GoodGuess = true;
    			
    		}[/COLOR]
    		
      } 
    } }

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

    Default

    i changed the code around and i know where the problem is but idk how to change it so that it works. When the player gets the answer right, the 1st image of the second array shows up, but the second array is not controllable by the JButtons next1 and prev1:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class yea extends JFrame implements ActionListener{
      ImageIcon[] fruit = new ImageIcon[5];
      ImageIcon[] mario = new ImageIcon[5];
     boolean GoodGuess;
      int frame = 1;
      [COLOR="Red"]int frame1 = 1;[/COLOR]
      Container p = this.getContentPane();
      JButton next=new JButton("Next");
      JButton prev=new JButton("Previous");
      J[COLOR="red"]Button next1=new JButton("next");
      JButton prev1=new JButton("previous");[/COLOR]
      
      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("Arrays");
    	  //load up the images
    	  fruit[1] = new ImageIcon("1.jpeg");
    	  fruit[2] = new ImageIcon("2.jpeg");
    	  fruit[3] = new ImageIcon("3.jpeg");
    	  fruit[4] = new ImageIcon("4.jpeg");
    	  
    	  mario[1] = new ImageIcon("11.jpeg");
    	  mario[2] = new ImageIcon("12.jpeg");
    	  mario[3] = new ImageIcon("13.jpeg");
    	  mario[4] = new ImageIcon("14.jpeg");
    	  
    	  
    	  
    	  next.addActionListener(this);
    	  prev.addActionListener(this);
    	  [COLOR="red"]next1.addActionListener(this);
    	  prev1.addActionListener(this);[/COLOR]
    	 
    	  this.setVisible(true);
    	  this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    	  p.setLayout(new BorderLayout());
    		
    	  this.setup();
    	  this.setVisible(true);
          this.pack();
          this.setSize(1270,1000);
    	  
      } // end constructor
      
      public void paint(Graphics g) {
    	  super.paint(g);
    	  
    	  [COLOR="red"]if(GoodGuess){
    		  g.drawImage(mario[frame1].getImage(),500, 300, null);
    		}
    		else{
    		   g.drawImage(fruit[frame].getImage(),300, 300, null);
    		}[/COLOR]
    	  
    	  
      } // end paint
      public void setup(){
    	  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);
    		pSouth.add(next1);
    		pSouth.add(prev1);
    		p.add(pNorth, BorderLayout.NORTH);
    		p.add(pSouth, BorderLayout.SOUTH);
    		next1.setVisible(false);	
    		 prev1.setVisible(false);
      }
     
      public void actionPerformed(ActionEvent e){
    	  GoodGuess = false;
    	  if(e.getSource() == prev){
    	        frame--;
    	        repaint();
    	     }
    	     else if(e.getSource() == next){
    	        frame++;
    	        repaint();
    	    
      String input = txtInput.getText();
      
      [COLOR="red"]if (input.equals("first")){
    			lblOutput.setText("yes!");
    			  GoodGuess = true;
    			   next1.setVisible(true);	
    			   prev1.setVisible(true);
    			   next.setVisible(false);	
    			   prev.setVisible(false);
    				
    			    if (GoodGuess = true){
    			    	
    			    	if(e.getSource()== prev1){
    				        frame1--;
    				        repaint();
    				     }
    				     else if(e.getSource() == next1){
    				        frame1++;
    				        repaint();
    			    }[/COLOR]
    				   
    				   
    			  } 
    			  
      
            }
    			if(input.equals("second")) {
    				lblOutput.setText("No!");
    				GoodGuess = false;
    		}
    	       if(input.equals("third")) {
    				lblOutput.setText("No!");
    				GoodGuess = false;
    		}
    		   if(input.equals("fourth")){ 
    				lblOutput.setText("No!");
    				GoodGuess = false;
    			
    		}
    	   } 
          }
         }

  9. #29
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I would try to simplify this by putting the images in ImageIcons and then displaying them in a single JLabel by calling setIcon(...) on the JLabel. I would hold the ImageIcons in an array or an ArrayList, and when processing my button presses, would check that the array or array list index is not below 0 or greater than or equal to the array length or list size. I would then give the JLabel a MouseListener to allow the user to click it if they feel the image doesn't belong. I would know which image was clicked because I would know the index of the item from array or ArrayList.

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

    Default

    so you are saying make an array that holds all my images in groups..i.e. the first set in my array would be the 1st 4 images, and the 2nd set of my array would hold the next 4 photos i want to come up?

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

    Default

    I have been trying to do what Furbarable said but i am new to this, and have not gotten it to work. I am lost as to what to do!

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

    Default

    there is no need for two additonal JButtons with a difference in a name by the letter '1' as far as I can see. Those original JButtons should suffice
    Last edited by al_Marshy_1981; 12-12-2010 at 04:02 AM. Reason: fort? lay off the civilization lol

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

    Default

    well then how would i go about fixing the problem i have. The 2nd array of images does not work...only the 1st image comes up. Ive been trying to get this all day and havent had any luck.

Page 2 of 2 FirstFirst 12

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
  •