Results 1 to 15 of 15
  1. #1
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default displaying image into screen !

    hi i have basically made the frame of code - got the image of my card
    i have my button

    now my problem is that i want to display the array of images that i made to go into the screen - the images are 13
    but i don't no how to do this :(
    i have tried everything - it just comes out blank with a frame heres my code.


    import javax.swing.*;



    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Random;
    import javax.swing.*;
    import java.awt.event.ActionListener;

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    import java.awt.BorderLayout;
    import java.util.Scanner;


    class practice34

    {


    public static void main (String [] args)
    {


    ImageIcon myIcon = new ImageIcon("cards/png");
    JLabel centerLabel = new JLabel(myIcon);
    JFrame frame= new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add(centerLabel, BorderLayout.SOUTH);
    JButton shuffle=new JButton("SHUFFLE U LOSER!");
    frame.add(shuffle,BorderLayout.NORTH);
    frame.setSize(600,600);
    frame.setVisible(true);
    }
    public practice34()
    {
    ImageIcon[] icons = new ImageIcon[13];
    //icons [0]=new ImageIcon("cards.png");
    icons[1]=new ImageIcon("cards/1.png");
    icons[2]=new ImageIcon("cards/2.png");
    icons[3]=new ImageIcon("cards/3.png");
    icons[4]=new ImageIcon("cards/4.png");
    icons[5]=new ImageIcon("cards/5.png");
    icons[6]=new ImageIcon("cards/6.png");
    icons[7]=new ImageIcon("cards/7.png");
    icons[8]=new ImageIcon("cards/8.png");
    icons[9]=new ImageIcon("cards/9.png");
    icons[10]=new ImageIcon("cards/10.png");
    icons[11]=new ImageIcon("cards/11.png");
    icons[12]=new ImageIcon("cards/12.png");
    icons[13]=new ImageIcon("cards/13.png");
    int n = 1; // Which card.
    int xPos = 1; // Where it should be placed initially.
    int yPos = 1;



    }


    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,227
    Rep Power
    6

    Default

    Read the section from the Swing tutorial on on How to Use Icons. Or maybe check out the section on "How to Use Lists" for a different approach.

  3. #3
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    not much help there - i checked !

  4. #4
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    i did another code
    here it is
    :
    import javax.swing.*;



    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Random;
    import javax.swing.*;
    import java.awt.event.ActionListener;

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    import java.awt.BorderLayout;
    import java.util.Scanner;


    class practice34

    {


    public static void main (String [] args)
    {


    ImageIcon myIcon = new ImageIcon("cards/png");
    JLabel centerLabel = new JLabel(myIcon);
    JFrame frame= new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add(centerLabel, BorderLayout.SOUTH);
    JButton shuffle=new JButton("SHUFFLE U LOSER!");
    frame.add(shuffle,BorderLayout.NORTH);
    frame.setSize(600,600);
    frame.setVisible(true);

    }






    public practice34()
    {
    ImageIcon[] icons = new ImageIcon[52];



    ImageIcon[] pictures=
    {
    pictures [0]= new ImageIcon("cards.png");
    pictures [1]= new ImageIcon("cards/1.png");
    pictures [2]=new ImageIcon("cards/2.png");
    pictures[3]=new ImageIcon("cards/3.png");
    pictures [4]=new ImageIcon("cards/4.png");
    pictures[5]=new ImageIcon("cards/5.png");
    pictures[6]=new ImageIcon("cards/6.png");
    pictures[7]=new ImageIcon("cards/7.png");
    pictures[8]=new ImageIcon("cards/8.png");
    pictures[9]=new ImageIcon("cards/9.png");
    pictures[10]=new ImageIcon("cards/10.png");
    pictures[11]=new ImageIcon("cards/11.png");
    pictures[12]=new ImageIcon("cards/12.png");
    pictures[13]=new ImageIcon("cards/13.png");}

    int [] packk = new int[52];

    for (int i=0;i<icons.length;i++) packk[i]=i;
    Random r= new Random();
    int n=52;
    for (int i=0;i<52;i++)
    {int k= r.nextInt(n);
    icons[ i ] = pictures[packk[k]%13];
    packk [k]=packk[n-1];
    n--;
    JLabel centerLabel = new JLabel(icons[ i ] );








    }

    }
    }

    nothing comes out !

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by burningflower12 View Post
    not much help there - i checked !
    That's where I learned how to use ImageIcons and Swing, and I'll venture that it's where camickr, and most all the other Swing mavens learned it. This tutorial has sample code that actually works. I would strongly suggest you go back to the tutorial and check again. The more you study the tutorials, the better your Swing coding will be.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by burningflower12 View Post
    i did another code
    here it is

    .....

    nothing comes out !
    Your code doesn't compile for me. Is this the correct code? Also, please use code tags when posting code. See my signature for details.

  7. #7
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.Random;
    import javax.swing.*;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    import java.awt.BorderLayout;
    import java.util.Scanner;
    
    
    	class practice34
    	
    	{
    	
    		
    		public static void main (String [] args)
    		{
    		
    		
    		ImageIcon myIcon = new ImageIcon("cards/png");
            JLabel centerLabel = new JLabel(myIcon);
            JFrame frame= new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(centerLabel, BorderLayout.SOUTH);
            JButton shuffle=new JButton("SHUFFLE U LOSER!");
            frame.add(shuffle,BorderLayout.NORTH);
            frame.setSize(600,600);
            frame.setVisible(true);
    
    		}
    	
    	
    	
    		 
            
            
            public practice34()
            {
            ImageIcon[] icons = new ImageIcon[52];
            
            
            
        	  ImageIcon[] pictures=
        	  {
        	 pictures [0]= new ImageIcon("cards.png");
             pictures [1]= new ImageIcon("cards/1.png");
        	 pictures [2]=new ImageIcon("cards/2.png");
        	 pictures[3]=new ImageIcon("cards/3.png");
        	 pictures [4]=new ImageIcon("cards/4.png");
        	 pictures[5]=new ImageIcon("cards/5.png");
        	 pictures[6]=new ImageIcon("cards/6.png");
        	 pictures[7]=new ImageIcon("cards/7.png");
        	 pictures[8]=new ImageIcon("cards/8.png");
             pictures[9]=new ImageIcon("cards/9.png");
        	 pictures[10]=new ImageIcon("cards/10.png");
        	pictures[11]=new ImageIcon("cards/11.png");
        	pictures[12]=new ImageIcon("cards/12.png");
        	pictures[13]=new ImageIcon("cards/13.png");}
        	  
            int [] packk = new int[52];
            
    		for (int i=0;i<icons.length;i++) packk[i]=i;
    		Random r= new Random();
    		int n=52;
    		for (int i=0;i<52;i++)
    		{int k= r.nextInt(n);
    		 icons[ i ] = pictures[packk[k]%13];
    		 packk [k]=packk[n-1];
             n--;
             JLabel centerLabel = new JLabel(icons[ i ] );
             
             
             
             
           
    		
         
                	
    		}
        	  
            }
    	}



    it works for me - i use eclipse

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by burningflower12 View Post
    it works for me - i use eclipse
    Thanks for using code tags. But I still have to wonder how your code compiles when you have this construct?:
    Java Code:
            ImageIcon[] pictures=
            {
           pictures [0]= new ImageIcon("cards.png");
             pictures [1]= new ImageIcon("cards/1.png");
           pictures [2]=new ImageIcon("cards/2.png");
           pictures[3]=new ImageIcon("cards/3.png");
           pictures [4]=new ImageIcon("cards/4.png");
           pictures[5]=new ImageIcon("cards/5.png");
           pictures[6]=new ImageIcon("cards/6.png");
           pictures[7]=new ImageIcon("cards/7.png");
           pictures[8]=new ImageIcon("cards/8.png");
             pictures[9]=new ImageIcon("cards/9.png");
           pictures[10]=new ImageIcon("cards/10.png");
          pictures[11]=new ImageIcon("cards/11.png");
          pictures[12]=new ImageIcon("cards/12.png");
          pictures[13]=new ImageIcon("cards/13.png");}

  9. #9
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    why is there something wrong with that structure

    - coz thats wat i am partial confused about

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by burningflower12 View Post
    why is there something wrong with that structure
    Yes, you can't declare an array like that. For one why even have picture[x] inside of the array itself, and for another you can't separate items with semicolons. You should have a look at the Sun tutorial on Arrays, as it will likely show you how to declare arrays correctly. This construct shouldn't compile period. So if your code is compiling and running, then it can't be this code, no exceptions.

    Much luck.

  11. #11
    Join Date
    Nov 2009
    Posts
    6
    Rep Power
    0

    Default

    thanks 4 ur help

  12. #12
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    yeah needs commas..I thought you couldn't declare the size of the array that you just filled it
    Last edited by aaroncarpet; 11-15-2009 at 09:38 PM. Reason: other thoughts

  13. #13
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Java Code:
    ImageIcon[] pictures= new ImageIcon[13];
            
           pictures [0]= new ImageIcon("cards.png");
             pictures [1]= new ImageIcon("cards/1.png");
           pictures [2]=new ImageIcon("cards/2.png");
           pictures[3]=new ImageIcon("cards/3.png");
           pictures [4]=new ImageIcon("cards/4.png");
           pictures[5]=new ImageIcon("cards/5.png");
           pictures[6]=new ImageIcon("cards/6.png");
           pictures[7]=new ImageIcon("cards/7.png");
           pictures[8]=new ImageIcon("cards/8.png");
             pictures[9]=new ImageIcon("cards/9.png");
           pictures[10]=new ImageIcon("cards/10.png");
          pictures[11]=new ImageIcon("cards/11.png");
          pictures[12]=new ImageIcon("cards/12.png");
          pictures[13]=new ImageIcon("cards/13.png");
    dont yell at me for fixing it

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by aaroncarpet View Post
    Java Code:
           ImageIcon[] pictures= new ImageIcon[13];
            
           pictures [0]= new ImageIcon("cards.png");
           pictures [1]= new ImageIcon("cards/1.png");
           pictures [2]=new ImageIcon("cards/2.png");
           pictures[3]=new ImageIcon("cards/3.png");
           pictures [4]=new ImageIcon("cards/4.png");
           pictures[5]=new ImageIcon("cards/5.png");
           pictures[6]=new ImageIcon("cards/6.png");
           pictures[7]=new ImageIcon("cards/7.png");
           pictures[8]=new ImageIcon("cards/8.png");
           pictures[9]=new ImageIcon("cards/9.png");
           pictures[10]=new ImageIcon("cards/10.png");
           pictures[11]=new ImageIcon("cards/11.png");
           pictures[12]=new ImageIcon("cards/12.png");
           pictures[13]=new ImageIcon("cards/13.png");
    dont yell at me for fixing it
    LOL, if this is for me, no, I won't yell, but I will have to wonder what the JVM says to you when it reaches this line:
    Java Code:
    pictures[13]=new ImageIcon("cards/13.png");
    after you've declared the array like so:
    Java Code:
    ImageIcon[] pictures= new ImageIcon[13];
    Also, isn't this simpler?
    Java Code:
      private static final int CARD_MAX = 14;
    
        // in some initialization code:
        ImageIcon[] pictures = new ImageIcon[CARD_MAX];
        pictures[0] = new ImageIcon("cards.png");
        for (int i = 1; i < pictures.length; i++) {
          pictures[i] = new ImageIcon("cards/" + i + ".png");
        }
    Last edited by Fubarable; 11-15-2009 at 09:53 PM.

  15. #15
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

Similar Threads

  1. ImageSearch in Java (find an image on the screen)
    By ribbs2521 in forum New To Java
    Replies: 7
    Last Post: 02-05-2009, 05:48 AM
  2. Displaying a georaster image using jsp
    By smart in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-27-2008, 10:45 AM
  3. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 06:01 AM
  4. Displaying different image types
    By splinter64uk in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2007, 08:12 AM
  5. Replies: 1
    Last Post: 07-31-2007, 04:01 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
  •