Results 1 to 15 of 15
Thread: displaying image into screen !
- 11-13-2009, 03:54 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
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;
}
}
- 11-13-2009, 06:57 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
- 11-13-2009, 11:42 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
not much help there - i checked !
- 11-13-2009, 11:43 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
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 !
-
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.
-
- 11-14-2009, 12:12 AM #7
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
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
-
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");}
- 11-14-2009, 12:34 AM #9
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
why is there something wrong with that structure
- coz thats wat i am partial confused about
-
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-14-2009, 12:42 AM #11
Member
- Join Date
- Nov 2009
- Posts
- 6
- Rep Power
- 0
thanks 4 ur help
- 11-15-2009, 09:33 PM #12
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
- 11-15-2009, 09:46 PM #13
dont yell at me for fixing itJava 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");
-
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:
after you've declared the array like so:Java Code:pictures[13]=new ImageIcon("cards/13.png");
Also, isn't this simpler?Java Code:ImageIcon[] pictures= new ImageIcon[13];
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.
- 11-16-2009, 02:02 AM #15
Similar Threads
-
ImageSearch in Java (find an image on the screen)
By ribbs2521 in forum New To JavaReplies: 7Last Post: 02-05-2009, 05:48 AM -
Displaying a georaster image using jsp
By smart in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-27-2008, 10:45 AM -
Displaying image in same jsp
By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 12-08-2008, 06:01 AM -
Displaying different image types
By splinter64uk in forum AWT / SwingReplies: 1Last Post: 12-05-2007, 08:12 AM -
Displaying numbers per line on a JOptionPane.showMessageDialog screen
By zoe in forum New To JavaReplies: 1Last Post: 07-31-2007, 04:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks