Results 1 to 2 of 2
Thread: Image from file
- 11-27-2010, 01:05 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 8
- Rep Power
- 0
Image from file
My problem is i need to read the image from a file and then display it onto the gui by getting it from another class. My teacher said something about pathname, but i don't know. Please check my codes below and answer me. I am new to java and its quite difficult for me to understand, very urgent.
Thank you in advance
These are my codes:
---The gui to display the image
Java Code:public class DisplayCD extends javax.swing.JFrame { /** Creates new form DisplayCD */ @SuppressWarnings("static-access") public DisplayCD() { try { initComponents(); ReadFromFile display = new ReadFromFile(); display.readFile(); image.setIcon(display.passedCD[0].getImage()); Title.setText(display.passedCD[currentCD].getTitle()); singer.setText(display.passedCD[currentCD].getSinger()); } catch (IOException ex) { Logger.getLogger(DisplayCD.class.getName()).log(Level.SEVERE, null, ex); } }
--- The class which gets and sets the arrays from readfile
-----Reading from file codeJava Code:public class CD { Icon image; String title; String singer; double price; public CD (Icon image,String title, String singer, double price,){ this.image=image; this.title=title; this.singer = singer; } public CD (){ } public void createTrackArray(int size){ track=new String[size]; time=new String[size]; } public Icon getImage() { return image; } public void setImage(Icon image) { this.image = image; } public void setTitle (String title){ this.title = title; } public String getTitle (){ return title; } }
Moderator Edit: Code tags addedJava Code:public class ReadFromFile { String line = ""; public static CD[] passedCD; int remain; int trackSize = 0; // public static void main (String[]args) throws IOException{ public void readFile() throws IOException { BufferedReader br = new BufferedReader(new FileReader("CD.txt")); //read 1st line to indicate number of cd int count = Integer.parseInt(br.readLine()); CD CDArray[] = new CD[count]; String tempArray[] = new String[count]; // create array to hold CD for (int i = 0; i < count; i++) { tempArray[i] = br.readLine(); // System.out.println("cd info---------------->" + tempArray[i]); } for (int j = 0; j < count; j++) { CD temp = new CD(); String CDInfo = tempArray[j]; StringTokenizer st = new StringTokenizer(CDInfo, ","); while (st.hasMoreTokens()) { String a = st.nextToken(); temp.setTitle(a); // System.out.println("CD title----->" + temp.getTitle()); temp.setSinger(st.nextToken()); // System.out.println("CD singer---->" + temp.getSinger()); a = st.nextToken(); double d = Double.parseDouble(a); temp.setPrice(d); // System.out.println("CD price------>" + temp.getPrice()); temp.setCategory(st.nextToken()); // System.out.println("CD cat-------->" + temp.getCategory()); temp.setDiscount(st.nextToken()); // System.out.println("CD discount---->" + temp.getDiscont()); a = st.nextToken(); Icon i = new ImageIcon(a); temp.setImage(i); trackSize = st.countTokens() / 2; temp.setNumSong(trackSize); // System.out.println("=============> trackSize" + trackSize); temp.createTrackArray(trackSize); for (int k = 0; k < trackSize; k++) { String track = st.nextToken(); // System.out.println("Next token----->" + track); temp.setTrackEach(k, track); String time = st.nextToken(); // System.out.println("Next time----->" + time); temp.setTimeEach(k, time); } }// readin before the tracks CDArray[j] = temp; remain = trackSize; } passedCD = CDArray; // System.out.println("!!!" + passedCD[0].title); } public static CD[] passCD(CD[] array) { return array; } }Last edited by Fubarable; 11-27-2010 at 02:10 PM. Reason: Moderator Edit: Code tags added
-
A couple of suggestions:
* Try to create a simple program that reads in an image first. They are much easier to debug. It could do nothing more than read in the image, put it into an ImageIcon, place the ImageIcon into a JLabel and then display the JLabel in a JOptionPane. Once you get this to work, you'll be able to integrate the code into your larger program.
* Leave all urgency out of your posts here. It suggests that you believe that your problem is more important than any others here (even if you didn't intend to suggest this), and we're all volunteers who don't like to be pressured.
* Use code tags when posting code here so that your code retains its formatting and is readable. I've added tags to your post above, and to learn to do this yourself, please read the link in my signature below.
An example of the small program I was mentioning:
Java Code:import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JOptionPane; public class ImageTest { public static void main(String[] args) throws IOException { Image image = // .... however you want to get your image ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon); JOptionPane.showMessageDialog(null, label); } }
Similar Threads
-
DOC to PDF or Image file
By wildbeat in forum AWT / SwingReplies: 0Last Post: 11-14-2010, 08:45 AM -
Print Image file
By anilkumar_vist in forum New To JavaReplies: 1Last Post: 04-06-2010, 02:46 PM -
how to get rgb value each pixel of an image file
By tOpach in forum New To JavaReplies: 1Last Post: 03-28-2009, 04:38 PM -
To open an image file such as Jpeg file using JAva Program
By itmani2020 in forum Advanced JavaReplies: 10Last Post: 07-11-2008, 09:57 AM -
Regarding conversion of Image file to Video file
By RamaDeviMsc in forum Advanced JavaReplies: 0Last Post: 06-25-2007, 02:28 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks